4 #include <microhttpd.h>
8 #define URL_INFO_AUTHOR 0x1
9 #define URL_INFO_RECORD 0x2
10 #define DERP "DERP DERP DERP"
11 #define NOTFOUND "<html><head><title>404 Not Found</title></head><body><h1>404 Not Found</h1>I couldn't find that.</body></html>"
12 #define JSON_SUCCESS "{status: \"success\"}"
15 struct MHD_PostProcessor *pp;
20 struct MHD_Response *response404;
21 struct MHD_Response *responseJSONSuccess;
23 void init_responses() {
24 response404 = MHD_create_response_from_data(strlen(NOTFOUND), NOTFOUND, MHD_NO, MHD_NO);
25 responseJSONSuccess = MHD_create_response_from_data(strlen(JSON_SUCCESS), JSON_SUCCESS, MHD_NO, MHD_NO);
28 int respond_404(struct MHD_Connection *connection) {
29 return MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response404);
32 int respond_JSONSuccess(struct MHD_Connection *connection) {
33 return MHD_queue_response(connection, MHD_HTTP_OK, responseJSONSuccess);
36 int parse_url_info(const char *url, char *author, uint64_t *record) {
49 memcpy(author, url, len);
51 ret |= URL_INFO_AUTHOR;
53 if (c != NULL && c[1] != 0) {
54 *record = strtoull(c + 1, NULL, 10);
55 ret |= URL_INFO_RECORD;
61 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) {
62 struct create_state *cs = cls;
64 if (strncmp(key, "username", 8) == 0) {
65 if (size > 32) size = 32;
66 memcpy(cs->username, data, size);
67 cs->username[size] = 0;
68 } else if (strncmp(key, "password", 8) == 0) {
69 if (size > 32) size = 32;
70 memcpy(cs->password, data, size);
71 cs->password[size] = 0;
76 ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const char *method,
77 const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) {
78 struct MHD_Response *response;
84 if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) {
85 if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
88 return respond_404(connection);
89 ret = parse_url_info(url + 5, author, &record);
90 if ((ret & URL_INFO_AUTHOR) == 0)
91 return respond_404(connection);
93 if (!blerg_exists(author))
94 return respond_404(connection);
96 if (ret & URL_INFO_RECORD) {
103 struct blerg *b = blerg_open(author);
104 ret = blerg_fetch(b, record, &data, &len);
108 return respond_404(connection);
110 response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
111 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
113 MHD_destroy_response(response);
122 response = MHD_create_response_from_data(strlen(DERP), DERP, MHD_NO, MHD_NO);
123 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
124 MHD_destroy_response(response);
128 } else if (strncmp(url, "/put", 4) == 0) {
130 const char *password = "testpass";
131 const char *realm = "Blerg Post";
133 #define OPAQUE "d29fb6db8f21a6e99903651a9f87470e"
134 #define DENIED "DENIED, MOTHERFUCKER"
135 #define PAGE "DERP DERP AUTHENTICATED DERP"
140 username = MHD_digest_auth_get_username(connection);
141 if (username == NULL) {
142 response = MHD_create_response_from_data(strlen (DENIED), DENIED, MHD_NO, MHD_NO);
143 ret = MHD_queue_auth_fail_response(connection, realm, OPAQUE, response, MHD_NO);
144 MHD_destroy_response(response);
148 printf("username: %s\n", username);
150 ret = MHD_digest_auth_check(connection, realm, username, password, 300);
153 if (ret == MHD_INVALID_NONCE || ret == MHD_NO) {
154 response = MHD_create_response_from_data(strlen (DENIED), DENIED, MHD_NO, MHD_NO);
155 ret = MHD_queue_auth_fail_response(connection, realm, OPAQUE, response,
156 (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);
157 MHD_destroy_response(response);
165 return respond_404(connection);
166 ret = parse_url_info(url + 5, author, &record);
167 if ((ret & URL_INFO_AUTHOR) == 0)
168 return respond_404(connection);
170 response = MHD_create_response_from_data(strlen(PAGE), PAGE, MHD_NO, MHD_NO);
171 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
172 MHD_destroy_response(response);
174 } else if (strncmp(url, "/create", 8) == 0) {
175 struct create_state *cs = (struct create_state *) *ptr;
178 if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
181 struct create_state *cs = malloc(sizeof(struct create_state));
182 cs->username[0] = cs->password[0] = 0;
183 cs->pp = MHD_create_post_processor(connection, 1024, &POST_create_iterator, cs);
188 if (*upload_data_size) {
189 MHD_post_process(cs->pp, upload_data, *upload_data_size);
190 *upload_data_size = 0;
193 printf("username: %s, password: %s\n", cs->username, cs->password);
195 if (cs->username[0] == 0 || cs->password[0] == 0)
196 return MHD_NO; // TODO: Give a better response
198 struct blerg *b = blerg_open(cs->username);
200 /* auth_set_password(cs->username, cs->password); */
202 MHD_destroy_post_processor(cs->pp);
204 return respond_JSONSuccess(connection);
207 return respond_404(connection);
212 int main(int argc, char *argv[]) {
213 struct MHD_Daemon *daemon;
219 daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
220 if (daemon == NULL) {
221 fprintf(stderr, "Could not start web server\n");
226 FD_ZERO(&rs); FD_ZERO(&ws); FD_ZERO(&es);
227 if (MHD_get_fdset(daemon, &rs, &ws, &es, &max) != MHD_YES) {
228 fprintf(stderr, "Fatal error getting fd sets\n");
231 select(max + 1, &rs, &ws, &es, NULL);
234 MHD_stop_daemon(daemon);