4 #include <microhttpd.h>
5 #include <yajl/yajl_gen.h>
9 #include "canned_responses.h"
13 yajl_gen_config yajl_c = { 0, 0 };
16 struct MHD_PostProcessor *pp;
22 struct MHD_PostProcessor *pp;
44 ssize_t GET_generate_list(void *cls, uint64_t pos, char *buf, size_t max) {
45 struct get_state *gs = cls;
46 const unsigned char *ybuf;
52 yajl_gen_get_buf(gs->g, &ybuf, &len);
53 size_t bytes_remaining = len - gs->yoff;
54 if (bytes_remaining > max) {
55 memcpy(buf, ybuf + gs->yoff, max);
59 memcpy(buf, ybuf + gs->yoff, bytes_remaining);
61 yajl_gen_clear(gs->g);
62 return bytes_remaining;
69 if (pos == 0) { /* Start iterating */
70 yajl_gen_array_open(gs->g);
73 /* Snarf one record */
74 json_generate_one_record(gs->g, NULL, gs->b, gs->entries[gs->i]);
77 yajl_gen_array_close(gs->g);
83 yajl_gen_get_buf(gs->g, &ybuf, &len);
85 memcpy(buf, ybuf, max);
89 memcpy(buf, ybuf, len);
90 yajl_gen_clear(gs->g);
95 void GET_generate_list_free(void *cls) {
96 struct get_state *gs = cls;
104 ssize_t GET_generate_taglist(void *cls, uint64_t pos, char *buf, size_t max) {
105 struct tag_state *ts = cls;
107 const unsigned char *ybuf;
111 yajl_gen_get_buf(ts->g, &ybuf, &len);
112 size_t bytes_remaining = len - ts->yoff;
113 if (bytes_remaining > max) {
114 memcpy(buf, ybuf + ts->yoff, max);
118 memcpy(buf, ybuf + ts->yoff, bytes_remaining);
120 yajl_gen_clear(ts->g);
121 return bytes_remaining;
128 if (pos == 0) { /* Start iterating */
129 yajl_gen_array_open(ts->g);
132 /* Snarf one record */
133 b = blerg_open(ts->results[ts->i].author);
135 json_generate_one_record(ts->g, ts->results[ts->i].author, b, ts->results[ts->i].record);
140 yajl_gen_array_close(ts->g);
146 yajl_gen_get_buf(ts->g, &ybuf, &len);
148 memcpy(buf, ybuf, max);
152 memcpy(buf, ybuf, len);
153 yajl_gen_clear(ts->g);
158 void GET_generate_taglist_free(void *cls) {
159 struct tag_state *ts = cls;
161 yajl_gen_free(ts->g);
166 int POST_auth_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) {
167 struct auth_state *as = cls;
169 if (strncmp(key, "username", 9) == 0) {
170 if (size > 32) size = 32;
171 memcpy(as->username, data, size);
172 as->username[size] = 0;
173 } else if (strncmp(key, "password", 9) == 0) {
174 if (size > 32) size = 32;
175 memcpy(as->password, data, size);
176 as->password[size] = 0;
182 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) {
183 struct put_state *ps = cls;
185 if (strncmp(key, "data", 5) == 0) {
186 if (ps->data == NULL) {
187 ps->data_size = size;
188 ps->data = malloc(size);
190 if (ps->data_size + size > MAX_RECORD_SIZE) {
191 size = MAX_RECORD_SIZE - ps->data_size;
193 ps->data_size += size;
194 ps->data = realloc(ps->data, ps->data_size);
196 memcpy(ps->data + off, data, size);
197 if (ps->data_size == MAX_RECORD_SIZE)
204 struct MHD_Response *create_response_for_range(struct blerg *b, uint64_t from, uint64_t to) {
205 struct MHD_Response *response;
206 struct get_state *gs = malloc(sizeof(struct get_state));
209 uint64_t record_count = blerg_get_record_count(b);
211 if (from > to || from >= record_count || to >= record_count || to - from > 99) {
217 gs->entries = make_sequential_list(from, to);
220 gs->g = yajl_gen_alloc(&yajl_c, NULL);
221 gs->yoff = gs->done = 0;
223 response = MHD_create_response_from_callback(-1, 262144, &GET_generate_list, gs, &GET_generate_list_free);
228 struct MHD_Response *create_tag_response(struct tag *results, uint64_t len) {
229 struct tag_state *ts = malloc(sizeof(struct tag_state));
230 ts->g = yajl_gen_alloc(&yajl_c, NULL);
231 ts->results = results;
233 ts->yoff = ts->done = 0;
235 return MHD_create_response_from_callback(-1, 262144, &GET_generate_taglist, ts, &GET_generate_taglist_free);
239 ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const char *method,
240 const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) {
241 struct MHD_Response *response;
243 struct url_info info;
246 if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) {
248 if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
249 return respond_405(connection);
256 return respond_404(connection);
258 ret = parse_url_info(url + 5, &info);
259 if ((ret & URL_INFO_AUTHOR) == 0)
260 return respond_404(connection);
262 if (!blerg_exists(info.author))
263 return respond_404(connection);
267 struct blerg *b = blerg_open(info.author);
269 if ((ret & URL_INFO_RECORD) && (ret & URL_INFO_RECORD_TO)) {
270 response = create_response_for_range(b, info.record, info.record_to);
271 } else if (ret & URL_INFO_RECORD) {
272 ret = blerg_fetch(b, info.record, &data, &len);
276 return respond_404(connection);
277 response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
279 uint64_t record_count, from, to;
280 record_count = blerg_get_record_count(b);
281 if (record_count == 0) {
283 response = MHD_create_response_from_data(2, "[]", MHD_NO, MHD_NO);
285 to = record_count - 1;
286 from = (record_count > 50 ? to - 49 : 0);
287 response = create_response_for_range(b, from, to);
291 if (response == NULL) {
293 return respond_JSON_Failure(connection);
296 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
297 MHD_destroy_response(response);
299 } else if (strncmp(url, "/tag", 4) == 0 && strlen(url) > 4) {
301 if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
302 return respond_405(connection);
309 return respond_404(connection);
311 ret = parse_url_info(url + 5, &info);
312 if ((ret & URL_INFO_AUTHOR) == 0)
313 return respond_404(connection);
315 if (!tag_exists(info.author))
316 return respond_404(connection);
319 struct tag *taglist = tag_list(info.author, 0, &recs, -1);
322 response = MHD_create_response_from_data(2, "[]", MHD_NO, MHD_NO);
324 response = create_tag_response(taglist, recs);
327 if (response == NULL)
328 return respond_JSON_Failure(connection);
330 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
331 MHD_destroy_response(response);
334 } else if (strncmp(url, "/put", 4) == 0) {
335 struct put_state *ps = (struct put_state *) *ptr;
340 if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
341 return respond_405(connection);
344 return respond_404(connection);
348 username = MHD_digest_auth_get_username(connection);
349 if (username == NULL)
350 return respond_401(connection, MHD_NO);
351 auth_get_password(username, password);
353 ret = MHD_digest_auth_check(connection, REALM, username, password, 300);
356 if (ret == MHD_INVALID_NONCE || ret == MHD_NO)
357 return respond_401(connection, (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);
359 struct put_state *ps = malloc(sizeof(struct put_state));
362 ps->pp = MHD_create_post_processor(connection, 16384, &POST_put_iterator, ps);
367 if (*upload_data_size) {
368 MHD_post_process(ps->pp, upload_data, *upload_data_size);
369 *upload_data_size = 0;
373 if (ps->data == NULL || ps->data_size == 0)
374 return respond_JSON_Failure(connection);
376 username = MHD_digest_auth_get_username(connection);
377 struct blerg *b = blerg_open(username);
379 return respond_JSON_Failure(connection);
380 if (blerg_store(b, ps->data, ps->data_size) == -1) {
382 return respond_JSON_Failure(connection);
386 MHD_destroy_post_processor(ps->pp);
392 return respond_JSON_Success(connection);
393 } else if (strncmp(url, "/info", 5) == 0) {
397 if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
398 return respond_405(connection);
404 return respond_404(connection);
406 ret = parse_url_info(url + 6, &info);
407 if ((ret & URL_INFO_AUTHOR) == 0)
408 return respond_404(connection);
410 if (!blerg_exists(info.author))
411 return respond_404(connection);
415 struct blerg *b = blerg_open(info.author);
416 uint64_t record_count = blerg_get_record_count(b);
420 yajl_gen g = yajl_gen_alloc(&yajl_c, NULL);
421 yajl_gen_map_open(g);
422 yajl_gen_string(g, "record_count", 12);
423 snprintf(number, 21, "%llu", record_count);
424 yajl_gen_string(g, number, strlen(number));
425 yajl_gen_map_close(g);
427 const unsigned char *ybuf;
428 yajl_gen_get_buf(g, &ybuf, &len);
430 response = MHD_create_response_from_data(len, (void *)ybuf, MHD_NO, MHD_YES);
431 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
432 MHD_destroy_response(response);
437 } else if (strncmp(url, "/create", 8) == 0) {
438 struct auth_state *as = (struct auth_state *) *ptr;
441 if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
442 return respond_405(connection);
444 struct auth_state *as = malloc(sizeof(struct auth_state));
445 as->username[0] = as->password[0] = 0;
446 as->pp = MHD_create_post_processor(connection, 1024, &POST_auth_iterator, as);
451 if (*upload_data_size) {
452 MHD_post_process(as->pp, upload_data, *upload_data_size);
453 *upload_data_size = 0;
457 if (as->username[0] == 0 || as->password[0] == 0)
458 return respond_JSON_Failure(connection);
460 if (blerg_exists(as->username))
461 return respond_JSON_Failure(connection);
463 struct blerg *b = blerg_open(as->username);
465 auth_set_password(as->username, as->password);
467 MHD_destroy_post_processor(as->pp);
471 return respond_JSON_Success(connection);
472 } else if (strncmp(url, "/login", 7) == 0) {
473 struct auth_state *as = (struct auth_state *) *ptr;
476 if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
477 return respond_405(connection);
479 struct auth_state *as = malloc(sizeof(struct auth_state));
480 as->username[0] = as->password[0] = 0;
481 as->pp = MHD_create_post_processor(connection, 1024, &POST_auth_iterator, as);
486 if (*upload_data_size) {
487 MHD_post_process(as->pp, upload_data, *upload_data_size);
488 *upload_data_size = 0;
492 if (as->username[0] == 0 || as->password[0] == 0)
493 return respond_JSON_Failure(connection);
495 if (!auth_login(as->username, as->password))
496 return respond_JSON_Failure(connection);
498 response = MHD_create_response_from_data(strlen(JSON_SUCCESS), JSON_SUCCESS, MHD_NO, MHD_NO);
500 char *token = auth_get_token(as->username);
502 snprintf(data, 512, "auth=%s", token);
503 MHD_add_response_header(response, "Set-Cookie", data);
507 MHD_destroy_post_processor(as->pp);
511 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
512 MHD_destroy_response(response);
515 } else if (strncmp(url, "/logout", 8) == 0) {
516 struct auth_state *as = (struct auth_state *) *ptr;
519 if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
520 return respond_405(connection);
522 struct auth_state *as = malloc(sizeof(struct auth_state));
523 as->username[0] = as->password[0] = 0;
524 as->pp = MHD_create_post_processor(connection, 1024, &POST_auth_iterator, as);
529 if (*upload_data_size) {
530 MHD_post_process(as->pp, upload_data, *upload_data_size);
531 *upload_data_size = 0;
535 auth_logout(as->username);
537 return respond_JSON_Success(connection);
539 return respond_404(connection);
544 int main(int argc, char *argv[]) {
545 struct MHD_Daemon *daemon;
551 daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
552 if (daemon == NULL) {
553 fprintf(stderr, "Could not start web server\n");
558 FD_ZERO(&rs); FD_ZERO(&ws); FD_ZERO(&es);
559 if (MHD_get_fdset(daemon, &rs, &ws, &es, &max) != MHD_YES) {
560 fprintf(stderr, "Fatal error getting fd sets\n");
563 select(max + 1, &rs, &ws, &es, NULL);
566 MHD_stop_daemon(daemon);