From: Chip Black Date: Thu, 23 Dec 2010 11:14:17 +0000 (-0600) Subject: Add /info endpoint X-Git-Tag: ohfuckreddit~41 X-Git-Url: http://git.bytex64.net/?a=commitdiff_plain;ds=inline;h=5cb7a8ed8b5f1b0d5cb23c8afc069d4958aa8596;p=blerg.git Add /info endpoint --- diff --git a/http_blerg.c b/http_blerg.c index 8c9aa51..b20bad4 100644 --- a/http_blerg.c +++ b/http_blerg.c @@ -14,6 +14,8 @@ #define URL_INFO_RECORD 0x2 #define DERP "DERP DERP DERP" +yajl_gen_config yajl_c = { 0, 0 }; + struct create_state { struct MHD_PostProcessor *pp; char username[33]; @@ -235,10 +237,7 @@ ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const c 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->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); @@ -308,6 +307,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, author, &record); + if ((ret & URL_INFO_AUTHOR) == 0) + return respond_404(connection); + + if (!blerg_exists(author)) + return respond_404(connection); + + *ptr == NULL; + + struct blerg *b = blerg_open(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;