-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttpserver.c
More file actions
435 lines (362 loc) · 10.2 KB
/
Copy pathhttpserver.c
File metadata and controls
435 lines (362 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <regex.h>
#include "httpserver.h"
#include <aul/net.h>
#include <aul/list.h>
#include <kernel.h>
#define NUM_BUFFERS 10
#define NUM_KEYVALUES 30
#define BUFFER_LEN 1000
static regex_t get_match;
static regex_t params_match;
static regex_t header_match;
typedef struct
{
const char * key;
const char * value;
hashentry_t hash_entry;
} httpkeyvalue_t;
typedef struct
{
const char * uri;
httpmatch_t match;
httpcallback_f callback;
void * userdata;
list_t filter_list;
} httpfilter_t;
typedef struct
{
bool inuse;
fdwatcher_t watcher;
char buffer[BUFFER_LEN];
size_t length;
httpcontext_t * ctx;
} httpbuffer_t;
struct __httpcontext_t
{
fdwatcher_t watcher;
list_t filters;
httpbuffer_t buffers[NUM_BUFFERS];
httpkeyvalue_t keyvalues[NUM_KEYVALUES];
hashtable_t headers;
hashtable_t parameters;
};
static inline string_t addr2string(uint32_t ip)
{
unsigned char a4 = (ip & 0xFF000000) >> 24;
unsigned char a3 = (ip & 0x00FF0000) >> 16;
unsigned char a2 = (ip & 0x0000FF00) >> 8;
unsigned char a1 = ip & 0xFF;
return string_new("%d.%d.%d.%d", a1, a2, a3, a4);
}
void http_urldecode(char * string)
{
char * i = NULL;
while ((i = strchr(string, '%')) != NULL)
{
size_t len = 0;
if ((len = strlen(i)) < 3)
{
break;
}
// Parse the value
const char x[] = { i[1], i[2], '\0' };
long v = strtol(x, NULL, 16);
// Collapse the string
memcpy(&i[1], &i[3], len-2);
// Set the value
i[0] = (char)v;
}
}
static bool http_newdata(mainloop_t * loop, int fd, fdcond_t cond, void * userdata)
{
httpbuffer_t * buffer = userdata;
ssize_t bytesread = recv(fd, buffer->buffer + buffer->length, BUFFER_LEN - buffer->length - 1, 0);
if (bytesread > 0)
{
buffer->length += bytesread;
buffer->buffer[buffer->length] = '\0';
if (strstr(buffer->buffer, "\r\n\r\n") != NULL)
{
// We have the full header, parse it
regmatch_t match[3];
memset(match, 0, sizeof(regmatch_t) * 3);
if (regexec(&get_match, buffer->buffer, 3, match, 0) == 0)
{
// The browser is using GET. Gooood.
char request_uri[250] = {0};
memcpy(request_uri, buffer->buffer + match[1].rm_so, min(sizeof(request_uri) - 1, match[1].rm_eo - match[1].rm_so));
size_t keyvalue_index = 0;
// Parse the GET parameters off the URI
hashtable_init(&buffer->ctx->parameters, hash_str, hash_streq);
if (strchr(request_uri, '?') != NULL)
{
char * params = strchr(request_uri, '?');
*params++ = '\0';
size_t offset = 0;
while (regexec(¶ms_match, params + offset, 3, match, 0) == 0 && keyvalue_index < NUM_KEYVALUES)
{
bool atend = params[offset + match[0].rm_eo] == '\0';
char * key = params + offset + match[1].rm_so;
key[match[1].rm_eo - match[1].rm_so] = '\0';
char * value = params + offset + match[2].rm_so;
value[match[2].rm_eo - match[2].rm_so] = '\0';
http_urldecode(key);
http_urldecode(value);
httpkeyvalue_t * kv = &buffer->ctx->keyvalues[keyvalue_index++];
kv->key = key;
kv->value = value;
hashtable_put(&buffer->ctx->parameters, key, &kv->hash_entry);
if (atend)
break;
else
offset += match[0].rm_eo+1;
}
}
// Pass the request through the filters
httpfilter_t * pass_match = NULL;
list_t * pos;
list_foreach(pos, &buffer->ctx->filters)
{
httpfilter_t * filter = list_entry(pos, httpfilter_t, filter_list);
switch (filter->match)
{
case MATCH_ALL:
{
if (strcmp(request_uri, filter->uri) == 0)
pass_match = filter;
break;
}
case MATCH_PREFIX:
{
if (strprefix(request_uri, filter->uri))
pass_match = filter;
break;
}
}
if (pass_match != NULL)
{
break;
}
}
if (pass_match != NULL)
{
// We have matched (at least) one filter. Parse the rest of the headers
//hashtable_t * headers = &buffer->ctx->headers;
hashtable_init(&buffer->ctx->headers, hash_str, hash_streq);
size_t offset = 0;
while (regexec(&header_match, buffer->buffer + offset, 3, match, 0) == 0 && keyvalue_index < NUM_KEYVALUES)
{
char * key = buffer->buffer + offset + match[1].rm_so;
key[match[1].rm_eo - match[1].rm_so] = '\0';
char * value = buffer->buffer + offset + match[2].rm_so;
value[match[2].rm_eo - match[2].rm_so] = '\0';
httpkeyvalue_t * kv = &buffer->ctx->keyvalues[keyvalue_index++];
kv->key = key;
kv->value = value;
hashtable_put(&buffer->ctx->headers, key, &kv->hash_entry);
offset += match[0].rm_eo+1;
}
if (keyvalue_index == NUM_KEYVALUES)
{
// We (most likely) ran out of http_keyvalues before completed parsing. Log it
LOG(LOG_WARN, "Ran out of key->value buffers in http_newdata. Consider increasing the number of key->value buffers (currently %d)", NUM_KEYVALUES);
}
pass_match->callback(&fd, buffer->ctx, request_uri);
}
else
{
// We have not passed the filters. Output 404
const char * msg = "HTTP/1.0 404 Not Found\r\n\r\n";
send(fd, msg, strlen(msg)+1, 0);
}
}
}
else if (buffer->length == BUFFER_LEN-1)
{
// Buffer overrun. We couldn't read all the header data. Too bad
LOG(LOG_WARN, "Buffer overrun while reading headers in http_newdata. Consider increasing the buffer length (currently %d)", BUFFER_LEN);
}
else
{
// We only have some of the header... no matter, just return true and we'll get the rest on the next pass
return true;
}
}
buffer->inuse = false;
return false;
}
static bool http_newclient(mainloop_t * loop, int fd, fdcond_t cond, void * userdata)
{
labels(end);
httpcontext_t * ctx = userdata;
struct sockaddr_in addr;
socklen_t socklen = sizeof(addr);
int sock = accept(fd, (struct sockaddr *)&addr, &socklen);
if (sock < 0)
{
if (errno != EAGAIN && errno != EWOULDBLOCK)
{
LOG(LOG_WARN, "Could not accept new http client socket: %s", strerror(errno));
}
goto end;
}
// TODO IMPORTANT - make sock non-blocking
// TODO - add lock around getting free buffers
httpbuffer_t * buffer = NULL;
for (size_t i = 0; i < NUM_BUFFERS; i++)
{
if (!ctx->buffers[i].inuse)
{
buffer = &ctx->buffers[i];
break;
}
}
if (buffer == NULL)
{
LOG(LOG_WARN, "Could not accept new HTTP client: Out of HTTP buffers!");
close(sock);
goto end;
}
// Set up the buffer
buffer->inuse = true;
watcher_newfd(&buffer->watcher, sock, FD_READ, http_newdata, buffer);
buffer->buffer[0] = '\0';
buffer->length = 0;
buffer->ctx = ctx;
// Now set up the watch on the accept'd file descriptor
exception_t * e = NULL;
if (!mainloop_addwatcher(loop, &buffer->watcher, &e) || exception_check(&e))
{
LOG(LOG_WARN, "Could not add http client fd to mainloop: %s", exception_message(e));
exception_free(e);
// Remove the in-use flag to make it available again
watcher_close(&buffer->watcher);
buffer->inuse = false;
goto end;
}
end:
return true;
}
httpcontext_t * http_new(uint16_t port, mainloop_t * mainloop, exception_t ** err)
{
// Sanity check
{
if unlikely(exception_check(err))
{
return NULL;
}
if (mainloop == NULL)
{
exception_set(err, EINVAL, "Bad arguments!");
return NULL;
}
}
int sock = tcp_server(port, err);
if (sock == -1 || exception_check(err))
{
return NULL;
}
// TODO IMPORTANT - make socket non-blocking
httpcontext_t * ctx = malloc(sizeof(httpcontext_t));
memset(ctx, 0, sizeof(httpcontext_t));
watcher_newfd(&ctx->watcher, sock, FD_READ, http_newclient, ctx);
list_init(&ctx->filters);
if (!mainloop_addwatcher(mainloop, &ctx->watcher, err) || exception_check(err))
{
http_destroy(ctx);
return NULL;
}
return ctx;
}
void http_adduri(httpcontext_t * ctx, const char * uri, httpmatch_t match, httpcallback_f cb, void * userdata)
{
httpfilter_t * filt = malloc(sizeof(httpfilter_t));
memset(filt, 0, sizeof(httpfilter_t));
filt->uri = uri;
filt->match = match;
filt->callback = cb;
filt->userdata = userdata;
list_add(&ctx->filters, &filt->filter_list);
}
void http_printf(httpconnection_t * conn, const char * fmt, ...)
{
va_list args;
va_start(args, fmt);
http_vprintf(conn, fmt, args);
va_end(args);
}
void http_vprintf(httpconnection_t * conn, const char * fmt, va_list args)
{
string_t msg = string_blank();
string_vappend(&msg, fmt, args);
http_write(conn, msg.string, msg.length);
}
void http_write(httpconnection_t * conn, const void * buf, size_t len)
{
send(*conn, buf, len, 0);
}
const char * http_getheader(httpcontext_t * ctx, const char * name)
{
hashentry_t * entry = hashtable_get(&ctx->headers, name);
if (entry != NULL)
{
httpkeyvalue_t * kv = hashtable_entry(entry, httpkeyvalue_t, hash_entry);
return kv->value;
}
return NULL;
}
const char * http_getparam(httpcontext_t * ctx, const char * name)
{
hashentry_t * entry = hashtable_get(&ctx->parameters, name);
if (entry != NULL)
{
httpkeyvalue_t * kv = hashtable_entry(entry, httpkeyvalue_t, hash_entry);
return kv->value;
}
return NULL;
}
void http_destroy(httpcontext_t * ctx)
{
// Remove http ctx watcher
{
exception_t * e = NULL;
if (!mainloop_removewatcher(&ctx->watcher, &e) || exception_check(&e))
{
LOG(LOG_WARN, "Could not remove http ctx watcher: %s", exception_message(e));
exception_free(e);
}
}
// Free all the filters
{
list_t * pos = NULL, * n = NULL;
list_foreach_safe(pos, n, &ctx->filters)
{
httpfilter_t * filt = list_entry(pos, httpfilter_t, filter_list);
list_remove(&filt->filter_list);
free(filt);
}
}
// Free the entire struct
free(ctx);
}
static void http_preactivate()
{
if (
regcomp(&get_match, "^GET \\(.*\\) HTTP.*$", REG_NEWLINE) != 0 ||
regcomp(&header_match, "^\\(.*\\): \\(.*\\)\r$", REG_NEWLINE) != 0 ||
regcomp(¶ms_match, "\\([^&]\\+\\)=\\([^&]\\+\\)", 0) != 0
)
{
LOG(LOG_FATAL, "Could not compile regular expression to match HTTP headers!!");
}
}
module_name("HTTP Server");
module_version(1,0,0);
module_author("Andrew Klofas - andrew@maxkernel.com");
module_description("Provides a simple (GET only) HTTP server. Intended to be used by other modules (not included directly)");
module_onpreactivate(http_preactivate);