-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttpserver.c
More file actions
384 lines (318 loc) · 8.74 KB
/
Copy pathhttpserver.c
File metadata and controls
384 lines (318 loc) · 8.74 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
#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>
MOD_PREACT(module_preact);
#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 hashentry;
} http_keyvalue;
typedef struct
{
const char * uri;
http_match match;
http_callback callback;
void * userdata;
list_t filter;
} http_filter;
typedef struct
{
bool inuse;
char buffer[BUFFER_LEN];
size_t length;
http_context * ctx;
} http_buffer;
struct __http_context
{
int socket;
mainloop_t * mainloop;
list_t filters;
http_buffer buffers[NUM_BUFFERS];
http_keyvalue 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)
{
http_buffer * 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];
ZERO(match);
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);
http_keyvalue * kv = &buffer->ctx->keyvalues[keyvalue_index++];
kv->key = key;
kv->value = value;
hashtable_put(&buffer->ctx->parameters, key, &kv->hashentry);
if (atend)
break;
else
offset += match[0].rm_eo+1;
}
}
// Pass the request through the filters
http_filter * pass_match = NULL;
list_t * pos;
list_foreach(pos, &buffer->ctx->filters)
{
http_filter * filter = list_entry(pos, http_filter, filter);
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';
http_keyvalue * kv = &buffer->ctx->keyvalues[keyvalue_index++];
kv->key = key;
kv->value = value;
hashtable_put(&buffer->ctx->headers, key, &kv->hashentry);
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;
close(fd);
return false;
}
static bool http_newclient(mainloop_t * loop, int fd, fdcond_t cond, void * userdata)
{
http_context * ctx = userdata;
struct sockaddr_in addr;
socklen_t socklen = sizeof(addr);
int sock = accept(fd, (struct sockaddr *)&addr, &socklen);
if (sock == -1)
{
LOG(LOG_WARN, "Could not accept new http client socket: %s", strerror(errno));
}
else
{
http_buffer * buffer = NULL;
int i;
for (i=0; i<NUM_BUFFERS; i++)
{
if (!ctx->buffers[i].inuse)
buffer = &ctx->buffers[i];
}
if (buffer == NULL)
{
LOG(LOG_WARN, "Could not accept new HTTP client: Out of HTTP buffers!");
close(sock);
}
else
{
// Set up the buffer
buffer->inuse = true;
buffer->buffer[0] = '\0';
buffer->length = 0;
buffer->ctx = ctx;
// Now set up the watch on the accept'd file descriptor
mainloop_addwatch(ctx->mainloop, sock, FD_READ, http_newdata, buffer);
}
}
return true;
}
http_context * http_new(uint16_t port, mainloop_t * mainloop, exception_t ** error)
{
if (exception_check(error))
{
LOG(LOG_ERR, "Error already set in function http_new");
return NULL;
}
http_context * ctx = malloc0(sizeof(http_context));
ctx->mainloop = mainloop;
LIST_INIT(&ctx->filters);
exception_t * err = NULL;
ctx->socket = tcp_server(port, &err);
if (err != NULL)
{
if (error != NULL)
*error = err;
else
exception_free(err);
free(ctx);
return NULL;
}
mainloop_addwatch(mainloop, ctx->socket, FD_READ, http_newclient, ctx);
return ctx;
}
void http_adduri(http_context * ctx, const char * uri, http_match match, http_callback cb, void * userdata)
{
http_filter * filt = malloc0(sizeof(http_filter));
filt->uri = uri;
filt->match = match;
filt->callback = cb;
filt->userdata = userdata;
list_add(&ctx->filters, &filt->filter);
}
void http_printf(http_connection * conn, const char * fmt, ...)
{
va_list args;
va_start(args, fmt);
http_vprintf(conn, fmt, args);
va_end(args);
}
void http_vprintf(http_connection * 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(http_connection * conn, const void * buf, size_t len)
{
send(*conn, buf, len, 0);
}
const char * http_getheader(http_context * ctx, const char * name)
{
hashentry_t * entry = hashtable_get(&ctx->headers, name);
if (entry != NULL)
{
http_keyvalue * kv = hashtable_entry(entry, http_keyvalue, hashentry);
return kv->value;
}
return NULL;
}
const char * http_getparam(http_context * ctx, const char * name)
{
hashentry_t * entry = hashtable_get(&ctx->parameters, name);
if (entry != NULL)
{
http_keyvalue * kv = hashtable_entry(entry, http_keyvalue, hashentry);
return kv->value;
}
return NULL;
}
void http_destroy(http_context * ctx)
{
//mainloop_removewatch(ctx->mainloop, ctx->socket, FD_READ);
close(ctx->socket);
free(ctx);
}
void module_preact()
{
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!!");
}
}