Clean up URL handling/parsing
authorChip Black <bytex64@bytex64.net>
Mon, 20 Dec 2010 08:25:50 +0000 (02:25 -0600)
committerChip Black <bytex64@bytex64.net>
Mon, 20 Dec 2010 08:25:50 +0000 (02:25 -0600)
http_blerg.c

index ef14096..8abebaa 100644 (file)
@@ -5,35 +5,65 @@
 #include "database.h"
 #include "tags.h"
 
+#define URL_INFO_AUTHOR 0x1
+#define URL_INFO_RECORD 0x2
 #define DERP "DERP DERP DERP"
-#define NOTFOUND "<html><head><title>404 Not Found</title></head><body><h1>404 Not Found</h1>I couldn't find that record.</body></html>"
+#define NOTFOUND "<html><head><title>404 Not Found</title></head><body><h1>404 Not Found</h1>I couldn't find that.</body></html>"
+
+struct MHD_Response *response404;
+
+void init_responses() {
+       response404 = MHD_create_response_from_data(strlen(NOTFOUND), NOTFOUND, MHD_NO, MHD_NO);
+}
+
+int respond_404(struct MHD_Connection *connection) {
+       return MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response404);
+}
+
+int parse_url_info(const char *url, char *author, uint64_t *record) {
+       const char *c;
+       int ret = 0;
+       int len;
+
+       c = strchr(url, '/');
+       if (c == NULL) {
+               len = strlen(url);
+       } else {
+               len = c - url;
+       }
+       if (len == 0)
+               return 0;
+       memcpy(author, url, len);
+       author[len] = 0;
+       ret |= URL_INFO_AUTHOR;
+
+       if (c != NULL && c[1] != 0) {
+               *record = strtoull(c + 1, NULL, 10);
+               ret |= URL_INFO_RECORD;
+       }
+
+       return ret;
+}
 
 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;
-       const char *c;
+       char author[33];
+       uint64_t record;
        char *data;
 
        if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) {
-               char author[33];
-               uint64_t record;
-
                if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
                        return MHD_NO;
-               c = strchr(url + 5, '/');
-               if (c == NULL) {
-                       len = strlen(url) - 5;
-               } else {
-                       len = c - (url + 5);
-               }
-               memcpy(author, url + 5, len);
-               author[len] = 0;
+               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);
 
-               c = url + (5 + len);
-               if (c[0] == '/' && c[1] != 0) {
-                       record = strtoull(c + 1, NULL, 10);
+               if (ret & URL_INFO_RECORD) {
                        if (*ptr == NULL) {
                                *ptr = (void *) 1;
                                return MHD_YES;
@@ -45,8 +75,7 @@ ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const c
                                blerg_close(b);
 
                                if (ret == 0) {
-                                       response = MHD_create_response_from_data(strlen(NOTFOUND), NOTFOUND, MHD_NO, MHD_NO);
-                                       ret = MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response);
+                                       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);
@@ -66,8 +95,9 @@ ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const c
                                return ret;
                        }
                }
+       } else if (strncmp(url, "/put", 4) == 0) {
        } else {
-               return MHD_NO;
+               return respond_404(connection);
        }
 }
 
@@ -77,6 +107,8 @@ int main(int argc, char *argv[]) {
        fd_set rs, ws, es;
        int max;
 
+       init_responses();
+
        daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
        if (daemon == NULL) {
                fprintf(stderr, "Could not start web server\n");