4 #include <microhttpd.h>
5 #include <yajl/yajl_gen.h>
11 #define URL_INFO_AUTHOR 0x1
12 #define URL_INFO_RECORD 0x2
13 #define DERP "DERP DERP DERP"
16 struct MHD_PostProcessor *pp;
22 struct MHD_PostProcessor *pp;
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;
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);
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);
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);
52 #define JSON_SUCCESS "{status: \"success\"}"
53 response_JSON_Success = MHD_create_response_from_data(strlen(JSON_SUCCESS), JSON_SUCCESS, MHD_NO, MHD_NO);
55 #define JSON_FAILURE "{status: \"failure\"}"
56 response_JSON_Failure = MHD_create_response_from_data(strlen(JSON_FAILURE), JSON_FAILURE, MHD_NO, MHD_NO);
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);
65 int respond_404(struct MHD_Connection *connection) {
66 return MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response_404);
69 int respond_405(struct MHD_Connection *connection) {
70 return MHD_queue_response(connection, MHD_HTTP_METHOD_NOT_ALLOWED, response_405);
73 int respond_JSON_Success(struct MHD_Connection *connection) {
74 return MHD_queue_response(connection, MHD_HTTP_OK, response_JSON_Success);
77 int respond_JSON_Failure(struct MHD_Connection *connection) {
78 return MHD_queue_response(connection, MHD_HTTP_OK, response_JSON_Failure);
81 int parse_url_info(const char *url, char *author, uint64_t *record) {
94 memcpy(author, url, len);
96 ret |= URL_INFO_AUTHOR;
98 if (c != NULL && c[1] != 0) {
99 *record = strtoull(c + 1, NULL, 10);
100 ret |= URL_INFO_RECORD;
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;
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);
120 memcpy(buf, ybuf + gs->yoff, bytes_remaining);
122 yajl_gen_clear(gs->g);
123 return bytes_remaining;
130 if (pos == 0) { /* Start iterating */
132 yajl_gen_array_open(gs->g);
133 uint64_t record_count = blerg_get_record_count(gs->b);
134 if (record_count == 0) {
138 gs->to = record_count - 1;
139 gs->from = (record_count > 50 ? gs->to - 49 : 0);
143 if (gs->from > gs->to) { /* Done iterating */
144 yajl_gen_array_close(gs->g);
147 /* Snarf one record */
148 blerg_fetch(gs->b, gs->from, &data, &len);
149 yajl_gen_string(gs->g, data, len);
155 yajl_gen_get_buf(gs->g, &ybuf, &len);
157 memcpy(buf, ybuf, max);
161 memcpy(buf, ybuf, len);
162 yajl_gen_clear(gs->g);
167 void GET_generate_list_free(void *cls) {
168 struct get_state *gs = cls;
171 yajl_gen_free(gs->g);
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;
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;
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;
194 if (strncmp(key, "data", 5) == 0) {
195 if (ps->data == NULL) {
196 ps->data_size = size;
197 ps->data = malloc(size);
199 if (ps->data_size + size > MAX_RECORD_SIZE) {
200 size = MAX_RECORD_SIZE - ps->data_size;
202 ps->data_size += size;
203 ps->data = realloc(ps->data, ps->data_size);
205 memcpy(ps->data + off, data, size);
206 if (ps->data_size == MAX_RECORD_SIZE)
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;
222 if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) {
224 if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
225 return respond_405(connection);
232 return respond_404(connection);
234 ret = parse_url_info(url + 5, author, &record);
235 if ((ret & URL_INFO_AUTHOR) == 0)
236 return respond_404(connection);
238 if (!blerg_exists(author))
239 return respond_404(connection);
243 if (ret & URL_INFO_RECORD) {
244 struct blerg *b = blerg_open(author);
245 ret = blerg_fetch(b, record, &data, &len);
249 return respond_404(connection);
251 response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
252 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
254 MHD_destroy_response(response);
260 struct get_state *gs = malloc(sizeof(struct get_state));
261 gs->b = blerg_open(author);
262 gs->g = yajl_gen_alloc(&c, NULL);
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);
272 } else if (strncmp(url, "/put", 4) == 0) {
273 struct put_state *ps = (struct put_state *) *ptr;
278 if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
279 return respond_405(connection);
282 return respond_404(connection);
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);
291 ret = MHD_digest_auth_check(connection, REALM, username, password, 300);
294 if (ret == MHD_INVALID_NONCE || ret == MHD_NO)
295 return respond_401(connection, (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);
297 struct put_state *ps = malloc(sizeof(struct put_state));
300 ps->pp = MHD_create_post_processor(connection, 16384, &POST_put_iterator, ps);
305 if (*upload_data_size) {
306 MHD_post_process(ps->pp, upload_data, *upload_data_size);
307 *upload_data_size = 0;
311 if (ps->data == NULL || ps->data_size == 0)
312 return respond_JSON_Failure(connection);
314 username = MHD_digest_auth_get_username(connection);
315 struct blerg *b = blerg_open(username);
317 return respond_JSON_Failure(connection);
318 if (blerg_store(b, ps->data, ps->data_size) == -1) {
320 return respond_JSON_Failure(connection);
324 MHD_destroy_post_processor(ps->pp);
330 return respond_JSON_Success(connection);
331 } else if (strncmp(url, "/create", 8) == 0) {
332 struct create_state *cs = (struct create_state *) *ptr;
335 if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
336 return respond_405(connection);
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);
345 if (*upload_data_size) {
346 MHD_post_process(cs->pp, upload_data, *upload_data_size);
347 *upload_data_size = 0;
351 if (cs->username[0] == 0 || cs->password[0] == 0)
352 return respond_JSON_Failure(connection);
354 if (blerg_exists(cs->username))
355 return respond_JSON_Failure(connection);
357 struct blerg *b = blerg_open(cs->username);
359 auth_set_password(cs->username, cs->password);
361 MHD_destroy_post_processor(cs->pp);
365 return respond_JSON_Success(connection);
367 return respond_404(connection);
372 int main(int argc, char *argv[]) {
373 struct MHD_Daemon *daemon;
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");
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");
391 select(max + 1, &rs, &ws, &es, NULL);
394 MHD_stop_daemon(daemon);