1 /* Blerg is (C) 2011 The Dominion of Awesome, and is distributed under a
2 * BSD-style license. Please see the COPYING file for details.
8 #include <yajl/yajl_gen.h>
12 #include "subscription.h"
13 #include "canned_responses.h"
17 yajl_gen_config yajl_c = { 0, 0 };
19 int check_auth(const char *username) {
20 if (username == NULL || username[0] == 0) {
25 const char *given_token = cgi_getcookie("auth");
26 if (!auth_check_token(username, given_token)) {
34 void respond_for_range(struct blerg *b, uint64_t from, uint64_t to) {
35 const unsigned char *ybuf;
39 uint64_t record_count = blerg_get_record_count(b);
41 printf("Content-type: application/json\r\n\r\n");
43 if (from > to || from >= record_count || to >= record_count || to - from > 99) {
44 respond_JSON_Failure();
48 g = yajl_gen_alloc(&yajl_c, NULL);
49 yajl_gen_array_open(g);
51 for (i = to; i != from - 1; i--) {
52 json_generate_one_record(g, NULL, b, i, 0);
53 yajl_gen_get_buf(g, &ybuf, &len);
54 fwrite(ybuf, len, 1, stdout);
58 yajl_gen_array_close(g);
59 yajl_gen_get_buf(g, &ybuf, &len);
60 fwrite(ybuf, len, 1, stdout);
64 void respond_blergref_list(struct blergref * results, int i) {
65 const unsigned char *ybuf;
72 printf("Content-type: application/json\r\n\r\n");
73 g = yajl_gen_alloc(&yajl_c, NULL);
75 yajl_gen_array_open(g);
78 b = blerg_open(results[i].author);
80 json_generate_one_record(g, results[i].author, b, results[i].record, 0);
83 yajl_gen_get_buf(g, &ybuf, &len);
84 fwrite(ybuf, len, 1, stdout);
90 yajl_gen_array_close(g);
91 yajl_gen_get_buf(g, &ybuf, &len);
92 fwrite(ybuf, len, 1, stdout);
96 int main(int argc, char *argv[]) {
100 struct url_info info;
103 if (cgi_init() != CGIERR_NONE)
106 path = getenv("PATH_INFO");
111 request_method = getenv("REQUEST_METHOD");
112 if (request_method == NULL) {
113 fprintf(stderr, "Request method is null!?\n");
117 if (strncmp(path, "/get", 4) == 0 && strlen(path) > 4) {
118 if (strncmp(request_method, "GET", 4) != 0) {
123 if (path[4] != '/') {
128 ret = parse_url_info(path + 5, &info);
129 if ((ret & URL_INFO_NAME) == 0) {
134 if (!blerg_exists(info.name)) {
139 struct blerg *b = blerg_open(info.name);
141 if ((ret & URL_INFO_RECORD) && (ret & URL_INFO_RECORD_TO)) {
142 respond_for_range(b, info.record, info.record_to);
143 } else if (ret & URL_INFO_RECORD) {
144 ret = blerg_fetch(b, info.record, &data, &len);
150 respond_simple_data(data, len);
152 uint64_t record_count, from, to;
153 record_count = blerg_get_record_count(b);
154 if (record_count == 0) {
155 respond_simple_data("[]", 2);
157 to = record_count - 1;
158 from = (record_count > 50 ? to - 49 : 0);
159 respond_for_range(b, from, to);
164 } else if (strncmp(path, "/tag", 4) == 0 && strlen(path) > 4) {
165 if (strcmp(request_method, "GET") != 0) {
170 if (path[4] != '/') {
175 ret = parse_url_info(path + 5, &info);
176 if ((ret & URL_INFO_NAME) == 0) {
181 if (info.name[0] == 'H')
183 if (!tag_exists(info.name)) {
189 struct blergref *taglist = tag_list(info.name, 0, &recs, -1);
192 respond_simple_data("[]", 2);
194 respond_blergref_list(taglist, recs);
196 } else if (strncmp(path, "/put", 4) == 0) {
197 if (strcmp(request_method, "POST") != 0) {
202 const char *username = cgi_getentrystr("username");
203 if (!check_auth(username))
206 if (path[4] == '/') {
211 const char *data = cgi_getentrystr("data");
212 if (data == NULL || data[0] == 0) {
213 respond_JSON_Failure();
217 struct blerg *b = blerg_open(username);
219 respond_JSON_Failure();
222 ret = blerg_store(b, data, strlen(data));
225 respond_JSON_Failure();
229 respond_JSON_Success();
230 } else if (strncmp(path, "/info", 5) == 0) {
231 if (strcmp(request_method, "GET") != 0) {
236 if (path[5] != '/') {
241 ret = parse_url_info(path + 6, &info);
242 if ((ret & URL_INFO_NAME) == 0) {
247 if (!blerg_exists(info.name)) {
252 struct blerg *b = blerg_open(info.name);
253 uint64_t record_count = blerg_get_record_count(b);
257 yajl_gen g = yajl_gen_alloc(&yajl_c, NULL);
258 yajl_gen_map_open(g);
259 yajl_gen_string(g, "record_count", 12);
260 snprintf(number, 21, "%llu", record_count);
261 yajl_gen_string(g, number, strlen(number));
262 yajl_gen_map_close(g);
264 const unsigned char *ybuf;
265 yajl_gen_get_buf(g, &ybuf, &len);
267 printf("Content-type: application/json\r\n");
268 printf("Content-length: %d\r\n\r\n", len);
269 fwrite(ybuf, len, 1, stdout);
272 } else if (strncmp(path, "/create", 8) == 0) {
273 if (strcmp(request_method, "POST") != 0) {
278 const char *username = cgi_getentrystr("username");
279 const char *password = cgi_getentrystr("password");
280 if (username == NULL || username[0] == 0 ||
281 password == NULL || password[0] == 0) {
282 respond_JSON_Failure();
286 if (blerg_exists(username)) {
287 respond_JSON_Failure();
291 struct blerg *b = blerg_open(username);
293 auth_set_password(username, password);
295 respond_JSON_Success();
296 } else if (strncmp(path, "/login", 7) == 0) {
297 if (strcmp(request_method, "POST") != 0) {
302 const char *username = cgi_getentrystr("username");
303 const char *password = cgi_getentrystr("password");
304 if (username == NULL || username[0] == 0 ||
305 password == NULL || password[0] == 0) {
306 respond_JSON_Failure();
310 char *token = auth_login(username, password);
312 respond_JSON_Failure();
316 printf("Set-Cookie: auth=%s\r\n", token);
319 respond_JSON_Success();
320 } else if (strncmp(path, "/logout", 8) == 0) {
321 if (strcmp(request_method, "POST") != 0) {
326 const char *username = cgi_getentrystr("username");
327 if (!check_auth(username))
330 const char *given_token = cgi_getcookie("auth");
331 auth_logout(username, given_token);
332 respond_JSON_Success();
333 } else if (strncmp(path, "/subscribe", 10) == 0 || strncmp(path, "/unsubscribe", 12) == 0) {
334 const char *username = cgi_getentrystr("username");
335 if (!check_auth(username))
338 if (path[1] == 'u') {
339 if (path[12] != '/') {
344 ret = parse_url_info(path + 13, &info);
345 if ((ret & URL_INFO_NAME) == 0) {
350 subscription_remove(username, info.name);
352 if (path[10] != '/') {
357 ret = parse_url_info(path + 11, &info);
358 if ((ret & URL_INFO_NAME) == 0) {
363 subscription_add(username, info.name);
365 respond_JSON_Success();
366 } else if (strncmp(path, "/feed", 6) == 0) {
367 const char *username = cgi_getentrystr("username");
368 if (!check_auth(username))
372 struct blergref *feedlist = subscription_list(username, 0, &recs, -1);
375 respond_simple_data("[]", 2);
377 respond_blergref_list(feedlist, recs);
380 struct blerg *b = blerg_open(username);
381 blerg_set_subscription_mark(b);
383 } else if (strncmp(path, "/feedinfo", 9) == 0) {
384 const char *username = cgi_getentrystr("username");
385 if (!check_auth(username))
388 yajl_gen g = yajl_gen_alloc(&yajl_c, NULL);
389 yajl_gen_map_open(g);
391 struct blerg *b = blerg_open(username);
392 uint64_t subscription_mark = blerg_get_subscription_mark(b);
395 yajl_gen_string(g, "new", 3);
396 yajl_gen_integer(g, subscription_count_items(username) - subscription_mark);
398 yajl_gen_string(g, "subscribed", 10);
399 ret = parse_url_info(path + 10, &info);
400 if ((ret & URL_INFO_NAME) == 1) {
401 yajl_gen_bool(g, is_subscribed(username, info.name));
406 yajl_gen_map_close(g);
408 const unsigned char *ybuf;
409 yajl_gen_get_buf(g, &ybuf, &len);
411 printf("Content-type: application/json\r\n");
412 printf("Content-length: %d\r\n\r\n", len);
413 fwrite(ybuf, len, 1, stdout);
416 } else if (strncmp(path, "/passwd", 7) == 0) {
417 const char *username = cgi_getentrystr("username");
418 if (!check_auth(username))
421 const char *password = cgi_getentrystr("password");
422 const char *new_password = cgi_getentrystr("new_password");
423 if (password == NULL || new_password == NULL) {
424 respond_JSON_Failure();
426 if (auth_check_password(username, password)) {
427 auth_set_password(username, new_password);
428 respond_JSON_Success();
430 respond_JSON_Failure();