-
Notifications
You must be signed in to change notification settings - Fork 9
/
r3_resty.c
226 lines (170 loc) · 4.33 KB
/
r3_resty.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
#include <netinet/in.h>
#include <arpa/inet.h>
#include "r3_resty.h"
void *
r3_create(int cap)
{
R3Node *tree = r3_tree_create(cap);
return (void *)tree;
}
void
r3_free(void * tree)
{
if (tree == NULL) {
return;
}
r3_tree_free((R3Node *)tree);
}
void *
r3_insert(void *tree, int method, const char *path,
int path_len, void *data, char **errstr)
{
R3Node *r3_tree = (R3Node *)tree;
R3Route *route = r3_tree_insert_routel_ex(r3_tree, method, path, path_len,
data, errstr);
return (void *)route;
}
int
r3_route_set_attr(void *router, const char *host, const char *remote_addr,
int remote_addr_bits)
{
R3Route *r3_router = (R3Route *)router;
if (r3_router->host.base) {
return -1;
}
if (host) {
r3_router->host.len = strlen(host);
char *host_buf = r3_mem_alloc(r3_router->host.len);
memcpy(host_buf, host, r3_router->host.len);
r3_router->host.base = host_buf;
}
if (remote_addr_bits == 0) {
r3_router->remote_addr_v4_bits = 0;
r3_router->remote_addr_v4 = 0;
} else {
r3_router->remote_addr_v4_bits = remote_addr_bits;
r3_router->remote_addr_v4 = inet_network(remote_addr);
}
// fprintf(stderr, "addr: %u bits: %d\n", r3_router->remote_addr_v4,
// r3_router->remote_addr_v4_bits);
return 0;
}
int
r3_route_attribute_free(void *router)
{
R3Route *r3_router = (R3Route *)router;
if (!r3_router->host.base) {
return 0;
}
free((void *)r3_router->host.base);
r3_router->host.base = NULL;
r3_router->host.len = 0;
return 0;
}
int
r3_compile(void *tree, char** errstr)
{
return r3_tree_compile((R3Node *)tree, errstr);
}
void *
r3_match_entry_create(const char *path, int method, const char *host,
const char *remote_addr)
{
match_entry *entry;
entry = match_entry_create(path);
entry->request_method = method;
if (host) {
entry->host.base = host;
entry->host.len = strlen(host);
}
if (remote_addr) {
entry->remote_addr.base = remote_addr;
entry->remote_addr.len = sizeof(remote_addr) - 1;
}
return (void *) entry;
}
void *
r3_match_route(const void *tree, void *entry)
{
R3Route *matched_route;
matched_route = r3_tree_match_route((R3Node *)tree,
(match_entry *)entry);
return (void *)matched_route;
}
void *
r3_match_route_fetch_idx(void *route)
{
R3Route *matched_route = route;
if (matched_route == NULL) {
return NULL;
}
return (void *)matched_route->data;
}
size_t
r3_match_entry_fetch_slugs(void *entry, size_t idx, char *val,
size_t *val_len)
{
match_entry *m_entry = entry;
int i;
if (val == NULL) {
return m_entry->vars.slugs.size;
}
if (idx >= m_entry->vars.slugs.size) {
return -1;
}
i = m_entry->vars.slugs.entries[idx].len;
*val_len = i;
sprintf(val, "%*.*s", i, i, m_entry->vars.slugs.entries[idx].base);
return m_entry->vars.slugs.size;
}
size_t
r3_match_entry_fetch_tokens(void *entry, size_t idx, char *val,
size_t *val_len)
{
match_entry *m_entry = entry;
int i_len;
if (val == NULL) {
return m_entry->vars.tokens.size;
}
if (idx >= m_entry->vars.tokens.size) {
return -1;
}
i_len = m_entry->vars.tokens.entries[idx].len;
*val_len = i_len;
sprintf(val, "%*.*s", i_len, i_len, m_entry->vars.tokens.entries[idx].base);
return m_entry->vars.tokens.size;
}
void
r3_match_entry_free(void *entry)
{
match_entry *r3_entry = (match_entry *)entry;
if (entry == NULL) {
return;
}
match_entry_free(r3_entry);
return;
}
int
is_valid_ipv4(const char *ipv4)
{
struct in_addr addr;
if(ipv4 == NULL) {
return -1;
}
if(inet_pton(AF_INET, ipv4, (void *)&addr) != 1) {
return -1;
}
return 0;
}
int
is_valid_ipv6(const char *ipv6)
{
struct in6_addr addr6;
if(ipv6 == NULL) {
return -1;
}
if(inet_pton(AF_INET6, ipv6, (void *)&addr6) != 1) {
return -1;
}
return 0;
}