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"
18 struct MHD_PostProcessor *pp;
24 struct MHD_PostProcessor *pp;
38 int parse_url_info(const char *url, char *author, uint64_t *record) {
51 memcpy(author, url, len);
53 ret |= URL_INFO_AUTHOR;
55 if (c != NULL && c[1] != 0) {
56 *record = strtoull(c + 1, NULL, 10);
57 ret |= URL_INFO_RECORD;
63 uint64_t *make_sequential_list(uint64_t from, uint64_t to) {
64 uint64_t len = to - from + 1;
65 uint64_t *list = malloc(len * sizeof(uint64_t));
68 for (i = 0; i < len; i++) {
75 ssize_t GET_generate_list(void *cls, uint64_t pos, char *buf, size_t max) {
76 struct get_state *gs = cls;
77 const unsigned char *ybuf;
83 yajl_gen_get_buf(gs->g, &ybuf, &len);
84 size_t bytes_remaining = len - gs->yoff;
85 if (bytes_remaining > max) {
86 memcpy(buf, ybuf + gs->yoff, max);
90 memcpy(buf, ybuf + gs->yoff, bytes_remaining);
92 yajl_gen_clear(gs->g);
93 return bytes_remaining;
100 if (pos == 0) { /* Start iterating */
101 yajl_gen_map_open(gs->g);
104 /* Snarf one record */
105 blerg_fetch(gs->b, gs->entries[gs->i], &data, &len);
106 snprintf(number, 21, "%llu", gs->entries[gs->i]);
107 yajl_gen_string(gs->g, number, strlen(number));
108 yajl_gen_string(gs->g, data, len);
111 yajl_gen_map_close(gs->g);
117 yajl_gen_get_buf(gs->g, &ybuf, &len);
119 memcpy(buf, ybuf, max);
123 memcpy(buf, ybuf, len);
124 yajl_gen_clear(gs->g);
129 void GET_generate_list_free(void *cls) {
130 struct get_state *gs = cls;
133 yajl_gen_free(gs->g);
138 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) {
139 struct create_state *cs = cls;
141 if (strncmp(key, "username", 9) == 0) {
142 if (size > 32) size = 32;
143 memcpy(cs->username, data, size);
144 cs->username[size] = 0;
145 } else if (strncmp(key, "password", 9) == 0) {
146 if (size > 32) size = 32;
147 memcpy(cs->password, data, size);
148 cs->password[size] = 0;
154 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) {
155 struct put_state *ps = cls;
157 if (strncmp(key, "data", 5) == 0) {
158 if (ps->data == NULL) {
159 ps->data_size = size;
160 ps->data = malloc(size);
162 if (ps->data_size + size > MAX_RECORD_SIZE) {
163 size = MAX_RECORD_SIZE - ps->data_size;
165 ps->data_size += size;
166 ps->data = realloc(ps->data, ps->data_size);
168 memcpy(ps->data + off, data, size);
169 if (ps->data_size == MAX_RECORD_SIZE)
177 ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const char *method,
178 const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) {
179 struct MHD_Response *response;
185 if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) {
187 if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
188 return respond_405(connection);
195 return respond_404(connection);
197 ret = parse_url_info(url + 5, author, &record);
198 if ((ret & URL_INFO_AUTHOR) == 0)
199 return respond_404(connection);
201 if (!blerg_exists(author))
202 return respond_404(connection);
206 if (ret & URL_INFO_RECORD) {
207 struct blerg *b = blerg_open(author);
208 ret = blerg_fetch(b, record, &data, &len);
212 return respond_404(connection);
214 response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
215 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
217 MHD_destroy_response(response);
220 struct get_state *gs = malloc(sizeof(struct get_state));
221 gs->b = blerg_open(author);
222 uint64_t record_count = blerg_get_record_count(gs->b);
223 if (record_count == 0) {
224 response = MHD_create_response_from_data(2, "[]", MHD_NO, MHD_NO);
226 uint64_t from, to, i, j;
227 to = record_count - 1;
228 from = (record_count > 50 ? to - 49 : 0);
229 gs->entries = make_sequential_list(from, to);
235 gs->g = yajl_gen_alloc(&c, NULL);
236 gs->yoff = gs->done = 0;
238 response = MHD_create_response_from_callback(-1, 262144, &GET_generate_list, gs, &GET_generate_list_free);
241 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
242 MHD_destroy_response(response);
246 } else if (strncmp(url, "/put", 4) == 0) {
247 struct put_state *ps = (struct put_state *) *ptr;
252 if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
253 return respond_405(connection);
256 return respond_404(connection);
260 username = MHD_digest_auth_get_username(connection);
261 if (username == NULL)
262 return respond_401(connection, MHD_NO);
263 auth_get_password(username, password);
265 ret = MHD_digest_auth_check(connection, REALM, username, password, 300);
268 if (ret == MHD_INVALID_NONCE || ret == MHD_NO)
269 return respond_401(connection, (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);
271 struct put_state *ps = malloc(sizeof(struct put_state));
274 ps->pp = MHD_create_post_processor(connection, 16384, &POST_put_iterator, ps);
279 if (*upload_data_size) {
280 MHD_post_process(ps->pp, upload_data, *upload_data_size);
281 *upload_data_size = 0;
285 if (ps->data == NULL || ps->data_size == 0)
286 return respond_JSON_Failure(connection);
288 username = MHD_digest_auth_get_username(connection);
289 struct blerg *b = blerg_open(username);
291 return respond_JSON_Failure(connection);
292 if (blerg_store(b, ps->data, ps->data_size) == -1) {
294 return respond_JSON_Failure(connection);
298 MHD_destroy_post_processor(ps->pp);
304 return respond_JSON_Success(connection);
305 } else if (strncmp(url, "/create", 8) == 0) {
306 struct create_state *cs = (struct create_state *) *ptr;
309 if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
310 return respond_405(connection);
312 struct create_state *cs = malloc(sizeof(struct create_state));
313 cs->username[0] = cs->password[0] = 0;
314 cs->pp = MHD_create_post_processor(connection, 1024, &POST_create_iterator, cs);
319 if (*upload_data_size) {
320 MHD_post_process(cs->pp, upload_data, *upload_data_size);
321 *upload_data_size = 0;
325 if (cs->username[0] == 0 || cs->password[0] == 0)
326 return respond_JSON_Failure(connection);
328 if (blerg_exists(cs->username))
329 return respond_JSON_Failure(connection);
331 struct blerg *b = blerg_open(cs->username);
333 auth_set_password(cs->username, cs->password);
335 MHD_destroy_post_processor(cs->pp);
339 return respond_JSON_Success(connection);
341 return respond_404(connection);
346 int main(int argc, char *argv[]) {
347 struct MHD_Daemon *daemon;
353 daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
354 if (daemon == NULL) {
355 fprintf(stderr, "Could not start web server\n");
360 FD_ZERO(&rs); FD_ZERO(&ws); FD_ZERO(&es);
361 if (MHD_get_fdset(daemon, &rs, &ws, &es, &max) != MHD_YES) {
362 fprintf(stderr, "Fatal error getting fd sets\n");
365 select(max + 1, &rs, &ws, &es, NULL);
368 MHD_stop_daemon(daemon);