In retrospect, using POST params for subscribe was stupid
authorChip Black <bytex64@bytex64.net>
Sat, 26 Feb 2011 11:16:23 +0000 (05:16 -0600)
committerChip Black <bytex64@bytex64.net>
Sat, 26 Feb 2011 11:16:23 +0000 (05:16 -0600)
http/http_blerg.c

index e6e04c5..85a1e5b 100644 (file)
@@ -29,12 +29,6 @@ struct put_state {
        int data_size;
 };
 
-struct subscribe_state {
-       struct MHD_PostProcessor *pp;
-       char username[33];
-       char to[33];
-};
-
 struct get_state {
        struct blerg *b;
        yajl_gen g;
@@ -240,22 +234,6 @@ int process_and_check_put(struct MHD_Connection *connection, const char *method,
        return MHD_NO;
 }
 
-int POST_subscribe_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 subscribe_state *ss = cls;
-
-       if (strncmp(key, "username", 9) == 0) {
-               if (size > 32) size = 32;
-               memcpy(ss->username, data, size);
-               ss->username[size] = 0;
-       } else if (strncmp(key, "to", 3) == 0) {
-               if (size > 32) size = 32;
-               memcpy(ss->to, data, size);
-               ss->to[size] = 0;
-       }
-
-       return MHD_YES;
-}
-
 int POST_auth_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 auth_state *as = cls;
 
@@ -572,37 +550,33 @@ ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const c
 
                auth_logout(as->username);
                return respond_JSON_Success(connection);
-       } else if (strncmp(url, "/subscribe", 11) == 0 || strncmp(url, "/unsubscribe", 13) == 0) {
-               struct subscribe_state *ss = (struct subscribe_state *) *ptr;
+       } else if (strncmp(url, "/subscribe", 10) == 0 || strncmp(url, "/unsubscribe", 12) == 0) {
+               ret = process_and_check_auth(connection, method, upload_data, upload_data_size, ptr);
+               if (ret == MHD_YES)
+                       return MHD_YES;
 
-               if (ss == NULL) {
-                       if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
-                               return respond_405(connection);
+               struct auth_state *as = (struct auth_state *) *ptr;
 
-                       struct subscribe_state *ss = malloc(sizeof(struct subscribe_state));
-                       ss->username[0] = ss->to[0] = 0;
-                       ss->pp = MHD_create_post_processor(connection, 1024, &POST_subscribe_iterator, ss);
-                       *ptr = ss;
-                       return MHD_YES;
-               }
+               if (url[1] == 'u') {
+                       if (url[12] != '/')
+                               return respond_404(connection);
 
-               if (*upload_data_size) {
-                       MHD_post_process(ss->pp, upload_data, *upload_data_size);
-                       *upload_data_size = 0;
-                       return MHD_YES;
-               }
+                       ret = parse_url_info(url + 13, &info);
+                       if ((ret & URL_INFO_NAME) == 0)
+                               return respond_404(connection);
 
-               const char *given_token = MHD_lookup_connection_value(connection, MHD_COOKIE_KIND, "auth");
-               if (auth_check_token(ss->username, given_token)) {
-                       if (url[1] == 'u') {
-                               subscription_remove(ss->username, ss->to);
-                       } else {
-                               subscription_add(ss->username, ss->to);
-                       }
-                       return respond_JSON_Success(connection);
+                       subscription_remove(as->username, info.name);
                } else {
-                       return respond_JSON_Failure(connection);
+                       if (url[10] != '/')
+                               return respond_404(connection);
+
+                       ret = parse_url_info(url + 11, &info);
+                       if ((ret & URL_INFO_NAME) == 0)
+                               return respond_404(connection);
+
+                       subscription_add(as->username, info.name);
                }
+               return respond_JSON_Success(connection);
        } else if (strncmp(url, "/feed", 6) == 0) {
                ret = process_and_check_auth(connection, method, upload_data, upload_data_size, ptr);
                if (ret == MHD_YES)