4 #include <microhttpd.h>
5 #include <yajl/yajl_gen.h>
10 #define URL_INFO_AUTHOR 0x1
11 #define URL_INFO_RECORD 0x2
12 #define DERP "DERP DERP DERP"
15 struct MHD_PostProcessor *pp;
21 struct MHD_PostProcessor *pp;
26 struct MHD_Response *response_401;
27 struct MHD_Response *response_404;
28 struct MHD_Response *response_405;
29 struct MHD_Response *response_JSON_Success;
30 struct MHD_Response *response_JSON_Failure;
32 void init_responses() {
33 #define CONTENT_401 "<html><head><title>401 Unauthorized</title></head><body><h1>401 Unauthorized</h1>DENIED</body></html>"
34 response_401 = MHD_create_response_from_data(strlen (CONTENT_401), CONTENT_401, MHD_NO, MHD_NO);
36 #define CONTENT_404 "<html><head><title>404 Not Found</title></head><body><h1>404 Not Found</h1>I couldn't find that.</body></html>"
37 response_404 = MHD_create_response_from_data(strlen(CONTENT_404), CONTENT_404, MHD_NO, MHD_NO);
39 #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>"
40 response_405 = MHD_create_response_from_data(strlen(CONTENT_405), CONTENT_405, MHD_NO, MHD_NO);
42 #define JSON_SUCCESS "{status: \"success\"}"
43 response_JSON_Success = MHD_create_response_from_data(strlen(JSON_SUCCESS), JSON_SUCCESS, MHD_NO, MHD_NO);
45 #define JSON_FAILURE "{status: \"failure\"}"
46 response_JSON_Failure = MHD_create_response_from_data(strlen(JSON_FAILURE), JSON_FAILURE, MHD_NO, MHD_NO);
50 #define OPAQUE "d29fb6db8f21a6e99903651a9f87470e"
51 int respond_401(struct MHD_Connection *connection, int signal_stale) {
52 return MHD_queue_auth_fail_response(connection, REALM, OPAQUE, response_401, signal_stale);
55 int respond_404(struct MHD_Connection *connection) {
56 return MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response_404);
59 int respond_405(struct MHD_Connection *connection) {
60 return MHD_queue_response(connection, MHD_HTTP_METHOD_NOT_ALLOWED, response_405);
63 int respond_JSON_Success(struct MHD_Connection *connection) {
64 return MHD_queue_response(connection, MHD_HTTP_OK, response_JSON_Success);
67 int respond_JSON_Failure(struct MHD_Connection *connection) {
68 return MHD_queue_response(connection, MHD_HTTP_OK, response_JSON_Failure);
71 int parse_url_info(const char *url, char *author, uint64_t *record) {
84 memcpy(author, url, len);
86 ret |= URL_INFO_AUTHOR;
88 if (c != NULL && c[1] != 0) {
89 *record = strtoull(c + 1, NULL, 10);
90 ret |= URL_INFO_RECORD;
96 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) {
97 struct create_state *cs = cls;
99 if (strncmp(key, "username", 9) == 0) {
100 if (size > 32) size = 32;
101 memcpy(cs->username, data, size);
102 cs->username[size] = 0;
103 } else if (strncmp(key, "password", 9) == 0) {
104 if (size > 32) size = 32;
105 memcpy(cs->password, data, size);
106 cs->password[size] = 0;
112 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) {
113 struct put_state *ps = cls;
115 if (strncmp(key, "data", 5) == 0) {
116 ps->data_size = size;
117 ps->data = malloc(size);
118 memcpy(ps->data, data, size);
126 ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const char *method,
127 const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) {
128 struct MHD_Response *response;
134 if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) {
136 if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
137 return respond_405(connection);
144 return respond_404(connection);
146 ret = parse_url_info(url + 5, author, &record);
147 if ((ret & URL_INFO_AUTHOR) == 0)
148 return respond_404(connection);
150 if (!blerg_exists(author))
151 return respond_404(connection);
155 if (ret & URL_INFO_RECORD) {
156 struct blerg *b = blerg_open(author);
157 ret = blerg_fetch(b, record, &data, &len);
161 return respond_404(connection);
163 response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
164 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
166 MHD_destroy_response(response);
169 response = MHD_create_response_from_data(strlen(DERP), DERP, MHD_NO, MHD_NO);
170 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
171 MHD_destroy_response(response);
174 } else if (strncmp(url, "/put", 4) == 0) {
175 struct put_state *ps = (struct put_state *) *ptr;
180 if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
181 return respond_405(connection);
184 return respond_404(connection);
188 username = MHD_digest_auth_get_username(connection);
189 if (username == NULL)
190 return respond_401(connection, MHD_NO);
191 auth_get_password(username, password);
193 ret = MHD_digest_auth_check(connection, REALM, username, password, 300);
196 if (ret == MHD_INVALID_NONCE || ret == MHD_NO)
197 return respond_401(connection, (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);
199 struct put_state *ps = malloc(sizeof(struct put_state));
201 ps->pp = MHD_create_post_processor(connection, 4096, &POST_put_iterator, ps);
206 if (*upload_data_size) {
207 MHD_post_process(ps->pp, upload_data, *upload_data_size);
208 *upload_data_size = 0;
212 if (ps->data == NULL || ps->data_size == 0)
213 return respond_JSON_Failure(connection);
215 username = MHD_digest_auth_get_username(connection);
216 struct blerg *b = blerg_open(username);
218 return respond_JSON_Failure(connection);
219 if (blerg_store(b, ps->data, ps->data_size) == -1) {
221 return respond_JSON_Failure(connection);
225 MHD_destroy_post_processor(ps->pp);
231 return respond_JSON_Success(connection);
232 } else if (strncmp(url, "/create", 8) == 0) {
233 struct create_state *cs = (struct create_state *) *ptr;
236 if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
237 return respond_405(connection);
239 struct create_state *cs = malloc(sizeof(struct create_state));
240 cs->username[0] = cs->password[0] = 0;
241 cs->pp = MHD_create_post_processor(connection, 1024, &POST_create_iterator, cs);
246 if (*upload_data_size) {
247 MHD_post_process(cs->pp, upload_data, *upload_data_size);
248 *upload_data_size = 0;
252 if (cs->username[0] == 0 || cs->password[0] == 0)
253 return respond_JSON_Failure(connection);
255 if (blerg_exists(cs->username))
256 return respond_JSON_Failure(connection);
258 struct blerg *b = blerg_open(cs->username);
260 auth_set_password(cs->username, cs->password);
262 MHD_destroy_post_processor(cs->pp);
266 return respond_JSON_Success(connection);
268 return respond_404(connection);
273 int main(int argc, char *argv[]) {
274 struct MHD_Daemon *daemon;
280 daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
281 if (daemon == NULL) {
282 fprintf(stderr, "Could not start web server\n");
287 FD_ZERO(&rs); FD_ZERO(&ws); FD_ZERO(&es);
288 if (MHD_get_fdset(daemon, &rs, &ws, &es, &max) != MHD_YES) {
289 fprintf(stderr, "Fatal error getting fd sets\n");
292 select(max + 1, &rs, &ws, &es, NULL);
295 MHD_stop_daemon(daemon);