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);
35 if (c[0] == '/' && c[1] != 0) {
36 record = strtoull(c + 1, NULL, 10);
43 struct blerg *b = blerg_open(author);
44 ret = blerg_fetch(b, record, &data, &len);
48 response = MHD_create_response_from_data(strlen(NOTFOUND), NOTFOUND, MHD_NO, MHD_NO);
49 ret = MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response);
51 response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
52 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
54 MHD_destroy_response(response);
63 response = MHD_create_response_from_data(strlen(DERP), DERP, MHD_NO, MHD_NO);
64 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
65 MHD_destroy_response(response);
75 int main(int argc, char *argv[]) {
76 struct MHD_Daemon *daemon;
80 daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
82 fprintf(stderr, "Could not start web server\n");
87 FD_ZERO(&rs); FD_ZERO(&ws); FD_ZERO(&es);
88 if (MHD_get_fdset(daemon, &rs, &ws, &es, &max) != MHD_YES) {
89 fprintf(stderr, "Fatal error getting fd sets\n");
92 select(max + 1, &rs, &ws, &es, NULL);
95 MHD_stop_daemon(daemon);