Organize source tree
[blerg.git] / http / http_blerg.c
diff --git a/http/http_blerg.c b/http/http_blerg.c
new file mode 100644 (file)
index 0000000..73f88fe
--- /dev/null
@@ -0,0 +1,582 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <microhttpd.h>
+#include <yajl/yajl_gen.h>
+#include "database.h"
+#include "tags.h"
+#include "auth.h"
+#include "canned_responses.h"
+#include "http.h"
+#include "config.h"
+
+#define URL_INFO_AUTHOR    0x1
+#define URL_INFO_RECORD    0x2
+#define URL_INFO_RECORD_TO 0x4
+#define DERP "DERP DERP DERP"
+
+yajl_gen_config yajl_c = { 0, 0 };
+
+struct create_state {
+       struct MHD_PostProcessor *pp;
+       char username[33];
+       char password[33];
+};
+
+struct put_state {
+       struct MHD_PostProcessor *pp;
+       char *data;
+       int data_size;
+};
+
+struct get_state {
+       struct blerg *b;
+       yajl_gen g;
+       unsigned int yoff;
+       uint64_t *entries;
+       uint64_t i;
+       int done;
+};
+
+struct tag_state {
+       yajl_gen g;
+       unsigned int yoff;
+       struct tag *results;
+       uint64_t i;
+       int done;
+};
+
+int parse_url_info(const char *url, struct url_info *info) {
+       const char *c;
+       int len;
+       info->contents = 0;
+
+       c = strchr(url, '/');
+       if (c == NULL) {
+               len = strlen(url);
+       } else {
+               len = c - url;
+       }
+       if (len == 0)
+               return 0;
+       memcpy(info->author, url, len);
+       info->author[len] = 0;
+       info->contents |= URL_INFO_AUTHOR;
+
+       if (c == NULL || c[1] == 0)
+               return info->contents;
+
+       info->record = strtoull(c + 1, NULL, 10);
+       info->contents |= URL_INFO_RECORD;
+
+       c = strchr(c, '-');
+       if (c == NULL || c[1] == 0)
+               return info->contents;
+
+       info->record_to = strtoull(c + 1, NULL, 10);
+       info->contents |= URL_INFO_RECORD_TO;
+
+       return info->contents;
+}
+
+uint64_t *make_sequential_list(uint64_t from, uint64_t to) {
+       uint64_t len = to - from + 1;
+       uint64_t *list = malloc(len * sizeof(uint64_t));
+       uint64_t i;
+
+       for (i = 0; i < len; i++) {
+               list[i] = from + i;
+       }
+
+       return list;
+}
+
+void json_generate_one_record(yajl_gen g, const char *author, struct blerg *b, uint64_t record) {
+       char *data;
+       char number[21];
+       int len;
+
+       if (!blerg_fetch(b, record, &data, &len)) {
+               fprintf(stderr, "Could not fetch record\n");
+               return;
+       }
+
+       yajl_gen_map_open(g);
+       if (author != NULL) {
+               yajl_gen_string(g, "author", 6);
+               yajl_gen_string(g, author, strlen(author));
+       }
+       yajl_gen_string(g, "record", 6);
+       snprintf(number, 21, "%llu", record);
+       yajl_gen_string(g, number, strlen(number));
+       yajl_gen_string(g, "timestamp", 9);
+       yajl_gen_integer(g, blerg_get_timestamp(b, record));
+       yajl_gen_string(g, "data", 4);
+       yajl_gen_string(g, data, len);
+       yajl_gen_map_close(g);
+
+       free(data);
+}
+
+ssize_t GET_generate_list(void *cls, uint64_t pos, char *buf, size_t max) {
+       struct get_state *gs = cls;
+       const unsigned char *ybuf;
+       char *data;
+       char number[21];
+       unsigned int len;
+
+       if (gs->yoff > 0) {
+               yajl_gen_get_buf(gs->g, &ybuf, &len);
+               size_t bytes_remaining = len - gs->yoff;
+               if (bytes_remaining > max) {
+                       memcpy(buf, ybuf + gs->yoff, max);
+                       gs->yoff += max;
+                       return max;
+               } else {
+                       memcpy(buf, ybuf + gs->yoff, bytes_remaining);
+                       gs->yoff = 0;
+                       yajl_gen_clear(gs->g);
+                       return bytes_remaining;
+               }
+       }
+
+       if (gs->done)
+               return -1;
+
+       if (pos == 0) { /* Start iterating */
+               yajl_gen_array_open(gs->g);
+       }
+
+       /* Snarf one record */
+       json_generate_one_record(gs->g, NULL, gs->b, gs->entries[gs->i]);
+
+       if (gs->i == 0) {
+               yajl_gen_array_close(gs->g);
+               gs->done = 1;
+       }
+       gs->i--;
+
+
+       yajl_gen_get_buf(gs->g, &ybuf, &len);
+       if (len > max) {
+               memcpy(buf, ybuf, max);
+               gs->yoff = max;
+               return max;
+       } else {
+               memcpy(buf, ybuf, len);
+               yajl_gen_clear(gs->g);
+               return len;
+       }
+}
+
+void GET_generate_list_free(void *cls) {
+       struct get_state *gs = cls;
+
+       blerg_close(gs->b);
+       yajl_gen_free(gs->g);
+       free(gs->entries);
+       free(gs);
+}
+
+ssize_t GET_generate_taglist(void *cls, uint64_t pos, char *buf, size_t max) {
+       struct tag_state *ts = cls;
+       struct blerg *b;
+       const unsigned char *ybuf;
+       unsigned int len;
+
+       if (ts->yoff > 0) {
+               yajl_gen_get_buf(ts->g, &ybuf, &len);
+               size_t bytes_remaining = len - ts->yoff;
+               if (bytes_remaining > max) {
+                       memcpy(buf, ybuf + ts->yoff, max);
+                       ts->yoff += max;
+                       return max;
+               } else {
+                       memcpy(buf, ybuf + ts->yoff, bytes_remaining);
+                       ts->yoff = 0;
+                       yajl_gen_clear(ts->g);
+                       return bytes_remaining;
+               }
+       }
+
+       if (ts->done)
+               return -1;
+
+       if (pos == 0) { /* Start iterating */
+               yajl_gen_array_open(ts->g);
+       }
+
+       /* Snarf one record */
+       b = blerg_open(ts->results[ts->i].author);
+       if (b == NULL)
+               goto skip_bad_blerg;
+
+       json_generate_one_record(ts->g, ts->results[ts->i].author, b, ts->results[ts->i].record);
+
+       blerg_close(b);
+
+skip_bad_blerg:
+       if (ts->i == 0) {
+               yajl_gen_array_close(ts->g);
+               ts->done = 1;
+       }
+
+       ts->i--;
+
+       yajl_gen_get_buf(ts->g, &ybuf, &len);
+       if (len > max) {
+               memcpy(buf, ybuf, max);
+               ts->yoff = max;
+               return max;
+       } else {
+               memcpy(buf, ybuf, len);
+               yajl_gen_clear(ts->g);
+               return len;
+       }
+}
+
+void GET_generate_taglist_free(void *cls) {
+       struct tag_state *ts = cls;
+
+       yajl_gen_free(ts->g);
+       free(ts->results);
+       free(ts);
+}
+
+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) {
+       struct create_state *cs = cls;
+
+       if (strncmp(key, "username", 9) == 0) {
+               if (size > 32) size = 32;
+               memcpy(cs->username, data, size);
+               cs->username[size] = 0;
+       } else if (strncmp(key, "password", 9) == 0) {
+               if (size > 32) size = 32;
+               memcpy(cs->password, data, size);
+               cs->password[size] = 0;
+       }
+
+       return MHD_YES;
+}
+
+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) {
+       struct put_state *ps = cls;
+
+       if (strncmp(key, "data", 5) == 0) {
+               if (ps->data == NULL) {
+                       ps->data_size = size;
+                       ps->data = malloc(size);
+               } else {
+                       if (ps->data_size + size > MAX_RECORD_SIZE) {
+                               size = MAX_RECORD_SIZE - ps->data_size;
+                       }
+                       ps->data_size += size;
+                       ps->data = realloc(ps->data, ps->data_size);
+               }
+               memcpy(ps->data + off, data, size);
+               if (ps->data_size == MAX_RECORD_SIZE)
+                       return MHD_NO;
+       }
+
+       return MHD_YES;
+}
+
+struct MHD_Response *create_response_for_range(struct blerg *b, uint64_t from, uint64_t to) {
+       struct MHD_Response *response;
+       struct get_state *gs = malloc(sizeof(struct get_state));
+       gs->b = b;
+
+       uint64_t record_count = blerg_get_record_count(b);
+
+       if (from > to || from >= record_count || to >= record_count || to - from > 99) {
+               blerg_close(b);
+               free(gs);
+               return NULL;
+       }
+
+       gs->entries = make_sequential_list(from, to);
+       gs->i = to - from;
+
+       gs->g = yajl_gen_alloc(&yajl_c, NULL);
+       gs->yoff = gs->done = 0;
+
+       response = MHD_create_response_from_callback(-1, 262144, &GET_generate_list, gs, &GET_generate_list_free);
+
+       return response;
+}
+
+struct MHD_Response *create_tag_response(struct tag *results, uint64_t len) {
+       struct tag_state *ts = malloc(sizeof(struct tag_state));
+       ts->g = yajl_gen_alloc(&yajl_c, NULL);
+       ts->results = results;
+       ts->i = len - 1;
+       ts->yoff = ts->done = 0;
+
+       return MHD_create_response_from_callback(-1, 262144, &GET_generate_taglist, ts, &GET_generate_taglist_free);
+}
+
+static int
+ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const char *method,
+         const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) {
+       struct MHD_Response *response;
+       int ret, len;
+       struct url_info info;
+       char *data;
+
+       if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) {
+               if (*ptr == NULL) {
+                       if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
+                               return respond_405(connection);
+
+                       *ptr = (void *) 1;
+                       return MHD_YES;
+               }
+
+               if (url[4] != '/')
+                       return respond_404(connection);
+
+               ret = parse_url_info(url + 5, &info);
+               if ((ret & URL_INFO_AUTHOR) == 0)
+                       return respond_404(connection);
+
+               if (!blerg_exists(info.author))
+                       return respond_404(connection);
+
+               *ptr == NULL;
+
+               struct blerg *b = blerg_open(info.author);
+
+               if ((ret & URL_INFO_RECORD) && (ret & URL_INFO_RECORD_TO)) {
+                       response = create_response_for_range(b, info.record, info.record_to);
+               } else if (ret & URL_INFO_RECORD) {
+                       ret = blerg_fetch(b, info.record, &data, &len);
+                       blerg_close(b);
+
+                       if (ret == 0)
+                               return respond_404(connection);
+                       response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
+               } else {
+                       uint64_t record_count, from, to;
+                       record_count = blerg_get_record_count(b);
+                       if (record_count == 0) {
+                               blerg_close(b);
+                               response = MHD_create_response_from_data(2, "[]", MHD_NO, MHD_NO);
+                       } else {
+                               to = record_count - 1;
+                               from = (record_count > 50 ? to - 49 : 0);
+                               response = create_response_for_range(b, from, to);
+                       }
+               }
+
+               if (response == NULL) {
+                       blerg_close(b);
+                       return respond_JSON_Failure(connection);
+               }
+
+               ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
+               MHD_destroy_response(response);
+               return ret;
+       } else if (strncmp(url, "/tag", 4) == 0 && strlen(url) > 4) {
+               if (*ptr == NULL) {
+                       if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
+                               return respond_405(connection);
+
+                       *ptr = (void *) 1;
+                       return MHD_YES;
+               }
+
+               if (url[4] != '/')
+                       return respond_404(connection);
+
+               ret = parse_url_info(url + 5, &info);
+               if ((ret & URL_INFO_AUTHOR) == 0)
+                       return respond_404(connection);
+
+               if (!tag_exists(info.author))
+                       return respond_404(connection);
+
+               uint64_t recs = 50;
+               struct tag *taglist = tag_list(info.author, 0, &recs, -1);
+
+               if (recs == 0) {
+                       response = MHD_create_response_from_data(2, "[]", MHD_NO, MHD_NO);
+               } else {
+                       response = create_tag_response(taglist, recs);
+               }
+
+               if (response == NULL)
+                       return respond_JSON_Failure(connection);
+
+               ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
+               MHD_destroy_response(response);
+
+               return ret;
+       } else if (strncmp(url, "/put", 4) == 0) {
+               struct put_state *ps = (struct put_state *) *ptr;
+               char *username;
+               char password[33];
+
+               if (*ptr == NULL) {
+                       if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
+                               return respond_405(connection);
+
+                       if (url[4] == '/')
+                               return respond_404(connection);
+
+                       *ptr = (void *) 1;
+
+                       username = MHD_digest_auth_get_username(connection);
+                       if (username == NULL)
+                               return respond_401(connection, MHD_NO);
+                       auth_get_password(username, password);
+
+                       ret = MHD_digest_auth_check(connection, REALM, username, password, 300);
+                       free(username);
+
+                       if (ret == MHD_INVALID_NONCE || ret == MHD_NO)
+                               return respond_401(connection, (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);
+
+                       struct put_state *ps = malloc(sizeof(struct put_state));
+                       ps->data = NULL;
+                       ps->data_size = 0;
+                       ps->pp = MHD_create_post_processor(connection, 16384, &POST_put_iterator, ps);
+                       *ptr = ps;
+                       return MHD_YES;
+               }
+
+               if (*upload_data_size) {
+                       MHD_post_process(ps->pp, upload_data, *upload_data_size);
+                       *upload_data_size = 0;
+                       return MHD_YES;
+               }
+
+               if (ps->data == NULL || ps->data_size == 0)
+                       return respond_JSON_Failure(connection);
+
+               username = MHD_digest_auth_get_username(connection);
+               struct blerg *b = blerg_open(username);
+               if (b == NULL)
+                       return respond_JSON_Failure(connection);
+               if (blerg_store(b, ps->data, ps->data_size) == -1) {
+                       blerg_close(b);
+                       return respond_JSON_Failure(connection);
+               }
+               blerg_close(b);
+
+               MHD_destroy_post_processor(ps->pp);
+               free(username);
+               free(ps->data);
+               free(ps);
+               *ptr = NULL;
+
+               return respond_JSON_Success(connection);
+       } else if (strncmp(url, "/info", 5) == 0) {
+               if (*ptr == NULL) {
+                       *ptr = (void *) 1;
+
+                       if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
+                               return respond_405(connection);
+                       return MHD_YES;
+               }
+
+
+               if (url[5] != '/')
+                       return respond_404(connection);
+
+               ret = parse_url_info(url + 6, &info);
+               if ((ret & URL_INFO_AUTHOR) == 0)
+                       return respond_404(connection);
+
+               if (!blerg_exists(info.author))
+                       return respond_404(connection);
+
+               *ptr == NULL;
+
+               struct blerg *b = blerg_open(info.author);
+               uint64_t record_count = blerg_get_record_count(b);
+               blerg_close(b);
+
+               char number[21];
+               yajl_gen g = yajl_gen_alloc(&yajl_c, NULL);
+               yajl_gen_map_open(g);
+               yajl_gen_string(g, "record_count", 12);
+               snprintf(number, 21, "%llu", record_count);
+               yajl_gen_string(g, number, strlen(number));
+               yajl_gen_map_close(g);
+
+               const unsigned char *ybuf;
+               yajl_gen_get_buf(g, &ybuf, &len);
+
+               response = MHD_create_response_from_data(len, (void *)ybuf, MHD_NO, MHD_YES);
+               ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
+               MHD_destroy_response(response);
+
+               yajl_gen_free(g);
+
+               return ret;
+       } else if (strncmp(url, "/create", 8) == 0) {
+               struct create_state *cs = (struct create_state *) *ptr;
+
+               if (cs == NULL) {
+                       if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
+                               return respond_405(connection);
+
+                       struct create_state *cs = malloc(sizeof(struct create_state));
+                       cs->username[0] = cs->password[0] = 0;
+                       cs->pp = MHD_create_post_processor(connection, 1024, &POST_create_iterator, cs);
+                       *ptr = cs;
+                       return MHD_YES;
+               }
+
+               if (*upload_data_size) {
+                       MHD_post_process(cs->pp, upload_data, *upload_data_size);
+                       *upload_data_size = 0;
+                       return MHD_YES;
+               }
+
+               if (cs->username[0] == 0 || cs->password[0] == 0)
+                       return respond_JSON_Failure(connection);
+
+               if (blerg_exists(cs->username))
+                       return respond_JSON_Failure(connection);
+
+               struct blerg *b = blerg_open(cs->username);
+               blerg_close(b);
+               auth_set_password(cs->username, cs->password);
+
+               MHD_destroy_post_processor(cs->pp);
+               free(cs);
+               *ptr = NULL;
+
+               return respond_JSON_Success(connection);
+       } else {
+               return respond_404(connection);
+       }
+}
+
+
+int main(int argc, char *argv[]) {
+       struct MHD_Daemon *daemon;
+       fd_set rs, ws, es;
+       int max;
+
+       init_responses();
+
+       daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
+       if (daemon == NULL) {
+               fprintf(stderr, "Could not start web server\n");
+               return 1;
+       }
+
+       while (1) {
+               FD_ZERO(&rs); FD_ZERO(&ws); FD_ZERO(&es);
+               if (MHD_get_fdset(daemon, &rs, &ws, &es, &max) != MHD_YES) {
+                       fprintf(stderr, "Fatal error getting fd sets\n");
+                       break;
+               }
+               select(max + 1, &rs, &ws, &es, NULL);
+               MHD_run(daemon);
+       }
+       MHD_stop_daemon(daemon);
+       return 0;
+}