f8513c51835c0adb9553f2c6681c47272edea9b0
[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 URL_INFO_AUTHOR 0x1
9 #define URL_INFO_RECORD 0x2
10 #define DERP "DERP DERP DERP"
11
12 struct create_state {
13         struct MHD_PostProcessor *pp;
14         char username[33];
15         char password[33];
16 };
17
18 struct MHD_Response *response_404;
19 struct MHD_Response *response_501;
20 struct MHD_Response *response_JSON_Success;
21
22 void init_responses() {
23 #define CONTENT_404 "<html><head><title>404 Not Found</title></head><body><h1>404 Not Found</h1>I couldn't find that.</body></html>"
24         response_404 = MHD_create_response_from_data(strlen(CONTENT_404), CONTENT_404, MHD_NO, MHD_NO);
25
26 #define CONTENT_501 "<html><head><title>501 Not Implemented</title></head><body><h1>501 Not Implemented</h1>I'm sorry, Dave. I'm afraid I can't do that.</body></html>"
27         response_501 = MHD_create_response_from_data(strlen(CONTENT_501), CONTENT_501, MHD_NO, MHD_NO);
28
29 #define JSON_SUCCESS "{status: \"success\"}"
30         response_JSON_Success = MHD_create_response_from_data(strlen(JSON_SUCCESS), JSON_SUCCESS, MHD_NO, MHD_NO);
31 }
32
33 int respond_404(struct MHD_Connection *connection) {
34         return MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response_404);
35 }
36
37 int respond_501(struct MHD_Connection *connection) {
38         return MHD_queue_response(connection, MHD_HTTP_NOT_IMPLEMENTED, response_501);
39 }
40
41 int respond_JSON_Success(struct MHD_Connection *connection) {
42         return MHD_queue_response(connection, MHD_HTTP_OK, response_JSON_Success);
43 }
44
45 int parse_url_info(const char *url, char *author, uint64_t *record) {
46         const char *c;
47         int ret = 0;
48         int len;
49
50         c = strchr(url, '/');
51         if (c == NULL) {
52                 len = strlen(url);
53         } else {
54                 len = c - url;
55         }
56         if (len == 0)
57                 return 0;
58         memcpy(author, url, len);
59         author[len] = 0;
60         ret |= URL_INFO_AUTHOR;
61
62         if (c != NULL && c[1] != 0) {
63                 *record = strtoull(c + 1, NULL, 10);
64                 ret |= URL_INFO_RECORD;
65         }
66
67         return ret;
68 }
69
70 int POST_create_iterator(void *cls, enum MHD_ValueKind kind, const char *key, const char *filename, const char *content_type, const char *transfer_encoding, const char *data, uint64_t off, size_t size) {
71         struct create_state *cs = cls;
72
73         if (strncmp(key, "username", 8) == 0) {
74                 if (size > 32) size = 32;
75                 memcpy(cs->username, data, size);
76                 cs->username[size] = 0;
77         } else if (strncmp(key, "password", 8) == 0) {
78                 if (size > 32) size = 32;
79                 memcpy(cs->password, data, size);
80                 cs->password[size] = 0;
81         }
82 }
83
84 static int
85 ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const char *method,
86           const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) {
87         struct MHD_Response *response;
88         int ret, len;
89         char author[33];
90         uint64_t record;
91         char *data;
92
93         if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) {
94                 if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
95                         return MHD_NO;
96                 if (url[4] != '/')
97                         return respond_404(connection);
98                 ret = parse_url_info(url + 5, author, &record);
99                 if ((ret & URL_INFO_AUTHOR) == 0)
100                         return respond_404(connection);
101
102                 if (!blerg_exists(author))
103                         return respond_404(connection);
104
105                 if (ret & URL_INFO_RECORD) {
106                         if (*ptr == NULL) {
107                                 *ptr = (void *) 1;
108                                 return MHD_YES;
109                         } else {
110                                 *ptr == NULL;
111
112                                 struct blerg *b = blerg_open(author);
113                                 ret = blerg_fetch(b, record, &data, &len);
114                                 blerg_close(b);
115
116                                 if (ret == 0) {
117                                         return respond_404(connection);
118                                 } else {
119                                         response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
120                                         ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
121                                 }
122                                 MHD_destroy_response(response);
123                                 return ret;
124                         }
125                 } else {
126                         if (*ptr == NULL) {
127                                 *ptr = (void*) 1;
128                                 return MHD_YES;
129                         } else {
130                                 *ptr == NULL;
131                                 response = MHD_create_response_from_data(strlen(DERP), DERP, MHD_NO, MHD_NO);
132                                 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
133                                 MHD_destroy_response(response);
134                                 return ret;
135                         }
136                 }
137         } else if (strncmp(url, "/put", 4) == 0) {
138                 char *username;
139                 const char *password = "testpass";
140                 const char *realm = "Blerg Post";
141
142 #define OPAQUE "d29fb6db8f21a6e99903651a9f87470e"
143 #define DENIED "DENIED, MOTHERFUCKER"
144 #define PAGE "DERP DERP AUTHENTICATED DERP"
145
146                 if (*ptr == NULL) {
147                         *ptr = (void *) 1;
148
149                         username = MHD_digest_auth_get_username(connection);
150                         if (username == NULL) {
151                                 response = MHD_create_response_from_data(strlen (DENIED), DENIED, MHD_NO, MHD_NO);  
152                                 ret = MHD_queue_auth_fail_response(connection, realm, OPAQUE, response, MHD_NO);    
153                                 MHD_destroy_response(response);  
154                                 return ret;
155                         }
156
157                         printf("username: %s\n", username);
158
159                         ret = MHD_digest_auth_check(connection, realm, username, password, 300);
160                         free(username);
161
162                         if (ret == MHD_INVALID_NONCE || ret == MHD_NO) {
163                                 response = MHD_create_response_from_data(strlen (DENIED), DENIED, MHD_NO, MHD_NO);  
164                                 ret = MHD_queue_auth_fail_response(connection, realm, OPAQUE, response,
165                                                                    (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);       
166                                 MHD_destroy_response(response);  
167                                 return ret;
168                         }
169                 }
170
171                 *ptr = NULL;
172
173                 if (url[4] != '/')
174                         return respond_404(connection);
175                 ret = parse_url_info(url + 5, author, &record);
176                 if ((ret & URL_INFO_AUTHOR) == 0)
177                         return respond_404(connection);
178
179                 response = MHD_create_response_from_data(strlen(PAGE), PAGE, MHD_NO, MHD_NO);
180                 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);  
181                 MHD_destroy_response(response);
182                 return ret;
183         } else if (strncmp(url, "/create", 8) == 0) {
184                 struct create_state *cs = (struct create_state *) *ptr;
185
186                 if (cs == NULL) {
187                         if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
188                                 return respond_501(connection);
189
190                         struct create_state *cs = malloc(sizeof(struct create_state));
191                         cs->username[0] = cs->password[0] = 0;
192                         cs->pp = MHD_create_post_processor(connection, 1024, &POST_create_iterator, cs);
193                         *ptr = cs;
194                         return MHD_YES;
195                 }
196
197                 if (*upload_data_size) {
198                         MHD_post_process(cs->pp, upload_data, *upload_data_size);
199                         *upload_data_size = 0;
200                         return MHD_YES;
201                 } else {
202                         printf("username: %s, password: %s\n", cs->username, cs->password);
203
204                         if (cs->username[0] == 0 || cs->password[0] == 0)
205                                 return MHD_NO; // TODO: Give a better response
206
207                         struct blerg *b = blerg_open(cs->username);
208                         blerg_close(b);
209                         /* auth_set_password(cs->username, cs->password); */
210
211                         MHD_destroy_post_processor(cs->pp);
212                         free(cs);
213                         return respond_JSON_Success(connection);
214                 }
215         } else {
216                 return respond_404(connection);
217         }
218 }
219
220
221 int main(int argc, char *argv[]) {
222         struct MHD_Daemon *daemon;
223         fd_set rs, ws, es;
224         int max;
225
226         init_responses();
227
228         daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
229         if (daemon == NULL) {
230                 fprintf(stderr, "Could not start web server\n");
231                 return 1;
232         }
233
234         while (1) {
235                 FD_ZERO(&rs); FD_ZERO(&ws); FD_ZERO(&es);
236                 if (MHD_get_fdset(daemon, &rs, &ws, &es, &max) != MHD_YES) {
237                         fprintf(stderr, "Fatal error getting fd sets\n");
238                         break;
239                 }
240                 select(max + 1, &rs, &ws, &es, NULL);
241                 MHD_run(daemon);
242         }
243         MHD_stop_daemon(daemon);
244         return 0;
245 }