4 #include <microhttpd.h>
5 #include <yajl/yajl_gen.h>
9 #include "canned_responses.h"
13 #define URL_INFO_AUTHOR 0x1
14 #define URL_INFO_RECORD 0x2
15 #define URL_INFO_RECORD_TO 0x4
16 #define DERP "DERP DERP DERP"
18 yajl_gen_config yajl_c = { 0, 0 };
21 struct MHD_PostProcessor *pp;
27 struct MHD_PostProcessor *pp;
49 int parse_url_info(const char *url, struct url_info *info) {
62 memcpy(info->author, url, len);
63 info->author[len] = 0;
64 info->contents |= URL_INFO_AUTHOR;
66 if (c == NULL || c[1] == 0)
67 return info->contents;
69 info->record = strtoull(c + 1, NULL, 10);
70 info->contents |= URL_INFO_RECORD;
73 if (c == NULL || c[1] == 0)
74 return info->contents;
76 info->record_to = strtoull(c + 1, NULL, 10);
77 info->contents |= URL_INFO_RECORD_TO;
79 return info->contents;
82 uint64_t *make_sequential_list(uint64_t from, uint64_t to) {
83 uint64_t len = to - from + 1;
84 uint64_t *list = malloc(len * sizeof(uint64_t));
87 for (i = 0; i < len; i++) {
94 void json_generate_one_record(yajl_gen g, const char *author, struct blerg *b, uint64_t record) {
99 if (!blerg_fetch(b, record, &data, &len)) {
100 fprintf(stderr, "Could not fetch record\n");
104 yajl_gen_map_open(g);
105 if (author != NULL) {
106 yajl_gen_string(g, "author", 6);
107 yajl_gen_string(g, author, strlen(author));
109 yajl_gen_string(g, "record", 6);
110 snprintf(number, 21, "%llu", record);
111 yajl_gen_string(g, number, strlen(number));
112 yajl_gen_string(g, "timestamp", 9);
113 yajl_gen_integer(g, blerg_get_timestamp(b, record));
114 yajl_gen_string(g, "data", 4);
115 yajl_gen_string(g, data, len);
116 yajl_gen_map_close(g);
121 ssize_t GET_generate_list(void *cls, uint64_t pos, char *buf, size_t max) {
122 struct get_state *gs = cls;
123 const unsigned char *ybuf;
129 yajl_gen_get_buf(gs->g, &ybuf, &len);
130 size_t bytes_remaining = len - gs->yoff;
131 if (bytes_remaining > max) {
132 memcpy(buf, ybuf + gs->yoff, max);
136 memcpy(buf, ybuf + gs->yoff, bytes_remaining);
138 yajl_gen_clear(gs->g);
139 return bytes_remaining;
146 if (pos == 0) { /* Start iterating */
147 yajl_gen_array_open(gs->g);
150 /* Snarf one record */
151 json_generate_one_record(gs->g, NULL, gs->b, gs->entries[gs->i]);
154 yajl_gen_array_close(gs->g);
160 yajl_gen_get_buf(gs->g, &ybuf, &len);
162 memcpy(buf, ybuf, max);
166 memcpy(buf, ybuf, len);
167 yajl_gen_clear(gs->g);
172 void GET_generate_list_free(void *cls) {
173 struct get_state *gs = cls;
176 yajl_gen_free(gs->g);
181 ssize_t GET_generate_taglist(void *cls, uint64_t pos, char *buf, size_t max) {
182 struct tag_state *ts = cls;
184 const unsigned char *ybuf;
188 yajl_gen_get_buf(ts->g, &ybuf, &len);
189 size_t bytes_remaining = len - ts->yoff;
190 if (bytes_remaining > max) {
191 memcpy(buf, ybuf + ts->yoff, max);
195 memcpy(buf, ybuf + ts->yoff, bytes_remaining);
197 yajl_gen_clear(ts->g);
198 return bytes_remaining;
205 if (pos == 0) { /* Start iterating */
206 yajl_gen_array_open(ts->g);
209 /* Snarf one record */
210 b = blerg_open(ts->results[ts->i].author);
214 json_generate_one_record(ts->g, ts->results[ts->i].author, b, ts->results[ts->i].record);
220 yajl_gen_array_close(ts->g);
226 yajl_gen_get_buf(ts->g, &ybuf, &len);
228 memcpy(buf, ybuf, max);
232 memcpy(buf, ybuf, len);
233 yajl_gen_clear(ts->g);
238 void GET_generate_taglist_free(void *cls) {
239 struct tag_state *ts = cls;
241 yajl_gen_free(ts->g);
246 int POST_create_iterator(void *cls, enum MHD_ValueKind kind, const char *key, const char *filename, const char *content_type, const char *transfer_encoding, const char *data, uint64_t off, size_t size) {
247 struct create_state *cs = cls;
249 if (strncmp(key, "username", 9) == 0) {
250 if (size > 32) size = 32;
251 memcpy(cs->username, data, size);
252 cs->username[size] = 0;
253 } else if (strncmp(key, "password", 9) == 0) {
254 if (size > 32) size = 32;
255 memcpy(cs->password, data, size);
256 cs->password[size] = 0;
262 int POST_put_iterator(void *cls, enum MHD_ValueKind kind, const char *key, const char *filename, const char *content_type, const char *transfer_encoding, const char *data, uint64_t off, size_t size) {
263 struct put_state *ps = cls;
265 if (strncmp(key, "data", 5) == 0) {
266 if (ps->data == NULL) {
267 ps->data_size = size;
268 ps->data = malloc(size);
270 if (ps->data_size + size > MAX_RECORD_SIZE) {
271 size = MAX_RECORD_SIZE - ps->data_size;
273 ps->data_size += size;
274 ps->data = realloc(ps->data, ps->data_size);
276 memcpy(ps->data + off, data, size);
277 if (ps->data_size == MAX_RECORD_SIZE)
284 struct MHD_Response *create_response_for_range(struct blerg *b, uint64_t from, uint64_t to) {
285 struct MHD_Response *response;
286 struct get_state *gs = malloc(sizeof(struct get_state));
289 uint64_t record_count = blerg_get_record_count(b);
291 if (from > to || from >= record_count || to >= record_count || to - from > 99) {
297 gs->entries = make_sequential_list(from, to);
300 gs->g = yajl_gen_alloc(&yajl_c, NULL);
301 gs->yoff = gs->done = 0;
303 response = MHD_create_response_from_callback(-1, 262144, &GET_generate_list, gs, &GET_generate_list_free);
308 struct MHD_Response *create_tag_response(struct tag *results, uint64_t len) {
309 struct tag_state *ts = malloc(sizeof(struct tag_state));
310 ts->g = yajl_gen_alloc(&yajl_c, NULL);
311 ts->results = results;
313 ts->yoff = ts->done = 0;
315 return MHD_create_response_from_callback(-1, 262144, &GET_generate_taglist, ts, &GET_generate_taglist_free);
319 ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const char *method,
320 const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) {
321 struct MHD_Response *response;
323 struct url_info info;
326 if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) {
328 if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
329 return respond_405(connection);
336 return respond_404(connection);
338 ret = parse_url_info(url + 5, &info);
339 if ((ret & URL_INFO_AUTHOR) == 0)
340 return respond_404(connection);
342 if (!blerg_exists(info.author))
343 return respond_404(connection);
347 struct blerg *b = blerg_open(info.author);
349 if ((ret & URL_INFO_RECORD) && (ret & URL_INFO_RECORD_TO)) {
350 response = create_response_for_range(b, info.record, info.record_to);
351 } else if (ret & URL_INFO_RECORD) {
352 ret = blerg_fetch(b, info.record, &data, &len);
356 return respond_404(connection);
357 response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
359 uint64_t record_count, from, to;
360 record_count = blerg_get_record_count(b);
361 if (record_count == 0) {
362 response = MHD_create_response_from_data(2, "[]", MHD_NO, MHD_NO);
364 to = record_count - 1;
365 from = (record_count > 50 ? to - 49 : 0);
366 response = create_response_for_range(b, from, to);
370 if (response == NULL)
371 return respond_JSON_Failure(connection);
373 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
374 MHD_destroy_response(response);
376 } else if (strncmp(url, "/tag", 4) == 0 && strlen(url) > 4) {
378 if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
379 return respond_405(connection);
386 return respond_404(connection);
388 ret = parse_url_info(url + 5, &info);
389 if ((ret & URL_INFO_AUTHOR) == 0)
390 return respond_404(connection);
392 if (!tag_exists(info.author))
393 return respond_404(connection);
396 struct tag *taglist = tag_list(info.author, 0, &recs, -1);
399 response = MHD_create_response_from_data(2, "[]", MHD_NO, MHD_NO);
401 response = create_tag_response(taglist, recs);
404 if (response == NULL)
405 return respond_JSON_Failure(connection);
407 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
408 MHD_destroy_response(response);
411 } else if (strncmp(url, "/put", 4) == 0) {
412 struct put_state *ps = (struct put_state *) *ptr;
417 if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
418 return respond_405(connection);
421 return respond_404(connection);
425 username = MHD_digest_auth_get_username(connection);
426 if (username == NULL)
427 return respond_401(connection, MHD_NO);
428 auth_get_password(username, password);
430 ret = MHD_digest_auth_check(connection, REALM, username, password, 300);
433 if (ret == MHD_INVALID_NONCE || ret == MHD_NO)
434 return respond_401(connection, (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);
436 struct put_state *ps = malloc(sizeof(struct put_state));
439 ps->pp = MHD_create_post_processor(connection, 16384, &POST_put_iterator, ps);
444 if (*upload_data_size) {
445 MHD_post_process(ps->pp, upload_data, *upload_data_size);
446 *upload_data_size = 0;
450 if (ps->data == NULL || ps->data_size == 0)
451 return respond_JSON_Failure(connection);
453 username = MHD_digest_auth_get_username(connection);
454 struct blerg *b = blerg_open(username);
456 return respond_JSON_Failure(connection);
457 if (blerg_store(b, ps->data, ps->data_size) == -1) {
459 return respond_JSON_Failure(connection);
463 MHD_destroy_post_processor(ps->pp);
469 return respond_JSON_Success(connection);
470 } else if (strncmp(url, "/info", 5) == 0) {
474 if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
475 return respond_405(connection);
481 return respond_404(connection);
483 ret = parse_url_info(url + 6, &info);
484 if ((ret & URL_INFO_AUTHOR) == 0)
485 return respond_404(connection);
487 if (!blerg_exists(info.author))
488 return respond_404(connection);
492 struct blerg *b = blerg_open(info.author);
493 uint64_t record_count = blerg_get_record_count(b);
497 yajl_gen g = yajl_gen_alloc(&yajl_c, NULL);
498 yajl_gen_map_open(g);
499 yajl_gen_string(g, "record_count", 12);
500 snprintf(number, 21, "%llu", record_count);
501 yajl_gen_string(g, number, strlen(number));
502 yajl_gen_map_close(g);
504 const unsigned char *ybuf;
505 yajl_gen_get_buf(g, &ybuf, &len);
507 response = MHD_create_response_from_data(len, (void *)ybuf, MHD_NO, MHD_YES);
508 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
509 MHD_destroy_response(response);
514 } else if (strncmp(url, "/create", 8) == 0) {
515 struct create_state *cs = (struct create_state *) *ptr;
518 if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
519 return respond_405(connection);
521 struct create_state *cs = malloc(sizeof(struct create_state));
522 cs->username[0] = cs->password[0] = 0;
523 cs->pp = MHD_create_post_processor(connection, 1024, &POST_create_iterator, cs);
528 if (*upload_data_size) {
529 MHD_post_process(cs->pp, upload_data, *upload_data_size);
530 *upload_data_size = 0;
534 if (cs->username[0] == 0 || cs->password[0] == 0)
535 return respond_JSON_Failure(connection);
537 if (blerg_exists(cs->username))
538 return respond_JSON_Failure(connection);
540 struct blerg *b = blerg_open(cs->username);
542 auth_set_password(cs->username, cs->password);
544 MHD_destroy_post_processor(cs->pp);
548 return respond_JSON_Success(connection);
550 return respond_404(connection);
555 int main(int argc, char *argv[]) {
556 struct MHD_Daemon *daemon;
562 daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
563 if (daemon == NULL) {
564 fprintf(stderr, "Could not start web server\n");
569 FD_ZERO(&rs); FD_ZERO(&ws); FD_ZERO(&es);
570 if (MHD_get_fdset(daemon, &rs, &ws, &es, &max) != MHD_YES) {
571 fprintf(stderr, "Fatal error getting fd sets\n");
574 select(max + 1, &rs, &ws, &es, NULL);
577 MHD_stop_daemon(daemon);