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;
41 int parse_url_info(const char *url, struct url_info *info) {
54 memcpy(info->author, url, len);
55 info->author[len] = 0;
56 info->contents |= URL_INFO_AUTHOR;
58 if (c == NULL || c[1] == 0)
59 return info->contents;
61 info->record = strtoull(c + 1, NULL, 10);
62 info->contents |= URL_INFO_RECORD;
65 if (c == NULL || c[1] == 0)
66 return info->contents;
68 info->record_to = strtoull(c + 1, NULL, 10);
69 info->contents |= URL_INFO_RECORD_TO;
71 return info->contents;
74 uint64_t *make_sequential_list(uint64_t from, uint64_t to) {
75 uint64_t len = to - from + 1;
76 uint64_t *list = malloc(len * sizeof(uint64_t));
79 for (i = 0; i < len; i++) {
86 ssize_t GET_generate_list(void *cls, uint64_t pos, char *buf, size_t max) {
87 struct get_state *gs = cls;
88 const unsigned char *ybuf;
94 yajl_gen_get_buf(gs->g, &ybuf, &len);
95 size_t bytes_remaining = len - gs->yoff;
96 if (bytes_remaining > max) {
97 memcpy(buf, ybuf + gs->yoff, max);
101 memcpy(buf, ybuf + gs->yoff, bytes_remaining);
103 yajl_gen_clear(gs->g);
104 return bytes_remaining;
111 if (pos == 0) { /* Start iterating */
112 yajl_gen_array_open(gs->g);
115 /* Snarf one record */
116 blerg_fetch(gs->b, gs->entries[gs->i], &data, &len);
118 yajl_gen_map_open(gs->g);
119 yajl_gen_string(gs->g, "record", 6);
120 snprintf(number, 21, "%llu", gs->entries[gs->i]);
121 yajl_gen_string(gs->g, number, strlen(number));
122 yajl_gen_string(gs->g, "timestamp", 9);
123 yajl_gen_integer(gs->g, blerg_get_timestamp(gs->b, gs->entries[gs->i]));
124 yajl_gen_string(gs->g, "data", 4);
125 yajl_gen_string(gs->g, data, len);
126 yajl_gen_map_close(gs->g);
130 yajl_gen_array_close(gs->g);
136 yajl_gen_get_buf(gs->g, &ybuf, &len);
138 memcpy(buf, ybuf, max);
142 memcpy(buf, ybuf, len);
143 yajl_gen_clear(gs->g);
148 void GET_generate_list_free(void *cls) {
149 struct get_state *gs = cls;
152 yajl_gen_free(gs->g);
157 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) {
158 struct create_state *cs = cls;
160 if (strncmp(key, "username", 9) == 0) {
161 if (size > 32) size = 32;
162 memcpy(cs->username, data, size);
163 cs->username[size] = 0;
164 } else if (strncmp(key, "password", 9) == 0) {
165 if (size > 32) size = 32;
166 memcpy(cs->password, data, size);
167 cs->password[size] = 0;
173 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) {
174 struct put_state *ps = cls;
176 if (strncmp(key, "data", 5) == 0) {
177 if (ps->data == NULL) {
178 ps->data_size = size;
179 ps->data = malloc(size);
181 if (ps->data_size + size > MAX_RECORD_SIZE) {
182 size = MAX_RECORD_SIZE - ps->data_size;
184 ps->data_size += size;
185 ps->data = realloc(ps->data, ps->data_size);
187 memcpy(ps->data + off, data, size);
188 if (ps->data_size == MAX_RECORD_SIZE)
195 struct MHD_Response *create_response_for_range(struct blerg *b, uint64_t from, uint64_t to) {
196 struct MHD_Response *response;
197 struct get_state *gs = malloc(sizeof(struct get_state));
200 uint64_t record_count = blerg_get_record_count(b);
202 if (from > to || from >= record_count || to >= record_count || to - from > 99) {
208 gs->entries = make_sequential_list(from, to);
211 gs->g = yajl_gen_alloc(&yajl_c, NULL);
212 gs->yoff = gs->done = 0;
214 response = MHD_create_response_from_callback(-1, 262144, &GET_generate_list, gs, &GET_generate_list_free);
220 ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const char *method,
221 const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) {
222 struct MHD_Response *response;
224 struct url_info info;
227 if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) {
229 if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
230 return respond_405(connection);
237 return respond_404(connection);
239 ret = parse_url_info(url + 5, &info);
240 if ((ret & URL_INFO_AUTHOR) == 0)
241 return respond_404(connection);
243 if (!blerg_exists(info.author))
244 return respond_404(connection);
248 struct blerg *b = blerg_open(info.author);
250 if ((ret & URL_INFO_RECORD) && (ret & URL_INFO_RECORD_TO)) {
251 response = create_response_for_range(b, info.record, info.record_to);
252 } else if (ret & URL_INFO_RECORD) {
253 ret = blerg_fetch(b, info.record, &data, &len);
257 return respond_404(connection);
258 response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
260 uint64_t record_count, from, to;
261 record_count = blerg_get_record_count(b);
262 if (record_count == 0) {
263 response = MHD_create_response_from_data(2, "[]", MHD_NO, MHD_NO);
265 to = record_count - 1;
266 from = (record_count > 50 ? to - 49 : 0);
267 response = create_response_for_range(b, from, to);
271 if (response == NULL)
272 return respond_JSON_Failure(connection);
274 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
275 MHD_destroy_response(response);
277 } else if (strncmp(url, "/put", 4) == 0) {
278 struct put_state *ps = (struct put_state *) *ptr;
283 if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
284 return respond_405(connection);
287 return respond_404(connection);
291 username = MHD_digest_auth_get_username(connection);
292 if (username == NULL)
293 return respond_401(connection, MHD_NO);
294 auth_get_password(username, password);
296 ret = MHD_digest_auth_check(connection, REALM, username, password, 300);
299 if (ret == MHD_INVALID_NONCE || ret == MHD_NO)
300 return respond_401(connection, (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);
302 struct put_state *ps = malloc(sizeof(struct put_state));
305 ps->pp = MHD_create_post_processor(connection, 16384, &POST_put_iterator, ps);
310 if (*upload_data_size) {
311 MHD_post_process(ps->pp, upload_data, *upload_data_size);
312 *upload_data_size = 0;
316 if (ps->data == NULL || ps->data_size == 0)
317 return respond_JSON_Failure(connection);
319 username = MHD_digest_auth_get_username(connection);
320 struct blerg *b = blerg_open(username);
322 return respond_JSON_Failure(connection);
323 if (blerg_store(b, ps->data, ps->data_size) == -1) {
325 return respond_JSON_Failure(connection);
329 MHD_destroy_post_processor(ps->pp);
335 return respond_JSON_Success(connection);
336 } else if (strncmp(url, "/info", 5) == 0) {
340 if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
341 return respond_405(connection);
347 return respond_404(connection);
349 ret = parse_url_info(url + 6, &info);
350 if ((ret & URL_INFO_AUTHOR) == 0)
351 return respond_404(connection);
353 if (!blerg_exists(info.author))
354 return respond_404(connection);
358 struct blerg *b = blerg_open(info.author);
359 uint64_t record_count = blerg_get_record_count(b);
363 yajl_gen g = yajl_gen_alloc(&yajl_c, NULL);
364 yajl_gen_map_open(g);
365 yajl_gen_string(g, "record_count", 12);
366 snprintf(number, 21, "%llu", record_count);
367 yajl_gen_string(g, number, strlen(number));
368 yajl_gen_map_close(g);
370 const unsigned char *ybuf;
371 yajl_gen_get_buf(g, &ybuf, &len);
373 response = MHD_create_response_from_data(len, (void *)ybuf, MHD_NO, MHD_YES);
374 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
375 MHD_destroy_response(response);
380 } else if (strncmp(url, "/create", 8) == 0) {
381 struct create_state *cs = (struct create_state *) *ptr;
384 if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
385 return respond_405(connection);
387 struct create_state *cs = malloc(sizeof(struct create_state));
388 cs->username[0] = cs->password[0] = 0;
389 cs->pp = MHD_create_post_processor(connection, 1024, &POST_create_iterator, cs);
394 if (*upload_data_size) {
395 MHD_post_process(cs->pp, upload_data, *upload_data_size);
396 *upload_data_size = 0;
400 if (cs->username[0] == 0 || cs->password[0] == 0)
401 return respond_JSON_Failure(connection);
403 if (blerg_exists(cs->username))
404 return respond_JSON_Failure(connection);
406 struct blerg *b = blerg_open(cs->username);
408 auth_set_password(cs->username, cs->password);
410 MHD_destroy_post_processor(cs->pp);
414 return respond_JSON_Success(connection);
416 return respond_404(connection);
421 int main(int argc, char *argv[]) {
422 struct MHD_Daemon *daemon;
428 daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
429 if (daemon == NULL) {
430 fprintf(stderr, "Could not start web server\n");
435 FD_ZERO(&rs); FD_ZERO(&ws); FD_ZERO(&es);
436 if (MHD_get_fdset(daemon, &rs, &ws, &es, &max) != MHD_YES) {
437 fprintf(stderr, "Fatal error getting fd sets\n");
440 select(max + 1, &rs, &ws, &es, NULL);
443 MHD_stop_daemon(daemon);