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, "data", 4);
123 yajl_gen_string(gs->g, data, len);
124 yajl_gen_map_close(gs->g);
128 yajl_gen_array_close(gs->g);
134 yajl_gen_get_buf(gs->g, &ybuf, &len);
136 memcpy(buf, ybuf, max);
140 memcpy(buf, ybuf, len);
141 yajl_gen_clear(gs->g);
146 void GET_generate_list_free(void *cls) {
147 struct get_state *gs = cls;
150 yajl_gen_free(gs->g);
155 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) {
156 struct create_state *cs = cls;
158 if (strncmp(key, "username", 9) == 0) {
159 if (size > 32) size = 32;
160 memcpy(cs->username, data, size);
161 cs->username[size] = 0;
162 } else if (strncmp(key, "password", 9) == 0) {
163 if (size > 32) size = 32;
164 memcpy(cs->password, data, size);
165 cs->password[size] = 0;
171 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) {
172 struct put_state *ps = cls;
174 if (strncmp(key, "data", 5) == 0) {
175 if (ps->data == NULL) {
176 ps->data_size = size;
177 ps->data = malloc(size);
179 if (ps->data_size + size > MAX_RECORD_SIZE) {
180 size = MAX_RECORD_SIZE - ps->data_size;
182 ps->data_size += size;
183 ps->data = realloc(ps->data, ps->data_size);
185 memcpy(ps->data + off, data, size);
186 if (ps->data_size == MAX_RECORD_SIZE)
193 struct MHD_Response *create_response_for_range(struct blerg *b, uint64_t from, uint64_t to) {
194 struct MHD_Response *response;
195 struct get_state *gs = malloc(sizeof(struct get_state));
198 uint64_t record_count = blerg_get_record_count(b);
200 if (from > to || from >= record_count || to >= record_count || to - from > 99) {
206 if (record_count == 0) {
207 response = MHD_create_response_from_data(2, "[]", MHD_NO, MHD_NO);
209 gs->entries = make_sequential_list(from, to);
212 gs->g = yajl_gen_alloc(&yajl_c, NULL);
213 gs->yoff = gs->done = 0;
215 response = MHD_create_response_from_callback(-1, 262144, &GET_generate_list, gs, &GET_generate_list_free);
222 ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const char *method,
223 const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) {
224 struct MHD_Response *response;
226 struct url_info info;
229 if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) {
231 if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
232 return respond_405(connection);
239 return respond_404(connection);
241 ret = parse_url_info(url + 5, &info);
242 if ((ret & URL_INFO_AUTHOR) == 0)
243 return respond_404(connection);
245 if (!blerg_exists(info.author))
246 return respond_404(connection);
250 struct blerg *b = blerg_open(info.author);
252 if ((ret & URL_INFO_RECORD) && (ret & URL_INFO_RECORD_TO)) {
253 response = create_response_for_range(b, info.record, info.record_to);
254 } else if (ret & URL_INFO_RECORD) {
255 ret = blerg_fetch(b, info.record, &data, &len);
259 return respond_404(connection);
260 response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
262 uint64_t record_count, from, to;
263 record_count = blerg_get_record_count(b);
264 if (record_count == 0) {
265 response = create_response_for_range(b, 0, 0);
267 to = record_count - 1;
268 from = (record_count > 50 ? to - 49 : 0);
269 response = create_response_for_range(b, from, to);
273 if (response == NULL)
274 return respond_JSON_Failure(connection);
276 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
277 MHD_destroy_response(response);
279 } else if (strncmp(url, "/put", 4) == 0) {
280 struct put_state *ps = (struct put_state *) *ptr;
285 if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
286 return respond_405(connection);
289 return respond_404(connection);
293 username = MHD_digest_auth_get_username(connection);
294 if (username == NULL)
295 return respond_401(connection, MHD_NO);
296 auth_get_password(username, password);
298 ret = MHD_digest_auth_check(connection, REALM, username, password, 300);
301 if (ret == MHD_INVALID_NONCE || ret == MHD_NO)
302 return respond_401(connection, (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);
304 struct put_state *ps = malloc(sizeof(struct put_state));
307 ps->pp = MHD_create_post_processor(connection, 16384, &POST_put_iterator, ps);
312 if (*upload_data_size) {
313 MHD_post_process(ps->pp, upload_data, *upload_data_size);
314 *upload_data_size = 0;
318 if (ps->data == NULL || ps->data_size == 0)
319 return respond_JSON_Failure(connection);
321 username = MHD_digest_auth_get_username(connection);
322 struct blerg *b = blerg_open(username);
324 return respond_JSON_Failure(connection);
325 if (blerg_store(b, ps->data, ps->data_size) == -1) {
327 return respond_JSON_Failure(connection);
331 MHD_destroy_post_processor(ps->pp);
337 return respond_JSON_Success(connection);
338 } else if (strncmp(url, "/info", 5) == 0) {
342 if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
343 return respond_405(connection);
349 return respond_404(connection);
351 ret = parse_url_info(url + 6, &info);
352 if ((ret & URL_INFO_AUTHOR) == 0)
353 return respond_404(connection);
355 if (!blerg_exists(info.author))
356 return respond_404(connection);
360 struct blerg *b = blerg_open(info.author);
361 uint64_t record_count = blerg_get_record_count(b);
365 yajl_gen g = yajl_gen_alloc(&yajl_c, NULL);
366 yajl_gen_map_open(g);
367 yajl_gen_string(g, "record_count", 12);
368 snprintf(number, 21, "%llu", record_count);
369 yajl_gen_string(g, number, strlen(number));
370 yajl_gen_map_close(g);
372 const unsigned char *ybuf;
373 yajl_gen_get_buf(g, &ybuf, &len);
375 response = MHD_create_response_from_data(len, (void *)ybuf, MHD_NO, MHD_YES);
376 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
377 MHD_destroy_response(response);
382 } else if (strncmp(url, "/create", 8) == 0) {
383 struct create_state *cs = (struct create_state *) *ptr;
386 if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
387 return respond_405(connection);
389 struct create_state *cs = malloc(sizeof(struct create_state));
390 cs->username[0] = cs->password[0] = 0;
391 cs->pp = MHD_create_post_processor(connection, 1024, &POST_create_iterator, cs);
396 if (*upload_data_size) {
397 MHD_post_process(cs->pp, upload_data, *upload_data_size);
398 *upload_data_size = 0;
402 if (cs->username[0] == 0 || cs->password[0] == 0)
403 return respond_JSON_Failure(connection);
405 if (blerg_exists(cs->username))
406 return respond_JSON_Failure(connection);
408 struct blerg *b = blerg_open(cs->username);
410 auth_set_password(cs->username, cs->password);
412 MHD_destroy_post_processor(cs->pp);
416 return respond_JSON_Success(connection);
418 return respond_404(connection);
423 int main(int argc, char *argv[]) {
424 struct MHD_Daemon *daemon;
430 daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
431 if (daemon == NULL) {
432 fprintf(stderr, "Could not start web server\n");
437 FD_ZERO(&rs); FD_ZERO(&ws); FD_ZERO(&es);
438 if (MHD_get_fdset(daemon, &rs, &ws, &es, &max) != MHD_YES) {
439 fprintf(stderr, "Fatal error getting fd sets\n");
442 select(max + 1, &rs, &ws, &es, NULL);
445 MHD_stop_daemon(daemon);