X-Git-Url: http://git.bytex64.net/?a=blobdiff_plain;f=http_blerg.c;h=9078f5352d54096980f31688b64e97b72a86ac32;hb=9974851d6c5f4b3e984a4b256e01bcb49e2c1918;hp=d46313c3b2f7ef8e40ca0a5e2313087523b8fcf6;hpb=fff174451c104dd5dab4517a71b660263ffd8b4e;p=blerg.git diff --git a/http_blerg.c b/http_blerg.c index d46313c..9078f53 100644 --- a/http_blerg.c +++ b/http_blerg.c @@ -10,10 +10,13 @@ #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 }; + struct create_state { struct MHD_PostProcessor *pp; char username[33]; @@ -35,10 +38,10 @@ struct get_state { int done; }; -int parse_url_info(const char *url, char *author, uint64_t *record) { +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) { @@ -48,16 +51,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; + + info->record = strtoull(c + 1, NULL, 10); + info->contents |= URL_INFO_RECORD; + + c = strchr(c, '-'); + if (c == NULL || c[1] == 0) + return info->contents; - return ret; + 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) { @@ -98,17 +109,23 @@ ssize_t GET_generate_list(void *cls, uint64_t pos, char *buf, size_t max) { return -1; if (pos == 0) { /* Start iterating */ - yajl_gen_map_open(gs->g); + 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_map_close(gs->g); + yajl_gen_array_close(gs->g); gs->done = 1; } gs->i--; @@ -173,13 +190,36 @@ 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; +} + 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) { @@ -194,55 +234,44 @@ 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; - - 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 = create_response_for_range(b, from, to); } + } - ret = MHD_queue_response(connection, MHD_HTTP_OK, response); - MHD_destroy_response(response); + if (response == NULL) + return respond_JSON_Failure(connection); - return ret; - } + 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; @@ -302,6 +331,50 @@ ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const c *ptr = NULL; return respond_JSON_Success(connection); + } else if (strncmp(url, "/info", 5) == 0) { + if (*ptr == NULL) { + *ptr = (void *) 1; + + if (strcmp(method, MHD_HTTP_METHOD_GET) != 0) + return respond_405(connection); + return MHD_YES; + } + + + if (url[5] != '/') + return respond_404(connection); + + ret = parse_url_info(url + 6, &info); + if ((ret & URL_INFO_AUTHOR) == 0) + return respond_404(connection); + + if (!blerg_exists(info.author)) + return respond_404(connection); + + *ptr == NULL; + + struct blerg *b = blerg_open(info.author); + uint64_t record_count = blerg_get_record_count(b); + blerg_close(b); + + char number[21]; + yajl_gen g = yajl_gen_alloc(&yajl_c, NULL); + yajl_gen_map_open(g); + yajl_gen_string(g, "record_count", 12); + snprintf(number, 21, "%llu", record_count); + yajl_gen_string(g, number, strlen(number)); + yajl_gen_map_close(g); + + const unsigned char *ybuf; + yajl_gen_get_buf(g, &ybuf, &len); + + response = MHD_create_response_from_data(len, (void *)ybuf, MHD_NO, MHD_YES); + ret = MHD_queue_response(connection, MHD_HTTP_OK, response); + MHD_destroy_response(response); + + yajl_gen_free(g); + + return ret; } else if (strncmp(url, "/create", 8) == 0) { struct create_state *cs = (struct create_state *) *ptr;