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"
14 #include "canned_responses.h"
18 yajl_gen_config yajl_c = { 0, 0 };
20 int check_auth(struct auth_cookie *ac) {
21 const char *given_cookie = cgi_getcookie("auth");
23 if (parse_auth_cookie(given_cookie, ac) != 1) {
28 if (!auth_check_token(ac->name, ac->token)) {
35 void respond_yajl(yajl_gen g) {
36 const unsigned char *ybuf;
37 unsigned int content_len;
39 yajl_gen_get_buf(g, &ybuf, &content_len);
41 printf("Content-type: application/json\r\n");
42 printf("Content-length: %d\r\n\r\n", content_len);
43 fwrite(ybuf, content_len, 1, stdout);
46 void respond_for_range(struct blerg *b, uint64_t from, uint64_t to) {
47 const unsigned char *ybuf;
51 uint64_t record_count = blerg_get_record_count(b);
53 printf("Content-type: application/json\r\n\r\n");
55 if (from > to || from >= record_count || to >= record_count || to - from > 99) {
56 respond_JSON_Failure();
60 g = yajl_gen_alloc(&yajl_c, NULL);
61 yajl_gen_array_open(g);
63 for (i = to; i != from - 1; i--) {
64 json_generate_one_record(g, NULL, b, i, 0);
65 yajl_gen_get_buf(g, &ybuf, &len);
66 fwrite(ybuf, len, 1, stdout);
70 yajl_gen_array_close(g);
71 yajl_gen_get_buf(g, &ybuf, &len);
72 fwrite(ybuf, len, 1, stdout);
76 void respond_blergref_list(struct blergref * results, int i) {
77 const unsigned char *ybuf;
84 printf("Content-type: application/json\r\n\r\n");
85 g = yajl_gen_alloc(&yajl_c, NULL);
87 yajl_gen_array_open(g);
90 b = blerg_open(results[i].author);
92 json_generate_one_record(g, results[i].author, b, results[i].record, 0);
95 yajl_gen_get_buf(g, &ybuf, &len);
96 fwrite(ybuf, len, 1, stdout);
102 yajl_gen_array_close(g);
103 yajl_gen_get_buf(g, &ybuf, &len);
104 fwrite(ybuf, len, 1, stdout);
108 int main(int argc, char *argv[]) {
110 char *request_method;
112 struct url_info info;
113 struct auth_cookie ac;
119 if (cgi_init() != CGIERR_NONE)
122 path = getenv("PATH_INFO");
127 request_method = getenv("REQUEST_METHOD");
128 if (request_method == NULL) {
129 fprintf(stderr, "Request method is null!?\n");
133 if (strncmp(path, "/get", 4) == 0 && strlen(path) > 4) {
134 if (strncmp(request_method, "GET", 4) != 0) {
139 if (path[4] != '/') {
144 ret = parse_url_info(path + 5, &info);
145 if ((ret & URL_INFO_NAME) == 0) {
150 if (!blerg_exists(info.name)) {
155 struct blerg *b = blerg_open(info.name);
157 if ((ret & URL_INFO_RECORD) && (ret & URL_INFO_RECORD_TO)) {
158 respond_for_range(b, info.record, info.record_to);
159 } else if (ret & URL_INFO_RECORD) {
160 ret = blerg_fetch(b, info.record, &data, &len);
166 respond_simple_data(data, len);
168 uint64_t record_count, from, to;
169 record_count = blerg_get_record_count(b);
170 if (record_count == 0) {
171 respond_simple_data("[]", 2);
173 to = record_count - 1;
174 from = (record_count > 50 ? to - 49 : 0);
175 respond_for_range(b, from, to);
180 } else if (strncmp(path, "/tag", 4) == 0 && strlen(path) > 4) {
181 if (strcmp(request_method, "GET") != 0) {
186 if (path[4] != '/') {
191 ret = parse_url_info(path + 5, &info);
192 if ((ret & URL_INFO_NAME) == 0) {
197 if (info.name[0] == 'H')
199 if (!tag_exists(info.name)) {
205 struct blergref *taglist = tag_list(info.name, 0, &recs, -1);
208 respond_simple_data("[]", 2);
210 respond_blergref_list(taglist, recs);
212 } else if (strncmp(path, "/put", 4) == 0) {
213 if (strcmp(request_method, "POST") != 0) {
218 if (!check_auth(&ac))
221 if (path[4] == '/') {
226 const char *data = cgi_getentrystr("data");
227 if (data == NULL || data[0] == 0) {
228 respond_JSON_Failure();
232 struct blerg *b = blerg_open(ac.name);
234 respond_JSON_Failure();
237 ret = blerg_store(b, data, strlen(data));
240 respond_JSON_Failure();
244 respond_JSON_Success();
245 } else if (strncmp(path, "/info", 5) == 0) {
246 if (strcmp(request_method, "GET") != 0) {
251 if (path[5] != '/') {
256 ret = parse_url_info(path + 6, &info);
257 if ((ret & URL_INFO_NAME) == 0) {
262 if (!blerg_exists(info.name)) {
267 struct blerg *b = blerg_open(info.name);
268 uint64_t record_count = blerg_get_record_count(b);
272 yajl_gen g = yajl_gen_alloc(&yajl_c, NULL);
273 yajl_gen_map_open(g);
274 yajl_gen_string(g, (unsigned char *)"record_count", 12);
275 snprintf(number, 21, "%llu", record_count);
276 yajl_gen_string(g, (unsigned char *)number, strlen(number));
277 yajl_gen_map_close(g);
282 } else if (strncmp(path, "/create", 8) == 0) {
283 if (strcmp(request_method, "POST") != 0) {
288 const char *username = cgi_getentrystr("username");
289 const char *password = cgi_getentrystr("password");
290 if (username == NULL || username[0] == 0 ||
291 password == NULL || password[0] == 0) {
292 respond_JSON_Failure();
296 if (blerg_exists(username)) {
297 respond_JSON_Failure();
301 struct blerg *b = blerg_open(username);
304 auth_set_password(username, password);
306 respond_JSON_Success();
308 respond_JSON_Failure();
310 } else if (strncmp(path, "/login", 7) == 0) {
311 if (strcmp(request_method, "POST") != 0) {
316 const char *username = cgi_getentrystr("username");
317 const char *password = cgi_getentrystr("password");
318 if (username == NULL || username[0] == 0 ||
319 password == NULL || password[0] == 0) {
320 respond_JSON_Failure();
324 char *token = auth_login(username, password);
326 respond_JSON_Failure();
330 printf("Set-Cookie: auth=%s/%s\r\n", username, token);
333 respond_JSON_Success();
334 } else if (strncmp(path, "/logout", 8) == 0) {
335 if (strcmp(request_method, "POST") != 0) {
340 if (!check_auth(&ac))
343 auth_logout(ac.name, ac.token);
344 printf("Set-Cookie: auth=X; Expires=Thu, 01 Jan 1970 00:00:00 GMT\r\n");
345 respond_JSON_Success();
346 } else if (strncmp(path, "/subscribe", 10) == 0) {
347 if (!check_auth(&ac))
350 if (path[10] != '/') {
355 ret = parse_url_info(path + 11, &info);
356 if ((ret & URL_INFO_NAME) == 0) {
361 const char *subscribed = cgi_getentrystr("subscribed");
363 if (strncmp(subscribed, "true", 4) == 0) {
364 subscription_add(ac.name, info.name);
365 } else if (strncmp(subscribed, "false", 5) == 0) {
366 subscription_remove(ac.name, info.name);
368 respond_JSON_Failure();
371 respond_JSON_Success();
372 } else if (strncmp(path, "/feed", 6) == 0) {
373 if (!check_auth(&ac))
377 struct blergref *feedlist = subscription_list(ac.name, 0, &recs, -1);
380 respond_simple_data("[]", 2);
382 respond_blergref_list(feedlist, recs);
384 } else if (strncmp(path, "/status", 7) == 0) {
385 if (!check_auth(&ac))
388 if (strncmp(request_method, "POST", 4) == 0) {
389 const char *clear = cgi_getentrystr("clear");
392 struct blerg *b = blerg_open(ac.name);
393 if (strncmp(clear, "feed", 4) == 0) {
394 blerg_set_subscription_mark(b);
395 } else if (strncmp(clear, "mentioned", 9) == 0) {
396 blerg_set_status(b, BLERGSTATUS_MENTIONED, 0);
399 respond_JSON_Success();
401 } else if (strncmp(request_method, "GET", 3) == 0) {
404 if (path[7] == 0) { /* No username */
405 g = yajl_gen_alloc(&yajl_c, NULL);
406 yajl_gen_map_open(g);
408 struct blerg *b = blerg_open(ac.name);
409 uint64_t subscription_mark = blerg_get_subscription_mark(b);
410 int mentioned = blerg_get_status(b, BLERGSTATUS_MENTIONED);
413 yajl_gen_string(g, (unsigned char *)"feed_new", 8);
414 yajl_gen_integer(g, subscription_count_items(ac.name) - subscription_mark);
416 yajl_gen_string(g, (unsigned char *)"mentioned", 9);
417 yajl_gen_bool(g, mentioned);
419 yajl_gen_map_close(g);
422 } else { /* with username */
423 g = yajl_gen_alloc(&yajl_c, NULL);
424 yajl_gen_map_open(g);
426 yajl_gen_string(g, (unsigned char *)"subscribed", 10);
427 ret = parse_url_info(path + 8, &info);
428 if ((ret & URL_INFO_NAME) == 1) {
429 yajl_gen_bool(g, is_subscribed(ac.name, info.name));
434 yajl_gen_map_close(g);
442 } else if (strncmp(path, "/passwd", 7) == 0) {
443 if (!check_auth(&ac))
446 const char *password = cgi_getentrystr("password");
447 const char *new_password = cgi_getentrystr("new_password");
448 if (password == NULL || new_password == NULL) {
449 respond_JSON_Failure();
451 if (auth_check_password(ac.name, password)) {
452 auth_set_password(ac.name, new_password);
453 respond_JSON_Success();
455 respond_JSON_Failure();