Some cleanup, genericize JSON iterator to accept a list of record IDs.
[blerg.git] / http_blerg.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <microhttpd.h>
5 #include <yajl/yajl_gen.h>
6 #include "database.h"
7 #include "tags.h"
8 #include "auth.h"
9 #include "canned_responses.h"
10 #include "http.h"
11 #include "config.h"
12
13 #define URL_INFO_AUTHOR 0x1
14 #define URL_INFO_RECORD 0x2
15 #define DERP "DERP DERP DERP"
16
17 struct create_state {
18         struct MHD_PostProcessor *pp;
19         char username[33];
20         char password[33];
21 };
22
23 struct put_state {
24         struct MHD_PostProcessor *pp;
25         char *data;
26         int data_size;
27 };
28
29 struct get_state {
30         struct blerg *b;
31         yajl_gen g;
32         unsigned int yoff;
33         uint64_t *entries;
34         uint64_t i;
35         int done;
36 };
37
38 int parse_url_info(const char *url, char *author, uint64_t *record) {
39         const char *c;
40         int ret = 0;
41         int len;
42
43         c = strchr(url, '/');
44         if (c == NULL) {
45                 len = strlen(url);
46         } else {
47                 len = c - url;
48         }
49         if (len == 0)
50                 return 0;
51         memcpy(author, url, len);
52         author[len] = 0;
53         ret |= URL_INFO_AUTHOR;
54
55         if (c != NULL && c[1] != 0) {
56                 *record = strtoull(c + 1, NULL, 10);
57                 ret |= URL_INFO_RECORD;
58         }
59
60         return ret;
61 }
62
63 uint64_t *make_sequential_list(uint64_t from, uint64_t to) {
64         uint64_t len = to - from + 1;
65         uint64_t *list = malloc(len * sizeof(uint64_t));
66         uint64_t i;
67
68         for (i = 0; i < len; i++) {
69                 list[i] = from + i;
70         }
71
72         return list;
73 }
74
75 ssize_t GET_generate_list(void *cls, uint64_t pos, char *buf, size_t max) {
76         struct get_state *gs = cls;
77         const unsigned char *ybuf;
78         char *data;
79         char number[21];
80         unsigned int len;
81
82         if (gs->yoff > 0) {
83                 yajl_gen_get_buf(gs->g, &ybuf, &len);
84                 size_t bytes_remaining = len - gs->yoff;
85                 if (bytes_remaining > max) {
86                         memcpy(buf, ybuf + gs->yoff, max);
87                         gs->yoff += max;
88                         return max;
89                 } else {
90                         memcpy(buf, ybuf + gs->yoff, bytes_remaining);
91                         gs->yoff = 0;
92                         yajl_gen_clear(gs->g);
93                         return bytes_remaining;
94                 }
95         }
96
97         if (gs->done)
98                 return -1;
99
100         if (pos == 0) { /* Start iterating */
101                 yajl_gen_map_open(gs->g);
102         }
103
104         /* Snarf one record */
105         blerg_fetch(gs->b, gs->entries[gs->i], &data, &len);
106         snprintf(number, 21, "%llu", gs->entries[gs->i]);
107         yajl_gen_string(gs->g, number, strlen(number));
108         yajl_gen_string(gs->g, data, len);
109         free(data);
110         if (gs->i == 0) {
111                 yajl_gen_map_close(gs->g);
112                 gs->done = 1;
113         }
114         gs->i--;
115
116
117         yajl_gen_get_buf(gs->g, &ybuf, &len);
118         if (len > max) {
119                 memcpy(buf, ybuf, max);
120                 gs->yoff = max;
121                 return max;
122         } else {
123                 memcpy(buf, ybuf, len);
124                 yajl_gen_clear(gs->g);
125                 return len;
126         }
127 }
128
129 void GET_generate_list_free(void *cls) {
130         struct get_state *gs = cls;
131
132         blerg_close(gs->b);
133         yajl_gen_free(gs->g);
134         free(gs->entries);
135         free(gs);
136 }
137
138 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) {
139         struct create_state *cs = cls;
140
141         if (strncmp(key, "username", 9) == 0) {
142                 if (size > 32) size = 32;
143                 memcpy(cs->username, data, size);
144                 cs->username[size] = 0;
145         } else if (strncmp(key, "password", 9) == 0) {
146                 if (size > 32) size = 32;
147                 memcpy(cs->password, data, size);
148                 cs->password[size] = 0;
149         }
150
151         return MHD_YES;
152 }
153
154 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) {
155         struct put_state *ps = cls;
156
157         if (strncmp(key, "data", 5) == 0) {
158                 if (ps->data == NULL) {
159                         ps->data_size = size;
160                         ps->data = malloc(size);
161                 } else {
162                         if (ps->data_size + size > MAX_RECORD_SIZE) {
163                                 size = MAX_RECORD_SIZE - ps->data_size;
164                         }
165                         ps->data_size += size;
166                         ps->data = realloc(ps->data, ps->data_size);
167                 }
168                 memcpy(ps->data + off, data, size);
169                 if (ps->data_size == MAX_RECORD_SIZE)
170                         return MHD_NO;
171         }
172
173         return MHD_YES;
174 }
175
176 static int
177 ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const char *method,
178           const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) {
179         struct MHD_Response *response;
180         int ret, len;
181         char author[33];
182         uint64_t record;
183         char *data;
184
185         if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) {
186                 if (*ptr == NULL) {
187                         if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
188                                 return respond_405(connection);
189
190                         *ptr = (void *) 1;
191                         return MHD_YES;
192                 }
193
194                 if (url[4] != '/')
195                         return respond_404(connection);
196
197                 ret = parse_url_info(url + 5, author, &record);
198                 if ((ret & URL_INFO_AUTHOR) == 0)
199                         return respond_404(connection);
200
201                 if (!blerg_exists(author))
202                         return respond_404(connection);
203
204                 *ptr == NULL;
205
206                 if (ret & URL_INFO_RECORD) {
207                         struct blerg *b = blerg_open(author);
208                         ret = blerg_fetch(b, record, &data, &len);
209                         blerg_close(b);
210
211                         if (ret == 0) {
212                                 return respond_404(connection);
213                         } else {
214                                 response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
215                                 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
216                         }
217                         MHD_destroy_response(response);
218                         return ret;
219                 } else {
220                         struct get_state *gs = malloc(sizeof(struct get_state));
221                         gs->b = blerg_open(author);
222                         uint64_t record_count = blerg_get_record_count(gs->b);
223                         if (record_count == 0) {
224                                 response = MHD_create_response_from_data(2, "[]", MHD_NO, MHD_NO);
225                         } else {
226                                 uint64_t from, to, i, j;
227                                 to = record_count - 1;
228                                 from = (record_count > 50 ? to - 49 : 0);
229                                 gs->entries = make_sequential_list(from, to);
230                                 gs->i = to - from;
231
232                                 yajl_gen_config c;
233                                 c.beautify = 0;
234
235                                 gs->g = yajl_gen_alloc(&c, NULL);
236                                 gs->yoff = gs->done = 0;
237
238                                 response = MHD_create_response_from_callback(-1, 262144, &GET_generate_list, gs, &GET_generate_list_free);
239                         }
240
241                         ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
242                         MHD_destroy_response(response);
243
244                         return ret;
245                 }
246         } else if (strncmp(url, "/put", 4) == 0) {
247                 struct put_state *ps = (struct put_state *) *ptr;
248                 char *username;
249                 char password[33];
250
251                 if (*ptr == NULL) {
252                         if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
253                                 return respond_405(connection);
254
255                         if (url[4] == '/')
256                                 return respond_404(connection);
257
258                         *ptr = (void *) 1;
259
260                         username = MHD_digest_auth_get_username(connection);
261                         if (username == NULL)
262                                 return respond_401(connection, MHD_NO);
263                         auth_get_password(username, password);
264
265                         ret = MHD_digest_auth_check(connection, REALM, username, password, 300);
266                         free(username);
267
268                         if (ret == MHD_INVALID_NONCE || ret == MHD_NO)
269                                 return respond_401(connection, (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);
270
271                         struct put_state *ps = malloc(sizeof(struct put_state));
272                         ps->data = NULL;
273                         ps->data_size = 0;
274                         ps->pp = MHD_create_post_processor(connection, 16384, &POST_put_iterator, ps);
275                         *ptr = ps;
276                         return MHD_YES;
277                 }
278
279                 if (*upload_data_size) {
280                         MHD_post_process(ps->pp, upload_data, *upload_data_size);
281                         *upload_data_size = 0;
282                         return MHD_YES;
283                 }
284
285                 if (ps->data == NULL || ps->data_size == 0)
286                         return respond_JSON_Failure(connection);
287
288                 username = MHD_digest_auth_get_username(connection);
289                 struct blerg *b = blerg_open(username);
290                 if (b == NULL)
291                         return respond_JSON_Failure(connection);
292                 if (blerg_store(b, ps->data, ps->data_size) == -1) {
293                         blerg_close(b);
294                         return respond_JSON_Failure(connection);
295                 }
296                 blerg_close(b);
297
298                 MHD_destroy_post_processor(ps->pp);
299                 free(username);
300                 free(ps->data);
301                 free(ps);
302                 *ptr = NULL;
303
304                 return respond_JSON_Success(connection);
305         } else if (strncmp(url, "/create", 8) == 0) {
306                 struct create_state *cs = (struct create_state *) *ptr;
307
308                 if (cs == NULL) {
309                         if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
310                                 return respond_405(connection);
311
312                         struct create_state *cs = malloc(sizeof(struct create_state));
313                         cs->username[0] = cs->password[0] = 0;
314                         cs->pp = MHD_create_post_processor(connection, 1024, &POST_create_iterator, cs);
315                         *ptr = cs;
316                         return MHD_YES;
317                 }
318
319                 if (*upload_data_size) {
320                         MHD_post_process(cs->pp, upload_data, *upload_data_size);
321                         *upload_data_size = 0;
322                         return MHD_YES;
323                 }
324
325                 if (cs->username[0] == 0 || cs->password[0] == 0)
326                         return respond_JSON_Failure(connection);
327
328                 if (blerg_exists(cs->username))
329                         return respond_JSON_Failure(connection);
330
331                 struct blerg *b = blerg_open(cs->username);
332                 blerg_close(b);
333                 auth_set_password(cs->username, cs->password);
334
335                 MHD_destroy_post_processor(cs->pp);
336                 free(cs);
337                 *ptr = NULL;
338
339                 return respond_JSON_Success(connection);
340         } else {
341                 return respond_404(connection);
342         }
343 }
344
345
346 int main(int argc, char *argv[]) {
347         struct MHD_Daemon *daemon;
348         fd_set rs, ws, es;
349         int max;
350
351         init_responses();
352
353         daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
354         if (daemon == NULL) {
355                 fprintf(stderr, "Could not start web server\n");
356                 return 1;
357         }
358
359         while (1) {
360                 FD_ZERO(&rs); FD_ZERO(&ws); FD_ZERO(&es);
361                 if (MHD_get_fdset(daemon, &rs, &ws, &es, &max) != MHD_YES) {
362                         fprintf(stderr, "Fatal error getting fd sets\n");
363                         break;
364                 }
365                 select(max + 1, &rs, &ws, &es, NULL);
366                 MHD_run(daemon);
367         }
368         MHD_stop_daemon(daemon);
369         return 0;
370 }