forked from unbit/uwsgi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routing.c
168 lines (136 loc) · 3.86 KB
/
routing.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
#ifdef UWSGI_ROUTING
#include "uwsgi.h"
extern struct uwsgi_server uwsgi;
int uwsgi_apply_routes(struct wsgi_request *wsgi_req) {
struct uwsgi_route *routes = uwsgi.routes;
if (!routes) return UWSGI_ROUTE_CONTINUE;
if (uwsgi_parse_vars(wsgi_req)) {
return UWSGI_ROUTE_BREAK;
}
while(routes) {
char **subject = (char **) (((char *)(wsgi_req))+routes->subject);
uint16_t *subject_len = (uint16_t *) (((char *)(wsgi_req))+routes->subject_len);
#ifdef UWSGI_DEBUG
uwsgi_log("route subject = %.*s\n", *subject_len, *subject);
#endif
int n = uwsgi_regexp_match_ovec(routes->pattern, routes->pattern_extra, *subject, *subject_len, routes->ovector, routes->ovn);
if (n>= 0) {
int ret = routes->func(wsgi_req, routes);
if (ret != UWSGI_ROUTE_NEXT) {
return ret;
}
}
routes = routes->next;
}
return UWSGI_ROUTE_CONTINUE;
}
int uwsgi_apply_routes_fast(struct wsgi_request *wsgi_req, char *uri, int len) {
struct uwsgi_route *routes = uwsgi.routes;
if (!routes) return UWSGI_ROUTE_CONTINUE;
while(routes) {
int n = uwsgi_regexp_match_ovec(routes->pattern, routes->pattern_extra, uri, len, routes->ovector, routes->ovn);
if (n>= 0) {
int ret = routes->func(wsgi_req, routes);
if (ret != UWSGI_ROUTE_NEXT) {
return ret;
}
}
routes = routes->next;
}
return UWSGI_ROUTE_CONTINUE;
}
void uwsgi_opt_add_route(char *opt, char *value, void *foobar) {
char *route = uwsgi_str(value);
char *space = strchr(route, ' ');
if (!space) {
uwsgi_log("invalid route syntax\n");
exit(1);
}
*space = 0;
struct uwsgi_route *ur = uwsgi.routes;
if (!ur) {
uwsgi.routes = uwsgi_calloc(sizeof(struct uwsgi_route));
ur = uwsgi.routes;
}
else {
while(ur) {
if (!ur->next) {
ur->next = uwsgi_calloc(sizeof(struct uwsgi_route));
ur = ur->next;
break;
}
ur = ur->next;
}
}
if (!strcmp(foobar, "http_host")) {
ur->subject = offsetof(struct wsgi_request, host);
ur->subject_len = offsetof(struct wsgi_request, host_len);
}
else if (!strcmp(foobar, "request_uri")) {
ur->subject = offsetof(struct wsgi_request, uri);
ur->subject_len = offsetof(struct wsgi_request, uri_len);
}
else if (!strcmp(foobar, "query_string")) {
ur->subject = offsetof(struct wsgi_request, query_string);
ur->subject_len = offsetof(struct wsgi_request, query_string_len);
}
else {
ur->subject = offsetof(struct wsgi_request, path_info);
ur->subject_len = offsetof(struct wsgi_request, path_info_len);
}
if (uwsgi_regexp_build(route, &ur->pattern, &ur->pattern_extra)) {
exit(1);
}
ur->ovn = uwsgi_regexp_ovector(ur->pattern, ur->pattern_extra);
if (ur->ovn > 0) {
ur->ovector = uwsgi_calloc(sizeof(int) * (3 * (ur->ovn + 1)) );
}
char *command = space+1;
char *colon = strchr(command, ':');
if (!colon) {
uwsgi_log("invalid route syntax\n");
exit(1);
}
*colon = 0;
struct uwsgi_router *r = uwsgi.routers;
while(r) {
if (!strcmp(r->name, command)) {
if (r->func(ur, colon+1) == 0) {
// apply is_last
struct uwsgi_route *last_ur = ur;
ur = uwsgi.routes;
while(ur) {
if (ur->func == last_ur->func) {
ur->is_last = 0;
}
ur = ur->next;
}
last_ur->is_last = 1;
return;
}
}
r = r->next;
}
uwsgi_log("unable to register route \"%s\"\n", value);
exit(1);
}
struct uwsgi_router *uwsgi_register_router(char *name, int (*func)(struct uwsgi_route *, char *)) {
struct uwsgi_router *ur = uwsgi.routers;
if (!ur) {
uwsgi.routers = uwsgi_calloc(sizeof(struct uwsgi_router));
uwsgi.routers->name = name;
uwsgi.routers->func = func;
return uwsgi.routers;
}
while(ur) {
if (!ur->next) {
ur->next = uwsgi_calloc(sizeof(struct uwsgi_router));
ur->next->name = name;
ur->next->func = func;
return ur->next;
}
ur = ur->next;
}
return NULL;
}
#endif