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;
19 struct MHD_Response *response_404;
20 struct MHD_Response *response_501;
21 struct MHD_Response *response_JSON_Success;
22 struct MHD_Response *response_JSON_Failure;
24 void init_responses() {
25 #define CONTENT_404 "<html><head><title>404 Not Found</title></head><body><h1>404 Not Found</h1>I couldn't find that.</body></html>"
26 response_404 = MHD_create_response_from_data(strlen(CONTENT_404), CONTENT_404, MHD_NO, MHD_NO);
28 #define CONTENT_501 "<html><head><title>501 Not Implemented</title></head><body><h1>501 Not Implemented</h1>I'm sorry, Dave. I'm afraid I can't do that.</body></html>"
29 response_501 = MHD_create_response_from_data(strlen(CONTENT_501), CONTENT_501, MHD_NO, MHD_NO);
31 #define JSON_SUCCESS "{status: \"success\"}"
32 response_JSON_Success = MHD_create_response_from_data(strlen(JSON_SUCCESS), JSON_SUCCESS, MHD_NO, MHD_NO);
34 #define JSON_FAILURE "{status: \"failure\"}"
35 response_JSON_Failure = MHD_create_response_from_data(strlen(JSON_FAILURE), JSON_FAILURE, MHD_NO, MHD_NO);
38 int respond_404(struct MHD_Connection *connection) {
39 return MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response_404);
42 int respond_501(struct MHD_Connection *connection) {
43 return MHD_queue_response(connection, MHD_HTTP_NOT_IMPLEMENTED, response_501);
46 int respond_JSON_Success(struct MHD_Connection *connection) {
47 return MHD_queue_response(connection, MHD_HTTP_OK, response_JSON_Success);
50 int respond_JSON_Failure(struct MHD_Connection *connection) {
51 return MHD_queue_response(connection, MHD_HTTP_OK, response_JSON_Failure);
54 int parse_url_info(const char *url, char *author, uint64_t *record) {
67 memcpy(author, url, len);
69 ret |= URL_INFO_AUTHOR;
71 if (c != NULL && c[1] != 0) {
72 *record = strtoull(c + 1, NULL, 10);
73 ret |= URL_INFO_RECORD;
79 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) {
80 struct create_state *cs = cls;
82 if (strncmp(key, "username", 8) == 0) {
83 if (size > 32) size = 32;
84 memcpy(cs->username, data, size);
85 cs->username[size] = 0;
86 } else if (strncmp(key, "password", 8) == 0) {
87 if (size > 32) size = 32;
88 memcpy(cs->password, data, size);
89 cs->password[size] = 0;
94 ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const char *method,
95 const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) {
96 struct MHD_Response *response;
102 if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) {
103 if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
106 return respond_404(connection);
107 ret = parse_url_info(url + 5, author, &record);
108 if ((ret & URL_INFO_AUTHOR) == 0)
109 return respond_404(connection);
111 if (!blerg_exists(author))
112 return respond_404(connection);
114 if (ret & URL_INFO_RECORD) {
121 struct blerg *b = blerg_open(author);
122 ret = blerg_fetch(b, record, &data, &len);
126 return respond_404(connection);
128 response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
129 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
131 MHD_destroy_response(response);
140 response = MHD_create_response_from_data(strlen(DERP), DERP, MHD_NO, MHD_NO);
141 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
142 MHD_destroy_response(response);
146 } else if (strncmp(url, "/put", 4) == 0) {
148 const char *password = "testpass";
149 const char *realm = "Blerg Post";
151 #define OPAQUE "d29fb6db8f21a6e99903651a9f87470e"
152 #define DENIED "DENIED, MOTHERFUCKER"
153 #define PAGE "DERP DERP AUTHENTICATED DERP"
158 username = MHD_digest_auth_get_username(connection);
159 if (username == NULL) {
160 response = MHD_create_response_from_data(strlen (DENIED), DENIED, MHD_NO, MHD_NO);
161 ret = MHD_queue_auth_fail_response(connection, realm, OPAQUE, response, MHD_NO);
162 MHD_destroy_response(response);
166 printf("username: %s\n", username);
168 ret = MHD_digest_auth_check(connection, realm, username, password, 300);
171 if (ret == MHD_INVALID_NONCE || ret == MHD_NO) {
172 response = MHD_create_response_from_data(strlen (DENIED), DENIED, MHD_NO, MHD_NO);
173 ret = MHD_queue_auth_fail_response(connection, realm, OPAQUE, response,
174 (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);
175 MHD_destroy_response(response);
183 return respond_404(connection);
184 ret = parse_url_info(url + 5, author, &record);
185 if ((ret & URL_INFO_AUTHOR) == 0)
186 return respond_404(connection);
188 response = MHD_create_response_from_data(strlen(PAGE), PAGE, MHD_NO, MHD_NO);
189 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
190 MHD_destroy_response(response);
192 } else if (strncmp(url, "/create", 8) == 0) {
193 struct create_state *cs = (struct create_state *) *ptr;
196 if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
197 return respond_501(connection);
199 struct create_state *cs = malloc(sizeof(struct create_state));
200 cs->username[0] = cs->password[0] = 0;
201 cs->pp = MHD_create_post_processor(connection, 1024, &POST_create_iterator, cs);
206 if (*upload_data_size) {
207 MHD_post_process(cs->pp, upload_data, *upload_data_size);
208 *upload_data_size = 0;
211 if (cs->username[0] == 0 || cs->password[0] == 0)
212 return respond_JSON_Failure(connection);
214 if (blerg_exists(cs->username))
215 return respond_JSON_Failure(connection);
217 struct blerg *b = blerg_open(cs->username);
219 auth_set_password(cs->username, cs->password);
221 MHD_destroy_post_processor(cs->pp);
223 return respond_JSON_Success(connection);
226 return respond_404(connection);
231 int main(int argc, char *argv[]) {
232 struct MHD_Daemon *daemon;
238 daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
239 if (daemon == NULL) {
240 fprintf(stderr, "Could not start web server\n");
245 FD_ZERO(&rs); FD_ZERO(&ws); FD_ZERO(&es);
246 if (MHD_get_fdset(daemon, &rs, &ws, &es, &max) != MHD_YES) {
247 fprintf(stderr, "Fatal error getting fd sets\n");
250 select(max + 1, &rs, &ws, &es, NULL);
253 MHD_stop_daemon(daemon);