This repository has been archived by the owner on Oct 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
route_funcs.c
166 lines (148 loc) · 3.35 KB
/
route_funcs.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
/*
* route_funcs.c
*
* Created on: Dec 28, 2014
* Author: dobes
*/
#include "unistd.h"
#include "postgres.h"
#include <fcntl.h>
#include <utils/elog.h>
#include <sys/queue.h>
#include <event2/keyvalq_struct.h>
#include "backend.h"
#include "util.h"
#include "routes.h"
#include "content_types.h"
static int
match_route_uri_pattern(const char *path_input, const char *path_pattern)
{
while(*path_input && *path_pattern)
{
if(*path_pattern == '{')
{
char looking_for;
while(*path_pattern && *path_pattern != '}')
path_pattern++;
if(*path_pattern == '}')
{
path_pattern++;
/* If we reached the end of the string, consider '/' the next separator */
looking_for = *path_pattern;
if(!looking_for)
looking_for='/';
while(*path_input && *path_input != looking_for)
path_input++;
}
}
else
{
if(*path_pattern != *path_input)
return -1;
path_pattern++;
path_input++;
}
}
/* If we did not match the entire input, then the route doesn't match */
if(*path_input || *path_pattern)
return -1;
return 1;
}
void
parse_route_uri_pattern(const char *path_input, const char *path_pattern, struct evkeyvalq *matchdict)
{
char keybuf[32];
char *kp;
char valbuf[512];
char *vp;
char looking_for;
char *val_decoded;
while(*path_input && *path_pattern)
{
if(*path_pattern == '{')
{
path_pattern++;
kp = keybuf;
while(*path_pattern && *path_pattern != '}')
{
int len = (kp - keybuf);
if(len+1 >= sizeof keybuf)
{
*kp = 0;
elog(ERROR, "Path pattern has varname with >= %d characters: %.*s", (int)sizeof keybuf, (int)len, kp);
return;
}
*kp = *path_pattern;
kp++;
path_pattern++;
}
*kp = 0;
if(*path_pattern == '}')
{
path_pattern++;
/* If we reached the end of the string, consider '/' the next separator */
looking_for = *path_pattern;
if(!looking_for)
looking_for='/';
vp = valbuf;
while(*path_input && *path_input != looking_for)
{
int len = (vp - valbuf);
if(len+1 >= sizeof valbuf)
{
elog(ERROR, "Path URI has component with >= %d characters: %.*s", (int)sizeof valbuf, (int)len, valbuf);
return;
}
*vp = *path_input;
vp++;
path_input++;
}
*vp = 0;
/* Save the value if the key is non-empty */
if(keybuf[0])
{
val_decoded = evhttp_uridecode(valbuf, 1, NULL);
evhttp_add_header(matchdict, keybuf, val_decoded);
free(val_decoded);
}
}
}
else
{
path_pattern++;
path_input++;
}
}
}
int
match_route(struct restgres_route *route, struct restgres_request *req)
{
int score;
/* Check method - req->cmd_type should have just one bit set but route->cmd_type is a mask */
if((req->cmd_type & route->cmd_type) == 0)
return -1;
/* Now check to make sure the path matches the path pattern */
score = match_route_uri_pattern(evhttp_uri_get_path(req->uri), route->pattern);
if(score < 0)
return -1;
/* TODO Check the accept and content-type headers */
return score;
}
struct restgres_route *
find_best_route(struct restgres_request *req)
{
struct restgres_route *best_route=NULL;
int best_score = 0;
int i;
for(i=0; i < num_routes; i++)
{
struct restgres_route *route = &routes[i];
int score = match_route(route, req);
if(score > best_score)
{
best_route = route;
best_score = score;
}
}
return best_route;
}