Fix some memory/fd leaks for tag searching and empty account listing
[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 URL_INFO_RECORD_TO 0x4
16 #define DERP "DERP DERP DERP"
17
18 yajl_gen_config yajl_c = { 0, 0 };
19
20 struct create_state {
21         struct MHD_PostProcessor *pp;
22         char username[33];
23         char password[33];
24 };
25
26 struct put_state {
27         struct MHD_PostProcessor *pp;
28         char *data;
29         int data_size;
30 };
31
32 struct get_state {
33         struct blerg *b;
34         yajl_gen g;
35         unsigned int yoff;
36         uint64_t *entries;
37         uint64_t i;
38         int done;
39 };
40
41 struct tag_state {
42         yajl_gen g;
43         unsigned int yoff;
44         struct tag *results;
45         uint64_t i;
46         int done;
47 };
48
49 int parse_url_info(const char *url, struct url_info *info) {
50         const char *c;
51         int len;
52         info->contents = 0;
53
54         c = strchr(url, '/');
55         if (c == NULL) {
56                 len = strlen(url);
57         } else {
58                 len = c - url;
59         }
60         if (len == 0)
61                 return 0;
62         memcpy(info->author, url, len);
63         info->author[len] = 0;
64         info->contents |= URL_INFO_AUTHOR;
65
66         if (c == NULL || c[1] == 0)
67                 return info->contents;
68
69         info->record = strtoull(c + 1, NULL, 10);
70         info->contents |= URL_INFO_RECORD;
71
72         c = strchr(c, '-');
73         if (c == NULL || c[1] == 0)
74                 return info->contents;
75
76         info->record_to = strtoull(c + 1, NULL, 10);
77         info->contents |= URL_INFO_RECORD_TO;
78
79         return info->contents;
80 }
81
82 uint64_t *make_sequential_list(uint64_t from, uint64_t to) {
83         uint64_t len = to - from + 1;
84         uint64_t *list = malloc(len * sizeof(uint64_t));
85         uint64_t i;
86
87         for (i = 0; i < len; i++) {
88                 list[i] = from + i;
89         }
90
91         return list;
92 }
93
94 void json_generate_one_record(yajl_gen g, const char *author, struct blerg *b, uint64_t record) {
95         char *data;
96         char number[21];
97         int len;
98
99         if (!blerg_fetch(b, record, &data, &len)) {
100                 fprintf(stderr, "Could not fetch record\n");
101                 return;
102         }
103
104         yajl_gen_map_open(g);
105         if (author != NULL) {
106                 yajl_gen_string(g, "author", 6);
107                 yajl_gen_string(g, author, strlen(author));
108         }
109         yajl_gen_string(g, "record", 6);
110         snprintf(number, 21, "%llu", record);
111         yajl_gen_string(g, number, strlen(number));
112         yajl_gen_string(g, "timestamp", 9);
113         yajl_gen_integer(g, blerg_get_timestamp(b, record));
114         yajl_gen_string(g, "data", 4);
115         yajl_gen_string(g, data, len);
116         yajl_gen_map_close(g);
117
118         free(data);
119 }
120
121 ssize_t GET_generate_list(void *cls, uint64_t pos, char *buf, size_t max) {
122         struct get_state *gs = cls;
123         const unsigned char *ybuf;
124         char *data;
125         char number[21];
126         unsigned int len;
127
128         if (gs->yoff > 0) {
129                 yajl_gen_get_buf(gs->g, &ybuf, &len);
130                 size_t bytes_remaining = len - gs->yoff;
131                 if (bytes_remaining > max) {
132                         memcpy(buf, ybuf + gs->yoff, max);
133                         gs->yoff += max;
134                         return max;
135                 } else {
136                         memcpy(buf, ybuf + gs->yoff, bytes_remaining);
137                         gs->yoff = 0;
138                         yajl_gen_clear(gs->g);
139                         return bytes_remaining;
140                 }
141         }
142
143         if (gs->done)
144                 return -1;
145
146         if (pos == 0) { /* Start iterating */
147                 yajl_gen_array_open(gs->g);
148         }
149
150         /* Snarf one record */
151         json_generate_one_record(gs->g, NULL, gs->b, gs->entries[gs->i]);
152
153         if (gs->i == 0) {
154                 yajl_gen_array_close(gs->g);
155                 gs->done = 1;
156         }
157         gs->i--;
158
159
160         yajl_gen_get_buf(gs->g, &ybuf, &len);
161         if (len > max) {
162                 memcpy(buf, ybuf, max);
163                 gs->yoff = max;
164                 return max;
165         } else {
166                 memcpy(buf, ybuf, len);
167                 yajl_gen_clear(gs->g);
168                 return len;
169         }
170 }
171
172 void GET_generate_list_free(void *cls) {
173         struct get_state *gs = cls;
174
175         blerg_close(gs->b);
176         yajl_gen_free(gs->g);
177         free(gs->entries);
178         free(gs);
179 }
180
181 ssize_t GET_generate_taglist(void *cls, uint64_t pos, char *buf, size_t max) {
182         struct tag_state *ts = cls;
183         struct blerg *b;
184         const unsigned char *ybuf;
185         unsigned int len;
186
187         if (ts->yoff > 0) {
188                 yajl_gen_get_buf(ts->g, &ybuf, &len);
189                 size_t bytes_remaining = len - ts->yoff;
190                 if (bytes_remaining > max) {
191                         memcpy(buf, ybuf + ts->yoff, max);
192                         ts->yoff += max;
193                         return max;
194                 } else {
195                         memcpy(buf, ybuf + ts->yoff, bytes_remaining);
196                         ts->yoff = 0;
197                         yajl_gen_clear(ts->g);
198                         return bytes_remaining;
199                 }
200         }
201
202         if (ts->done)
203                 return -1;
204
205         if (pos == 0) { /* Start iterating */
206                 yajl_gen_array_open(ts->g);
207         }
208
209         /* Snarf one record */
210         b = blerg_open(ts->results[ts->i].author);
211         if (b == NULL)
212                 goto skip_bad_blerg;
213
214         json_generate_one_record(ts->g, ts->results[ts->i].author, b, ts->results[ts->i].record);
215
216         blerg_close(b);
217
218 skip_bad_blerg:
219         if (ts->i == 0) {
220                 yajl_gen_array_close(ts->g);
221                 ts->done = 1;
222         }
223
224         ts->i--;
225
226         yajl_gen_get_buf(ts->g, &ybuf, &len);
227         if (len > max) {
228                 memcpy(buf, ybuf, max);
229                 ts->yoff = max;
230                 return max;
231         } else {
232                 memcpy(buf, ybuf, len);
233                 yajl_gen_clear(ts->g);
234                 return len;
235         }
236 }
237
238 void GET_generate_taglist_free(void *cls) {
239         struct tag_state *ts = cls;
240
241         yajl_gen_free(ts->g);
242         free(ts->results);
243         free(ts);
244 }
245
246 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) {
247         struct create_state *cs = cls;
248
249         if (strncmp(key, "username", 9) == 0) {
250                 if (size > 32) size = 32;
251                 memcpy(cs->username, data, size);
252                 cs->username[size] = 0;
253         } else if (strncmp(key, "password", 9) == 0) {
254                 if (size > 32) size = 32;
255                 memcpy(cs->password, data, size);
256                 cs->password[size] = 0;
257         }
258
259         return MHD_YES;
260 }
261
262 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) {
263         struct put_state *ps = cls;
264
265         if (strncmp(key, "data", 5) == 0) {
266                 if (ps->data == NULL) {
267                         ps->data_size = size;
268                         ps->data = malloc(size);
269                 } else {
270                         if (ps->data_size + size > MAX_RECORD_SIZE) {
271                                 size = MAX_RECORD_SIZE - ps->data_size;
272                         }
273                         ps->data_size += size;
274                         ps->data = realloc(ps->data, ps->data_size);
275                 }
276                 memcpy(ps->data + off, data, size);
277                 if (ps->data_size == MAX_RECORD_SIZE)
278                         return MHD_NO;
279         }
280
281         return MHD_YES;
282 }
283
284 struct MHD_Response *create_response_for_range(struct blerg *b, uint64_t from, uint64_t to) {
285         struct MHD_Response *response;
286         struct get_state *gs = malloc(sizeof(struct get_state));
287         gs->b = b;
288
289         uint64_t record_count = blerg_get_record_count(b);
290
291         if (from > to || from >= record_count || to >= record_count || to - from > 99) {
292                 blerg_close(b);
293                 free(gs);
294                 return NULL;
295         }
296
297         gs->entries = make_sequential_list(from, to);
298         gs->i = to - from;
299
300         gs->g = yajl_gen_alloc(&yajl_c, NULL);
301         gs->yoff = gs->done = 0;
302
303         response = MHD_create_response_from_callback(-1, 262144, &GET_generate_list, gs, &GET_generate_list_free);
304
305         return response;
306 }
307
308 struct MHD_Response *create_tag_response(struct tag *results, uint64_t len) {
309         struct tag_state *ts = malloc(sizeof(struct tag_state));
310         ts->g = yajl_gen_alloc(&yajl_c, NULL);
311         ts->results = results;
312         ts->i = len - 1;
313         ts->yoff = ts->done = 0;
314
315         return MHD_create_response_from_callback(-1, 262144, &GET_generate_taglist, ts, &GET_generate_taglist_free);
316 }
317
318 static int
319 ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const char *method,
320           const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) {
321         struct MHD_Response *response;
322         int ret, len;
323         struct url_info info;
324         char *data;
325
326         if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) {
327                 if (*ptr == NULL) {
328                         if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
329                                 return respond_405(connection);
330
331                         *ptr = (void *) 1;
332                         return MHD_YES;
333                 }
334
335                 if (url[4] != '/')
336                         return respond_404(connection);
337
338                 ret = parse_url_info(url + 5, &info);
339                 if ((ret & URL_INFO_AUTHOR) == 0)
340                         return respond_404(connection);
341
342                 if (!blerg_exists(info.author))
343                         return respond_404(connection);
344
345                 *ptr == NULL;
346
347                 struct blerg *b = blerg_open(info.author);
348
349                 if ((ret & URL_INFO_RECORD) && (ret & URL_INFO_RECORD_TO)) {
350                         response = create_response_for_range(b, info.record, info.record_to);
351                 } else if (ret & URL_INFO_RECORD) {
352                         ret = blerg_fetch(b, info.record, &data, &len);
353                         blerg_close(b);
354
355                         if (ret == 0)
356                                 return respond_404(connection);
357                         response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
358                 } else {
359                         uint64_t record_count, from, to;
360                         record_count = blerg_get_record_count(b);
361                         if (record_count == 0) {
362                                 blerg_close(b);
363                                 response = MHD_create_response_from_data(2, "[]", MHD_NO, MHD_NO);
364                         } else {
365                                 to = record_count - 1;
366                                 from = (record_count > 50 ? to - 49 : 0);
367                                 response = create_response_for_range(b, from, to);
368                         }
369                 }
370
371                 if (response == NULL) {
372                         blerg_close(b);
373                         return respond_JSON_Failure(connection);
374                 }
375
376                 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
377                 MHD_destroy_response(response);
378                 return ret;
379         } else if (strncmp(url, "/tag", 4) == 0 && strlen(url) > 4) {
380                 if (*ptr == NULL) {
381                         if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
382                                 return respond_405(connection);
383
384                         *ptr = (void *) 1;
385                         return MHD_YES;
386                 }
387
388                 if (url[4] != '/')
389                         return respond_404(connection);
390
391                 ret = parse_url_info(url + 5, &info);
392                 if ((ret & URL_INFO_AUTHOR) == 0)
393                         return respond_404(connection);
394
395                 if (!tag_exists(info.author))
396                         return respond_404(connection);
397
398                 uint64_t recs = 50;
399                 struct tag *taglist = tag_list(info.author, 0, &recs, -1);
400
401                 if (recs == 0) {
402                         response = MHD_create_response_from_data(2, "[]", MHD_NO, MHD_NO);
403                 } else {
404                         response = create_tag_response(taglist, recs);
405                 }
406
407                 if (response == NULL)
408                         return respond_JSON_Failure(connection);
409
410                 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
411                 MHD_destroy_response(response);
412
413                 return ret;
414         } else if (strncmp(url, "/put", 4) == 0) {
415                 struct put_state *ps = (struct put_state *) *ptr;
416                 char *username;
417                 char password[33];
418
419                 if (*ptr == NULL) {
420                         if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
421                                 return respond_405(connection);
422
423                         if (url[4] == '/')
424                                 return respond_404(connection);
425
426                         *ptr = (void *) 1;
427
428                         username = MHD_digest_auth_get_username(connection);
429                         if (username == NULL)
430                                 return respond_401(connection, MHD_NO);
431                         auth_get_password(username, password);
432
433                         ret = MHD_digest_auth_check(connection, REALM, username, password, 300);
434                         free(username);
435
436                         if (ret == MHD_INVALID_NONCE || ret == MHD_NO)
437                                 return respond_401(connection, (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);
438
439                         struct put_state *ps = malloc(sizeof(struct put_state));
440                         ps->data = NULL;
441                         ps->data_size = 0;
442                         ps->pp = MHD_create_post_processor(connection, 16384, &POST_put_iterator, ps);
443                         *ptr = ps;
444                         return MHD_YES;
445                 }
446
447                 if (*upload_data_size) {
448                         MHD_post_process(ps->pp, upload_data, *upload_data_size);
449                         *upload_data_size = 0;
450                         return MHD_YES;
451                 }
452
453                 if (ps->data == NULL || ps->data_size == 0)
454                         return respond_JSON_Failure(connection);
455
456                 username = MHD_digest_auth_get_username(connection);
457                 struct blerg *b = blerg_open(username);
458                 if (b == NULL)
459                         return respond_JSON_Failure(connection);
460                 if (blerg_store(b, ps->data, ps->data_size) == -1) {
461                         blerg_close(b);
462                         return respond_JSON_Failure(connection);
463                 }
464                 blerg_close(b);
465
466                 MHD_destroy_post_processor(ps->pp);
467                 free(username);
468                 free(ps->data);
469                 free(ps);
470                 *ptr = NULL;
471
472                 return respond_JSON_Success(connection);
473         } else if (strncmp(url, "/info", 5) == 0) {
474                 if (*ptr == NULL) {
475                         *ptr = (void *) 1;
476
477                         if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
478                                 return respond_405(connection);
479                         return MHD_YES;
480                 }
481
482
483                 if (url[5] != '/')
484                         return respond_404(connection);
485
486                 ret = parse_url_info(url + 6, &info);
487                 if ((ret & URL_INFO_AUTHOR) == 0)
488                         return respond_404(connection);
489
490                 if (!blerg_exists(info.author))
491                         return respond_404(connection);
492
493                 *ptr == NULL;
494
495                 struct blerg *b = blerg_open(info.author);
496                 uint64_t record_count = blerg_get_record_count(b);
497                 blerg_close(b);
498
499                 char number[21];
500                 yajl_gen g = yajl_gen_alloc(&yajl_c, NULL);
501                 yajl_gen_map_open(g);
502                 yajl_gen_string(g, "record_count", 12);
503                 snprintf(number, 21, "%llu", record_count);
504                 yajl_gen_string(g, number, strlen(number));
505                 yajl_gen_map_close(g);
506
507                 const unsigned char *ybuf;
508                 yajl_gen_get_buf(g, &ybuf, &len);
509
510                 response = MHD_create_response_from_data(len, (void *)ybuf, MHD_NO, MHD_YES);
511                 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
512                 MHD_destroy_response(response);
513
514                 yajl_gen_free(g);
515
516                 return ret;
517         } else if (strncmp(url, "/create", 8) == 0) {
518                 struct create_state *cs = (struct create_state *) *ptr;
519
520                 if (cs == NULL) {
521                         if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
522                                 return respond_405(connection);
523
524                         struct create_state *cs = malloc(sizeof(struct create_state));
525                         cs->username[0] = cs->password[0] = 0;
526                         cs->pp = MHD_create_post_processor(connection, 1024, &POST_create_iterator, cs);
527                         *ptr = cs;
528                         return MHD_YES;
529                 }
530
531                 if (*upload_data_size) {
532                         MHD_post_process(cs->pp, upload_data, *upload_data_size);
533                         *upload_data_size = 0;
534                         return MHD_YES;
535                 }
536
537                 if (cs->username[0] == 0 || cs->password[0] == 0)
538                         return respond_JSON_Failure(connection);
539
540                 if (blerg_exists(cs->username))
541                         return respond_JSON_Failure(connection);
542
543                 struct blerg *b = blerg_open(cs->username);
544                 blerg_close(b);
545                 auth_set_password(cs->username, cs->password);
546
547                 MHD_destroy_post_processor(cs->pp);
548                 free(cs);
549                 *ptr = NULL;
550
551                 return respond_JSON_Success(connection);
552         } else {
553                 return respond_404(connection);
554         }
555 }
556
557
558 int main(int argc, char *argv[]) {
559         struct MHD_Daemon *daemon;
560         fd_set rs, ws, es;
561         int max;
562
563         init_responses();
564
565         daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
566         if (daemon == NULL) {
567                 fprintf(stderr, "Could not start web server\n");
568                 return 1;
569         }
570
571         while (1) {
572                 FD_ZERO(&rs); FD_ZERO(&ws); FD_ZERO(&es);
573                 if (MHD_get_fdset(daemon, &rs, &ws, &es, &max) != MHD_YES) {
574                         fprintf(stderr, "Fatal error getting fd sets\n");
575                         break;
576                 }
577                 select(max + 1, &rs, &ws, &es, NULL);
578                 MHD_run(daemon);
579         }
580         MHD_stop_daemon(daemon);
581         return 0;
582 }