Blob


1 /*
2 * Copyright (c) 2020 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <ctype.h>
18 #include <string.h>
20 #include "gmid.h"
22 /*
23 * Notes from RFC3986
24 *
25 * => gemini://tanso.net/rfc/rfc3986.txt
26 *
27 *
28 * ABNF
29 * ====
30 *
31 * pct-encoded "%" HEXDIG HEXDIG
32 *
33 * reserved = gen-delims / sub-delimis
34 * gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
35 * sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
36 * / "*" / "+" / "," / ";" / "="
37 *
38 * unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
39 *
40 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
41 *
42 * hier-part = "//" authority path-abempty
43 * / path-absolute
44 * / path-rootless
45 * / path-empty
46 *
47 * scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
48 *
49 * authority = [ userinfo "@" ] host [ ":" port ]
50 *
51 * (note that userinfo isn't used for Gemini URL)
52 *
53 * host = IP-literal / IPv4address / reg-name
54 * reg-name = *( unreserved / pct-encoded / sub-delims )
55 *
56 * port = *DIGIT
57 *
58 * path = path-abemty ; begins with "/" or is empty
59 * / path-absolute ; begins with "/" but not "//"
60 * / path-noscheme ; begins with a non-colon segment
61 * / path-rootless ; begins with a segment
62 * / path-empty ; zero characters
63 *
64 * path-abemty = *( "/" segment )
65 * path-absolute = "/" [ segment-nz *( "/" segment ) ]
66 * path-noscheme = ; not used
67 * path-rootless = ; not used
68 * path-empty = ; not used
69 *
70 * segment = *pchar
71 * segment-nz = 1*pchar
72 * segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" )
73 * pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
74 *
75 * query = *( pchar / "/" / "?" )
76 *
77 * fragment = *( pchar / "/" / "?" )
78 *
79 *
80 * EXAMPLE
81 * =======
82 *
83 * foo://example.com:8042/over/there?name=ferret#nose
84 * \_/ \______________/\_________/ \_________/ \__/
85 * | | | | |
86 * scheme authority path query fragment
87 *
88 */
90 struct parser {
91 char *uri;
92 struct uri *parsed;
93 const char *err;
94 };
96 #define CONT_BYTE(b) ((b & 0xC0) == 0x80)
98 /* XXX: these macros will expand multiple times their argument */
100 #define UNRESERVED(p) \
101 (isalnum(p) \
102 || p == '-' \
103 || p == '.' \
104 || p == '_' \
105 || p == '~')
107 #define SUB_DELIMITERS(p) \
108 (p == '!' \
109 || p == '$' \
110 || p == '&' \
111 || p == '\'' \
112 || p == '(' \
113 || p == ')' \
114 || p == '*' \
115 || p == '+' \
116 || p == ',' \
117 || p == ';' \
118 || p == '=')
120 /* NOTE: the increments are one less what they should be, because the
121 * caller will add one byte after we return. */
122 static int
123 valid_multibyte_utf8(struct parser *p)
125 uint32_t c;
126 uint8_t s;
128 c = 0;
129 s = *p->uri;
131 if ((s & 0xE0) == 0xC0) {
132 if (!CONT_BYTE(*(p->uri+1)))
133 return 0;
134 c = ((s & 0x1F) << 6) | (*(p->uri+1) & 0x3F);
135 p->uri += 1;
136 } else if ((s & 0xF0) == 0xE0) {
137 if (!CONT_BYTE(*(p->uri+1)) ||
138 !CONT_BYTE(*(p->uri+2)))
139 return 0;
140 c = (s & 0x0F) << 12
141 | ((*(p->uri+1) & 0x3F) << 6)
142 | ((*(p->uri+2) & 0x3F));
143 p->uri += 2;
144 } else if ((s & 0xF8) == 0xF0) {
145 if (!CONT_BYTE(*(p->uri+1)) ||
146 !CONT_BYTE(*(p->uri+2)) ||
147 !CONT_BYTE(*(p->uri+3)))
148 return 0;
149 c = (s & 0x07) << 18
150 | ((*(p->uri+1) & 0x3F) << 12)
151 | ((*(p->uri+2) & 0x3F) << 6)
152 | ((*(p->uri+3) & 0x3F));
153 p->uri += 3;
154 } else
155 return 0;
157 return (((0x080 <= c) && (c <= 0x7FF))
158 || (((0x800 <= c) && (c <= 0xFFFF)))
159 || (((0x10000 <= c) && (c <= 0x10FFFF))));
162 static int
163 parse_pct_encoded(struct parser *p)
165 if (*p->uri != '%')
166 return 0;
168 if (!isxdigit(*(p->uri+1)) || !isxdigit(*(p->uri+2))) {
169 p->err = "illegal percent-encoding";
170 return 0;
173 sscanf(p->uri+1, "%2hhx", p->uri);
174 memmove(p->uri+1, p->uri+3, strlen(p->uri+3)+1);
175 if (*p->uri == '\0') {
176 p->err = "illegal percent-encoding";
177 return 0;
180 return 1;
183 /* ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) "://" */
184 static int
185 parse_scheme(struct parser *p)
187 p->parsed->schema = p->uri;
189 if (!isalpha(*p->uri)) {
190 p->err = "illegal character in scheme";
191 return 0;
194 p->uri++;
195 while (isalnum(*p->uri)
196 || *p->uri == '+'
197 || *p->uri == '-'
198 || *p->uri == '.')
199 p->uri++;
201 if (*p->uri != ':') {
202 p->err = "illegal character in scheme";
203 return 0;
206 *p->uri = '\0';
207 if (*(++p->uri) != '/' || *(++p->uri) != '/') {
208 p->err = "invalid marker after scheme";
209 return 0;
212 p->uri++;
213 return 1;
216 /* *DIGIT */
217 static int
218 parse_port(struct parser *p)
220 uint32_t i = 0;
222 p->parsed->port = p->uri;
224 for (; isdigit(*p->uri); p->uri++) {
225 i = i * 10 + *p->uri - '0';
226 if (i > UINT16_MAX) {
227 p->err = "port number too large";
228 return 0;
232 if (*p->uri != '/' && *p->uri != '\0') {
233 p->err = "illegal character in port number";
234 return 0;
237 p->parsed->port_no = i;
239 if (*p->uri != '\0') {
240 *p->uri = '\0';
241 p->uri++;
244 return 1;
247 /* TODO: add support for ip-literal and ipv4addr ? */
248 /* *( unreserved / sub-delims / pct-encoded ) */
249 static int
250 parse_authority(struct parser *p)
252 p->parsed->host = p->uri;
254 while (UNRESERVED(*p->uri)
255 || SUB_DELIMITERS(*p->uri)
256 || parse_pct_encoded(p))
257 p->uri++;
259 if (p->err != NULL)
260 return 0;
262 if (*p->uri == ':') {
263 *p->uri = '\0';
264 p->uri++;
265 return parse_port(p);
268 if (*p->uri == '/') {
269 *p->uri = '\0';
270 p->uri++;
271 return 1;
274 if (*p->uri == '\0')
275 return 1;
277 p->err = "illegal character in authority section";
278 return 0;
281 /* Routine for path_clean. Elide the pointed .. with the preceding
282 * element. Return 0 if it's not possible. incr is the length of
283 * the increment, 3 for ../ and 2 for .. */
284 static int
285 path_elide_dotdot(char *path, char *i, int incr)
287 char *j;
289 if (i == path)
290 return 0;
291 for (j = i-2; j != path && *j != '/'; j--)
292 /* noop */ ;
293 if (*j == '/')
294 j++;
295 i += incr;
296 memmove(j, i, strlen(i)+1);
297 return 1;
300 /*
301 * Use an algorithm similar to the one implemented in go' path.Clean:
303 * 1. Replace multiple slashes with a single slash
304 * 2. Eliminate each . path name element
305 * 3. Eliminate each inner .. along with the non-.. element that precedes it
306 * 4. Eliminate trailing .. if possible or error (go would only discard)
308 * Unlike path.Clean, this function return the empty string if the
309 * original path is equivalent to "/".
310 */
311 static int
312 path_clean(char *path)
314 char *i;
316 /* 1. replace multiple slashes with a single one */
317 for (i = path; *i; ++i) {
318 if (*i == '/' && *(i+1) == '/') {
319 memmove(i, i+1, strlen(i)); /* move also the \0 */
320 i--;
324 /* 2. eliminate each . path name element */
325 for (i = path; *i; ++i) {
326 if ((i == path || *i == '/') && *(i+1) == '.' &&
327 *(i+2) == '/') {
328 /* move also the \0 */
329 memmove(i, i+2, strlen(i)-1);
330 i--;
333 if (!strcmp(path, ".") || !strcmp(path, "/.")) {
334 *path = '\0';
335 return 1;
338 /* 3. eliminate each inner .. along with the preceding non-.. */
339 for (i = strstr(path, "../"); i != NULL; i = strstr(path, ".."))
340 if (!path_elide_dotdot(path, i, 3))
341 return 0;
343 /* 4. eliminate trailing ..*/
344 if ((i = strstr(path, "..")) != NULL)
345 if (!path_elide_dotdot(path, i, 2))
346 return 0;
348 return 1;
351 static int
352 parse_query(struct parser *p)
354 p->parsed->query = p->uri;
355 if (*p->uri == '\0')
356 return 1;
358 while (UNRESERVED(*p->uri)
359 || SUB_DELIMITERS(*p->uri)
360 || *p->uri == '/'
361 || *p->uri == '?'
362 || parse_pct_encoded(p)
363 || valid_multibyte_utf8(p))
364 p->uri++;
366 if (p->err != NULL)
367 return 0;
369 if (*p->uri != '\0' && *p->uri != '#') {
370 p->err = "illegal character in query";
371 return 0;
374 if (*p->uri != '\0') {
375 *p->uri = '\0';
376 p->uri++;
379 return 1;
382 /* don't even bother */
383 static int
384 parse_fragment(struct parser *p)
386 p->parsed->fragment = p->uri;
387 return 1;
390 /* XXX: is it too broad? */
391 /* *(pchar / "/") */
392 static int
393 parse_path(struct parser *p)
395 char c;
397 p->parsed->path = p->uri;
398 if (*p->uri == '\0') {
399 p->parsed->query = p->parsed->fragment = p->uri;
400 return 1;
403 while (UNRESERVED(*p->uri)
404 || SUB_DELIMITERS(*p->uri)
405 || *p->uri == '/'
406 || parse_pct_encoded(p)
407 || valid_multibyte_utf8(p))
408 p->uri++;
410 if (p->err != NULL)
411 return 0;
413 if (*p->uri != '\0' && *p->uri != '?' && *p->uri != '#') {
414 p->err = "illegal character in path";
415 return 0;
418 if (*p->uri != '\0') {
419 c = *p->uri;
420 *p->uri = '\0';
421 p->uri++;
423 if (c == '#') {
424 if (!parse_fragment(p))
425 return 0;
426 } else
427 if (!parse_query(p) || !parse_fragment(p))
428 return 0;
431 if (!path_clean(p->parsed->path)) {
432 p->err = "illegal path";
433 return 0;
436 return 1;
439 int
440 parse_uri(char *uri, struct uri *ret, const char **err_ret)
442 char *end;
443 struct parser p = {uri, ret, NULL};
445 bzero(ret, sizeof(*ret));
447 /* initialize optional stuff to the empty string */
448 end = uri + strlen(uri);
449 p.parsed->port = end;
450 p.parsed->path = end;
451 p.parsed->query = end;
452 p.parsed->fragment = end;
454 if (!parse_scheme(&p) || !parse_authority(&p) || !parse_path(&p)) {
455 *err_ret = p.err;
456 return 0;
459 *err_ret = NULL;
460 return 1;
463 int
464 trim_req_uri(char *uri)
466 char *i;
468 if ((i = strstr(uri, "\r\n")) == NULL)
469 return 0;
470 *i = '\0';
471 return 1;