Some refactoring, remove build bits for libmicrohttpd
[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
10 #define URL_INFO_AUTHOR 0x1
11 #define URL_INFO_RECORD 0x2
12 #define DERP "DERP DERP DERP"
13
14 struct create_state {
15         struct MHD_PostProcessor *pp;
16         char username[33];
17         char password[33];
18 };
19
20 struct put_state {
21         struct MHD_PostProcessor *pp;
22         char *data;
23         int data_size;
24 };
25
26 struct MHD_Response *response_401;
27 struct MHD_Response *response_404;
28 struct MHD_Response *response_405;
29 struct MHD_Response *response_JSON_Success;
30 struct MHD_Response *response_JSON_Failure;
31
32 void init_responses() {
33 #define CONTENT_401 "<html><head><title>401 Unauthorized</title></head><body><h1>401 Unauthorized</h1>DENIED</body></html>"
34         response_401 = MHD_create_response_from_data(strlen (CONTENT_401), CONTENT_401, MHD_NO, MHD_NO);  
35
36 #define CONTENT_404 "<html><head><title>404 Not Found</title></head><body><h1>404 Not Found</h1>I couldn't find that.</body></html>"
37         response_404 = MHD_create_response_from_data(strlen(CONTENT_404), CONTENT_404, MHD_NO, MHD_NO);
38
39 #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>"
40         response_405 = MHD_create_response_from_data(strlen(CONTENT_405), CONTENT_405, MHD_NO, MHD_NO);
41
42 #define JSON_SUCCESS "{status: \"success\"}"
43         response_JSON_Success = MHD_create_response_from_data(strlen(JSON_SUCCESS), JSON_SUCCESS, MHD_NO, MHD_NO);
44
45 #define JSON_FAILURE "{status: \"failure\"}"
46         response_JSON_Failure = MHD_create_response_from_data(strlen(JSON_FAILURE), JSON_FAILURE, MHD_NO, MHD_NO);
47 }
48
49 #define REALM "Blerg"
50 #define OPAQUE "d29fb6db8f21a6e99903651a9f87470e"
51 int respond_401(struct MHD_Connection *connection, int signal_stale) {
52         return MHD_queue_auth_fail_response(connection, REALM, OPAQUE, response_401, signal_stale);    
53 }
54
55 int respond_404(struct MHD_Connection *connection) {
56         return MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response_404);
57 }
58
59 int respond_405(struct MHD_Connection *connection) {
60         return MHD_queue_response(connection, MHD_HTTP_METHOD_NOT_ALLOWED, response_405);
61 }
62
63 int respond_JSON_Success(struct MHD_Connection *connection) {
64         return MHD_queue_response(connection, MHD_HTTP_OK, response_JSON_Success);
65 }
66
67 int respond_JSON_Failure(struct MHD_Connection *connection) {
68         return MHD_queue_response(connection, MHD_HTTP_OK, response_JSON_Failure);
69 }
70
71 int parse_url_info(const char *url, char *author, uint64_t *record) {
72         const char *c;
73         int ret = 0;
74         int len;
75
76         c = strchr(url, '/');
77         if (c == NULL) {
78                 len = strlen(url);
79         } else {
80                 len = c - url;
81         }
82         if (len == 0)
83                 return 0;
84         memcpy(author, url, len);
85         author[len] = 0;
86         ret |= URL_INFO_AUTHOR;
87
88         if (c != NULL && c[1] != 0) {
89                 *record = strtoull(c + 1, NULL, 10);
90                 ret |= URL_INFO_RECORD;
91         }
92
93         return ret;
94 }
95
96 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) {
97         struct create_state *cs = cls;
98
99         if (strncmp(key, "username", 9) == 0) {
100                 if (size > 32) size = 32;
101                 memcpy(cs->username, data, size);
102                 cs->username[size] = 0;
103         } else if (strncmp(key, "password", 9) == 0) {
104                 if (size > 32) size = 32;
105                 memcpy(cs->password, data, size);
106                 cs->password[size] = 0;
107         }
108
109         return MHD_YES;
110 }
111
112 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) {
113         struct put_state *ps = cls;
114
115         if (strncmp(key, "data", 5) == 0) {
116                 ps->data_size = size;
117                 ps->data = malloc(size);
118                 memcpy(ps->data, data, size);
119                 return MHD_NO;
120         }
121
122         return MHD_YES;
123 }
124
125 static int
126 ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const char *method,
127           const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) {
128         struct MHD_Response *response;
129         int ret, len;
130         char author[33];
131         uint64_t record;
132         char *data;
133
134         if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) {
135                 if (*ptr == NULL) {
136                         if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
137                                 return respond_405(connection);
138
139                         *ptr = (void *) 1;
140                         return MHD_YES;
141                 }
142
143                 if (url[4] != '/')
144                         return respond_404(connection);
145
146                 ret = parse_url_info(url + 5, author, &record);
147                 if ((ret & URL_INFO_AUTHOR) == 0)
148                         return respond_404(connection);
149
150                 if (!blerg_exists(author))
151                         return respond_404(connection);
152
153                 *ptr == NULL;
154
155                 if (ret & URL_INFO_RECORD) {
156                         struct blerg *b = blerg_open(author);
157                         ret = blerg_fetch(b, record, &data, &len);
158                         blerg_close(b);
159
160                         if (ret == 0) {
161                                 return respond_404(connection);
162                         } else {
163                                 response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
164                                 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
165                         }
166                         MHD_destroy_response(response);
167                         return ret;
168                 } else {
169                         response = MHD_create_response_from_data(strlen(DERP), DERP, MHD_NO, MHD_NO);
170                         ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
171                         MHD_destroy_response(response);
172                         return ret;
173                 }
174         } else if (strncmp(url, "/put", 4) == 0) {
175                 struct put_state *ps = (struct put_state *) *ptr;
176                 char *username;
177                 char password[33];
178
179                 if (*ptr == NULL) {
180                         if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
181                                 return respond_405(connection);
182
183                         if (url[4] == '/')
184                                 return respond_404(connection);
185
186                         *ptr = (void *) 1;
187
188                         username = MHD_digest_auth_get_username(connection);
189                         if (username == NULL)
190                                 return respond_401(connection, MHD_NO);
191                         auth_get_password(username, password);
192
193                         ret = MHD_digest_auth_check(connection, REALM, username, password, 300);
194                         free(username);
195
196                         if (ret == MHD_INVALID_NONCE || ret == MHD_NO)
197                                 return respond_401(connection, (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);
198
199                         struct put_state *ps = malloc(sizeof(struct put_state));
200                         ps->data = NULL;
201                         ps->pp = MHD_create_post_processor(connection, 4096, &POST_put_iterator, ps);
202                         *ptr = ps;
203                         return MHD_YES;
204                 }
205
206                 if (*upload_data_size) {
207                         MHD_post_process(ps->pp, upload_data, *upload_data_size);
208                         *upload_data_size = 0;
209                         return MHD_YES;
210                 }
211
212                 if (ps->data == NULL || ps->data_size == 0)
213                         return respond_JSON_Failure(connection);
214
215                 username = MHD_digest_auth_get_username(connection);
216                 struct blerg *b = blerg_open(username);
217                 if (b == NULL)
218                         return respond_JSON_Failure(connection);
219                 if (blerg_store(b, ps->data, ps->data_size) == -1) {
220                         blerg_close(b);
221                         return respond_JSON_Failure(connection);
222                 }
223                 blerg_close(b);
224
225                 MHD_destroy_post_processor(ps->pp);
226                 free(username);
227                 free(ps->data);
228                 free(ps);
229                 *ptr = NULL;
230
231                 return respond_JSON_Success(connection);
232         } else if (strncmp(url, "/create", 8) == 0) {
233                 struct create_state *cs = (struct create_state *) *ptr;
234
235                 if (cs == NULL) {
236                         if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
237                                 return respond_405(connection);
238
239                         struct create_state *cs = malloc(sizeof(struct create_state));
240                         cs->username[0] = cs->password[0] = 0;
241                         cs->pp = MHD_create_post_processor(connection, 1024, &POST_create_iterator, cs);
242                         *ptr = cs;
243                         return MHD_YES;
244                 }
245
246                 if (*upload_data_size) {
247                         MHD_post_process(cs->pp, upload_data, *upload_data_size);
248                         *upload_data_size = 0;
249                         return MHD_YES;
250                 }
251
252                 if (cs->username[0] == 0 || cs->password[0] == 0)
253                         return respond_JSON_Failure(connection);
254
255                 if (blerg_exists(cs->username))
256                         return respond_JSON_Failure(connection);
257
258                 struct blerg *b = blerg_open(cs->username);
259                 blerg_close(b);
260                 auth_set_password(cs->username, cs->password);
261
262                 MHD_destroy_post_processor(cs->pp);
263                 free(cs);
264                 *ptr = NULL;
265
266                 return respond_JSON_Success(connection);
267         } else {
268                 return respond_404(connection);
269         }
270 }
271
272
273 int main(int argc, char *argv[]) {
274         struct MHD_Daemon *daemon;
275         fd_set rs, ws, es;
276         int max;
277
278         init_responses();
279
280         daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
281         if (daemon == NULL) {
282                 fprintf(stderr, "Could not start web server\n");
283                 return 1;
284         }
285
286         while (1) {
287                 FD_ZERO(&rs); FD_ZERO(&ws); FD_ZERO(&es);
288                 if (MHD_get_fdset(daemon, &rs, &ws, &es, &max) != MHD_YES) {
289                         fprintf(stderr, "Fatal error getting fd sets\n");
290                         break;
291                 }
292                 select(max + 1, &rs, &ws, &es, NULL);
293                 MHD_run(daemon);
294         }
295         MHD_stop_daemon(daemon);
296         return 0;
297 }