From: Chip Black Date: Tue, 21 Dec 2010 09:50:16 +0000 (-0600) Subject: Add beginnings of user creation API X-Git-Tag: ohfuckreddit~52 X-Git-Url: http://git.bytex64.net/?a=commitdiff_plain;h=650b60caf46762f4a16ed9f3f522e6a2ada7ffaf;p=blerg.git Add beginnings of user creation API --- diff --git a/database.c b/database.c index 6e9f494..c3ac954 100644 --- a/database.c +++ b/database.c @@ -108,6 +108,22 @@ open_failed_index_open: return 0; } +int blerg_exists(const char *name) { + int namelen = strlen(name); + char filename[512]; + + if (namelen > 32) { + perror("Name too long"); + return 0; + } + + snprintf(filename, 512, "%s/%s", DATA_PATH, name); + if (access(filename, F_OK) == -1) + return 0; + else + return 1; +} + struct blerg *blerg_open(const char *name) { int namelen = strlen(name); char filename[512]; diff --git a/database.h b/database.h index 7c90308..ab5cb97 100644 --- a/database.h +++ b/database.h @@ -26,6 +26,7 @@ struct blerg { int data_size; }; +int blerg_exists(const char *); struct blerg *blerg_open(const char *); int blerg_close(struct blerg *); int blerg_store(struct blerg *, const char *, int); diff --git a/http_blerg.c b/http_blerg.c index 8543fb8..7a9e497 100644 --- a/http_blerg.c +++ b/http_blerg.c @@ -9,17 +9,30 @@ #define URL_INFO_RECORD 0x2 #define DERP "DERP DERP DERP" #define NOTFOUND "404 Not Found

404 Not Found

I couldn't find that." +#define JSON_SUCCESS "{status: \"success\"}" + +struct create_state { + struct MHD_PostProcessor *pp; + char username[33]; + char password[33]; +}; struct MHD_Response *response404; +struct MHD_Response *responseJSONSuccess; void init_responses() { response404 = MHD_create_response_from_data(strlen(NOTFOUND), NOTFOUND, MHD_NO, MHD_NO); + responseJSONSuccess = MHD_create_response_from_data(strlen(JSON_SUCCESS), JSON_SUCCESS, MHD_NO, MHD_NO); } int respond_404(struct MHD_Connection *connection) { return MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response404); } +int respond_JSONSuccess(struct MHD_Connection *connection) { + return MHD_queue_response(connection, MHD_HTTP_OK, responseJSONSuccess); +} + int parse_url_info(const char *url, char *author, uint64_t *record) { const char *c; int ret = 0; @@ -45,6 +58,20 @@ int parse_url_info(const char *url, char *author, uint64_t *record) { return ret; } +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", 8) == 0) { + if (size > 32) size = 32; + memcpy(cs->username, data, size); + cs->username[size] = 0; + } else if (strncmp(key, "password", 8) == 0) { + if (size > 32) size = 32; + memcpy(cs->password, data, size); + cs->password[size] = 0; + } +} + 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) { @@ -60,7 +87,10 @@ 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); - if (ret & URL_INFO_AUTHOR == 0) + if ((ret & URL_INFO_AUTHOR) == 0) + return respond_404(connection); + + if (!blerg_exists(author)) return respond_404(connection); if (ret & URL_INFO_RECORD) { @@ -141,6 +171,38 @@ ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const c ret = MHD_queue_response(connection, MHD_HTTP_OK, response); MHD_destroy_response(response); return ret; + } 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 MHD_NO; + + 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; + } else { + printf("username: %s, password: %s\n", cs->username, cs->password); + + if (cs->username[0] == 0 || cs->password[0] == 0) + return MHD_NO; // TODO: Give a better response + + 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); + return respond_JSONSuccess(connection); + } } else { return respond_404(connection); }