4 #include <microhttpd.h>
8 #define DERP "DERP DERP DERP"
9 #define NOTFOUND "<html><head><title>404 Not Found</title></head><body><h1>404 Not Found</h1>I couldn't find that record.</body></html>"
12 ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const char *method,
13 const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) {
14 struct MHD_Response *response;
19 if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) {
23 if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
25 c = strchr(url + 5, '/');
27 len = strlen(url) - 5;
31 memcpy(author, url + 5, len);
33 printf("author: %s\n", author);
36 if (c[0] == '/' && c[1] != 0) {
37 record = strtoull(c + 1, NULL, 10);
38 printf("record: %s %l\n", c + 1, record);
45 struct blerg *b = blerg_open(author);
46 ret = blerg_fetch(b, record, &data, &len);
50 response = MHD_create_response_from_data(strlen(NOTFOUND), NOTFOUND, MHD_NO, MHD_NO);
51 ret = MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response);
53 response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
54 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
56 MHD_destroy_response(response);
65 response = MHD_create_response_from_data(strlen(DERP), DERP, MHD_NO, MHD_NO);
66 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
67 MHD_destroy_response(response);
77 int main(int argc, char *argv[]) {
78 struct MHD_Daemon *daemon;
82 daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
84 fprintf(stderr, "Could not start web server\n");
89 FD_ZERO(&rs); FD_ZERO(&ws); FD_ZERO(&es);
90 if (MHD_get_fdset(daemon, &rs, &ws, &es, &max) != MHD_YES) {
91 fprintf(stderr, "Fatal error getting fd sets\n");
94 select(max + 1, &rs, &ws, &es, NULL);
97 MHD_stop_daemon(daemon);