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