4 #include <microhttpd.h>
5 #include <yajl/yajl_gen.h>
9 #include "canned_responses.h"
13 #define URL_INFO_AUTHOR 0x1
14 #define URL_INFO_RECORD 0x2
15 #define DERP "DERP DERP DERP"
17 yajl_gen_config yajl_c = { 0, 0 };
20 struct MHD_PostProcessor *pp;
26 struct MHD_PostProcessor *pp;
40 int parse_url_info(const char *url, char *author, uint64_t *record) {
53 memcpy(author, url, len);
55 ret |= URL_INFO_AUTHOR;
57 if (c != NULL && c[1] != 0) {
58 *record = strtoull(c + 1, NULL, 10);
59 ret |= URL_INFO_RECORD;
65 uint64_t *make_sequential_list(uint64_t from, uint64_t to) {
66 uint64_t len = to - from + 1;
67 uint64_t *list = malloc(len * sizeof(uint64_t));
70 for (i = 0; i < len; i++) {
77 ssize_t GET_generate_list(void *cls, uint64_t pos, char *buf, size_t max) {
78 struct get_state *gs = cls;
79 const unsigned char *ybuf;
85 yajl_gen_get_buf(gs->g, &ybuf, &len);
86 size_t bytes_remaining = len - gs->yoff;
87 if (bytes_remaining > max) {
88 memcpy(buf, ybuf + gs->yoff, max);
92 memcpy(buf, ybuf + gs->yoff, bytes_remaining);
94 yajl_gen_clear(gs->g);
95 return bytes_remaining;
102 if (pos == 0) { /* Start iterating */
103 yajl_gen_array_open(gs->g);
106 /* Snarf one record */
107 blerg_fetch(gs->b, gs->entries[gs->i], &data, &len);
109 yajl_gen_map_open(gs->g);
110 yajl_gen_string(gs->g, "record", 6);
111 snprintf(number, 21, "%llu", gs->entries[gs->i]);
112 yajl_gen_string(gs->g, number, strlen(number));
113 yajl_gen_string(gs->g, "data", 4);
114 yajl_gen_string(gs->g, data, len);
115 yajl_gen_map_close(gs->g);
119 yajl_gen_array_close(gs->g);
125 yajl_gen_get_buf(gs->g, &ybuf, &len);
127 memcpy(buf, ybuf, max);
131 memcpy(buf, ybuf, len);
132 yajl_gen_clear(gs->g);
137 void GET_generate_list_free(void *cls) {
138 struct get_state *gs = cls;
141 yajl_gen_free(gs->g);
146 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) {
147 struct create_state *cs = cls;
149 if (strncmp(key, "username", 9) == 0) {
150 if (size > 32) size = 32;
151 memcpy(cs->username, data, size);
152 cs->username[size] = 0;
153 } else if (strncmp(key, "password", 9) == 0) {
154 if (size > 32) size = 32;
155 memcpy(cs->password, data, size);
156 cs->password[size] = 0;
162 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) {
163 struct put_state *ps = cls;
165 if (strncmp(key, "data", 5) == 0) {
166 if (ps->data == NULL) {
167 ps->data_size = size;
168 ps->data = malloc(size);
170 if (ps->data_size + size > MAX_RECORD_SIZE) {
171 size = MAX_RECORD_SIZE - ps->data_size;
173 ps->data_size += size;
174 ps->data = realloc(ps->data, ps->data_size);
176 memcpy(ps->data + off, data, size);
177 if (ps->data_size == MAX_RECORD_SIZE)
185 ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const char *method,
186 const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) {
187 struct MHD_Response *response;
193 if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) {
195 if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
196 return respond_405(connection);
203 return respond_404(connection);
205 ret = parse_url_info(url + 5, author, &record);
206 if ((ret & URL_INFO_AUTHOR) == 0)
207 return respond_404(connection);
209 if (!blerg_exists(author))
210 return respond_404(connection);
214 if (ret & URL_INFO_RECORD) {
215 struct blerg *b = blerg_open(author);
216 ret = blerg_fetch(b, record, &data, &len);
220 return respond_404(connection);
222 response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
223 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
225 MHD_destroy_response(response);
228 struct get_state *gs = malloc(sizeof(struct get_state));
229 gs->b = blerg_open(author);
230 uint64_t record_count = blerg_get_record_count(gs->b);
231 if (record_count == 0) {
232 response = MHD_create_response_from_data(2, "[]", MHD_NO, MHD_NO);
234 uint64_t from, to, i, j;
235 to = record_count - 1;
236 from = (record_count > 50 ? to - 49 : 0);
237 gs->entries = make_sequential_list(from, to);
240 gs->g = yajl_gen_alloc(&yajl_c, NULL);
241 gs->yoff = gs->done = 0;
243 response = MHD_create_response_from_callback(-1, 262144, &GET_generate_list, gs, &GET_generate_list_free);
246 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
247 MHD_destroy_response(response);
251 } else if (strncmp(url, "/put", 4) == 0) {
252 struct put_state *ps = (struct put_state *) *ptr;
257 if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
258 return respond_405(connection);
261 return respond_404(connection);
265 username = MHD_digest_auth_get_username(connection);
266 if (username == NULL)
267 return respond_401(connection, MHD_NO);
268 auth_get_password(username, password);
270 ret = MHD_digest_auth_check(connection, REALM, username, password, 300);
273 if (ret == MHD_INVALID_NONCE || ret == MHD_NO)
274 return respond_401(connection, (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);
276 struct put_state *ps = malloc(sizeof(struct put_state));
279 ps->pp = MHD_create_post_processor(connection, 16384, &POST_put_iterator, ps);
284 if (*upload_data_size) {
285 MHD_post_process(ps->pp, upload_data, *upload_data_size);
286 *upload_data_size = 0;
290 if (ps->data == NULL || ps->data_size == 0)
291 return respond_JSON_Failure(connection);
293 username = MHD_digest_auth_get_username(connection);
294 struct blerg *b = blerg_open(username);
296 return respond_JSON_Failure(connection);
297 if (blerg_store(b, ps->data, ps->data_size) == -1) {
299 return respond_JSON_Failure(connection);
303 MHD_destroy_post_processor(ps->pp);
309 return respond_JSON_Success(connection);
310 } else if (strncmp(url, "/info", 5) == 0) {
314 if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
315 return respond_405(connection);
321 return respond_404(connection);
323 ret = parse_url_info(url + 6, author, &record);
324 if ((ret & URL_INFO_AUTHOR) == 0)
325 return respond_404(connection);
327 if (!blerg_exists(author))
328 return respond_404(connection);
332 struct blerg *b = blerg_open(author);
333 uint64_t record_count = blerg_get_record_count(b);
337 yajl_gen g = yajl_gen_alloc(&yajl_c, NULL);
338 yajl_gen_map_open(g);
339 yajl_gen_string(g, "record_count", 12);
340 snprintf(number, 21, "%llu", record_count);
341 yajl_gen_string(g, number, strlen(number));
342 yajl_gen_map_close(g);
344 const unsigned char *ybuf;
345 yajl_gen_get_buf(g, &ybuf, &len);
347 response = MHD_create_response_from_data(len, (void *)ybuf, MHD_NO, MHD_YES);
348 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
349 MHD_destroy_response(response);
354 } else if (strncmp(url, "/create", 8) == 0) {
355 struct create_state *cs = (struct create_state *) *ptr;
358 if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
359 return respond_405(connection);
361 struct create_state *cs = malloc(sizeof(struct create_state));
362 cs->username[0] = cs->password[0] = 0;
363 cs->pp = MHD_create_post_processor(connection, 1024, &POST_create_iterator, cs);
368 if (*upload_data_size) {
369 MHD_post_process(cs->pp, upload_data, *upload_data_size);
370 *upload_data_size = 0;
374 if (cs->username[0] == 0 || cs->password[0] == 0)
375 return respond_JSON_Failure(connection);
377 if (blerg_exists(cs->username))
378 return respond_JSON_Failure(connection);
380 struct blerg *b = blerg_open(cs->username);
382 auth_set_password(cs->username, cs->password);
384 MHD_destroy_post_processor(cs->pp);
388 return respond_JSON_Success(connection);
390 return respond_404(connection);
395 int main(int argc, char *argv[]) {
396 struct MHD_Daemon *daemon;
402 daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
403 if (daemon == NULL) {
404 fprintf(stderr, "Could not start web server\n");
409 FD_ZERO(&rs); FD_ZERO(&ws); FD_ZERO(&es);
410 if (MHD_get_fdset(daemon, &rs, &ws, &es, &max) != MHD_YES) {
411 fprintf(stderr, "Fatal error getting fd sets\n");
414 select(max + 1, &rs, &ws, &es, NULL);
417 MHD_stop_daemon(daemon);