Some cleanup, genericize JSON iterator to accept a list of record IDs.
authorChip Black <bytex64@bytex64.net>
Thu, 23 Dec 2010 09:23:15 +0000 (03:23 -0600)
committerChip Black <bytex64@bytex64.net>
Thu, 23 Dec 2010 09:23:15 +0000 (03:23 -0600)
Makefile
canned_responses.c [new file with mode: 0644]
canned_responses.h [new file with mode: 0644]
http.h [new file with mode: 0644]
http_blerg.c

index bf27046..5722ba5 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,7 @@ LDFLAGS = -Llibmicrohttpd-0.9.3/src/daemon/.libs -Lyajl/build/yajl-1.0.11/lib
 targets = blerg.a blergtool http_blerg
 blerg_a_objects = database.o tags.o
 blergtool_objects = blergtool.o blerg.a
-http_blerg_objects = http_blerg.o auth.o blerg.a
+http_blerg_objects = http_blerg.o canned_responses.o auth.o blerg.a
 
 all: $(targets)
 
diff --git a/canned_responses.c b/canned_responses.c
new file mode 100644 (file)
index 0000000..c61e2a7
--- /dev/null
@@ -0,0 +1,43 @@
+#include <string.h>
+#include <microhttpd.h>
+#include "http.h"
+#include "canned_responses.h"
+
+void init_responses() {
+#define CONTENT_401 "<html><head><title>401 Unauthorized</title></head><body><h1>401 Unauthorized</h1>DENIED</body></html>"
+       response_401 = MHD_create_response_from_data(strlen (CONTENT_401), CONTENT_401, MHD_NO, MHD_NO);  
+
+#define CONTENT_404 "<html><head><title>404 Not Found</title></head><body><h1>404 Not Found</h1>I couldn't find that.</body></html>"
+       response_404 = MHD_create_response_from_data(strlen(CONTENT_404), CONTENT_404, MHD_NO, MHD_NO);
+
+#define CONTENT_405 "<html><head><title>405 Method Not Allowed</title></head><body><h1>405 Method Not Allowed</h1>I'm sorry, Dave. I'm afraid I can't do that.</body></html>"
+       response_405 = MHD_create_response_from_data(strlen(CONTENT_405), CONTENT_405, MHD_NO, MHD_NO);
+
+#define JSON_SUCCESS "{status: \"success\"}"
+       response_JSON_Success = MHD_create_response_from_data(strlen(JSON_SUCCESS), JSON_SUCCESS, MHD_NO, MHD_NO);
+
+#define JSON_FAILURE "{status: \"failure\"}"
+       response_JSON_Failure = MHD_create_response_from_data(strlen(JSON_FAILURE), JSON_FAILURE, MHD_NO, MHD_NO);
+}
+
+#define OPAQUE "d29fb6db8f21a6e99903651a9f87470e"
+int respond_401(struct MHD_Connection *connection, int signal_stale) {
+       return MHD_queue_auth_fail_response(connection, REALM, OPAQUE, response_401, signal_stale);    
+}
+
+int respond_404(struct MHD_Connection *connection) {
+       return MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response_404);
+}
+
+int respond_405(struct MHD_Connection *connection) {
+       return MHD_queue_response(connection, MHD_HTTP_METHOD_NOT_ALLOWED, response_405);
+}
+
+int respond_JSON_Success(struct MHD_Connection *connection) {
+       return MHD_queue_response(connection, MHD_HTTP_OK, response_JSON_Success);
+}
+
+int respond_JSON_Failure(struct MHD_Connection *connection) {
+       return MHD_queue_response(connection, MHD_HTTP_OK, response_JSON_Failure);
+}
+
diff --git a/canned_responses.h b/canned_responses.h
new file mode 100644 (file)
index 0000000..b04ee3e
--- /dev/null
@@ -0,0 +1,12 @@
+#ifndef _CANNED_RESPONSES
+#define _CANNED_RESPONSES
+
+struct MHD_Response *response_401;
+struct MHD_Response *response_404;
+struct MHD_Response *response_405;
+struct MHD_Response *response_JSON_Success;
+struct MHD_Response *response_JSON_Failure;
+
+void init_responses();
+
+#endif /* _CANNED_RESPONSES */
diff --git a/http.h b/http.h
new file mode 100644 (file)
index 0000000..e915578
--- /dev/null
+++ b/http.h
@@ -0,0 +1,6 @@
+#ifndef _HTTP_H
+#define _HTTP_H
+
+#define REALM "Blerg"
+
+#endif /* _HTTP_H */
index 0bf237b..d46313c 100644 (file)
@@ -6,6 +6,8 @@
 #include "database.h"
 #include "tags.h"
 #include "auth.h"
+#include "canned_responses.h"
+#include "http.h"
 #include "config.h"
 
 #define URL_INFO_AUTHOR 0x1
@@ -28,56 +30,11 @@ struct get_state {
        struct blerg *b;
        yajl_gen g;
        unsigned int yoff;
-       uint64_t from;
-       uint64_t to;
+       uint64_t *entries;
+       uint64_t i;
        int done;
 };
 
-struct MHD_Response *response_401;
-struct MHD_Response *response_404;
-struct MHD_Response *response_405;
-struct MHD_Response *response_JSON_Success;
-struct MHD_Response *response_JSON_Failure;
-
-void init_responses() {
-#define CONTENT_401 "<html><head><title>401 Unauthorized</title></head><body><h1>401 Unauthorized</h1>DENIED</body></html>"
-       response_401 = MHD_create_response_from_data(strlen (CONTENT_401), CONTENT_401, MHD_NO, MHD_NO);  
-
-#define CONTENT_404 "<html><head><title>404 Not Found</title></head><body><h1>404 Not Found</h1>I couldn't find that.</body></html>"
-       response_404 = MHD_create_response_from_data(strlen(CONTENT_404), CONTENT_404, MHD_NO, MHD_NO);
-
-#define CONTENT_405 "<html><head><title>405 Method Not Allowed</title></head><body><h1>405 Method Not Allowed</h1>I'm sorry, Dave. I'm afraid I can't do that.</body></html>"
-       response_405 = MHD_create_response_from_data(strlen(CONTENT_405), CONTENT_405, MHD_NO, MHD_NO);
-
-#define JSON_SUCCESS "{status: \"success\"}"
-       response_JSON_Success = MHD_create_response_from_data(strlen(JSON_SUCCESS), JSON_SUCCESS, MHD_NO, MHD_NO);
-
-#define JSON_FAILURE "{status: \"failure\"}"
-       response_JSON_Failure = MHD_create_response_from_data(strlen(JSON_FAILURE), JSON_FAILURE, MHD_NO, MHD_NO);
-}
-
-#define REALM "Blerg"
-#define OPAQUE "d29fb6db8f21a6e99903651a9f87470e"
-int respond_401(struct MHD_Connection *connection, int signal_stale) {
-       return MHD_queue_auth_fail_response(connection, REALM, OPAQUE, response_401, signal_stale);    
-}
-
-int respond_404(struct MHD_Connection *connection) {
-       return MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response_404);
-}
-
-int respond_405(struct MHD_Connection *connection) {
-       return MHD_queue_response(connection, MHD_HTTP_METHOD_NOT_ALLOWED, response_405);
-}
-
-int respond_JSON_Success(struct MHD_Connection *connection) {
-       return MHD_queue_response(connection, MHD_HTTP_OK, response_JSON_Success);
-}
-
-int respond_JSON_Failure(struct MHD_Connection *connection) {
-       return MHD_queue_response(connection, MHD_HTTP_OK, response_JSON_Failure);
-}
-
 int parse_url_info(const char *url, char *author, uint64_t *record) {
        const char *c;
        int ret = 0;
@@ -103,10 +60,23 @@ int parse_url_info(const char *url, char *author, uint64_t *record) {
        return ret;
 }
 
+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;
+}
+
 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) {
@@ -128,28 +98,20 @@ ssize_t GET_generate_list(void *cls, uint64_t pos, char *buf, size_t max) {
                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);
-               }
+               yajl_gen_map_open(gs->g);
        }
 
-       if (gs->from > gs->to) { /* Done iterating */
-               yajl_gen_array_close(gs->g);
+       /* Snarf one record */
+       blerg_fetch(gs->b, gs->entries[gs->i], &data, &len);
+       snprintf(number, 21, "%llu", gs->entries[gs->i]);
+       yajl_gen_string(gs->g, number, strlen(number));
+       yajl_gen_string(gs->g, data, len);
+       free(data);
+       if (gs->i == 0) {
+               yajl_gen_map_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++;
        }
+       gs->i--;
 
 
        yajl_gen_get_buf(gs->g, &ybuf, &len);
@@ -169,6 +131,7 @@ void GET_generate_list_free(void *cls) {
 
        blerg_close(gs->b);
        yajl_gen_free(gs->g);
+       free(gs->entries);
        free(gs);
 }
 
@@ -254,16 +217,27 @@ ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const c
                        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;
+                       uint64_t record_count = blerg_get_record_count(gs->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;
+
+                               yajl_gen_config c;
+                               c.beautify = 0;
+
+                               gs->g = yajl_gen_alloc(&c, NULL);
+                               gs->yoff = gs->done = 0;
+
+                               response = MHD_create_response_from_callback(-1, 262144, &GET_generate_list, gs, &GET_generate_list_free);
+                       }
 
-                       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);