X-Git-Url: http://git.bytex64.net/?a=blobdiff_plain;f=http_blerg.c;h=0bf237b931a313720f7e40f69d06c6ec17570b16;hb=1bce37f32d8cfc9441b35f40fd0070163b0e945d;hp=8543fb8195f6baa2809d31499cb278ec02ab1a46;hpb=e66e830db19502f2e6d02e91a6f427930e374590;p=blerg.git diff --git a/http_blerg.c b/http_blerg.c index 8543fb8..0bf237b 100644 --- a/http_blerg.c +++ b/http_blerg.c @@ -2,22 +2,80 @@ #include #include #include +#include #include "database.h" #include "tags.h" +#include "auth.h" +#include "config.h" #define URL_INFO_AUTHOR 0x1 #define URL_INFO_RECORD 0x2 #define DERP "DERP DERP DERP" -#define NOTFOUND "404 Not Found

404 Not Found

I couldn't find that." -struct MHD_Response *response404; +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() { - response404 = MHD_create_response_from_data(strlen(NOTFOUND), NOTFOUND, MHD_NO, MHD_NO); +#define CONTENT_401 "401 Unauthorized

401 Unauthorized

DENIED" + response_401 = MHD_create_response_from_data(strlen (CONTENT_401), CONTENT_401, MHD_NO, MHD_NO); + +#define CONTENT_404 "404 Not Found

404 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 Allowed

405 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, response404); + 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) { @@ -45,6 +103,113 @@ int parse_url_info(const char *url, char *author, uint64_t *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; +} + 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) { @@ -55,92 +220,149 @@ ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const c char *data; if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) { - if (strcmp(method, MHD_HTTP_METHOD_GET) != 0) - return MHD_NO; + if (*ptr == NULL) { + if (strcmp(method, MHD_HTTP_METHOD_GET) != 0) + return respond_405(connection); + + *ptr = (void *) 1; + return MHD_YES; + } + if (url[4] != '/') return respond_404(connection); + ret = parse_url_info(url + 5, author, &record); - if (ret & URL_INFO_AUTHOR == 0) + if ((ret & URL_INFO_AUTHOR) == 0) + return respond_404(connection); + + if (!blerg_exists(author)) return respond_404(connection); + *ptr == NULL; + if (ret & URL_INFO_RECORD) { - if (*ptr == NULL) { - *ptr = (void *) 1; - return MHD_YES; - } else { - *ptr == NULL; - - struct blerg *b = blerg_open(author); - ret = blerg_fetch(b, record, &data, &len); - blerg_close(b); - - 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; - } - } else { - if (*ptr == NULL) { - *ptr = (void*) 1; - return MHD_YES; + struct blerg *b = blerg_open(author); + ret = blerg_fetch(b, record, &data, &len); + blerg_close(b); + + if (ret == 0) { + return respond_404(connection); } else { - *ptr == NULL; - response = MHD_create_response_from_data(strlen(DERP), DERP, MHD_NO, MHD_NO); + 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; } + MHD_destroy_response(response); + return ret; + } else { + yajl_gen_config c; + c.beautify = 0; + + struct get_state *gs = malloc(sizeof(struct get_state)); + gs->b = blerg_open(author); + gs->g = yajl_gen_alloc(&c, NULL); + gs->yoff = 0; + 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) { + struct put_state *ps = (struct put_state *) *ptr; char *username; - const char *password = "testpass"; - const char *realm = "Blerg Post"; - -#define OPAQUE "d29fb6db8f21a6e99903651a9f87470e" -#define DENIED "DENIED, MOTHERFUCKER" -#define PAGE "DERP DERP AUTHENTICATED DERP" + char password[33]; if (*ptr == NULL) { + if (strcmp(method, MHD_HTTP_METHOD_POST) != 0) + return respond_405(connection); + + if (url[4] == '/') + return respond_404(connection); + *ptr = (void *) 1; username = MHD_digest_auth_get_username(connection); - if (username == NULL) { - response = MHD_create_response_from_data(strlen (DENIED), DENIED, MHD_NO, MHD_NO); - ret = MHD_queue_auth_fail_response(connection, realm, OPAQUE, response, MHD_NO); - MHD_destroy_response(response); - return ret; - } + if (username == NULL) + return respond_401(connection, MHD_NO); + auth_get_password(username, password); - printf("username: %s\n", username); - - ret = MHD_digest_auth_check(connection, realm, username, password, 300); + ret = MHD_digest_auth_check(connection, REALM, username, password, 300); free(username); - if (ret == MHD_INVALID_NONCE || ret == MHD_NO) { - response = MHD_create_response_from_data(strlen (DENIED), DENIED, MHD_NO, MHD_NO); - ret = MHD_queue_auth_fail_response(connection, realm, OPAQUE, response, - (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO); - MHD_destroy_response(response); - return ret; - } + if (ret == MHD_INVALID_NONCE || ret == MHD_NO) + return respond_401(connection, (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO); + + struct put_state *ps = malloc(sizeof(struct put_state)); + ps->data = NULL; + ps->data_size = 0; + ps->pp = MHD_create_post_processor(connection, 16384, &POST_put_iterator, ps); + *ptr = ps; + return MHD_YES; + } + + if (*upload_data_size) { + MHD_post_process(ps->pp, upload_data, *upload_data_size); + *upload_data_size = 0; + return MHD_YES; } + if (ps->data == NULL || ps->data_size == 0) + return respond_JSON_Failure(connection); + + username = MHD_digest_auth_get_username(connection); + struct blerg *b = blerg_open(username); + if (b == NULL) + return respond_JSON_Failure(connection); + if (blerg_store(b, ps->data, ps->data_size) == -1) { + blerg_close(b); + return respond_JSON_Failure(connection); + } + blerg_close(b); + + MHD_destroy_post_processor(ps->pp); + free(username); + free(ps->data); + free(ps); *ptr = NULL; - if (url[4] != '/') - return respond_404(connection); - ret = parse_url_info(url + 5, author, &record); - if ((ret & URL_INFO_AUTHOR) == 0) - return respond_404(connection); + return respond_JSON_Success(connection); + } else if (strncmp(url, "/create", 8) == 0) { + struct create_state *cs = (struct create_state *) *ptr; + + if (cs == NULL) { + if (strcmp(method, MHD_HTTP_METHOD_POST) != 0) + return respond_405(connection); + + struct create_state *cs = malloc(sizeof(struct create_state)); + cs->username[0] = cs->password[0] = 0; + cs->pp = MHD_create_post_processor(connection, 1024, &POST_create_iterator, cs); + *ptr = cs; + return MHD_YES; + } + + if (*upload_data_size) { + MHD_post_process(cs->pp, upload_data, *upload_data_size); + *upload_data_size = 0; + return MHD_YES; + } + + if (cs->username[0] == 0 || cs->password[0] == 0) + return respond_JSON_Failure(connection); + + if (blerg_exists(cs->username)) + return respond_JSON_Failure(connection); + + struct blerg *b = blerg_open(cs->username); + blerg_close(b); + auth_set_password(cs->username, cs->password); + + MHD_destroy_post_processor(cs->pp); + free(cs); + *ptr = NULL; - response = MHD_create_response_from_data(strlen(PAGE), PAGE, MHD_NO, MHD_NO); - ret = MHD_queue_response(connection, MHD_HTTP_OK, response); - MHD_destroy_response(response); - return ret; + return respond_JSON_Success(connection); } else { return respond_404(connection); }