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) {
363 response = MHD_create_response_from_data(2, "[]", MHD_NO, MHD_NO);
365 to = record_count - 1;
366 from = (record_count > 50 ? to - 49 : 0);
367 response = create_response_for_range(b, from, to);
371 if (response == NULL) {
373 return respond_JSON_Failure(connection);
376 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
377 MHD_destroy_response(response);
379 } else if (strncmp(url, "/tag", 4) == 0 && strlen(url) > 4) {
381 if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
382 return respond_405(connection);
389 return respond_404(connection);
391 ret = parse_url_info(url + 5, &info);
392 if ((ret & URL_INFO_AUTHOR) == 0)
393 return respond_404(connection);
395 if (!tag_exists(info.author))
396 return respond_404(connection);
399 struct tag *taglist = tag_list(info.author, 0, &recs, -1);
402 response = MHD_create_response_from_data(2, "[]", MHD_NO, MHD_NO);
404 response = create_tag_response(taglist, recs);
407 if (response == NULL)
408 return respond_JSON_Failure(connection);
410 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
411 MHD_destroy_response(response);
414 } else if (strncmp(url, "/put", 4) == 0) {
415 struct put_state *ps = (struct put_state *) *ptr;
420 if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
421 return respond_405(connection);
424 return respond_404(connection);
428 username = MHD_digest_auth_get_username(connection);
429 if (username == NULL)
430 return respond_401(connection, MHD_NO);
431 auth_get_password(username, password);
433 ret = MHD_digest_auth_check(connection, REALM, username, password, 300);
436 if (ret == MHD_INVALID_NONCE || ret == MHD_NO)
437 return respond_401(connection, (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);
439 struct put_state *ps = malloc(sizeof(struct put_state));
442 ps->pp = MHD_create_post_processor(connection, 16384, &POST_put_iterator, ps);
447 if (*upload_data_size) {
448 MHD_post_process(ps->pp, upload_data, *upload_data_size);
449 *upload_data_size = 0;
453 if (ps->data == NULL || ps->data_size == 0)
454 return respond_JSON_Failure(connection);
456 username = MHD_digest_auth_get_username(connection);
457 struct blerg *b = blerg_open(username);
459 return respond_JSON_Failure(connection);
460 if (blerg_store(b, ps->data, ps->data_size) == -1) {
462 return respond_JSON_Failure(connection);
466 MHD_destroy_post_processor(ps->pp);
472 return respond_JSON_Success(connection);
473 } else if (strncmp(url, "/info", 5) == 0) {
477 if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
478 return respond_405(connection);
484 return respond_404(connection);
486 ret = parse_url_info(url + 6, &info);
487 if ((ret & URL_INFO_AUTHOR) == 0)
488 return respond_404(connection);
490 if (!blerg_exists(info.author))
491 return respond_404(connection);
495 struct blerg *b = blerg_open(info.author);
496 uint64_t record_count = blerg_get_record_count(b);
500 yajl_gen g = yajl_gen_alloc(&yajl_c, NULL);
501 yajl_gen_map_open(g);
502 yajl_gen_string(g, "record_count", 12);
503 snprintf(number, 21, "%llu", record_count);
504 yajl_gen_string(g, number, strlen(number));
505 yajl_gen_map_close(g);
507 const unsigned char *ybuf;
508 yajl_gen_get_buf(g, &ybuf, &len);
510 response = MHD_create_response_from_data(len, (void *)ybuf, MHD_NO, MHD_YES);
511 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
512 MHD_destroy_response(response);
517 } else if (strncmp(url, "/create", 8) == 0) {
518 struct create_state *cs = (struct create_state *) *ptr;
521 if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
522 return respond_405(connection);
524 struct create_state *cs = malloc(sizeof(struct create_state));
525 cs->username[0] = cs->password[0] = 0;
526 cs->pp = MHD_create_post_processor(connection, 1024, &POST_create_iterator, cs);
531 if (*upload_data_size) {
532 MHD_post_process(cs->pp, upload_data, *upload_data_size);
533 *upload_data_size = 0;
537 if (cs->username[0] == 0 || cs->password[0] == 0)
538 return respond_JSON_Failure(connection);
540 if (blerg_exists(cs->username))
541 return respond_JSON_Failure(connection);
543 struct blerg *b = blerg_open(cs->username);
545 auth_set_password(cs->username, cs->password);
547 MHD_destroy_post_processor(cs->pp);
551 return respond_JSON_Success(connection);
553 return respond_404(connection);
558 int main(int argc, char *argv[]) {
559 struct MHD_Daemon *daemon;
565 daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
566 if (daemon == NULL) {
567 fprintf(stderr, "Could not start web server\n");
572 FD_ZERO(&rs); FD_ZERO(&ws); FD_ZERO(&es);
573 if (MHD_get_fdset(daemon, &rs, &ws, &es, &max) != MHD_YES) {
574 fprintf(stderr, "Fatal error getting fd sets\n");
577 select(max + 1, &rs, &ws, &es, NULL);
580 MHD_stop_daemon(daemon);