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