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 URL_INFO_RECORD_TO 0x4
16 #define DERP "DERP DERP DERP"
18 yajl_gen_config yajl_c = { 0, 0 };
21 struct MHD_PostProcessor *pp;
27 struct MHD_PostProcessor *pp;
41 int parse_url_info(const char *url, struct url_info *info) {
54 memcpy(info->author, url, len);
55 info->author[len] = 0;
56 info->contents |= URL_INFO_AUTHOR;
58 if (c == NULL || c[1] == 0)
59 return info->contents;
61 info->record = strtoull(c + 1, NULL, 10);
62 info->contents |= URL_INFO_RECORD;
65 if (c == NULL || c[1] == 0)
66 return info->contents;
68 info->record_to = strtoull(c + 1, NULL, 10);
69 info->contents |= URL_INFO_RECORD_TO;
71 return info->contents;
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));
79 for (i = 0; i < len; i++) {
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;
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);
101 memcpy(buf, ybuf + gs->yoff, bytes_remaining);
103 yajl_gen_clear(gs->g);
104 return bytes_remaining;
111 if (pos == 0) { /* Start iterating */
112 yajl_gen_array_open(gs->g);
115 /* Snarf one record */
116 blerg_fetch(gs->b, gs->entries[gs->i], &data, &len);
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);
128 yajl_gen_array_close(gs->g);
134 yajl_gen_get_buf(gs->g, &ybuf, &len);
136 memcpy(buf, ybuf, max);
140 memcpy(buf, ybuf, len);
141 yajl_gen_clear(gs->g);
146 void GET_generate_list_free(void *cls) {
147 struct get_state *gs = cls;
150 yajl_gen_free(gs->g);
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;
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;
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;
174 if (strncmp(key, "data", 5) == 0) {
175 if (ps->data == NULL) {
176 ps->data_size = size;
177 ps->data = malloc(size);
179 if (ps->data_size + size > MAX_RECORD_SIZE) {
180 size = MAX_RECORD_SIZE - ps->data_size;
182 ps->data_size += size;
183 ps->data = realloc(ps->data, ps->data_size);
185 memcpy(ps->data + off, data, size);
186 if (ps->data_size == MAX_RECORD_SIZE)
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));
198 uint64_t record_count = blerg_get_record_count(b);
200 if (from > to || from >= record_count || to >= record_count || to - from > 99) {
206 gs->entries = make_sequential_list(from, to);
209 gs->g = yajl_gen_alloc(&yajl_c, NULL);
210 gs->yoff = gs->done = 0;
212 response = MHD_create_response_from_callback(-1, 262144, &GET_generate_list, gs, &GET_generate_list_free);
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;
222 struct url_info info;
225 if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) {
227 if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
228 return respond_405(connection);
235 return respond_404(connection);
237 ret = parse_url_info(url + 5, &info);
238 if ((ret & URL_INFO_AUTHOR) == 0)
239 return respond_404(connection);
241 if (!blerg_exists(info.author))
242 return respond_404(connection);
246 struct blerg *b = blerg_open(info.author);
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);
255 return respond_404(connection);
256 response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
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);
263 to = record_count - 1;
264 from = (record_count > 50 ? to - 49 : 0);
265 response = create_response_for_range(b, from, to);
269 if (response == NULL)
270 return respond_JSON_Failure(connection);
272 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
273 MHD_destroy_response(response);
275 } else if (strncmp(url, "/put", 4) == 0) {
276 struct put_state *ps = (struct put_state *) *ptr;
281 if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
282 return respond_405(connection);
285 return respond_404(connection);
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);
294 ret = MHD_digest_auth_check(connection, REALM, username, password, 300);
297 if (ret == MHD_INVALID_NONCE || ret == MHD_NO)
298 return respond_401(connection, (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);
300 struct put_state *ps = malloc(sizeof(struct put_state));
303 ps->pp = MHD_create_post_processor(connection, 16384, &POST_put_iterator, ps);
308 if (*upload_data_size) {
309 MHD_post_process(ps->pp, upload_data, *upload_data_size);
310 *upload_data_size = 0;
314 if (ps->data == NULL || ps->data_size == 0)
315 return respond_JSON_Failure(connection);
317 username = MHD_digest_auth_get_username(connection);
318 struct blerg *b = blerg_open(username);
320 return respond_JSON_Failure(connection);
321 if (blerg_store(b, ps->data, ps->data_size) == -1) {
323 return respond_JSON_Failure(connection);
327 MHD_destroy_post_processor(ps->pp);
333 return respond_JSON_Success(connection);
334 } else if (strncmp(url, "/info", 5) == 0) {
338 if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
339 return respond_405(connection);
345 return respond_404(connection);
347 ret = parse_url_info(url + 6, &info);
348 if ((ret & URL_INFO_AUTHOR) == 0)
349 return respond_404(connection);
351 if (!blerg_exists(info.author))
352 return respond_404(connection);
356 struct blerg *b = blerg_open(info.author);
357 uint64_t record_count = blerg_get_record_count(b);
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);
368 const unsigned char *ybuf;
369 yajl_gen_get_buf(g, &ybuf, &len);
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);
378 } else if (strncmp(url, "/create", 8) == 0) {
379 struct create_state *cs = (struct create_state *) *ptr;
382 if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
383 return respond_405(connection);
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);
392 if (*upload_data_size) {
393 MHD_post_process(cs->pp, upload_data, *upload_data_size);
394 *upload_data_size = 0;
398 if (cs->username[0] == 0 || cs->password[0] == 0)
399 return respond_JSON_Failure(connection);
401 if (blerg_exists(cs->username))
402 return respond_JSON_Failure(connection);
404 struct blerg *b = blerg_open(cs->username);
406 auth_set_password(cs->username, cs->password);
408 MHD_destroy_post_processor(cs->pp);
412 return respond_JSON_Success(connection);
414 return respond_404(connection);
419 int main(int argc, char *argv[]) {
420 struct MHD_Daemon *daemon;
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");
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");
438 select(max + 1, &rs, &ws, &es, NULL);
441 MHD_stop_daemon(daemon);