X-Git-Url: http://git.bytex64.net/?a=blobdiff_plain;ds=sidebyside;f=http_blerg.c;h=8c9aa5149f382042544629532293cf0092013186;hb=8e09dd305e599a87aea36f61c374ef2bcdc69398;hp=e6eddad7fe3ce13e9ed8cdc65fa5aea63166f51c;hpb=3d6d277d4cdfff7c11729cde0e2498b4b3912a98;p=blerg.git
diff --git a/http_blerg.c b/http_blerg.c
index e6eddad..8c9aa51 100644
--- a/http_blerg.c
+++ b/http_blerg.c
@@ -6,6 +6,9 @@
#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
@@ -23,50 +26,14 @@ struct put_state {
int data_size;
};
-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 "
401 Unauthorized401 Unauthorized
DENIED"
- response_401 = MHD_create_response_from_data(strlen (CONTENT_401), CONTENT_401, MHD_NO, MHD_NO);
-
-#define CONTENT_404 "404 Not Found404 Not Found
I couldn't find that."
- response_404 = MHD_create_response_from_data(strlen(CONTENT_404), CONTENT_404, MHD_NO, MHD_NO);
-
-#define CONTENT_405 "405 Method Not Allowed405 Method Not Allowed
I'm sorry, Dave. I'm afraid I can't do that."
- 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);
-}
+struct get_state {
+ struct blerg *b;
+ yajl_gen g;
+ unsigned int yoff;
+ uint64_t *entries;
+ uint64_t i;
+ int done;
+};
int parse_url_info(const char *url, char *author, uint64_t *record) {
const char *c;
@@ -93,6 +60,87 @@ 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) {
+ 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 */
+ blerg_fetch(gs->b, gs->entries[gs->i], &data, &len);
+
+ 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;
+ }
+ 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);
+}
+
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;
@@ -113,10 +161,19 @@ 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);
+ }
+ memcpy(ps->data + off, data, size);
+ if (ps->data_size == MAX_RECORD_SIZE)
+ return MHD_NO;
}
return MHD_YES;
@@ -166,9 +223,30 @@ ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const c
MHD_destroy_response(response);
return ret;
} else {
- response = MHD_create_response_from_data(strlen(DERP), DERP, MHD_NO, MHD_NO);
+ struct get_state *gs = malloc(sizeof(struct get_state));
+ gs->b = blerg_open(author);
+ 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);
+ }
+
ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
MHD_destroy_response(response);
+
return ret;
}
} else if (strncmp(url, "/put", 4) == 0) {
@@ -198,7 +276,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;
}