Add partially-working web server
[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                 printf("author: %s\n",  author);
34
35                 c = url + (5 + len);
36                 if (c[0] == '/' && c[1] != 0) {
37                         record = strtoull(c + 1, NULL, 10);
38                         printf("record: %s %l\n", c + 1, record);
39                         if (*ptr == NULL) {
40                                 *ptr = (void *) 1;
41                                 return MHD_YES;
42                         } else {
43                                 *ptr == NULL;
44
45                                 struct blerg *b = blerg_open(author);
46                                 ret = blerg_fetch(b, record, &data, &len);
47                                 blerg_close(b);
48
49                                 if (ret == 0) {
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);
52                                 } else {
53                                         response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
54                                         ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
55                                 }
56                                 MHD_destroy_response(response);
57                                 return ret;
58                         }
59                 } else {
60                         if (*ptr == NULL) {
61                                 *ptr = (void*) 1;
62                                 return MHD_YES;
63                         } else {
64                                 *ptr == NULL;
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);
68                                 return ret;
69                         }
70                 }
71         } else {
72                 return MHD_NO;
73         }
74 }
75
76
77 int main(int argc, char *argv[]) {
78         struct MHD_Daemon *daemon;
79         fd_set rs, ws, es;
80         int max;
81
82         daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
83         if (daemon == NULL) {
84                 fprintf(stderr, "Could not start web server\n");
85                 return 1;
86         }
87
88         while (1) {
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");
92                         break;
93                 }
94                 select(max + 1, &rs, &ws, &es, NULL);
95                 MHD_run(daemon);
96         }
97         MHD_stop_daemon(daemon);
98         return 0;
99 }