-
Notifications
You must be signed in to change notification settings - Fork 1
/
route.c
327 lines (294 loc) · 9.94 KB
/
route.c
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
#include <dlfcn.h>
#include <setjmp.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <string.h>
#include "pcre.h"
#include "message.h"
#include "plugin.h"
#include "request.h"
#include "route.h"
#include "server.h"
#include "util.h"
/* Used for returning control when we catch segfaults */
jmp_buf context;
int parse_routes(const char *filename, route_t ***routelist, int *numroutes)
{
char *line;
FILE *input;
int status;
int linenum;
int retcode;
route_t **routes;
route_t *route;
void *tmp;
line = malloc(1024);
input = fopen(filename, "r");
if(input == NULL)
{
syslog(LOG_ERR, "%s(): failed to open file '%s' for reading", __func__, filename);
retcode = 1;
goto end;
}
linenum = 0;
routes = NULL;
while(fgets(line, 1024, input))
{
chomp(line);
tmp = realloc(routes, sizeof(route_t*) * (linenum + 1));
if(tmp == NULL)
{
syslog(LOG_ALERT, "%s(): reallocf() failed prepping to parse line #%d", __func__, linenum);
retcode = 2;
goto end1;
}
routes = tmp;
status = parse_routeline(line, &routes[linenum]);
if(status)
{
syslog(LOG_WARNING, "%s(): parse_routeline() returned %d, moving to next line", __func__, status);
//retcode = 3;
//goto end;
continue;
}
route = routes[linenum];
syslog(LOG_DEBUG, "%s(): route->handler: %p", __func__, route->handler);
syslog(LOG_DEBUG, "%s(): route->method: %d", __func__, route->method);
linenum++;
}
if(!feof(input))
{
syslog(LOG_WARNING, "%s(): fgets() encountered an error (not EOF)", __func__);
retcode = 3;
goto end1;
}
*routelist = routes;
*numroutes = linenum;
retcode = 0;
end1:
fclose(input);
end:
free(line);
return retcode;
}
int parse_routeline(char* line, route_t **route)
{
int status, matches;
pcre *test_re, *route_re;
char *pattern;
const char *errmsg;
int erroffset;
void *FUNC;
int outputvec[MAX_OVECS];
char method[16], path[256], handler[256];
int method_code;
route_t *myroute;
/* Build the pattern we use to extract fields from the line */
pattern = "^(\\w+)\\s+(\\S+)\\s+(\\S+)";
test_re = pcre_compile( pattern,
0,
&errmsg,
&erroffset,
NULL);
if(test_re == NULL)
{
syslog(LOG_CRIT, "%s(): PCRE compilation error", __func__);
return 1;
}
// FIXME probable memory leak on this RE; we never free() it
/* Invoke the pattern-match against the line */
status = pcre_exec( test_re,
NULL,
line,
strlen(line),
0,
0,
outputvec,
MAX_OVECS);
/* pcre_exec returns 0 for both non-matches and true errors */
if(status < 0)
{
switch(status)
{
case PCRE_ERROR_NOMATCH:
syslog(LOG_WARNING, "%s(): line '%s' does not conform to regex '%s'; aborting", __func__, line, pattern);
return 2;
default:
syslog(LOG_ERR, "%s(): PCRE matching error %d on line '%s'", __func__, status, line);
return 2;
}
}
matches = status;
/* Extract fields from the line */
status = pcre_copy_substring(line, outputvec, matches, 1, method, 16);
if(status <= 0)
return 3;
status = pcre_copy_substring(line, outputvec, matches, 2, path, 256);
if(status <= 0)
return 3;
status = pcre_copy_substring(line, outputvec, matches, 3, handler, 256);
if(status <= 0)
return 3;
/* Resolve the handler-symbol into a function pointer */
#ifdef RTLD_SELF
/* Darwin's dlsym() searches all shared objects that were linked to the executable,
but NOT those that have been linked with dlopen() */
/* First search linked-in symbols */
FUNC = dlsym(RTLD_SELF, handler);
if(FUNC != NULL)
syslog(LOG_DEBUG, "%s(): Found symbol '%s' in builtins", __func__, handler);
/* Next search among loaded plugins */
for(int i = 0; i < NUMPLUGINS && FUNC == NULL; i++)
{
FUNC = dlsym(PLUGINS[i], handler);
if(FUNC != NULL)
syslog(LOG_DEBUG, "%s(): Found symbol '%s' in plugin #%d'", __func__, handler, i);
}
#else
/* Linux's dlsym() searches all loaded libraries for us */
Dl_info info;
FUNC = dlsym(RTLD_DEFAULT, handler);
if(FUNC != NULL)
{
dladdr(FUNC, &info);
syslog(LOG_DEBUG, "%s(): Found symbol '%s' in module '%s'", __func__, handler, info.dli_fname);
}
#endif
/* Finally, fail if it remains unfound */
if(FUNC == NULL)
{
syslog(LOG_WARNING, "%s(): Failed to find symbol '%s' in builtins or plugins", __func__, handler);
return 4;
}
/* Compile the path component into an RE we can use for matching */
route_re = pcre_compile( path,
0,
&errmsg,
&erroffset,
NULL);
if(route_re == NULL)
{
syslog(LOG_WARNING, "%s(): PCRE compilation failed at offset %d with error '%s' in pattern '%s'", __func__, erroffset, errmsg, path);
return 5;
}
// FIXME call pcre_inspect()
/* Convert the method component into a symbolic integer */
method_code = match_http_method(method);
if(method_code == -1)
{
syslog(LOG_WARNING, "%s(): Unrecognized HTTP method '%s'", __func__, method);
return 6;
}
/* Populate and return the route object */
myroute = calloc(1, sizeof(route_t));
myroute->re = route_re;
myroute->method = method_code;
myroute->handler = FUNC;
*route = myroute;
return 0;
}
int route_request(client_t *c)
{
route_t **routes;
int i;
int outputvec[MAX_OVECS];
int status;
char *splat[MAX_OVECS / 2];
int substring_len;
int splat_len;
message_t *m;
int (*handler)(client_t*, char**, int);
m = c->msg;
syslog(LOG_DEBUG, "%s():...", __func__);
/*** I keep this here, hoping one day to do away with the global...
Thank you http-parser for forcing me to use globals. ***/
routes = c->srv->routes;
/* Find a matching route, matching on the (method, request_path) tuple */
for(i = 0; i < c->srv->numroutes; i++)
{
if(m->method != routes[i]->method)
continue;
status = pcre_exec( routes[i]->re,
NULL,
m->request_path,
m->request_pathlen,
0,
0,
outputvec,
MAX_OVECS);
/* pcre_exec returns 0 for both non-matches and true errors */
if(status < 0)
{
switch(status)
{
case PCRE_ERROR_NOMATCH:
continue;
default:
syslog(LOG_ERR, "%s(): PCRE matching error %d on path '%s'", __func__, status, m->request_path);
return 1;
}
}
/* We have a match, break out of the loop rather than matching additional handlers */
syslog(LOG_DEBUG, "%s(): Found a match!", __func__);
break;
}
/* We may've broken out of the loop without finding a match, in which case we should send a
404 */
if(status < 0)
{
; /* TODO send a 404 here */
return 2; /* FIXME returning error for the time being, there should be a default 404 handler though */
}
/* Otherwise, grab the handler associated with the route */
handler = routes[i]->handler;
/* If we found a match but ran out of outputvectors, pcre_exec will return 0 */
if(status == 0)
{
syslog(LOG_ERR, "%s(): PCRE route-match found, but some matches lost due to not enough output-vectors", __func__);
splat_len = 0;
}
/* Otherwise we should save aside the strings PCRE matched for us */
else
{
splat_len = status;
for(i=0; i < splat_len; i++)
{
substring_len = outputvec[2*i+1] - outputvec[2*i];
splat[i] = calloc(substring_len + 1, sizeof(char));
strncpy(splat[i], &m->request_path[outputvec[2*i]], substring_len);
/*printf("Match %d: '%s'\n", i, splat[i]);
*/
}
}
/* Prepare to catch segfaults thrown by the handler */
struct sigaction sa, old_sa;
memset(&sa, 0, sizeof(sigaction));
sigemptyset(&sa.sa_mask);
sa.sa_sigaction = catch_sigsegv_from_handler;
sa.sa_flags = SA_SIGINFO | SA_RESETHAND;
sigaction(SIGSEGV, &sa, &old_sa);
if(setjmp(context))
{
syslog(LOG_ERR, "%s(): Caught SIGSEGV from handler, if the heap was mangled we may not recover cleanly!", __func__);
terminate_client(c);
}
/* Call the handler associated with the route */
else
{
status = handler(c, splat, splat_len);
syslog(LOG_DEBUG, "%s(): Handler returned %d", __func__, status);
}
/* Restore signal handling for segfaults */
sigaction(SIGSEGV, &old_sa, NULL);
/* Clean-up any matched strings we saved, since they were heap-allocated */
for(i=0; i < splat_len; i++)
free(splat[i]);
return 0;
}
void catch_sigsegv_from_handler(int signal, siginfo_t *si, void *arg)
{
syslog(LOG_DEBUG, "%s(): Caught segfault trying to access address %p", __func__, si->si_addr);
longjmp(context, SIGSEGV);
}