Add tag searching to web API
[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                                 response = MHD_create_response_from_data(2, "[]", MHD_NO, MHD_NO);
363                         } else {
364                                 to = record_count - 1;
365                                 from = (record_count > 50 ? to - 49 : 0);
366                                 response = create_response_for_range(b, from, to);
367                         }
368                 }
369
370                 if (response == NULL)
371                         return respond_JSON_Failure(connection);
372
373                 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
374                 MHD_destroy_response(response);
375                 return ret;
376         } else if (strncmp(url, "/tag", 4) == 0 && strlen(url) > 4) {
377                 if (*ptr == NULL) {
378                         if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
379                                 return respond_405(connection);
380
381                         *ptr = (void *) 1;
382                         return MHD_YES;
383                 }
384
385                 if (url[4] != '/')
386                         return respond_404(connection);
387
388                 ret = parse_url_info(url + 5, &info);
389                 if ((ret & URL_INFO_AUTHOR) == 0)
390                         return respond_404(connection);
391
392                 if (!tag_exists(info.author))
393                         return respond_404(connection);
394
395                 uint64_t recs = 50;
396                 struct tag *taglist = tag_list(info.author, 0, &recs, -1);
397
398                 if (recs == 0) {
399                         response = MHD_create_response_from_data(2, "[]", MHD_NO, MHD_NO);
400                 } else {
401                         response = create_tag_response(taglist, recs);
402                 }
403
404                 if (response == NULL)
405                         return respond_JSON_Failure(connection);
406
407                 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
408                 MHD_destroy_response(response);
409
410                 return ret;
411         } else if (strncmp(url, "/put", 4) == 0) {
412                 struct put_state *ps = (struct put_state *) *ptr;
413                 char *username;
414                 char password[33];
415
416                 if (*ptr == NULL) {
417                         if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
418                                 return respond_405(connection);
419
420                         if (url[4] == '/')
421                                 return respond_404(connection);
422
423                         *ptr = (void *) 1;
424
425                         username = MHD_digest_auth_get_username(connection);
426                         if (username == NULL)
427                                 return respond_401(connection, MHD_NO);
428                         auth_get_password(username, password);
429
430                         ret = MHD_digest_auth_check(connection, REALM, username, password, 300);
431                         free(username);
432
433                         if (ret == MHD_INVALID_NONCE || ret == MHD_NO)
434                                 return respond_401(connection, (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);
435
436                         struct put_state *ps = malloc(sizeof(struct put_state));
437                         ps->data = NULL;
438                         ps->data_size = 0;
439                         ps->pp = MHD_create_post_processor(connection, 16384, &POST_put_iterator, ps);
440                         *ptr = ps;
441                         return MHD_YES;
442                 }
443
444                 if (*upload_data_size) {
445                         MHD_post_process(ps->pp, upload_data, *upload_data_size);
446                         *upload_data_size = 0;
447                         return MHD_YES;
448                 }
449
450                 if (ps->data == NULL || ps->data_size == 0)
451                         return respond_JSON_Failure(connection);
452
453                 username = MHD_digest_auth_get_username(connection);
454                 struct blerg *b = blerg_open(username);
455                 if (b == NULL)
456                         return respond_JSON_Failure(connection);
457                 if (blerg_store(b, ps->data, ps->data_size) == -1) {
458                         blerg_close(b);
459                         return respond_JSON_Failure(connection);
460                 }
461                 blerg_close(b);
462
463                 MHD_destroy_post_processor(ps->pp);
464                 free(username);
465                 free(ps->data);
466                 free(ps);
467                 *ptr = NULL;
468
469                 return respond_JSON_Success(connection);
470         } else if (strncmp(url, "/info", 5) == 0) {
471                 if (*ptr == NULL) {
472                         *ptr = (void *) 1;
473
474                         if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
475                                 return respond_405(connection);
476                         return MHD_YES;
477                 }
478
479
480                 if (url[5] != '/')
481                         return respond_404(connection);
482
483                 ret = parse_url_info(url + 6, &info);
484                 if ((ret & URL_INFO_AUTHOR) == 0)
485                         return respond_404(connection);
486
487                 if (!blerg_exists(info.author))
488                         return respond_404(connection);
489
490                 *ptr == NULL;
491
492                 struct blerg *b = blerg_open(info.author);
493                 uint64_t record_count = blerg_get_record_count(b);
494                 blerg_close(b);
495
496                 char number[21];
497                 yajl_gen g = yajl_gen_alloc(&yajl_c, NULL);
498                 yajl_gen_map_open(g);
499                 yajl_gen_string(g, "record_count", 12);
500                 snprintf(number, 21, "%llu", record_count);
501                 yajl_gen_string(g, number, strlen(number));
502                 yajl_gen_map_close(g);
503
504                 const unsigned char *ybuf;
505                 yajl_gen_get_buf(g, &ybuf, &len);
506
507                 response = MHD_create_response_from_data(len, (void *)ybuf, MHD_NO, MHD_YES);
508                 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
509                 MHD_destroy_response(response);
510
511                 yajl_gen_free(g);
512
513                 return ret;
514         } else if (strncmp(url, "/create", 8) == 0) {
515                 struct create_state *cs = (struct create_state *) *ptr;
516
517                 if (cs == NULL) {
518                         if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
519                                 return respond_405(connection);
520
521                         struct create_state *cs = malloc(sizeof(struct create_state));
522                         cs->username[0] = cs->password[0] = 0;
523                         cs->pp = MHD_create_post_processor(connection, 1024, &POST_create_iterator, cs);
524                         *ptr = cs;
525                         return MHD_YES;
526                 }
527
528                 if (*upload_data_size) {
529                         MHD_post_process(cs->pp, upload_data, *upload_data_size);
530                         *upload_data_size = 0;
531                         return MHD_YES;
532                 }
533
534                 if (cs->username[0] == 0 || cs->password[0] == 0)
535                         return respond_JSON_Failure(connection);
536
537                 if (blerg_exists(cs->username))
538                         return respond_JSON_Failure(connection);
539
540                 struct blerg *b = blerg_open(cs->username);
541                 blerg_close(b);
542                 auth_set_password(cs->username, cs->password);
543
544                 MHD_destroy_post_processor(cs->pp);
545                 free(cs);
546                 *ptr = NULL;
547
548                 return respond_JSON_Success(connection);
549         } else {
550                 return respond_404(connection);
551         }
552 }
553
554
555 int main(int argc, char *argv[]) {
556         struct MHD_Daemon *daemon;
557         fd_set rs, ws, es;
558         int max;
559
560         init_responses();
561
562         daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
563         if (daemon == NULL) {
564                 fprintf(stderr, "Could not start web server\n");
565                 return 1;
566         }
567
568         while (1) {
569                 FD_ZERO(&rs); FD_ZERO(&ws); FD_ZERO(&es);
570                 if (MHD_get_fdset(daemon, &rs, &ws, &es, &max) != MHD_YES) {
571                         fprintf(stderr, "Fatal error getting fd sets\n");
572                         break;
573                 }
574                 select(max + 1, &rs, &ws, &es, NULL);
575                 MHD_run(daemon);
576         }
577         MHD_stop_daemon(daemon);
578         return 0;
579 }