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;
106 if (cgi_init() != CGIERR_NONE)
109 path = getenv("PATH_INFO");
114 request_method = getenv("REQUEST_METHOD");
115 if (request_method == NULL) {
116 fprintf(stderr, "Request method is null!?\n");
120 if (strncmp(path, "/get", 4) == 0 && strlen(path) > 4) {
121 if (strncmp(request_method, "GET", 4) != 0) {
126 if (path[4] != '/') {
131 ret = parse_url_info(path + 5, &info);
132 if ((ret & URL_INFO_NAME) == 0) {
137 if (!blerg_exists(info.name)) {
142 struct blerg *b = blerg_open(info.name);
144 if ((ret & URL_INFO_RECORD) && (ret & URL_INFO_RECORD_TO)) {
145 respond_for_range(b, info.record, info.record_to);
146 } else if (ret & URL_INFO_RECORD) {
147 ret = blerg_fetch(b, info.record, &data, &len);
153 respond_simple_data(data, len);
155 uint64_t record_count, from, to;
156 record_count = blerg_get_record_count(b);
157 if (record_count == 0) {
158 respond_simple_data("[]", 2);
160 to = record_count - 1;
161 from = (record_count > 50 ? to - 49 : 0);
162 respond_for_range(b, from, to);
167 } else if (strncmp(path, "/tag", 4) == 0 && strlen(path) > 4) {
168 if (strcmp(request_method, "GET") != 0) {
173 if (path[4] != '/') {
178 ret = parse_url_info(path + 5, &info);
179 if ((ret & URL_INFO_NAME) == 0) {
184 if (info.name[0] == 'H')
186 if (!tag_exists(info.name)) {
192 struct blergref *taglist = tag_list(info.name, 0, &recs, -1);
195 respond_simple_data("[]", 2);
197 respond_blergref_list(taglist, recs);
199 } else if (strncmp(path, "/put", 4) == 0) {
200 if (strcmp(request_method, "POST") != 0) {
205 const char *username = cgi_getentrystr("username");
206 if (!check_auth(username))
209 if (path[4] == '/') {
214 const char *data = cgi_getentrystr("data");
215 if (data == NULL || data[0] == 0) {
216 respond_JSON_Failure();
220 struct blerg *b = blerg_open(username);
222 respond_JSON_Failure();
225 ret = blerg_store(b, data, strlen(data));
228 respond_JSON_Failure();
232 respond_JSON_Success();
233 } else if (strncmp(path, "/info", 5) == 0) {
234 if (strcmp(request_method, "GET") != 0) {
239 if (path[5] != '/') {
244 ret = parse_url_info(path + 6, &info);
245 if ((ret & URL_INFO_NAME) == 0) {
250 if (!blerg_exists(info.name)) {
255 struct blerg *b = blerg_open(info.name);
256 uint64_t record_count = blerg_get_record_count(b);
260 yajl_gen g = yajl_gen_alloc(&yajl_c, NULL);
261 yajl_gen_map_open(g);
262 yajl_gen_string(g, "record_count", 12);
263 snprintf(number, 21, "%llu", record_count);
264 yajl_gen_string(g, number, strlen(number));
265 yajl_gen_map_close(g);
267 const unsigned char *ybuf;
268 yajl_gen_get_buf(g, &ybuf, &len);
270 printf("Content-type: application/json\r\n");
271 printf("Content-length: %d\r\n\r\n", len);
272 fwrite(ybuf, len, 1, stdout);
275 } else if (strncmp(path, "/create", 8) == 0) {
276 if (strcmp(request_method, "POST") != 0) {
281 const char *username = cgi_getentrystr("username");
282 const char *password = cgi_getentrystr("password");
283 if (username == NULL || username[0] == 0 ||
284 password == NULL || password[0] == 0) {
285 respond_JSON_Failure();
289 if (blerg_exists(username)) {
290 respond_JSON_Failure();
294 struct blerg *b = blerg_open(username);
296 auth_set_password(username, password);
298 respond_JSON_Success();
299 } else if (strncmp(path, "/login", 7) == 0) {
300 if (strcmp(request_method, "POST") != 0) {
305 const char *username = cgi_getentrystr("username");
306 const char *password = cgi_getentrystr("password");
307 if (username == NULL || username[0] == 0 ||
308 password == NULL || password[0] == 0) {
309 respond_JSON_Failure();
313 char *token = auth_login(username, password);
315 respond_JSON_Failure();
319 printf("Set-Cookie: auth=%s\r\n", token);
322 respond_JSON_Success();
323 } else if (strncmp(path, "/logout", 8) == 0) {
324 if (strcmp(request_method, "POST") != 0) {
329 const char *username = cgi_getentrystr("username");
330 if (!check_auth(username))
333 const char *given_token = cgi_getcookie("auth");
334 auth_logout(username, given_token);
335 respond_JSON_Success();
336 } else if (strncmp(path, "/subscribe", 10) == 0 || strncmp(path, "/unsubscribe", 12) == 0) {
337 const char *username = cgi_getentrystr("username");
338 if (!check_auth(username))
341 if (path[1] == 'u') {
342 if (path[12] != '/') {
347 ret = parse_url_info(path + 13, &info);
348 if ((ret & URL_INFO_NAME) == 0) {
353 subscription_remove(username, info.name);
355 if (path[10] != '/') {
360 ret = parse_url_info(path + 11, &info);
361 if ((ret & URL_INFO_NAME) == 0) {
366 subscription_add(username, info.name);
368 respond_JSON_Success();
369 } else if (strncmp(path, "/feed", 6) == 0) {
370 const char *username = cgi_getentrystr("username");
371 if (!check_auth(username))
375 struct blergref *feedlist = subscription_list(username, 0, &recs, -1);
378 respond_simple_data("[]", 2);
380 respond_blergref_list(feedlist, recs);
383 struct blerg *b = blerg_open(username);
384 blerg_set_subscription_mark(b);
386 } else if (strncmp(path, "/feedinfo", 9) == 0) {
387 const char *username = cgi_getentrystr("username");
388 if (!check_auth(username))
391 yajl_gen g = yajl_gen_alloc(&yajl_c, NULL);
392 yajl_gen_map_open(g);
394 struct blerg *b = blerg_open(username);
395 uint64_t subscription_mark = blerg_get_subscription_mark(b);
398 yajl_gen_string(g, "new", 3);
399 yajl_gen_integer(g, subscription_count_items(username) - subscription_mark);
401 yajl_gen_string(g, "subscribed", 10);
402 ret = parse_url_info(path + 10, &info);
403 if ((ret & URL_INFO_NAME) == 1) {
404 yajl_gen_bool(g, is_subscribed(username, info.name));
409 yajl_gen_map_close(g);
411 const unsigned char *ybuf;
412 yajl_gen_get_buf(g, &ybuf, &len);
414 printf("Content-type: application/json\r\n");
415 printf("Content-length: %d\r\n\r\n", len);
416 fwrite(ybuf, len, 1, stdout);
419 } else if (strncmp(path, "/passwd", 7) == 0) {
420 const char *username = cgi_getentrystr("username");
421 if (!check_auth(username))
424 const char *password = cgi_getentrystr("password");
425 const char *new_password = cgi_getentrystr("new_password");
426 if (password == NULL || new_password == NULL) {
427 respond_JSON_Failure();
429 if (auth_check_password(username, password)) {
430 auth_set_password(username, new_password);
431 respond_JSON_Success();
433 respond_JSON_Failure();