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);
137 json_generate_one_record(ts->g, ts->results[ts->i].author, b, ts->results[ts->i].record);
143 yajl_gen_array_close(ts->g);
149 yajl_gen_get_buf(ts->g, &ybuf, &len);
151 memcpy(buf, ybuf, max);
155 memcpy(buf, ybuf, len);
156 yajl_gen_clear(ts->g);
161 void GET_generate_taglist_free(void *cls) {
162 struct tag_state *ts = cls;
164 yajl_gen_free(ts->g);
169 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) {
170 struct create_state *cs = cls;
172 if (strncmp(key, "username", 9) == 0) {
173 if (size > 32) size = 32;
174 memcpy(cs->username, data, size);
175 cs->username[size] = 0;
176 } else if (strncmp(key, "password", 9) == 0) {
177 if (size > 32) size = 32;
178 memcpy(cs->password, data, size);
179 cs->password[size] = 0;
185 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) {
186 struct put_state *ps = cls;
188 if (strncmp(key, "data", 5) == 0) {
189 if (ps->data == NULL) {
190 ps->data_size = size;
191 ps->data = malloc(size);
193 if (ps->data_size + size > MAX_RECORD_SIZE) {
194 size = MAX_RECORD_SIZE - ps->data_size;
196 ps->data_size += size;
197 ps->data = realloc(ps->data, ps->data_size);
199 memcpy(ps->data + off, data, size);
200 if (ps->data_size == MAX_RECORD_SIZE)
207 struct MHD_Response *create_response_for_range(struct blerg *b, uint64_t from, uint64_t to) {
208 struct MHD_Response *response;
209 struct get_state *gs = malloc(sizeof(struct get_state));
212 uint64_t record_count = blerg_get_record_count(b);
214 if (from > to || from >= record_count || to >= record_count || to - from > 99) {
220 gs->entries = make_sequential_list(from, to);
223 gs->g = yajl_gen_alloc(&yajl_c, NULL);
224 gs->yoff = gs->done = 0;
226 response = MHD_create_response_from_callback(-1, 262144, &GET_generate_list, gs, &GET_generate_list_free);
231 struct MHD_Response *create_tag_response(struct tag *results, uint64_t len) {
232 struct tag_state *ts = malloc(sizeof(struct tag_state));
233 ts->g = yajl_gen_alloc(&yajl_c, NULL);
234 ts->results = results;
236 ts->yoff = ts->done = 0;
238 return MHD_create_response_from_callback(-1, 262144, &GET_generate_taglist, ts, &GET_generate_taglist_free);
242 ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const char *method,
243 const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) {
244 struct MHD_Response *response;
246 struct url_info info;
249 if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) {
251 if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
252 return respond_405(connection);
259 return respond_404(connection);
261 ret = parse_url_info(url + 5, &info);
262 if ((ret & URL_INFO_AUTHOR) == 0)
263 return respond_404(connection);
265 if (!blerg_exists(info.author))
266 return respond_404(connection);
270 struct blerg *b = blerg_open(info.author);
272 if ((ret & URL_INFO_RECORD) && (ret & URL_INFO_RECORD_TO)) {
273 response = create_response_for_range(b, info.record, info.record_to);
274 } else if (ret & URL_INFO_RECORD) {
275 ret = blerg_fetch(b, info.record, &data, &len);
279 return respond_404(connection);
280 response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
282 uint64_t record_count, from, to;
283 record_count = blerg_get_record_count(b);
284 if (record_count == 0) {
286 response = MHD_create_response_from_data(2, "[]", MHD_NO, MHD_NO);
288 to = record_count - 1;
289 from = (record_count > 50 ? to - 49 : 0);
290 response = create_response_for_range(b, from, to);
294 if (response == NULL) {
296 return respond_JSON_Failure(connection);
299 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
300 MHD_destroy_response(response);
302 } else if (strncmp(url, "/tag", 4) == 0 && strlen(url) > 4) {
304 if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
305 return respond_405(connection);
312 return respond_404(connection);
314 ret = parse_url_info(url + 5, &info);
315 if ((ret & URL_INFO_AUTHOR) == 0)
316 return respond_404(connection);
318 if (!tag_exists(info.author))
319 return respond_404(connection);
322 struct tag *taglist = tag_list(info.author, 0, &recs, -1);
325 response = MHD_create_response_from_data(2, "[]", MHD_NO, MHD_NO);
327 response = create_tag_response(taglist, recs);
330 if (response == NULL)
331 return respond_JSON_Failure(connection);
333 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
334 MHD_destroy_response(response);
337 } else if (strncmp(url, "/put", 4) == 0) {
338 struct put_state *ps = (struct put_state *) *ptr;
343 if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
344 return respond_405(connection);
347 return respond_404(connection);
351 username = MHD_digest_auth_get_username(connection);
352 if (username == NULL)
353 return respond_401(connection, MHD_NO);
354 auth_get_password(username, password);
356 ret = MHD_digest_auth_check(connection, REALM, username, password, 300);
359 if (ret == MHD_INVALID_NONCE || ret == MHD_NO)
360 return respond_401(connection, (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);
362 struct put_state *ps = malloc(sizeof(struct put_state));
365 ps->pp = MHD_create_post_processor(connection, 16384, &POST_put_iterator, ps);
370 if (*upload_data_size) {
371 MHD_post_process(ps->pp, upload_data, *upload_data_size);
372 *upload_data_size = 0;
376 if (ps->data == NULL || ps->data_size == 0)
377 return respond_JSON_Failure(connection);
379 username = MHD_digest_auth_get_username(connection);
380 struct blerg *b = blerg_open(username);
382 return respond_JSON_Failure(connection);
383 if (blerg_store(b, ps->data, ps->data_size) == -1) {
385 return respond_JSON_Failure(connection);
389 MHD_destroy_post_processor(ps->pp);
395 return respond_JSON_Success(connection);
396 } else if (strncmp(url, "/info", 5) == 0) {
400 if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
401 return respond_405(connection);
407 return respond_404(connection);
409 ret = parse_url_info(url + 6, &info);
410 if ((ret & URL_INFO_AUTHOR) == 0)
411 return respond_404(connection);
413 if (!blerg_exists(info.author))
414 return respond_404(connection);
418 struct blerg *b = blerg_open(info.author);
419 uint64_t record_count = blerg_get_record_count(b);
423 yajl_gen g = yajl_gen_alloc(&yajl_c, NULL);
424 yajl_gen_map_open(g);
425 yajl_gen_string(g, "record_count", 12);
426 snprintf(number, 21, "%llu", record_count);
427 yajl_gen_string(g, number, strlen(number));
428 yajl_gen_map_close(g);
430 const unsigned char *ybuf;
431 yajl_gen_get_buf(g, &ybuf, &len);
433 response = MHD_create_response_from_data(len, (void *)ybuf, MHD_NO, MHD_YES);
434 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
435 MHD_destroy_response(response);
440 } else if (strncmp(url, "/create", 8) == 0) {
441 struct create_state *cs = (struct create_state *) *ptr;
444 if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
445 return respond_405(connection);
447 struct create_state *cs = malloc(sizeof(struct create_state));
448 cs->username[0] = cs->password[0] = 0;
449 cs->pp = MHD_create_post_processor(connection, 1024, &POST_create_iterator, cs);
454 if (*upload_data_size) {
455 MHD_post_process(cs->pp, upload_data, *upload_data_size);
456 *upload_data_size = 0;
460 if (cs->username[0] == 0 || cs->password[0] == 0)
461 return respond_JSON_Failure(connection);
463 if (blerg_exists(cs->username))
464 return respond_JSON_Failure(connection);
466 struct blerg *b = blerg_open(cs->username);
468 auth_set_password(cs->username, cs->password);
470 MHD_destroy_post_processor(cs->pp);
474 return respond_JSON_Success(connection);
476 return respond_404(connection);
481 int main(int argc, char *argv[]) {
482 struct MHD_Daemon *daemon;
488 daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
489 if (daemon == NULL) {
490 fprintf(stderr, "Could not start web server\n");
495 FD_ZERO(&rs); FD_ZERO(&ws); FD_ZERO(&es);
496 if (MHD_get_fdset(daemon, &rs, &ws, &es, &max) != MHD_YES) {
497 fprintf(stderr, "Fatal error getting fd sets\n");
500 select(max + 1, &rs, &ws, &es, NULL);
503 MHD_stop_daemon(daemon);