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