Whoops, what the hell was that?
[blerg.git] / http_blerg.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <microhttpd.h>
5 #include "database.h"
6 #include "tags.h"
7
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>"
10
11 static int
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;
15         int ret, len;
16         const char *c;
17         char *data;
18
19         if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) {
20                 char author[33];
21                 uint64_t record;
22
23                 if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
24                         return MHD_NO;
25                 c = strchr(url + 5, '/');
26                 if (c == NULL) {
27                         len = strlen(url) - 5;
28                 } else {
29                         len = c - (url + 5);
30                 }
31                 memcpy(author, url + 5, len);
32                 author[len] = 0;
33
34                 c = url + (5 + len);
35                 if (c[0] == '/' && c[1] != 0) {
36                         record = strtoull(c + 1, NULL, 10);
37                         if (*ptr == NULL) {
38                                 *ptr = (void *) 1;
39                                 return MHD_YES;
40                         } else {
41                                 *ptr == NULL;
42
43                                 struct blerg *b = blerg_open(author);
44                                 ret = blerg_fetch(b, record, &data, &len);
45                                 blerg_close(b);
46
47                                 if (ret == 0) {
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);
50                                 } else {
51                                         response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
52                                         ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
53                                 }
54                                 MHD_destroy_response(response);
55                                 return ret;
56                         }
57                 } else {
58                         if (*ptr == NULL) {
59                                 *ptr = (void*) 1;
60                                 return MHD_YES;
61                         } else {
62                                 *ptr == NULL;
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);
66                                 return ret;
67                         }
68                 }
69         } else {
70                 return MHD_NO;
71         }
72 }
73
74
75 int main(int argc, char *argv[]) {
76         struct MHD_Daemon *daemon;
77         fd_set rs, ws, es;
78         int max;
79
80         daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
81         if (daemon == NULL) {
82                 fprintf(stderr, "Could not start web server\n");
83                 return 1;
84         }
85
86         while (1) {
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");
90                         break;
91                 }
92                 select(max + 1, &rs, &ws, &es, NULL);
93                 MHD_run(daemon);
94         }
95         MHD_stop_daemon(daemon);
96         return 0;
97 }