-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute.c
332 lines (289 loc) · 7.88 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
328
329
330
331
332
#include "route.h"
#include "response.h"
int
resp_404(
struct lal_request *request,
struct lal_job *job,
const char *msg
) {
char path[request->pathlen + 1];
snprintf(path, request->pathlen + 1, request->path);
//log_error("Responding with HTTP 404: lal_route_request failed: %s: %s", msg, path);
struct lal_response *resp = lal_create_response("404 Not found");
lal_append_to_entries(resp->headers, "Content-Type", "text/plain; charset=utf-8");
lal_append_to_entries(resp->headers, "Server", "lal");
lal_append_to_body(resp->body, msg);
lal_append_to_body(resp->body, ": ");
lal_append_to_body(resp->body, path);
long long len;
char *response = lal_serialize_response(resp, &len);
send(job->socket, response, len, 0);
free(response);
lal_destroy_response(resp);
if (~request->method)
lal_destroy_request(request);
return EXIT_FAILURE;
}
int
lal_route_request (void *arg)
{
LAL_HEADER_ERROR header_error;
struct lal_job *job = arg;
char *header = NULL;
struct lal_request *request;
struct lal_route *route, *routes = (struct lal_route *)job->extra;
if ((header_error = lal_parse_header(job->socket, &header))) {
log_warn(
"Couldn't parse header: (%li) %s.",
job->hitcount,
header_error == RECV_FAILED
? strerror(errno)
: lal_header_error_msg(header_error)
);
struct lal_request q = {
.path = "(null)",
.pathlen = 6,
.method = -1
};
return resp_404(&q, job, lal_header_error_msg(header_error));
}
request = lal_create_request(header);
if (request == NULL) {
log_error(
"`request` was NULL: socket: %i; header: \"%s\"",
job->socket, header
);
return EXIT_FAILURE;
}
if (!~request->method) {
log_error("route_request failed: method %s not implemented",
lal_method_to_string(request->method));
lal_destroy_request(request);
return EXIT_FAILURE;
}
route = lal_get_route(routes, request);
if (route) {
request->extra = route->extra;
(void) route->handler(job->socket, request);
}
else {
return resp_404(request, job, "Thank you for my life");
}
lal_destroy_request(request);
return EXIT_SUCCESS;
}
struct lal_route *
lal_init_routes()
{
struct lal_route r = {
.path = NULL,
.pathlen = 0,
.method = 0,
.handler = NULL,
.next = NULL
}, *routes = (struct lal_route *) malloc(
sizeof(r)
);
memcpy(routes, &r, sizeof(r));
return routes;
}
void
print_route(const struct lal_route *route)
{
if (!route->path) {
log_error(
"Route for method \"%s\" registered without a `path`",
lal_method_to_string(route->method)
);
return;
}
char path[route->pathlen + 1];
snprintf(path, route->pathlen + 1, route->path);
log_info(
"%s, %s, 0x%02x",
path, lal_method_to_string(route->method), (void *)route->handler
);
}
void
lal_register_route (
struct lal_route *routes,
const enum lal_http_method method,
const char *path,
int(*handler)(int, struct lal_request *),
void *extra
) {
struct lal_route *route = routes;
size_t pathlen = 0;
char *path_copy;
if (!handler) {
fprintf(stderr,
"lal_register_route failed: No handler specified\n");
}
while (route->next)
route = route->next;
if (route->handler) {
route->next = (struct lal_route *)malloc(sizeof(struct lal_route));
route = route->next;
}
if (path) {
pathlen = strlen(path);
path_copy = (char *) malloc(
pathlen * sizeof(char)
);
memcpy((void *)path_copy, path, pathlen);
}
else {
path_copy = NULL;
pathlen = 0;
}
struct lal_route r = {
.method = method,
.handler = handler,
.pathlen = pathlen,
.path = path_copy,
.next = NULL,
.extra = extra
};
memcpy(route, &r, sizeof(r));
log_info(
//"Route registered: %s, %s, 0x%x",
"Route registered: %s, %s, handler: %p, extra: %p",
path, lal_method_to_string(route->method), (void *)route->handler, route->extra
);
}
bool
methods_match(
const struct lal_request *request,
const struct lal_route *route
) {
return route->method == ANY || request->method == route->method;
}
bool path_exhausted(const char *pos, const char *path, size_t len)
{ return pos - path == len; }
struct lal_route *
lal_get_route (struct lal_route *routes, struct lal_request *request)
{
struct lal_route *route = routes;
while (route) {
if (!methods_match(request, route))
goto next_route;
if (route->path == NULL)
return route;
const char *rpos = route->path, *qpos = request->path;
bool route_exhausted = false;
bool request_exhausted = false;
compare:
while (*rpos++ == *qpos++) {
route_exhausted = path_exhausted(rpos, route->path, route->pathlen);
request_exhausted = path_exhausted(qpos, request->path, request->pathlen);
if (route_exhausted && request_exhausted)
return route;
else if (route_exhausted || request_exhausted) {
if (route_exhausted && *qpos == '?')
return route;
else goto next_route;
}
}
/*
* It's at this point where the two paths diverge. They both have length
* remaining, but they no longer match. The goal now is to find out whether
* we've encountered a route argument (`:`) or not; if so, fast-forward to the
* next `'/'` in both `rpos` and `qpos`. If either one is exhausted before reaching
* a `'/'`, `goto next_route`.
* */
rpos--; qpos--;
if (*qpos == '?') goto next_route;
if (*rpos == ':') {
while (!path_exhausted(rpos, route->path, route->pathlen) && *rpos != '/')
rpos++;
while (!path_exhausted(qpos, request->path, request->pathlen) && *qpos != '/')
qpos++;
route_exhausted = path_exhausted(rpos, route->path, route->pathlen);
request_exhausted = path_exhausted(qpos, request->path, request->pathlen);
if (route_exhausted && request_exhausted)
return route;
else if (*rpos == '/' && *qpos == '/')
goto compare;
else if (*rpos == '/' || *qpos == '/') {
if (request->pathlen - (qpos - request->path) > 1)
goto next_route;
else return route;
}
else goto next_route;
}
next_route:
route = route->next;
}
return NULL;
}
//struct lal_route *
//lal_get_route (struct lal_route *routes, struct lal_request *request)
//{
// struct lal_route *route = routes;
// const char *route_pos, *request_pos;
//
// while (route) {
// if (!route->path) {
// if (route->method == request->method ||
// route->method == ANY)
// return route;
// else return NULL;
// }
// route_pos = route->path;
// request_pos = request->path;
//compare:
// while (*route_pos++ == *request_pos++) {
// int traversed = request_pos - request->path;
// if (traversed == request->pathlen &&
// route_pos - route->path == route->pathlen) {
// if (route->method == request->method ||
// route->method == ANY) {
// return route;
// }
// else break;
// }
// }
//
// if (*request_pos == '?' &&
// (route->method == request->method ||
// route->method == ANY)) {
// return route;
// }
//
//printf("*route_pos: %c\n", *route_pos);
//
// if (*route_pos == ':') {
//write(1, request->path, request->pathlen);
//puts("");
//write(1, route->path, route->pathlen);
//puts("");
// int contains_param = 0;
// while (*route_pos != '/' && *route_pos != '\0')
// route_pos++;
// while (*request_pos != '/' &&
// request_pos - request->path < request->pathlen) {
// contains_param = 1;
// request_pos++;
// }
// if (contains_param && *route_pos == *request_pos)
// goto compare;
// else return route;
// }
// route = route->next;
// }
// return NULL;
//}
void
lal_destroy_routes (struct lal_route *routes)
{
struct lal_route *prev;
int i = 0;
while (routes) {
free((void *)routes->path);
prev = routes;
routes = routes->next;
free(prev);
i++;
}
fprintf(stderr, "destroyed %i routes\n", i);
}