Fix empty response for empty accounts
[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         gs->entries = make_sequential_list(from, to);
207         gs->i = to - from;
208
209         gs->g = yajl_gen_alloc(&yajl_c, NULL);
210         gs->yoff = gs->done = 0;
211
212         response = MHD_create_response_from_callback(-1, 262144, &GET_generate_list, gs, &GET_generate_list_free);
213
214         return response;
215 }
216
217 static int
218 ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const char *method,
219           const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) {
220         struct MHD_Response *response;
221         int ret, len;
222         struct url_info info;
223         char *data;
224
225         if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) {
226                 if (*ptr == NULL) {
227                         if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
228                                 return respond_405(connection);
229
230                         *ptr = (void *) 1;
231                         return MHD_YES;
232                 }
233
234                 if (url[4] != '/')
235                         return respond_404(connection);
236
237                 ret = parse_url_info(url + 5, &info);
238                 if ((ret & URL_INFO_AUTHOR) == 0)
239                         return respond_404(connection);
240
241                 if (!blerg_exists(info.author))
242                         return respond_404(connection);
243
244                 *ptr == NULL;
245
246                 struct blerg *b = blerg_open(info.author);
247
248                 if ((ret & URL_INFO_RECORD) && (ret & URL_INFO_RECORD_TO)) {
249                         response = create_response_for_range(b, info.record, info.record_to);
250                 } else if (ret & URL_INFO_RECORD) {
251                         ret = blerg_fetch(b, info.record, &data, &len);
252                         blerg_close(b);
253
254                         if (ret == 0)
255                                 return respond_404(connection);
256                         response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
257                 } else {
258                         uint64_t record_count, from, to;
259                         record_count = blerg_get_record_count(b);
260                         if (record_count == 0) {
261                                 response = MHD_create_response_from_data(2, "[]", MHD_NO, MHD_NO);
262                         } else {
263                                 to = record_count - 1;
264                                 from = (record_count > 50 ? to - 49 : 0);
265                                 response = create_response_for_range(b, from, to);
266                         }
267                 }
268
269                 if (response == NULL)
270                         return respond_JSON_Failure(connection);
271
272                 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
273                 MHD_destroy_response(response);
274                 return ret;
275         } else if (strncmp(url, "/put", 4) == 0) {
276                 struct put_state *ps = (struct put_state *) *ptr;
277                 char *username;
278                 char password[33];
279
280                 if (*ptr == NULL) {
281                         if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
282                                 return respond_405(connection);
283
284                         if (url[4] == '/')
285                                 return respond_404(connection);
286
287                         *ptr = (void *) 1;
288
289                         username = MHD_digest_auth_get_username(connection);
290                         if (username == NULL)
291                                 return respond_401(connection, MHD_NO);
292                         auth_get_password(username, password);
293
294                         ret = MHD_digest_auth_check(connection, REALM, username, password, 300);
295                         free(username);
296
297                         if (ret == MHD_INVALID_NONCE || ret == MHD_NO)
298                                 return respond_401(connection, (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);
299
300                         struct put_state *ps = malloc(sizeof(struct put_state));
301                         ps->data = NULL;
302                         ps->data_size = 0;
303                         ps->pp = MHD_create_post_processor(connection, 16384, &POST_put_iterator, ps);
304                         *ptr = ps;
305                         return MHD_YES;
306                 }
307
308                 if (*upload_data_size) {
309                         MHD_post_process(ps->pp, upload_data, *upload_data_size);
310                         *upload_data_size = 0;
311                         return MHD_YES;
312                 }
313
314                 if (ps->data == NULL || ps->data_size == 0)
315                         return respond_JSON_Failure(connection);
316
317                 username = MHD_digest_auth_get_username(connection);
318                 struct blerg *b = blerg_open(username);
319                 if (b == NULL)
320                         return respond_JSON_Failure(connection);
321                 if (blerg_store(b, ps->data, ps->data_size) == -1) {
322                         blerg_close(b);
323                         return respond_JSON_Failure(connection);
324                 }
325                 blerg_close(b);
326
327                 MHD_destroy_post_processor(ps->pp);
328                 free(username);
329                 free(ps->data);
330                 free(ps);
331                 *ptr = NULL;
332
333                 return respond_JSON_Success(connection);
334         } else if (strncmp(url, "/info", 5) == 0) {
335                 if (*ptr == NULL) {
336                         *ptr = (void *) 1;
337
338                         if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
339                                 return respond_405(connection);
340                         return MHD_YES;
341                 }
342
343
344                 if (url[5] != '/')
345                         return respond_404(connection);
346
347                 ret = parse_url_info(url + 6, &info);
348                 if ((ret & URL_INFO_AUTHOR) == 0)
349                         return respond_404(connection);
350
351                 if (!blerg_exists(info.author))
352                         return respond_404(connection);
353
354                 *ptr == NULL;
355
356                 struct blerg *b = blerg_open(info.author);
357                 uint64_t record_count = blerg_get_record_count(b);
358                 blerg_close(b);
359
360                 char number[21];
361                 yajl_gen g = yajl_gen_alloc(&yajl_c, NULL);
362                 yajl_gen_map_open(g);
363                 yajl_gen_string(g, "record_count", 12);
364                 snprintf(number, 21, "%llu", record_count);
365                 yajl_gen_string(g, number, strlen(number));
366                 yajl_gen_map_close(g);
367
368                 const unsigned char *ybuf;
369                 yajl_gen_get_buf(g, &ybuf, &len);
370
371                 response = MHD_create_response_from_data(len, (void *)ybuf, MHD_NO, MHD_YES);
372                 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
373                 MHD_destroy_response(response);
374
375                 yajl_gen_free(g);
376
377                 return ret;
378         } else if (strncmp(url, "/create", 8) == 0) {
379                 struct create_state *cs = (struct create_state *) *ptr;
380
381                 if (cs == NULL) {
382                         if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
383                                 return respond_405(connection);
384
385                         struct create_state *cs = malloc(sizeof(struct create_state));
386                         cs->username[0] = cs->password[0] = 0;
387                         cs->pp = MHD_create_post_processor(connection, 1024, &POST_create_iterator, cs);
388                         *ptr = cs;
389                         return MHD_YES;
390                 }
391
392                 if (*upload_data_size) {
393                         MHD_post_process(cs->pp, upload_data, *upload_data_size);
394                         *upload_data_size = 0;
395                         return MHD_YES;
396                 }
397
398                 if (cs->username[0] == 0 || cs->password[0] == 0)
399                         return respond_JSON_Failure(connection);
400
401                 if (blerg_exists(cs->username))
402                         return respond_JSON_Failure(connection);
403
404                 struct blerg *b = blerg_open(cs->username);
405                 blerg_close(b);
406                 auth_set_password(cs->username, cs->password);
407
408                 MHD_destroy_post_processor(cs->pp);
409                 free(cs);
410                 *ptr = NULL;
411
412                 return respond_JSON_Success(connection);
413         } else {
414                 return respond_404(connection);
415         }
416 }
417
418
419 int main(int argc, char *argv[]) {
420         struct MHD_Daemon *daemon;
421         fd_set rs, ws, es;
422         int max;
423
424         init_responses();
425
426         daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
427         if (daemon == NULL) {
428                 fprintf(stderr, "Could not start web server\n");
429                 return 1;
430         }
431
432         while (1) {
433                 FD_ZERO(&rs); FD_ZERO(&ws); FD_ZERO(&es);
434                 if (MHD_get_fdset(daemon, &rs, &ws, &es, &max) != MHD_YES) {
435                         fprintf(stderr, "Fatal error getting fd sets\n");
436                         break;
437                 }
438                 select(max + 1, &rs, &ws, &es, NULL);
439                 MHD_run(daemon);
440         }
441         MHD_stop_daemon(daemon);
442         return 0;
443 }