0bf237b931a313720f7e40f69d06c6ec17570b16
[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 "config.h"
10
11 #define URL_INFO_AUTHOR 0x1
12 #define URL_INFO_RECORD 0x2
13 #define DERP "DERP DERP DERP"
14
15 struct create_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 from;
32         uint64_t to;
33         int done;
34 };
35
36 struct MHD_Response *response_401;
37 struct MHD_Response *response_404;
38 struct MHD_Response *response_405;
39 struct MHD_Response *response_JSON_Success;
40 struct MHD_Response *response_JSON_Failure;
41
42 void init_responses() {
43 #define CONTENT_401 "<html><head><title>401 Unauthorized</title></head><body><h1>401 Unauthorized</h1>DENIED</body></html>"
44         response_401 = MHD_create_response_from_data(strlen (CONTENT_401), CONTENT_401, MHD_NO, MHD_NO);  
45
46 #define CONTENT_404 "<html><head><title>404 Not Found</title></head><body><h1>404 Not Found</h1>I couldn't find that.</body></html>"
47         response_404 = MHD_create_response_from_data(strlen(CONTENT_404), CONTENT_404, MHD_NO, MHD_NO);
48
49 #define CONTENT_405 "<html><head><title>405 Method Not Allowed</title></head><body><h1>405 Method Not Allowed</h1>I'm sorry, Dave. I'm afraid I can't do that.</body></html>"
50         response_405 = MHD_create_response_from_data(strlen(CONTENT_405), CONTENT_405, MHD_NO, MHD_NO);
51
52 #define JSON_SUCCESS "{status: \"success\"}"
53         response_JSON_Success = MHD_create_response_from_data(strlen(JSON_SUCCESS), JSON_SUCCESS, MHD_NO, MHD_NO);
54
55 #define JSON_FAILURE "{status: \"failure\"}"
56         response_JSON_Failure = MHD_create_response_from_data(strlen(JSON_FAILURE), JSON_FAILURE, MHD_NO, MHD_NO);
57 }
58
59 #define REALM "Blerg"
60 #define OPAQUE "d29fb6db8f21a6e99903651a9f87470e"
61 int respond_401(struct MHD_Connection *connection, int signal_stale) {
62         return MHD_queue_auth_fail_response(connection, REALM, OPAQUE, response_401, signal_stale);    
63 }
64
65 int respond_404(struct MHD_Connection *connection) {
66         return MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response_404);
67 }
68
69 int respond_405(struct MHD_Connection *connection) {
70         return MHD_queue_response(connection, MHD_HTTP_METHOD_NOT_ALLOWED, response_405);
71 }
72
73 int respond_JSON_Success(struct MHD_Connection *connection) {
74         return MHD_queue_response(connection, MHD_HTTP_OK, response_JSON_Success);
75 }
76
77 int respond_JSON_Failure(struct MHD_Connection *connection) {
78         return MHD_queue_response(connection, MHD_HTTP_OK, response_JSON_Failure);
79 }
80
81 int parse_url_info(const char *url, char *author, uint64_t *record) {
82         const char *c;
83         int ret = 0;
84         int len;
85
86         c = strchr(url, '/');
87         if (c == NULL) {
88                 len = strlen(url);
89         } else {
90                 len = c - url;
91         }
92         if (len == 0)
93                 return 0;
94         memcpy(author, url, len);
95         author[len] = 0;
96         ret |= URL_INFO_AUTHOR;
97
98         if (c != NULL && c[1] != 0) {
99                 *record = strtoull(c + 1, NULL, 10);
100                 ret |= URL_INFO_RECORD;
101         }
102
103         return ret;
104 }
105
106 ssize_t GET_generate_list(void *cls, uint64_t pos, char *buf, size_t max) {
107         struct get_state *gs = cls;
108         const unsigned char *ybuf;
109         char *data;
110         unsigned int len;
111
112         if (gs->yoff > 0) {
113                 yajl_gen_get_buf(gs->g, &ybuf, &len);
114                 size_t bytes_remaining = len - gs->yoff;
115                 if (bytes_remaining > max) {
116                         memcpy(buf, ybuf + gs->yoff, max);
117                         gs->yoff += max;
118                         return max;
119                 } else {
120                         memcpy(buf, ybuf + gs->yoff, bytes_remaining);
121                         gs->yoff = 0;
122                         yajl_gen_clear(gs->g);
123                         return bytes_remaining;
124                 }
125         }
126
127         if (gs->done)
128                 return -1;
129
130         if (pos == 0) { /* Start iterating */
131                 gs->yoff = 0;
132                 yajl_gen_array_open(gs->g);
133                 uint64_t record_count = blerg_get_record_count(gs->b);
134                 if (record_count == 0) {
135                         gs->from = 1;
136                         gs->to = 0;
137                 } else {
138                         gs->to = record_count - 1;
139                         gs->from = (record_count > 50 ? gs->to - 49 : 0);
140                 }
141         }
142
143         if (gs->from > gs->to) { /* Done iterating */
144                 yajl_gen_array_close(gs->g);
145                 gs->done = 1;
146         } else {
147                 /* Snarf one record */
148                 blerg_fetch(gs->b, gs->from, &data, &len);
149                 yajl_gen_string(gs->g, data, len);
150                 free(data);
151                 gs->from++;
152         }
153
154
155         yajl_gen_get_buf(gs->g, &ybuf, &len);
156         if (len > max) {
157                 memcpy(buf, ybuf, max);
158                 gs->yoff = max;
159                 return max;
160         } else {
161                 memcpy(buf, ybuf, len);
162                 yajl_gen_clear(gs->g);
163                 return len;
164         }
165 }
166
167 void GET_generate_list_free(void *cls) {
168         struct get_state *gs = cls;
169
170         blerg_close(gs->b);
171         yajl_gen_free(gs->g);
172         free(gs);
173 }
174
175 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) {
176         struct create_state *cs = cls;
177
178         if (strncmp(key, "username", 9) == 0) {
179                 if (size > 32) size = 32;
180                 memcpy(cs->username, data, size);
181                 cs->username[size] = 0;
182         } else if (strncmp(key, "password", 9) == 0) {
183                 if (size > 32) size = 32;
184                 memcpy(cs->password, data, size);
185                 cs->password[size] = 0;
186         }
187
188         return MHD_YES;
189 }
190
191 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) {
192         struct put_state *ps = cls;
193
194         if (strncmp(key, "data", 5) == 0) {
195                 if (ps->data == NULL) {
196                         ps->data_size = size;
197                         ps->data = malloc(size);
198                 } else {
199                         if (ps->data_size + size > MAX_RECORD_SIZE) {
200                                 size = MAX_RECORD_SIZE - ps->data_size;
201                         }
202                         ps->data_size += size;
203                         ps->data = realloc(ps->data, ps->data_size);
204                 }
205                 memcpy(ps->data + off, data, size);
206                 if (ps->data_size == MAX_RECORD_SIZE)
207                         return MHD_NO;
208         }
209
210         return MHD_YES;
211 }
212
213 static int
214 ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const char *method,
215           const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) {
216         struct MHD_Response *response;
217         int ret, len;
218         char author[33];
219         uint64_t record;
220         char *data;
221
222         if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) {
223                 if (*ptr == NULL) {
224                         if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
225                                 return respond_405(connection);
226
227                         *ptr = (void *) 1;
228                         return MHD_YES;
229                 }
230
231                 if (url[4] != '/')
232                         return respond_404(connection);
233
234                 ret = parse_url_info(url + 5, author, &record);
235                 if ((ret & URL_INFO_AUTHOR) == 0)
236                         return respond_404(connection);
237
238                 if (!blerg_exists(author))
239                         return respond_404(connection);
240
241                 *ptr == NULL;
242
243                 if (ret & URL_INFO_RECORD) {
244                         struct blerg *b = blerg_open(author);
245                         ret = blerg_fetch(b, record, &data, &len);
246                         blerg_close(b);
247
248                         if (ret == 0) {
249                                 return respond_404(connection);
250                         } else {
251                                 response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
252                                 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
253                         }
254                         MHD_destroy_response(response);
255                         return ret;
256                 } else {
257                         yajl_gen_config c;
258                         c.beautify = 0;
259
260                         struct get_state *gs = malloc(sizeof(struct get_state));
261                         gs->b = blerg_open(author);
262                         gs->g = yajl_gen_alloc(&c, NULL);
263                         gs->yoff = 0;
264                         gs->done = 0;
265
266                         response = MHD_create_response_from_callback(-1, 262144, &GET_generate_list, gs, &GET_generate_list_free);
267                         ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
268                         MHD_destroy_response(response);
269
270                         return ret;
271                 }
272         } else if (strncmp(url, "/put", 4) == 0) {
273                 struct put_state *ps = (struct put_state *) *ptr;
274                 char *username;
275                 char password[33];
276
277                 if (*ptr == NULL) {
278                         if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
279                                 return respond_405(connection);
280
281                         if (url[4] == '/')
282                                 return respond_404(connection);
283
284                         *ptr = (void *) 1;
285
286                         username = MHD_digest_auth_get_username(connection);
287                         if (username == NULL)
288                                 return respond_401(connection, MHD_NO);
289                         auth_get_password(username, password);
290
291                         ret = MHD_digest_auth_check(connection, REALM, username, password, 300);
292                         free(username);
293
294                         if (ret == MHD_INVALID_NONCE || ret == MHD_NO)
295                                 return respond_401(connection, (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);
296
297                         struct put_state *ps = malloc(sizeof(struct put_state));
298                         ps->data = NULL;
299                         ps->data_size = 0;
300                         ps->pp = MHD_create_post_processor(connection, 16384, &POST_put_iterator, ps);
301                         *ptr = ps;
302                         return MHD_YES;
303                 }
304
305                 if (*upload_data_size) {
306                         MHD_post_process(ps->pp, upload_data, *upload_data_size);
307                         *upload_data_size = 0;
308                         return MHD_YES;
309                 }
310
311                 if (ps->data == NULL || ps->data_size == 0)
312                         return respond_JSON_Failure(connection);
313
314                 username = MHD_digest_auth_get_username(connection);
315                 struct blerg *b = blerg_open(username);
316                 if (b == NULL)
317                         return respond_JSON_Failure(connection);
318                 if (blerg_store(b, ps->data, ps->data_size) == -1) {
319                         blerg_close(b);
320                         return respond_JSON_Failure(connection);
321                 }
322                 blerg_close(b);
323
324                 MHD_destroy_post_processor(ps->pp);
325                 free(username);
326                 free(ps->data);
327                 free(ps);
328                 *ptr = NULL;
329
330                 return respond_JSON_Success(connection);
331         } else if (strncmp(url, "/create", 8) == 0) {
332                 struct create_state *cs = (struct create_state *) *ptr;
333
334                 if (cs == NULL) {
335                         if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
336                                 return respond_405(connection);
337
338                         struct create_state *cs = malloc(sizeof(struct create_state));
339                         cs->username[0] = cs->password[0] = 0;
340                         cs->pp = MHD_create_post_processor(connection, 1024, &POST_create_iterator, cs);
341                         *ptr = cs;
342                         return MHD_YES;
343                 }
344
345                 if (*upload_data_size) {
346                         MHD_post_process(cs->pp, upload_data, *upload_data_size);
347                         *upload_data_size = 0;
348                         return MHD_YES;
349                 }
350
351                 if (cs->username[0] == 0 || cs->password[0] == 0)
352                         return respond_JSON_Failure(connection);
353
354                 if (blerg_exists(cs->username))
355                         return respond_JSON_Failure(connection);
356
357                 struct blerg *b = blerg_open(cs->username);
358                 blerg_close(b);
359                 auth_set_password(cs->username, cs->password);
360
361                 MHD_destroy_post_processor(cs->pp);
362                 free(cs);
363                 *ptr = NULL;
364
365                 return respond_JSON_Success(connection);
366         } else {
367                 return respond_404(connection);
368         }
369 }
370
371
372 int main(int argc, char *argv[]) {
373         struct MHD_Daemon *daemon;
374         fd_set rs, ws, es;
375         int max;
376
377         init_responses();
378
379         daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
380         if (daemon == NULL) {
381                 fprintf(stderr, "Could not start web server\n");
382                 return 1;
383         }
384
385         while (1) {
386                 FD_ZERO(&rs); FD_ZERO(&ws); FD_ZERO(&es);
387                 if (MHD_get_fdset(daemon, &rs, &ws, &es, &max) != MHD_YES) {
388                         fprintf(stderr, "Fatal error getting fd sets\n");
389                         break;
390                 }
391                 select(max + 1, &rs, &ws, &es, NULL);
392                 MHD_run(daemon);
393         }
394         MHD_stop_daemon(daemon);
395         return 0;
396 }