This repository has been archived by the owner on Oct 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
uri_parser.rl
437 lines (379 loc) · 9.79 KB
/
uri_parser.rl
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#include "uri_parser.h"
#include <stdlib.h>
#include <assert.h>
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#define URI_DEFINE_SETTER(field) \
void uri_set_##field(uri_t *u, const char *s, ssize_t l) { \
g_assert(u != NULL); \
g_assert(u->chunk != NULL); \
if (s == 0) { u->field = 0; return; } \
if (l == -1) { l = strlen(s); } \
u->field = g_string_chunk_insert_len(u->chunk, s, l); \
}
URI_DEFINE_SETTER(scheme)
URI_DEFINE_SETTER(userinfo)
URI_DEFINE_SETTER(host)
URI_DEFINE_SETTER(path)
URI_DEFINE_SETTER(query)
URI_DEFINE_SETTER(fragment)
%%{
machine uri_parser;
action mark {
mark = fpc;
}
action scheme {
uri_set_scheme(u, mark, fpc-mark);
}
action userinfo {
uri_set_userinfo(u, mark, fpc-mark);
}
action host {
/* host may be called multiple times because
* the parser isn't able to figure out the difference
* between the userinfo and host until after the @ is encountered
*/
uri_set_host(u, mark, fpc-mark);
}
action port {
if (mark) u->port = strtol(mark, NULL, 0);
}
action path {
uri_set_path(u, mark, fpc-mark);
}
action query {
uri_set_query(u, mark, fpc-mark);
}
action fragment {
uri_set_fragment(u, mark, fpc-mark);
}
include uri_grammar "uri_grammar.rl";
main := URI;
}%%
%% write data;
static void uri_zero(uri_t *u) {
/* zero everything *except* chunk */
u->scheme = NULL;
u->userinfo = NULL;
u->host = NULL;
u->path = NULL;
u->query = NULL;
u->fragment = NULL;
u->port = 0;
}
uri_t *uri_new(void) {
uri_t *u = g_slice_new0(uri_t);
g_assert(u != NULL);
u->chunk = g_string_chunk_new(1024);
g_assert(u->chunk != NULL);
return u;
}
int uri_parse(uri_t *u, const char *buf, size_t len, const char **error_at) {
g_assert(u != NULL);
const char *mark = NULL;
const char *p, *pe, *eof;
int cs = 0;
if (error_at != NULL) {
*error_at = NULL;
}
%% write init;
p = buf;
pe = buf+len;
eof = pe;
%% write exec;
if (cs == uri_parser_error && error_at != NULL) {
*error_at = p;
}
return (cs != uri_parser_error && cs >= uri_parser_first_final);
}
void uri_clear(uri_t *u) {
g_assert(u != NULL);
if (u == NULL) return;
if (u->chunk) g_string_chunk_clear(u->chunk);
uri_zero(u);
}
void uri_free(uri_t *u) {
g_assert(u != NULL);
if (u == NULL) return;
if (u->chunk) g_string_chunk_free(u->chunk);
u->chunk = NULL;
g_slice_free(uri_t, u);
}
%%{
# simple machine to normalize percent encoding in uris
machine normalize_pct_encoded;
action pct_unicode_encoded {
char *d1 = (char *)(fpc-3);
char *d2 = (char *)(fpc-2);
char *d3 = (char *)(fpc-1);
char *d4 = (char *)fpc;
/* convert hex digits to upper case */
*d1 = toupper(*d1);
*d2 = toupper(*d2);
*d3 = toupper(*d3);
*d4 = toupper(*d4);
}
action pct_encoded {
char *d1 = (char *)(fpc-1);
char *d2 = (char *)fpc;
/* convert hex digits to upper case */
*d1 = toupper(*d1);
*d2 = toupper(*d2);
/* convert hex to character */
char c = 0;
for (int i=0; i<2; i++) {
if (d1[i] < 57) {
c += (d1[i]-48)*(1<<(4*(1-i)));
} else {
c += (d1[i]-55)*(1<<(4*(1-i)));
}
}
/* unescape characters that shouldn't be escaped */
if (
((c >= 0x41 && c <= 0x5A) || (c >= 0x61 && c <= 0x7A)) /* alpha */
|| (c >= 0x30 && c <= 0x39) /* digit */
|| (c == 0x2D) /* hyphen */
|| (c == 0x2E) /* period */
|| (c == 0x5F) /* underscore */
|| (c == 0x7E) /* tilde */
)
{
/* replace encoded value with character */
*(d1-1) = c;
memmove(d1, d2+1, pe-(d2+1));
/* adjust internal state to match the buffer after memmove */
pe = pe-2;
/* add null terminator */
*((char *)pe) = 0;
fexec fpc-2;
}
}
pct_noencode = ("%" ^xdigit?);
pct_unicode_encoded = ("%u" xdigit xdigit xdigit xdigit) @pct_unicode_encoded;
pct_encoded = ("%" xdigit xdigit) @pct_encoded;
main := (^"%"+ | pct_unicode_encoded | pct_noencode | pct_encoded)*;
}%%
%% write data;
static void normalize_pct_encoded(const char *buf) {
const char *p, *pe;
int cs = 0;
%% write init;
p = buf;
pe = buf+strlen(buf);
%% write exec;
assert (cs != normalize_pct_encoded_error);
}
static void remove_dot_segments(uri_t *u) {
if (u->path == 0) return;
size_t plen = strlen(u->path);
switch (plen) {
case 0:
uri_set_path(u, "/", 1);
return;
case 1:
return;
}
size_t segs_size = plen * sizeof(char *);
unsigned int segm = 0;
char *p = 0;
if (segs_size) {
char **segs = malloc(segs_size);
char *segb = u->path;
/* split path on / into a list of segments */
for (p = u->path; *p != 0; p++) {
if (*p == '/' && p != u->path) {
/* ignore empty segment. for example: // */
if (segb+1 != p) {
*p = 0;
/* put the pointer to the segment in segments array */
segs[segm] = segb+1;
segm++;
}
/* move begin pointer forward. reset end pointer to 0 */
segb = p;
}
}
/* final segment */
if (segb+1 < u->path + plen) {
segs[segm] = segb+1;
segm++;
} else if (segb+1 == u->path + plen) {
segs[segm] = segb;
segm++;
}
for (unsigned int segi = 0; segi < segm; segi++) {
if (g_strcmp0(".", segs[segi]) == 0) {
segs[segi] = 0;
} else if (g_strcmp0("..", segs[segi]) == 0) {
segs[segi] = 0;
unsigned int segx = segi;
while (segx >= 1 && segs[segx] == 0) {
segx--;
}
segs[segx] = 0;
}
}
/* reassemble path from segment pointers */
p = u->path;
for (unsigned int segi = 0; segi < segm; segi++) {
char *pn = segs[segi];
if (pn == 0) continue;
*p = '/'; p++;
if (*pn == '/') continue;
while (*pn) {
*p = *pn;
p++;
pn++;
}
}
if (segm && segs[segm-1] == 0) {
*p = '/'; p++;
}
free(segs);
}
if (segm == 0 && p) {
*p = '/'; p++;
}
if (p) *p = 0;
}
void uri_normalize(uri_t *u) {
g_assert(u != NULL);
for (char *p = u->scheme; *p != 0; p++) {
*p = tolower(*p);
}
if (u->userinfo) {
normalize_pct_encoded(u->userinfo);
}
for (char *p = u->host; *p != 0; p++) {
*p = tolower(*p);
}
normalize_pct_encoded(u->host);
normalize_pct_encoded(u->path);
remove_dot_segments(u);
if (u->query) {
normalize_pct_encoded(u->query);
}
if (u->fragment) {
normalize_pct_encoded(u->fragment);
}
}
static char *_uri_compose(uri_t *u, int path_only) {
GString *s = g_string_sized_new(1024);
if (!path_only) {
if (u->scheme) {
g_string_append_printf(s, "%s:", u->scheme);
}
/* authority */
if (u->userinfo || u->host) {
g_string_append(s, "//");
}
if (u->userinfo) {
g_string_append_printf(s, "%s@", u->userinfo);
}
if (u->host) {
g_string_append(s, u->host);
if (u->port) {
g_string_append_printf(s, ":%u", u->port);
}
}
}
if (u->path) {
if (!g_str_has_prefix(u->path, "/")) {
g_string_append(s, "/");
}
g_string_append(s, u->path);
}
if (u->query) {
g_string_append(s, u->query);
}
if (u->fragment) {
g_string_append(s, u->fragment);
}
char *result = s->str;
g_string_free(s, FALSE);
return result;
}
char *uri_compose(uri_t *u) {
g_assert(u != NULL);
return _uri_compose(u, 0);
}
char *uri_compose_partial(uri_t *u) {
g_assert(u != NULL);
return _uri_compose(u, 1);
}
static void merge_path(uri_t *base, uri_t *r, uri_t *t) {
if (g_strcmp0(base->path, "") == 0 ||
g_strcmp0(base->path, "/") == 0 ||
base->path == NULL)
{
GString *s = g_string_sized_new(strlen(r->path) + 2);
g_string_printf(s, "/%s", r->path);
uri_set_path(t, s->str, s->len);
g_string_free(s, TRUE);
} else if (r->path) {
size_t len = strlen(base->path);
/* NOTE: skip the last character because the path might end in /
* but we need to skip the entire last path *segment*
*/
char *last_slash = &base->path[len - 1];
while (last_slash > base->path && *last_slash != '/') {
last_slash--;
}
GString *s = g_string_new_len(base->path, (last_slash - base->path)+1);
g_string_append(s, r->path);
uri_set_path(t, s->str, s->len);
g_string_free(s, TRUE);
} else if (base->path) {
uri_set_path(t, base->path, -1);
}
}
void uri_transform(uri_t *base, uri_t *r, uri_t *t) {
g_assert(base != NULL);
g_assert(r != NULL);
g_assert(t != NULL);
uri_clear(t);
if (r->scheme) {
uri_set_scheme(t, r->scheme, -1);
uri_set_userinfo(t, r->userinfo, -1);
uri_set_host(t, r->host, -1);
t->port = r->port;
uri_set_path(t, r->path, -1);
remove_dot_segments(t);
uri_set_query(t, r->query, -1);
} else {
/* authority */
if (r->userinfo || r->host) {
uri_set_userinfo(t, r->userinfo, -1);
uri_set_host(t, r->host, -1);
t->port = r->port;
uri_set_path(t, r->path, -1);
remove_dot_segments(t);
uri_set_query(t, r->query, -1);
} else {
if (r->path == 0 || g_strcmp0(r->path, "") == 0) {
uri_set_path(t, base->path, -1);
if (r->query) {
uri_set_query(t, r->query, -1);
} else if (base->query) {
uri_set_query(t, base->query, -1);
}
} else {
if (r->path && g_str_has_prefix(r->path, "/")) {
uri_set_path(t, r->path, -1);
remove_dot_segments(t);
} else {
merge_path(base, r, t);
remove_dot_segments(t);
}
uri_set_query(t, r->query, -1);
}
/* authority */
uri_set_userinfo(t, base->userinfo, -1);
uri_set_host(t, base->host, -1);
t->port = base->port;
}
uri_set_scheme(t, base->scheme, -1);
}
uri_set_fragment(t, r->fragment, -1);
}