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 if (!auth_login(username, password)) {
311 respond_JSON_Failure();
315 char *token = auth_get_token(username);
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 auth_logout(username);
331 respond_JSON_Success();
332 } else if (strncmp(path, "/subscribe", 10) == 0 || strncmp(path, "/unsubscribe", 12) == 0) {
333 const char *username = cgi_getentrystr("username");
334 if (!check_auth(username))
337 if (path[1] == 'u') {
338 if (path[12] != '/') {
343 ret = parse_url_info(path + 13, &info);
344 if ((ret & URL_INFO_NAME) == 0) {
349 subscription_remove(username, info.name);
351 if (path[10] != '/') {
356 ret = parse_url_info(path + 11, &info);
357 if ((ret & URL_INFO_NAME) == 0) {
362 subscription_add(username, info.name);
364 respond_JSON_Success();
365 } else if (strncmp(path, "/feed", 6) == 0) {
366 const char *username = cgi_getentrystr("username");
367 if (!check_auth(username))
371 struct blergref *feedlist = subscription_list(username, 0, &recs, -1);
374 respond_simple_data("[]", 2);
376 respond_blergref_list(feedlist, recs);
378 } else if (strncmp(path, "/feedinfo", 9) == 0) {
379 const char *username = cgi_getentrystr("username");
380 if (!check_auth(username))
383 if (path[9] != '/') {
388 ret = parse_url_info(path + 10, &info);
389 if ((ret & URL_INFO_NAME) == 0) {
394 yajl_gen g = yajl_gen_alloc(&yajl_c, NULL);
395 yajl_gen_map_open(g);
396 yajl_gen_string(g, "subscribed", 10);
397 yajl_gen_bool(g, is_subscribed(username, info.name));
398 yajl_gen_map_close(g);
400 const unsigned char *ybuf;
401 yajl_gen_get_buf(g, &ybuf, &len);
403 printf("Content-type: application/json\r\n");
404 printf("Content-length: %d\r\n\r\n", len);
405 fwrite(ybuf, len, 1, stdout);