4 #include <microhttpd.h>
9 #define URL_INFO_AUTHOR 0x1
10 #define URL_INFO_RECORD 0x2
11 #define DERP "DERP DERP DERP"
14 struct MHD_PostProcessor *pp;
20 struct MHD_PostProcessor *pp;
25 struct MHD_Response *response_401;
26 struct MHD_Response *response_404;
27 struct MHD_Response *response_405;
28 struct MHD_Response *response_JSON_Success;
29 struct MHD_Response *response_JSON_Failure;
31 void init_responses() {
32 #define CONTENT_401 "<html><head><title>401 Unauthorized</title></head><body><h1>401 Unauthorized</h1>DENIED</body></html>"
33 response_401 = MHD_create_response_from_data(strlen (CONTENT_401), CONTENT_401, MHD_NO, MHD_NO);
35 #define CONTENT_404 "<html><head><title>404 Not Found</title></head><body><h1>404 Not Found</h1>I couldn't find that.</body></html>"
36 response_404 = MHD_create_response_from_data(strlen(CONTENT_404), CONTENT_404, MHD_NO, MHD_NO);
38 #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>"
39 response_405 = MHD_create_response_from_data(strlen(CONTENT_405), CONTENT_405, MHD_NO, MHD_NO);
41 #define JSON_SUCCESS "{status: \"success\"}"
42 response_JSON_Success = MHD_create_response_from_data(strlen(JSON_SUCCESS), JSON_SUCCESS, MHD_NO, MHD_NO);
44 #define JSON_FAILURE "{status: \"failure\"}"
45 response_JSON_Failure = MHD_create_response_from_data(strlen(JSON_FAILURE), JSON_FAILURE, MHD_NO, MHD_NO);
49 #define OPAQUE "d29fb6db8f21a6e99903651a9f87470e"
50 int respond_401(struct MHD_Connection *connection, int signal_stale) {
51 return MHD_queue_auth_fail_response(connection, REALM, OPAQUE, response_401, signal_stale);
54 int respond_404(struct MHD_Connection *connection) {
55 return MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response_404);
58 int respond_405(struct MHD_Connection *connection) {
59 return MHD_queue_response(connection, MHD_HTTP_METHOD_NOT_ALLOWED, response_405);
62 int respond_JSON_Success(struct MHD_Connection *connection) {
63 return MHD_queue_response(connection, MHD_HTTP_OK, response_JSON_Success);
66 int respond_JSON_Failure(struct MHD_Connection *connection) {
67 return MHD_queue_response(connection, MHD_HTTP_OK, response_JSON_Failure);
70 int parse_url_info(const char *url, char *author, uint64_t *record) {
83 memcpy(author, url, len);
85 ret |= URL_INFO_AUTHOR;
87 if (c != NULL && c[1] != 0) {
88 *record = strtoull(c + 1, NULL, 10);
89 ret |= URL_INFO_RECORD;
95 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) {
96 struct create_state *cs = cls;
98 if (strncmp(key, "username", 9) == 0) {
99 if (size > 32) size = 32;
100 memcpy(cs->username, data, size);
101 cs->username[size] = 0;
102 } else if (strncmp(key, "password", 9) == 0) {
103 if (size > 32) size = 32;
104 memcpy(cs->password, data, size);
105 cs->password[size] = 0;
111 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) {
112 struct put_state *ps = cls;
114 if (strncmp(key, "data", 5) == 0) {
115 ps->data_size = size;
116 ps->data = malloc(size);
117 memcpy(ps->data, data, size);
125 ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const char *method,
126 const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) {
127 struct MHD_Response *response;
133 if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) {
134 if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
137 return respond_404(connection);
138 ret = parse_url_info(url + 5, author, &record);
139 if ((ret & URL_INFO_AUTHOR) == 0)
140 return respond_404(connection);
142 if (!blerg_exists(author))
143 return respond_404(connection);
145 if (ret & URL_INFO_RECORD) {
152 struct blerg *b = blerg_open(author);
153 ret = blerg_fetch(b, record, &data, &len);
157 return respond_404(connection);
159 response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
160 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
162 MHD_destroy_response(response);
171 response = MHD_create_response_from_data(strlen(DERP), DERP, MHD_NO, MHD_NO);
172 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
173 MHD_destroy_response(response);
177 } else if (strncmp(url, "/put", 4) == 0) {
178 struct put_state *ps = (struct put_state *) *ptr;
183 if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
184 return respond_405(connection);
187 return respond_404(connection);
191 username = MHD_digest_auth_get_username(connection);
192 if (username == NULL)
193 return respond_401(connection, MHD_NO);
194 auth_get_password(username, password);
196 ret = MHD_digest_auth_check(connection, REALM, username, password, 300);
199 if (ret == MHD_INVALID_NONCE || ret == MHD_NO)
200 return respond_401(connection, (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);
202 struct put_state *ps = malloc(sizeof(struct put_state));
204 ps->pp = MHD_create_post_processor(connection, 4096, &POST_put_iterator, ps);
209 if (*upload_data_size) {
210 MHD_post_process(ps->pp, upload_data, *upload_data_size);
211 *upload_data_size = 0;
215 if (ps->data == NULL || ps->data_size == 0)
216 return respond_JSON_Failure(connection);
218 username = MHD_digest_auth_get_username(connection);
219 struct blerg *b = blerg_open(username);
221 return respond_JSON_Failure(connection);
222 if (blerg_store(b, ps->data, ps->data_size) == -1) {
224 return respond_JSON_Failure(connection);
228 MHD_destroy_post_processor(ps->pp);
234 return respond_JSON_Success(connection);
235 } else if (strncmp(url, "/create", 8) == 0) {
236 struct create_state *cs = (struct create_state *) *ptr;
239 if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
240 return respond_405(connection);
242 struct create_state *cs = malloc(sizeof(struct create_state));
243 cs->username[0] = cs->password[0] = 0;
244 cs->pp = MHD_create_post_processor(connection, 1024, &POST_create_iterator, cs);
249 if (*upload_data_size) {
250 MHD_post_process(cs->pp, upload_data, *upload_data_size);
251 *upload_data_size = 0;
255 if (cs->username[0] == 0 || cs->password[0] == 0)
256 return respond_JSON_Failure(connection);
258 if (blerg_exists(cs->username))
259 return respond_JSON_Failure(connection);
261 struct blerg *b = blerg_open(cs->username);
263 auth_set_password(cs->username, cs->password);
265 MHD_destroy_post_processor(cs->pp);
269 return respond_JSON_Success(connection);
271 return respond_404(connection);
276 int main(int argc, char *argv[]) {
277 struct MHD_Daemon *daemon;
283 daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
284 if (daemon == NULL) {
285 fprintf(stderr, "Could not start web server\n");
290 FD_ZERO(&rs); FD_ZERO(&ws); FD_ZERO(&es);
291 if (MHD_get_fdset(daemon, &rs, &ws, &es, &max) != MHD_YES) {
292 fprintf(stderr, "Fatal error getting fd sets\n");
295 select(max + 1, &rs, &ws, &es, NULL);
298 MHD_stop_daemon(daemon);