Add tag searching to web API
[blerg.git] / http_blerg.c
index b20bad4..93d0984 100644 (file)
@@ -10,8 +10,9 @@
 #include "http.h"
 #include "config.h"
 
-#define URL_INFO_AUTHOR 0x1
-#define URL_INFO_RECORD 0x2
+#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 };
@@ -37,10 +38,18 @@ struct get_state {
        int done;
 };
 
-int parse_url_info(const char *url, char *author, uint64_t *record) {
+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 ret = 0;
        int len;
+       info->contents = 0;
 
        c = strchr(url, '/');
        if (c == NULL) {
@@ -50,16 +59,24 @@ int parse_url_info(const char *url, char *author, uint64_t *record) {
        }
        if (len == 0)
                return 0;
-       memcpy(author, url, len);
-       author[len] = 0;
-       ret |= URL_INFO_AUTHOR;
+       memcpy(info->author, url, len);
+       info->author[len] = 0;
+       info->contents |= URL_INFO_AUTHOR;
 
-       if (c != NULL && c[1] != 0) {
-               *record = strtoull(c + 1, NULL, 10);
-               ret |= URL_INFO_RECORD;
-       }
+       if (c == NULL || c[1] == 0)
+               return info->contents;
 
-       return ret;
+       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) {
@@ -74,6 +91,33 @@ uint64_t *make_sequential_list(uint64_t from, uint64_t to) {
        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;
@@ -104,17 +148,8 @@ ssize_t GET_generate_list(void *cls, uint64_t pos, char *buf, size_t max) {
        }
 
        /* Snarf one record */
-       blerg_fetch(gs->b, gs->entries[gs->i], &data, &len);
+       json_generate_one_record(gs->g, NULL, gs->b, gs->entries[gs->i]);
 
-       yajl_gen_map_open(gs->g);
-       yajl_gen_string(gs->g, "record", 6);
-       snprintf(number, 21, "%llu", gs->entries[gs->i]);
-       yajl_gen_string(gs->g, number, strlen(number));
-       yajl_gen_string(gs->g, "data", 4);
-       yajl_gen_string(gs->g, data, len);
-       yajl_gen_map_close(gs->g);
-
-       free(data);
        if (gs->i == 0) {
                yajl_gen_array_close(gs->g);
                gs->done = 1;
@@ -143,6 +178,71 @@ void GET_generate_list_free(void *cls) {
        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;
 
@@ -181,13 +281,46 @@ int POST_put_iterator(void *cls, enum MHD_ValueKind kind, const char *key, const
        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;
-       char author[33];
-       uint64_t record;
+       struct url_info info;
        char *data;
 
        if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) {
@@ -202,52 +335,79 @@ ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const c
                if (url[4] != '/')
                        return respond_404(connection);
 
-               ret = parse_url_info(url + 5, author, &record);
+               ret = parse_url_info(url + 5, &info);
                if ((ret & URL_INFO_AUTHOR) == 0)
                        return respond_404(connection);
 
-               if (!blerg_exists(author))
+               if (!blerg_exists(info.author))
                        return respond_404(connection);
 
                *ptr == NULL;
 
-               if (ret & URL_INFO_RECORD) {
-                       struct blerg *b = blerg_open(author);
-                       ret = blerg_fetch(b, record, &data, &len);
+               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) {
+                       if (ret == 0)
                                return respond_404(connection);
-                       } else {
-                               response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
-                               ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
-                       }
-                       MHD_destroy_response(response);
-                       return ret;
+                       response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
                } else {
-                       struct get_state *gs = malloc(sizeof(struct get_state));
-                       gs->b = blerg_open(author);
-                       uint64_t record_count = blerg_get_record_count(gs->b);
+                       uint64_t record_count, from, to;
+                       record_count = blerg_get_record_count(b);
                        if (record_count == 0) {
                                response = MHD_create_response_from_data(2, "[]", MHD_NO, MHD_NO);
                        } else {
-                               uint64_t from, to, i, j;
                                to = record_count - 1;
                                from = (record_count > 50 ? to - 49 : 0);
-                               gs->entries = make_sequential_list(from, to);
-                               gs->i = to - from;
+                               response = create_response_for_range(b, from, to);
+                       }
+               }
 
-                               gs->g = yajl_gen_alloc(&yajl_c, NULL);
-                               gs->yoff = gs->done = 0;
+               if (response == NULL)
+                       return respond_JSON_Failure(connection);
 
-                               response = MHD_create_response_from_callback(-1, 262144, &GET_generate_list, gs, &GET_generate_list_free);
-                       }
+               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 = MHD_queue_response(connection, MHD_HTTP_OK, response);
-                       MHD_destroy_response(response);
+               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);
 
-                       return ret;
+               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;
@@ -320,16 +480,16 @@ ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const c
                if (url[5] != '/')
                        return respond_404(connection);
 
-               ret = parse_url_info(url + 6, author, &record);
+               ret = parse_url_info(url + 6, &info);
                if ((ret & URL_INFO_AUTHOR) == 0)
                        return respond_404(connection);
 
-               if (!blerg_exists(author))
+               if (!blerg_exists(info.author))
                        return respond_404(connection);
 
                *ptr == NULL;
 
-               struct blerg *b = blerg_open(author);
+               struct blerg *b = blerg_open(info.author);
                uint64_t record_count = blerg_get_record_count(b);
                blerg_close(b);