4 #include <microhttpd.h>
8 #define URL_INFO_AUTHOR 0x1
9 #define URL_INFO_RECORD 0x2
10 #define DERP "DERP DERP DERP"
13 struct MHD_PostProcessor *pp;
18 struct MHD_Response *response_404;
19 struct MHD_Response *response_501;
20 struct MHD_Response *response_JSON_Success;
22 void init_responses() {
23 #define CONTENT_404 "<html><head><title>404 Not Found</title></head><body><h1>404 Not Found</h1>I couldn't find that.</body></html>"
24 response_404 = MHD_create_response_from_data(strlen(CONTENT_404), CONTENT_404, MHD_NO, MHD_NO);
26 #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>"
27 response_501 = MHD_create_response_from_data(strlen(CONTENT_501), CONTENT_501, MHD_NO, MHD_NO);
29 #define JSON_SUCCESS "{status: \"success\"}"
30 response_JSON_Success = MHD_create_response_from_data(strlen(JSON_SUCCESS), JSON_SUCCESS, MHD_NO, MHD_NO);
33 int respond_404(struct MHD_Connection *connection) {
34 return MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response_404);
37 int respond_501(struct MHD_Connection *connection) {
38 return MHD_queue_response(connection, MHD_HTTP_NOT_IMPLEMENTED, response_501);
41 int respond_JSON_Success(struct MHD_Connection *connection) {
42 return MHD_queue_response(connection, MHD_HTTP_OK, response_JSON_Success);
45 int parse_url_info(const char *url, char *author, uint64_t *record) {
58 memcpy(author, url, len);
60 ret |= URL_INFO_AUTHOR;
62 if (c != NULL && c[1] != 0) {
63 *record = strtoull(c + 1, NULL, 10);
64 ret |= URL_INFO_RECORD;
70 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) {
71 struct create_state *cs = cls;
73 if (strncmp(key, "username", 8) == 0) {
74 if (size > 32) size = 32;
75 memcpy(cs->username, data, size);
76 cs->username[size] = 0;
77 } else if (strncmp(key, "password", 8) == 0) {
78 if (size > 32) size = 32;
79 memcpy(cs->password, data, size);
80 cs->password[size] = 0;
85 ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const char *method,
86 const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) {
87 struct MHD_Response *response;
93 if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) {
94 if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
97 return respond_404(connection);
98 ret = parse_url_info(url + 5, author, &record);
99 if ((ret & URL_INFO_AUTHOR) == 0)
100 return respond_404(connection);
102 if (!blerg_exists(author))
103 return respond_404(connection);
105 if (ret & URL_INFO_RECORD) {
112 struct blerg *b = blerg_open(author);
113 ret = blerg_fetch(b, record, &data, &len);
117 return respond_404(connection);
119 response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
120 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
122 MHD_destroy_response(response);
131 response = MHD_create_response_from_data(strlen(DERP), DERP, MHD_NO, MHD_NO);
132 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
133 MHD_destroy_response(response);
137 } else if (strncmp(url, "/put", 4) == 0) {
139 const char *password = "testpass";
140 const char *realm = "Blerg Post";
142 #define OPAQUE "d29fb6db8f21a6e99903651a9f87470e"
143 #define DENIED "DENIED, MOTHERFUCKER"
144 #define PAGE "DERP DERP AUTHENTICATED DERP"
149 username = MHD_digest_auth_get_username(connection);
150 if (username == NULL) {
151 response = MHD_create_response_from_data(strlen (DENIED), DENIED, MHD_NO, MHD_NO);
152 ret = MHD_queue_auth_fail_response(connection, realm, OPAQUE, response, MHD_NO);
153 MHD_destroy_response(response);
157 printf("username: %s\n", username);
159 ret = MHD_digest_auth_check(connection, realm, username, password, 300);
162 if (ret == MHD_INVALID_NONCE || ret == MHD_NO) {
163 response = MHD_create_response_from_data(strlen (DENIED), DENIED, MHD_NO, MHD_NO);
164 ret = MHD_queue_auth_fail_response(connection, realm, OPAQUE, response,
165 (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);
166 MHD_destroy_response(response);
174 return respond_404(connection);
175 ret = parse_url_info(url + 5, author, &record);
176 if ((ret & URL_INFO_AUTHOR) == 0)
177 return respond_404(connection);
179 response = MHD_create_response_from_data(strlen(PAGE), PAGE, MHD_NO, MHD_NO);
180 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
181 MHD_destroy_response(response);
183 } else if (strncmp(url, "/create", 8) == 0) {
184 struct create_state *cs = (struct create_state *) *ptr;
187 if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
188 return respond_501(connection);
190 struct create_state *cs = malloc(sizeof(struct create_state));
191 cs->username[0] = cs->password[0] = 0;
192 cs->pp = MHD_create_post_processor(connection, 1024, &POST_create_iterator, cs);
197 if (*upload_data_size) {
198 MHD_post_process(cs->pp, upload_data, *upload_data_size);
199 *upload_data_size = 0;
202 printf("username: %s, password: %s\n", cs->username, cs->password);
204 if (cs->username[0] == 0 || cs->password[0] == 0)
205 return MHD_NO; // TODO: Give a better response
207 struct blerg *b = blerg_open(cs->username);
209 /* auth_set_password(cs->username, cs->password); */
211 MHD_destroy_post_processor(cs->pp);
213 return respond_JSON_Success(connection);
216 return respond_404(connection);
221 int main(int argc, char *argv[]) {
222 struct MHD_Daemon *daemon;
228 daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
229 if (daemon == NULL) {
230 fprintf(stderr, "Could not start web server\n");
235 FD_ZERO(&rs); FD_ZERO(&ws); FD_ZERO(&es);
236 if (MHD_get_fdset(daemon, &rs, &ws, &es, &max) != MHD_YES) {
237 fprintf(stderr, "Fatal error getting fd sets\n");
240 select(max + 1, &rs, &ws, &es, NULL);
243 MHD_stop_daemon(daemon);