Modify backend to use new auth cookie format
[blerg.git] / http / http_blerg.c
index e994574..8a5b9a5 100644 (file)
@@ -1,3 +1,6 @@
+/* Blerg is (C) 2011 The Dominion of Awesome, and is distributed under a
+ * BSD-style license.  Please see the COPYING file for details.
+ */
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -5,6 +8,7 @@
 #include <yajl/yajl_gen.h>
 #include "database.h"
 #include "tags.h"
+#include "subscription.h"
 #include "auth.h"
 #include "canned_responses.h"
 #include "app.h"
@@ -20,6 +24,7 @@ struct auth_state {
 
 struct put_state {
        struct MHD_PostProcessor *pp;
+       char username[33];
        char *data;
        int data_size;
 };
@@ -33,10 +38,10 @@ struct get_state {
        int done;
 };
 
-struct tag_state {
+struct blergref_state {
        yajl_gen g;
        unsigned int yoff;
-       struct tag *results;
+       struct blergref *results;
        uint64_t i;
        int done;
 };
@@ -71,7 +76,7 @@ ssize_t GET_generate_list(void *cls, uint64_t pos, char *buf, size_t max) {
        }
 
        /* Snarf one record */
-       json_generate_one_record(gs->g, NULL, gs->b, gs->entries[gs->i]);
+       json_generate_one_record(gs->g, NULL, gs->b, gs->entries[gs->i], 0);
 
        if (gs->i == 0) {
                yajl_gen_array_close(gs->g);
@@ -101,82 +106,66 @@ void GET_generate_list_free(void *cls) {
        free(gs);
 }
 
-ssize_t GET_generate_taglist(void *cls, uint64_t pos, char *buf, size_t max) {
-       struct tag_state *ts = cls;
+ssize_t GET_generate_blergref_list(void *cls, uint64_t pos, char *buf, size_t max) {
+       struct blergref_state *bs = cls;
        struct blerg *b;
        const unsigned char *ybuf;
        unsigned int len;
 
-       if (ts->yoff > 0) {
-               yajl_gen_get_buf(ts->g, &ybuf, &len);
-               size_t bytes_remaining = len - ts->yoff;
+       if (bs->yoff > 0) {
+               yajl_gen_get_buf(bs->g, &ybuf, &len);
+               size_t bytes_remaining = len - bs->yoff;
                if (bytes_remaining > max) {
-                       memcpy(buf, ybuf + ts->yoff, max);
-                       ts->yoff += max;
+                       memcpy(buf, ybuf + bs->yoff, max);
+                       bs->yoff += max;
                        return max;
                } else {
-                       memcpy(buf, ybuf + ts->yoff, bytes_remaining);
-                       ts->yoff = 0;
-                       yajl_gen_clear(ts->g);
+                       memcpy(buf, ybuf + bs->yoff, bytes_remaining);
+                       bs->yoff = 0;
+                       yajl_gen_clear(bs->g);
                        return bytes_remaining;
                }
        }
 
-       if (ts->done)
+       if (bs->done)
                return -1;
 
        if (pos == 0) { /* Start iterating */
-               yajl_gen_array_open(ts->g);
+               yajl_gen_array_open(bs->g);
        }
 
        /* Snarf one record */
-       b = blerg_open(ts->results[ts->i].author);
+       b = blerg_open(bs->results[bs->i].author);
        if (b != NULL) {
-               json_generate_one_record(ts->g, ts->results[ts->i].author, b, ts->results[ts->i].record);
+               json_generate_one_record(bs->g, bs->results[bs->i].author, b, bs->results[bs->i].record, 0);
                blerg_close(b);
        }
 
-       if (ts->i == 0) {
-               yajl_gen_array_close(ts->g);
-               ts->done = 1;
+       if (bs->i == 0) {
+               yajl_gen_array_close(bs->g);
+               bs->done = 1;
        }
 
-       ts->i--;
+       bs->i--;
 
-       yajl_gen_get_buf(ts->g, &ybuf, &len);
+       yajl_gen_get_buf(bs->g, &ybuf, &len);
        if (len > max) {
                memcpy(buf, ybuf, max);
-               ts->yoff = max;
+               bs->yoff = max;
                return max;
        } else {
                memcpy(buf, ybuf, len);
-               yajl_gen_clear(ts->g);
+               yajl_gen_clear(bs->g);
                return len;
        }
 }
 
-void GET_generate_taglist_free(void *cls) {
-       struct tag_state *ts = cls;
-
-       yajl_gen_free(ts->g);
-       free(ts->results);
-       free(ts);
-}
-
-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;
-
-       if (strncmp(key, "username", 9) == 0) {
-               if (size > 32) size = 32;
-               memcpy(as->username, data, size);
-               as->username[size] = 0;
-       } else if (strncmp(key, "password", 9) == 0) {
-               if (size > 32) size = 32;
-               memcpy(as->password, data, size);
-               as->password[size] = 0;
-       }
+void GET_generate_blergref_list_free(void *cls) {
+       struct blergref_state *bs = cls;
 
-       return MHD_YES;
+       yajl_gen_free(bs->g);
+       free(bs->results);
+       free(bs);
 }
 
 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) {
@@ -196,11 +185,107 @@ int POST_put_iterator(void *cls, enum MHD_ValueKind kind, const char *key, const
                memcpy(ps->data + off, data, size);
                if (ps->data_size == MAX_RECORD_SIZE)
                        return MHD_NO;
+       } else if (strncmp(key, "username", 9) == 0) {
+               if (size > 32) size = 32;
+               memcpy(ps->username, data, size);
+               ps->username[size] = 0;
        }
 
        return MHD_YES;
 }
 
+int process_put(struct MHD_Connection *connection, const char *method, const char *upload_data, size_t *upload_data_size, void **ptr) {
+       struct put_state *ps = (struct put_state *) *ptr;
+
+       if (ps == NULL) {
+               if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
+                       return respond_405(connection);
+
+               *ptr = (void *) 1;
+
+               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);
+               ps->username[0] = 0;
+               *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;
+       }
+
+       return MHD_NO;
+}
+
+int process_and_check_put(struct MHD_Connection *connection, const char *method, const char *upload_data, size_t *upload_data_size, void **ptr) {
+       struct put_state *ps = (struct put_state *) *ptr;
+
+       if (process_put(connection, method, upload_data, upload_data_size, ptr) == MHD_YES)
+               return MHD_YES;
+
+       const char *given_token = MHD_lookup_connection_value(connection, MHD_COOKIE_KIND, "auth");
+       if (!auth_check_token(ps->username, given_token))
+               return respond_403(connection);
+
+       return MHD_NO;
+}
+
+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;
+
+       if (strncmp(key, "username", 9) == 0) {
+               if (size > 32) size = 32;
+               memcpy(as->username, data, size);
+               as->username[size] = 0;
+       } else if (strncmp(key, "password", 9) == 0) {
+               if (size > 32) size = 32;
+               memcpy(as->password, data, size);
+               as->password[size] = 0;
+       }
+
+       return MHD_YES;
+}
+
+int process_auth(struct MHD_Connection *connection, const char *method, const char *upload_data, size_t *upload_data_size, void **ptr) {
+       struct auth_state *as = (struct auth_state *) *ptr;
+
+       if (as == NULL) {
+               if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
+                       return respond_405(connection);
+
+               as = malloc(sizeof(struct auth_state));
+               as->username[0] = as->password[0] = 0;
+               as->pp = MHD_create_post_processor(connection, 1024, &POST_auth_iterator, as);
+               *ptr = as;
+               return MHD_YES;
+       }
+
+       if (*upload_data_size) {
+               MHD_post_process(as->pp, upload_data, *upload_data_size);
+               *upload_data_size = 0;
+               return MHD_YES;
+       }
+
+       return MHD_NO;
+}
+
+int process_and_check_auth(struct MHD_Connection *connection, const char *method, const char *upload_data, size_t *upload_data_size, void **ptr) {
+       struct auth_state *as = (struct auth_state *) *ptr;
+
+       if (process_auth(connection, method, upload_data, upload_data_size, ptr) == MHD_YES)
+               return MHD_YES;
+
+       const char *given_token = MHD_lookup_connection_value(connection, MHD_COOKIE_KIND, "auth");
+       if (!auth_check_token(as->username, given_token))
+               return respond_403(connection);
+
+       return MHD_NO;
+}
+
 struct MHD_Response *create_response_for_range(struct blerg *b, uint64_t from, uint64_t to) {
        struct MHD_Response *response;
        struct get_state *gs = malloc(sizeof(struct get_state));
@@ -209,7 +294,6 @@ struct MHD_Response *create_response_for_range(struct blerg *b, uint64_t from, u
        uint64_t record_count = blerg_get_record_count(b);
 
        if (from > to || from >= record_count || to >= record_count || to - from > 99) {
-               blerg_close(b);
                free(gs);
                return NULL;
        }
@@ -225,14 +309,14 @@ struct MHD_Response *create_response_for_range(struct blerg *b, uint64_t from, u
        return response;
 }
 
-struct MHD_Response *create_tag_response(struct tag *results, uint64_t len) {
-       struct tag_state *ts = malloc(sizeof(struct tag_state));
-       ts->g = yajl_gen_alloc(&yajl_c, NULL);
-       ts->results = results;
-       ts->i = len - 1;
-       ts->yoff = ts->done = 0;
+struct MHD_Response *create_blergref_response(struct blergref *results, uint64_t len) {
+       struct blergref_state *bs = malloc(sizeof(struct blergref_state));
+       bs->g = yajl_gen_alloc(&yajl_c, NULL);
+       bs->results = results;
+       bs->i = len - 1;
+       bs->yoff = bs->done = 0;
 
-       return MHD_create_response_from_callback(-1, 262144, &GET_generate_taglist, ts, &GET_generate_taglist_free);
+       return MHD_create_response_from_callback(-1, 262144, &GET_generate_blergref_list, bs, &GET_generate_blergref_list_free);
 }
 
 static int
@@ -256,15 +340,15 @@ ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const c
                        return respond_404(connection);
 
                ret = parse_url_info(url + 5, &info);
-               if ((ret & URL_INFO_AUTHOR) == 0)
+               if ((ret & URL_INFO_NAME) == 0)
                        return respond_404(connection);
 
-               if (!blerg_exists(info.author))
+               if (!blerg_exists(info.name))
                        return respond_404(connection);
 
                *ptr == NULL;
 
-               struct blerg *b = blerg_open(info.author);
+               struct blerg *b = blerg_open(info.name);
 
                if ((ret & URL_INFO_RECORD) && (ret & URL_INFO_RECORD_TO)) {
                        response = create_response_for_range(b, info.record, info.record_to);
@@ -309,19 +393,21 @@ ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const c
                        return respond_404(connection);
 
                ret = parse_url_info(url + 5, &info);
-               if ((ret & URL_INFO_AUTHOR) == 0)
+               if ((ret & URL_INFO_NAME) == 0)
                        return respond_404(connection);
 
-               if (!tag_exists(info.author))
+               if (info.name[0] == 'H')
+                       info.name[0] = '#';
+               if (!tag_exists(info.name))
                        return respond_404(connection);
 
                int recs = 50;
-               struct tag *taglist = tag_list(info.author, 0, &recs, -1);
+               struct blergref *taglist = tag_list(info.name, 0, &recs, -1);
 
                if (recs == 0) {
                        response = MHD_create_response_from_data(2, "[]", MHD_NO, MHD_NO);
                } else {
-                       response = create_tag_response(taglist, recs);
+                       response = create_blergref_response(taglist, recs);
                }
 
                if (response == NULL)
@@ -332,59 +418,27 @@ ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const c
 
                return ret;
        } else if (strncmp(url, "/put", 4) == 0) {
-               struct put_state *ps = (struct put_state *) *ptr;
-               char *username;
-               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)
-                               return respond_401(connection, MHD_NO);
-                       auth_get_password(username, password);
-
-                       ret = MHD_digest_auth_check(connection, REALM, username, password, 300);
-                       free(username);
-
-                       if (ret == MHD_INVALID_NONCE || ret == MHD_NO)
-                               return respond_401(connection, (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);
+               if (url[4] == '/')
+                       return respond_404(connection);
 
-                       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;
+               ret = process_and_check_put(connection, method, upload_data, upload_data_size, ptr);
+               if (ret == MHD_YES)
                        return MHD_YES;
-               }
 
-               if (*upload_data_size) {
-                       MHD_post_process(ps->pp, upload_data, *upload_data_size);
-                       *upload_data_size = 0;
-                       return MHD_YES;
-               }
+               struct put_state *ps = (struct put_state *) *ptr;
 
                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);
+               struct blerg *b = blerg_open(ps->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);
-               }
+               ret = blerg_store(b, ps->data, ps->data_size);
                blerg_close(b);
+               if (ret == -1)
+                       return respond_JSON_Failure(connection);
 
                MHD_destroy_post_processor(ps->pp);
-               free(username);
                free(ps->data);
                free(ps);
                *ptr = NULL;
@@ -404,15 +458,15 @@ ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const c
                        return respond_404(connection);
 
                ret = parse_url_info(url + 6, &info);
-               if ((ret & URL_INFO_AUTHOR) == 0)
+               if ((ret & URL_INFO_NAME) == 0)
                        return respond_404(connection);
 
-               if (!blerg_exists(info.author))
+               if (!blerg_exists(info.name))
                        return respond_404(connection);
 
                *ptr == NULL;
 
-               struct blerg *b = blerg_open(info.author);
+               struct blerg *b = blerg_open(info.name);
                uint64_t record_count = blerg_get_record_count(b);
                blerg_close(b);
 
@@ -435,24 +489,11 @@ ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const c
 
                return ret;
        } else if (strncmp(url, "/create", 8) == 0) {
-               struct auth_state *as = (struct auth_state *) *ptr;
-
-               if (as == NULL) {
-                       if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
-                               return respond_405(connection);
-
-                       struct auth_state *as = malloc(sizeof(struct auth_state));
-                       as->username[0] = as->password[0] = 0;
-                       as->pp = MHD_create_post_processor(connection, 1024, &POST_auth_iterator, as);
-                       *ptr = as;
+               ret = process_auth(connection, method, upload_data, upload_data_size, ptr);
+               if (ret == MHD_YES)
                        return MHD_YES;
-               }
 
-               if (*upload_data_size) {
-                       MHD_post_process(as->pp, upload_data, *upload_data_size);
-                       *upload_data_size = 0;
-                       return MHD_YES;
-               }
+               struct auth_state *as = (struct auth_state *) *ptr;
 
                if (as->username[0] == 0 || as->password[0] == 0)
                        return respond_JSON_Failure(connection);
@@ -470,34 +511,21 @@ ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const c
 
                return respond_JSON_Success(connection);
        } else if (strncmp(url, "/login", 7) == 0) {
-               struct auth_state *as = (struct auth_state *) *ptr;
-
-               if (as == NULL) {
-                       if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
-                               return respond_405(connection);
-
-                       struct auth_state *as = malloc(sizeof(struct auth_state));
-                       as->username[0] = as->password[0] = 0;
-                       as->pp = MHD_create_post_processor(connection, 1024, &POST_auth_iterator, as);
-                       *ptr = as;
+               ret = process_auth(connection, method, upload_data, upload_data_size, ptr);
+               if (ret == MHD_YES)
                        return MHD_YES;
-               }
 
-               if (*upload_data_size) {
-                       MHD_post_process(as->pp, upload_data, *upload_data_size);
-                       *upload_data_size = 0;
-                       return MHD_YES;
-               }
+               struct auth_state *as = (struct auth_state *) *ptr;
 
                if (as->username[0] == 0 || as->password[0] == 0)
                        return respond_JSON_Failure(connection);
 
-               if (!auth_login(as->username, as->password))
+               char *token = auth_login(as->username, as->password);
+               if (token == NULL)
                        return respond_JSON_Failure(connection);
 
                response = MHD_create_response_from_data(strlen(JSON_SUCCESS), JSON_SUCCESS, MHD_NO, MHD_NO);
 
-               char *token = auth_get_token(as->username);
                data = malloc(512);
                snprintf(data, 512, "auth=%s", token);
                MHD_add_response_header(response, "Set-Cookie", data);
@@ -513,32 +541,96 @@ ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const c
 
                return ret;
        } else if (strncmp(url, "/logout", 8) == 0) {
-               struct auth_state *as = (struct auth_state *) *ptr;
+               ret = process_and_check_auth(connection, method, upload_data, upload_data_size, ptr);
+               if (ret == MHD_YES)
+                       return MHD_YES;
 
-               if (as == NULL) {
-                       if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
-                               return respond_405(connection);
+               struct auth_state *as = (struct auth_state *) *ptr;
 
-                       struct auth_state *as = malloc(sizeof(struct auth_state));
-                       as->username[0] = as->password[0] = 0;
-                       as->pp = MHD_create_post_processor(connection, 1024, &POST_auth_iterator, as);
-                       *ptr = as;
+               const char *given_token = MHD_lookup_connection_value(connection, MHD_COOKIE_KIND, "auth");
+               auth_logout(as->username, given_token);
+               return respond_JSON_Success(connection);
+       } 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 (*upload_data_size) {
-                       MHD_post_process(as->pp, upload_data, *upload_data_size);
-                       *upload_data_size = 0;
-                       return MHD_YES;
+               struct auth_state *as = (struct auth_state *) *ptr;
+
+               if (url[1] == 'u') {
+                       if (url[12] != '/')
+                               return respond_404(connection);
+
+                       ret = parse_url_info(url + 13, &info);
+                       if ((ret & URL_INFO_NAME) == 0)
+                               return respond_404(connection);
+
+                       subscription_remove(as->username, info.name);
+               } else {
+                       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)
+                       return MHD_YES;
 
-               const char *given_token = MHD_lookup_connection_value(connection, MHD_COOKIE_KIND, "auth");
-               if (given_token != NULL && auth_check_token(as->username, given_token)) {
-                       auth_logout(as->username);
-                       return respond_JSON_Success(connection);
+               struct auth_state *as = (struct auth_state *) *ptr;
+
+               int recs = 50;
+               struct blergref *feedlist = subscription_list(as->username, 0, &recs, -1);
+
+               if (recs == 0) {
+                       response = MHD_create_response_from_data(2, "[]", MHD_NO, MHD_NO);
                } else {
-                       return respond_JSON_Failure(connection);
+                       response = create_blergref_response(feedlist, recs);
                }
+
+               if (response == NULL)
+                       return respond_JSON_Failure(connection);
+
+               ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
+               MHD_destroy_response(response);
+
+               return ret;
+       } else if (strncmp(url, "/feedinfo", 9) == 0) {
+               ret = process_and_check_auth(connection, method, upload_data, upload_data_size, ptr);
+               if (ret == MHD_YES)
+                       return MHD_YES;
+
+               struct auth_state *as = (struct auth_state *) *ptr;
+
+               if (url[9] != '/')
+                       return respond_404(connection);
+
+               ret = parse_url_info(url + 10, &info);
+               if ((ret & URL_INFO_NAME) == 0)
+                       return respond_404(connection);
+
+               yajl_gen g = yajl_gen_alloc(&yajl_c, NULL);
+               yajl_gen_map_open(g);
+               yajl_gen_string(g, "subscribed", 10);
+               yajl_gen_bool(g, is_subscribed(as->username, info.name));
+               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);
+               free(as);
+
+               return ret;
        } else {
                return respond_404(connection);
        }
@@ -552,7 +644,7 @@ int main(int argc, char *argv[]) {
 
        init_responses();
 
-       daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
+       daemon = MHD_start_daemon(MHD_USE_DEBUG, HTTP_BLERG_PORT, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
        if (daemon == NULL) {
                fprintf(stderr, "Could not start web server\n");
                return 1;