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>"
13 struct MHD_Response *response404;
15 void init_responses() {
16 response404 = MHD_create_response_from_data(strlen(NOTFOUND), NOTFOUND, MHD_NO, MHD_NO);
19 int respond_404(struct MHD_Connection *connection) {
20 return MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response404);
23 int parse_url_info(const char *url, char *author, uint64_t *record) {
36 memcpy(author, url, len);
38 ret |= URL_INFO_AUTHOR;
40 if (c != NULL && c[1] != 0) {
41 *record = strtoull(c + 1, NULL, 10);
42 ret |= URL_INFO_RECORD;
49 ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const char *method,
50 const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) {
51 struct MHD_Response *response;
57 if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) {
58 if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
61 return respond_404(connection);
62 ret = parse_url_info(url + 5, author, &record);
63 if (ret & URL_INFO_AUTHOR == 0)
64 return respond_404(connection);
66 if (ret & URL_INFO_RECORD) {
73 struct blerg *b = blerg_open(author);
74 ret = blerg_fetch(b, record, &data, &len);
78 return respond_404(connection);
80 response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
81 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
83 MHD_destroy_response(response);
92 response = MHD_create_response_from_data(strlen(DERP), DERP, MHD_NO, MHD_NO);
93 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
94 MHD_destroy_response(response);
98 } else if (strncmp(url, "/put", 4) == 0) {
100 const char *password = "testpass";
101 const char *realm = "Blerg Post";
103 #define OPAQUE "d29fb6db8f21a6e99903651a9f87470e"
104 #define DENIED "DENIED, MOTHERFUCKER"
105 #define PAGE "DERP DERP AUTHENTICATED DERP"
110 username = MHD_digest_auth_get_username(connection);
111 if (username == NULL) {
112 response = MHD_create_response_from_data(strlen (DENIED), DENIED, MHD_NO, MHD_NO);
113 ret = MHD_queue_auth_fail_response(connection, realm, OPAQUE, response, MHD_NO);
114 MHD_destroy_response(response);
118 printf("username: %s\n", username);
120 ret = MHD_digest_auth_check(connection, realm, username, password, 300);
123 if (ret == MHD_INVALID_NONCE || ret == MHD_NO) {
124 response = MHD_create_response_from_data(strlen (DENIED), DENIED, MHD_NO, MHD_NO);
125 ret = MHD_queue_auth_fail_response(connection, realm, OPAQUE, response,
126 (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);
127 MHD_destroy_response(response);
135 return respond_404(connection);
136 ret = parse_url_info(url + 5, author, &record);
137 if ((ret & URL_INFO_AUTHOR) == 0)
138 return respond_404(connection);
140 response = MHD_create_response_from_data(strlen(PAGE), PAGE, MHD_NO, MHD_NO);
141 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
142 MHD_destroy_response(response);
145 return respond_404(connection);
150 int main(int argc, char *argv[]) {
151 struct MHD_Daemon *daemon;
157 daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
158 if (daemon == NULL) {
159 fprintf(stderr, "Could not start web server\n");
164 FD_ZERO(&rs); FD_ZERO(&ws); FD_ZERO(&es);
165 if (MHD_get_fdset(daemon, &rs, &ws, &es, &max) != MHD_YES) {
166 fprintf(stderr, "Fatal error getting fd sets\n");
169 select(max + 1, &rs, &ws, &es, NULL);
172 MHD_stop_daemon(daemon);