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