+
+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 from;
+ uint64_t to;
+ 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;
+ int len;
+
+ c = strchr(url, '/');
+ if (c == NULL) {
+ len = strlen(url);
+ } else {
+ len = c - url;
+ }
+ if (len == 0)
+ return 0;
+ memcpy(author, url, len);
+ author[len] = 0;
+ ret |= URL_INFO_AUTHOR;
+
+ if (c != NULL && c[1] != 0) {
+ *record = strtoull(c + 1, NULL, 10);
+ ret |= URL_INFO_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;
+ 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 */
+ 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);
+ 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);
+}
+
+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;
+}