From: Chip Black Date: Fri, 24 Dec 2010 02:02:05 +0000 (-0600) Subject: Add support for fetching ranges of records X-Git-Tag: ohfuckreddit~40 X-Git-Url: http://git.bytex64.net/?a=commitdiff_plain;h=268230019ea44f384c6c0b34df71fbcdcb3602fc;p=blerg.git Add support for fetching ranges of records --- diff --git a/http.h b/http.h index e915578..959fdb6 100644 --- a/http.h +++ b/http.h @@ -3,4 +3,11 @@ #define REALM "Blerg" +struct url_info { + unsigned int contents; + char author[33]; + uint64_t record; + uint64_t record_to; +}; + #endif /* _HTTP_H */ diff --git a/http_blerg.c b/http_blerg.c index b20bad4..a73ac1e 100644 --- a/http_blerg.c +++ b/http_blerg.c @@ -10,8 +10,9 @@ #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 }; @@ -37,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) { @@ -50,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; + + info->record_to = strtoull(c + 1, NULL, 10); + info->contents |= URL_INFO_RECORD_TO; - return ret; + return info->contents; } uint64_t *make_sequential_list(uint64_t from, uint64_t to) { @@ -181,13 +190,40 @@ 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; + } + + if (record_count == 0) { + response = MHD_create_response_from_data(2, "[]", MHD_NO, MHD_NO); + } else { + 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) { @@ -202,52 +238,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); + response = create_response_for_range(b, 0, 0); } 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; - - 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); + 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; @@ -320,16 +348,16 @@ ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const c if (url[5] != '/') return respond_404(connection); - ret = parse_url_info(url + 6, author, &record); + ret = parse_url_info(url + 6, &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; - struct blerg *b = blerg_open(author); + struct blerg *b = blerg_open(info.author); uint64_t record_count = blerg_get_record_count(b); blerg_close(b);