Properly iterate on /put and /get
[blerg.git] / http_blerg.c
index 3cbc207..58dd115 100644 (file)
@@ -2,9 +2,11 @@
 #include <stdlib.h>
 #include <string.h>
 #include <microhttpd.h>
+#include <yajl/yajl_gen.h>
 #include "database.h"
 #include "tags.h"
 #include "auth.h"
+#include "config.h"
 
 #define URL_INFO_AUTHOR 0x1
 #define URL_INFO_RECORD 0x2
@@ -22,6 +24,15 @@ struct put_state {
        int data_size;
 };
 
+struct get_state {
+       struct blerg *b;
+       yajl_gen g;
+       unsigned int yoff;
+       uint64_t from;
+       uint64_t to;
+       int done;
+};
+
 struct MHD_Response *response_401;
 struct MHD_Response *response_404;
 struct MHD_Response *response_405;
@@ -92,6 +103,80 @@ int parse_url_info(const char *url, char *author, uint64_t *record) {
        return ret;
 }
 
+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;
+       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;
+                       printf("generate list: gave %d bytes\n", max);
+                       return max;
+               } else {
+                       memcpy(buf, ybuf + gs->yoff, bytes_remaining);
+                       gs->yoff = 0;
+                       yajl_gen_clear(gs->g);
+                       printf("generate list: gave %d bytes\n", bytes_remaining);
+                       return bytes_remaining;
+               }
+       }
+
+       if (gs->done)
+               return -1;
+
+       if (pos == 0) { /* Start iterating */
+               gs->yoff = 0;
+               yajl_gen_array_open(gs->g);
+               uint64_t record_count = blerg_get_record_count(gs->b);
+               if (record_count == 0) {
+                       gs->from = 1;
+                       gs->to = 0;
+               } else {
+                       gs->to = record_count - 1;
+                       gs->from = (record_count > 50 ? gs->to - 49 : 0);
+               }
+       }
+
+       if (gs->from > gs->to) { /* Done iterating */
+               yajl_gen_array_close(gs->g);
+               gs->done = 1;
+       } else {
+               /* Snarf one record */
+               blerg_fetch(gs->b, gs->from, &data, &len);
+               yajl_gen_string(gs->g, data, len);
+               free(data);
+               gs->from++;
+       }
+
+
+       yajl_gen_get_buf(gs->g, &ybuf, &len);
+       printf("%d bytes in yajl buffer\n", len);
+       if (len > max) {
+               memcpy(buf, ybuf, max);
+               gs->yoff = max;
+               printf("generate list: gave %d bytes\n", max);
+               return max;
+       } else {
+               memcpy(buf, ybuf, len);
+               yajl_gen_clear(gs->g);
+               printf("generate list: gave %d bytes\n", len);
+               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);
+}
+
 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;
 
@@ -112,10 +197,20 @@ int POST_put_iterator(void *cls, enum MHD_ValueKind kind, const char *key, const
        struct put_state *ps = cls;
 
        if (strncmp(key, "data", 5) == 0) {
-               ps->data_size = size;
-               ps->data = malloc(size);
-               memcpy(ps->data, data, size);
-               return MHD_NO;
+               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);
+               }
+               printf("Copying %d bytes, total size %d\n", size, ps->data_size);
+               memcpy(ps->data + off, data, size);
+               if (ps->data_size == MAX_RECORD_SIZE)
+                       return MHD_NO;
        }
 
        return MHD_YES;
@@ -131,10 +226,17 @@ ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const c
        char *data;
 
        if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) {
-               if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
-                       return MHD_NO;
+               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, author, &record);
                if ((ret & URL_INFO_AUTHOR) == 0)
                        return respond_404(connection);
@@ -142,37 +244,36 @@ ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const c
                if (!blerg_exists(author))
                        return respond_404(connection);
 
+               *ptr == NULL;
+
                if (ret & URL_INFO_RECORD) {
-                       if (*ptr == NULL) {
-                               *ptr = (void *) 1;
-                               return MHD_YES;
-                       } else {
-                               *ptr == NULL;
-
-                               struct blerg *b = blerg_open(author);
-                               ret = blerg_fetch(b, record, &data, &len);
-                               blerg_close(b);
-
-                               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;
-                       }
-               } else {
-                       if (*ptr == NULL) {
-                               *ptr = (void*) 1;
-                               return MHD_YES;
+                       struct blerg *b = blerg_open(author);
+                       ret = blerg_fetch(b, record, &data, &len);
+                       blerg_close(b);
+
+                       if (ret == 0) {
+                               return respond_404(connection);
                        } else {
-                               *ptr == NULL;
-                               response = MHD_create_response_from_data(strlen(DERP), DERP, MHD_NO, MHD_NO);
+                               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;
                        }
+                       MHD_destroy_response(response);
+                       return ret;
+               } else {
+                       yajl_gen_config c;
+                       c.beautify = 0;
+
+                       struct get_state *gs = malloc(sizeof(struct get_state));
+                       gs->b = blerg_open(author);
+                       gs->g = yajl_gen_alloc(&c, NULL);
+                       gs->yoff = 0;
+                       gs->done = 0;
+
+                       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, "/put", 4) == 0) {
                struct put_state *ps = (struct put_state *) *ptr;
@@ -201,7 +302,8 @@ ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const c
 
                        struct put_state *ps = malloc(sizeof(struct put_state));
                        ps->data = NULL;
-                       ps->pp = MHD_create_post_processor(connection, 4096, &POST_put_iterator, ps);
+                       ps->data_size = 0;
+                       ps->pp = MHD_create_post_processor(connection, 16384, &POST_put_iterator, ps);
                        *ptr = ps;
                        return MHD_YES;
                }