845f31202796ce04f0fded3fa05f80ad7c396826
[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 int parse_url_info(const char *url, struct url_info *info) {
42         const char *c;
43         int len;
44         info->contents = 0;
45
46         c = strchr(url, '/');
47         if (c == NULL) {
48                 len = strlen(url);
49         } else {
50                 len = c - url;
51         }
52         if (len == 0)
53                 return 0;
54         memcpy(info->author, url, len);
55         info->author[len] = 0;
56         info->contents |= URL_INFO_AUTHOR;
57
58         if (c == NULL || c[1] == 0)
59                 return info->contents;
60
61         info->record = strtoull(c + 1, NULL, 10);
62         info->contents |= URL_INFO_RECORD;
63
64         c = strchr(c, '-');
65         if (c == NULL || c[1] == 0)
66                 return info->contents;
67
68         info->record_to = strtoull(c + 1, NULL, 10);
69         info->contents |= URL_INFO_RECORD_TO;
70
71         return info->contents;
72 }
73
74 uint64_t *make_sequential_list(uint64_t from, uint64_t to) {
75         uint64_t len = to - from + 1;
76         uint64_t *list = malloc(len * sizeof(uint64_t));
77         uint64_t i;
78
79         for (i = 0; i < len; i++) {
80                 list[i] = from + i;
81         }
82
83         return list;
84 }
85
86 ssize_t GET_generate_list(void *cls, uint64_t pos, char *buf, size_t max) {
87         struct get_state *gs = cls;
88         const unsigned char *ybuf;
89         char *data;
90         char number[21];
91         unsigned int len;
92
93         if (gs->yoff > 0) {
94                 yajl_gen_get_buf(gs->g, &ybuf, &len);
95                 size_t bytes_remaining = len - gs->yoff;
96                 if (bytes_remaining > max) {
97                         memcpy(buf, ybuf + gs->yoff, max);
98                         gs->yoff += max;
99                         return max;
100                 } else {
101                         memcpy(buf, ybuf + gs->yoff, bytes_remaining);
102                         gs->yoff = 0;
103                         yajl_gen_clear(gs->g);
104                         return bytes_remaining;
105                 }
106         }
107
108         if (gs->done)
109                 return -1;
110
111         if (pos == 0) { /* Start iterating */
112                 yajl_gen_array_open(gs->g);
113         }
114
115         /* Snarf one record */
116         blerg_fetch(gs->b, gs->entries[gs->i], &data, &len);
117
118         yajl_gen_map_open(gs->g);
119         yajl_gen_string(gs->g, "record", 6);
120         snprintf(number, 21, "%llu", gs->entries[gs->i]);
121         yajl_gen_string(gs->g, number, strlen(number));
122         yajl_gen_string(gs->g, "timestamp", 9);
123         yajl_gen_integer(gs->g, blerg_get_timestamp(gs->b, gs->entries[gs->i]));
124         yajl_gen_string(gs->g, "data", 4);
125         yajl_gen_string(gs->g, data, len);
126         yajl_gen_map_close(gs->g);
127
128         free(data);
129         if (gs->i == 0) {
130                 yajl_gen_array_close(gs->g);
131                 gs->done = 1;
132         }
133         gs->i--;
134
135
136         yajl_gen_get_buf(gs->g, &ybuf, &len);
137         if (len > max) {
138                 memcpy(buf, ybuf, max);
139                 gs->yoff = max;
140                 return max;
141         } else {
142                 memcpy(buf, ybuf, len);
143                 yajl_gen_clear(gs->g);
144                 return len;
145         }
146 }
147
148 void GET_generate_list_free(void *cls) {
149         struct get_state *gs = cls;
150
151         blerg_close(gs->b);
152         yajl_gen_free(gs->g);
153         free(gs->entries);
154         free(gs);
155 }
156
157 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) {
158         struct create_state *cs = cls;
159
160         if (strncmp(key, "username", 9) == 0) {
161                 if (size > 32) size = 32;
162                 memcpy(cs->username, data, size);
163                 cs->username[size] = 0;
164         } else if (strncmp(key, "password", 9) == 0) {
165                 if (size > 32) size = 32;
166                 memcpy(cs->password, data, size);
167                 cs->password[size] = 0;
168         }
169
170         return MHD_YES;
171 }
172
173 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) {
174         struct put_state *ps = cls;
175
176         if (strncmp(key, "data", 5) == 0) {
177                 if (ps->data == NULL) {
178                         ps->data_size = size;
179                         ps->data = malloc(size);
180                 } else {
181                         if (ps->data_size + size > MAX_RECORD_SIZE) {
182                                 size = MAX_RECORD_SIZE - ps->data_size;
183                         }
184                         ps->data_size += size;
185                         ps->data = realloc(ps->data, ps->data_size);
186                 }
187                 memcpy(ps->data + off, data, size);
188                 if (ps->data_size == MAX_RECORD_SIZE)
189                         return MHD_NO;
190         }
191
192         return MHD_YES;
193 }
194
195 struct MHD_Response *create_response_for_range(struct blerg *b, uint64_t from, uint64_t to) {
196         struct MHD_Response *response;
197         struct get_state *gs = malloc(sizeof(struct get_state));
198         gs->b = b;
199
200         uint64_t record_count = blerg_get_record_count(b);
201
202         if (from > to || from >= record_count || to >= record_count || to - from > 99) {
203                 blerg_close(b);
204                 free(gs);
205                 return NULL;
206         }
207
208         gs->entries = make_sequential_list(from, to);
209         gs->i = to - from;
210
211         gs->g = yajl_gen_alloc(&yajl_c, NULL);
212         gs->yoff = gs->done = 0;
213
214         response = MHD_create_response_from_callback(-1, 262144, &GET_generate_list, gs, &GET_generate_list_free);
215
216         return response;
217 }
218
219 static int
220 ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const char *method,
221           const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) {
222         struct MHD_Response *response;
223         int ret, len;
224         struct url_info info;
225         char *data;
226
227         if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) {
228                 if (*ptr == NULL) {
229                         if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
230                                 return respond_405(connection);
231
232                         *ptr = (void *) 1;
233                         return MHD_YES;
234                 }
235
236                 if (url[4] != '/')
237                         return respond_404(connection);
238
239                 ret = parse_url_info(url + 5, &info);
240                 if ((ret & URL_INFO_AUTHOR) == 0)
241                         return respond_404(connection);
242
243                 if (!blerg_exists(info.author))
244                         return respond_404(connection);
245
246                 *ptr == NULL;
247
248                 struct blerg *b = blerg_open(info.author);
249
250                 if ((ret & URL_INFO_RECORD) && (ret & URL_INFO_RECORD_TO)) {
251                         response = create_response_for_range(b, info.record, info.record_to);
252                 } else if (ret & URL_INFO_RECORD) {
253                         ret = blerg_fetch(b, info.record, &data, &len);
254                         blerg_close(b);
255
256                         if (ret == 0)
257                                 return respond_404(connection);
258                         response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
259                 } else {
260                         uint64_t record_count, from, to;
261                         record_count = blerg_get_record_count(b);
262                         if (record_count == 0) {
263                                 response = MHD_create_response_from_data(2, "[]", MHD_NO, MHD_NO);
264                         } else {
265                                 to = record_count - 1;
266                                 from = (record_count > 50 ? to - 49 : 0);
267                                 response = create_response_for_range(b, from, to);
268                         }
269                 }
270
271                 if (response == NULL)
272                         return respond_JSON_Failure(connection);
273
274                 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
275                 MHD_destroy_response(response);
276                 return ret;
277         } else if (strncmp(url, "/put", 4) == 0) {
278                 struct put_state *ps = (struct put_state *) *ptr;
279                 char *username;
280                 char password[33];
281
282                 if (*ptr == NULL) {
283                         if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
284                                 return respond_405(connection);
285
286                         if (url[4] == '/')
287                                 return respond_404(connection);
288
289                         *ptr = (void *) 1;
290
291                         username = MHD_digest_auth_get_username(connection);
292                         if (username == NULL)
293                                 return respond_401(connection, MHD_NO);
294                         auth_get_password(username, password);
295
296                         ret = MHD_digest_auth_check(connection, REALM, username, password, 300);
297                         free(username);
298
299                         if (ret == MHD_INVALID_NONCE || ret == MHD_NO)
300                                 return respond_401(connection, (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);
301
302                         struct put_state *ps = malloc(sizeof(struct put_state));
303                         ps->data = NULL;
304                         ps->data_size = 0;
305                         ps->pp = MHD_create_post_processor(connection, 16384, &POST_put_iterator, ps);
306                         *ptr = ps;
307                         return MHD_YES;
308                 }
309
310                 if (*upload_data_size) {
311                         MHD_post_process(ps->pp, upload_data, *upload_data_size);
312                         *upload_data_size = 0;
313                         return MHD_YES;
314                 }
315
316                 if (ps->data == NULL || ps->data_size == 0)
317                         return respond_JSON_Failure(connection);
318
319                 username = MHD_digest_auth_get_username(connection);
320                 struct blerg *b = blerg_open(username);
321                 if (b == NULL)
322                         return respond_JSON_Failure(connection);
323                 if (blerg_store(b, ps->data, ps->data_size) == -1) {
324                         blerg_close(b);
325                         return respond_JSON_Failure(connection);
326                 }
327                 blerg_close(b);
328
329                 MHD_destroy_post_processor(ps->pp);
330                 free(username);
331                 free(ps->data);
332                 free(ps);
333                 *ptr = NULL;
334
335                 return respond_JSON_Success(connection);
336         } else if (strncmp(url, "/info", 5) == 0) {
337                 if (*ptr == NULL) {
338                         *ptr = (void *) 1;
339
340                         if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
341                                 return respond_405(connection);
342                         return MHD_YES;
343                 }
344
345
346                 if (url[5] != '/')
347                         return respond_404(connection);
348
349                 ret = parse_url_info(url + 6, &info);
350                 if ((ret & URL_INFO_AUTHOR) == 0)
351                         return respond_404(connection);
352
353                 if (!blerg_exists(info.author))
354                         return respond_404(connection);
355
356                 *ptr == NULL;
357
358                 struct blerg *b = blerg_open(info.author);
359                 uint64_t record_count = blerg_get_record_count(b);
360                 blerg_close(b);
361
362                 char number[21];
363                 yajl_gen g = yajl_gen_alloc(&yajl_c, NULL);
364                 yajl_gen_map_open(g);
365                 yajl_gen_string(g, "record_count", 12);
366                 snprintf(number, 21, "%llu", record_count);
367                 yajl_gen_string(g, number, strlen(number));
368                 yajl_gen_map_close(g);
369
370                 const unsigned char *ybuf;
371                 yajl_gen_get_buf(g, &ybuf, &len);
372
373                 response = MHD_create_response_from_data(len, (void *)ybuf, MHD_NO, MHD_YES);
374                 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
375                 MHD_destroy_response(response);
376
377                 yajl_gen_free(g);
378
379                 return ret;
380         } else if (strncmp(url, "/create", 8) == 0) {
381                 struct create_state *cs = (struct create_state *) *ptr;
382
383                 if (cs == NULL) {
384                         if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
385                                 return respond_405(connection);
386
387                         struct create_state *cs = malloc(sizeof(struct create_state));
388                         cs->username[0] = cs->password[0] = 0;
389                         cs->pp = MHD_create_post_processor(connection, 1024, &POST_create_iterator, cs);
390                         *ptr = cs;
391                         return MHD_YES;
392                 }
393
394                 if (*upload_data_size) {
395                         MHD_post_process(cs->pp, upload_data, *upload_data_size);
396                         *upload_data_size = 0;
397                         return MHD_YES;
398                 }
399
400                 if (cs->username[0] == 0 || cs->password[0] == 0)
401                         return respond_JSON_Failure(connection);
402
403                 if (blerg_exists(cs->username))
404                         return respond_JSON_Failure(connection);
405
406                 struct blerg *b = blerg_open(cs->username);
407                 blerg_close(b);
408                 auth_set_password(cs->username, cs->password);
409
410                 MHD_destroy_post_processor(cs->pp);
411                 free(cs);
412                 *ptr = NULL;
413
414                 return respond_JSON_Success(connection);
415         } else {
416                 return respond_404(connection);
417         }
418 }
419
420
421 int main(int argc, char *argv[]) {
422         struct MHD_Daemon *daemon;
423         fd_set rs, ws, es;
424         int max;
425
426         init_responses();
427
428         daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
429         if (daemon == NULL) {
430                 fprintf(stderr, "Could not start web server\n");
431                 return 1;
432         }
433
434         while (1) {
435                 FD_ZERO(&rs); FD_ZERO(&ws); FD_ZERO(&es);
436                 if (MHD_get_fdset(daemon, &rs, &ws, &es, &max) != MHD_YES) {
437                         fprintf(stderr, "Fatal error getting fd sets\n");
438                         break;
439                 }
440                 select(max + 1, &rs, &ws, &es, NULL);
441                 MHD_run(daemon);
442         }
443         MHD_stop_daemon(daemon);
444         return 0;
445 }