2 * Copyright (c) 2020 Omar Polo <op@omarpolo.com>
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.
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.
25 * => gemini://tanso.net/rfc/rfc3986.txt
31 * pct-encoded "%" HEXDIG HEXDIG
33 * reserved = gen-delims / sub-delimis
34 * gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
35 * sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
36 * / "*" / "+" / "," / ";" / "="
38 * unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
40 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
42 * hier-part = "//" authority path-abempty
47 * scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
49 * authority = [ userinfo "@" ] host [ ":" port ]
51 * (note that userinfo isn't used for Gemini URL)
53 * host = IP-literal / IPv4address / reg-name
54 * reg-name = *( unreserved / pct-encoded / sub-delims )
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
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
71 * segment-nz = 1*pchar
72 * segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" )
73 * pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
75 * query = *( pchar / "/" / "?" )
77 * fragment = *( pchar / "/" / "?" )
83 * foo://example.com:8042/over/there?name=ferret#nose
84 * \_/ \______________/\_________/ \_________/ \__/
86 * scheme authority path query fragment
96 #define CONT_BYTE(b) ((b & 0xC0) == 0x80)
98 /* XXX: these macros will expand multiple times their argument */
100 #define UNRESERVED(p) \
107 #define SUB_DELIMITERS(p) \
120 /* NOTE: the increments are one less what they should be, because the
121 * caller will add one byte after we return. */
123 valid_multibyte_utf8(struct parser *p)
131 if ((s & 0xE0) == 0xC0) {
132 if (!CONT_BYTE(*(p->uri+1)))
134 c = ((s & 0x1F) << 6) | (*(p->uri+1) & 0x3F);
136 } else if ((s & 0xF0) == 0xE0) {
137 if (!CONT_BYTE(*(p->uri+1)) ||
138 !CONT_BYTE(*(p->uri+2)))
141 | ((*(p->uri+1) & 0x3F) << 6)
142 | ((*(p->uri+2) & 0x3F));
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)))
150 | ((*(p->uri+1) & 0x3F) << 12)
151 | ((*(p->uri+2) & 0x3F) << 6)
152 | ((*(p->uri+3) & 0x3F));
157 return (((0x080 <= c) && (c <= 0x7FF))
158 || (((0x800 <= c) && (c <= 0xFFFF)))
159 || (((0x10000 <= c) && (c <= 0x10FFFF))));
163 parse_pct_encoded(struct parser *p)
168 if (!isxdigit(*(p->uri+1)) || !isxdigit(*(p->uri+2))) {
169 p->err = "illegal percent-encoding";
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";
183 /* ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) "://" */
185 parse_scheme(struct parser *p)
187 p->parsed->schema = p->uri;
189 if (!isalpha(*p->uri)) {
190 p->err = "illegal character in scheme";
195 while (isalnum(*p->uri)
201 if (*p->uri != ':') {
202 p->err = "illegal character in scheme";
207 if (*(++p->uri) != '/' || *(++p->uri) != '/') {
208 p->err = "invalid marker after scheme";
218 parse_port(struct parser *p)
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";
232 if (*p->uri != '/' && *p->uri != '\0') {
233 p->err = "illegal character in port number";
237 p->parsed->port_no = i;
239 if (*p->uri != '\0') {
247 /* TODO: add support for ip-literal and ipv4addr ? */
248 /* *( unreserved / sub-delims / pct-encoded ) */
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))
262 if (*p->uri == ':') {
265 return parse_port(p);
268 if (*p->uri == '/') {
277 p->err = "illegal character in authority section";
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 .. */
285 path_elide_dotdot(char *path, char *i, int incr)
291 for (j = i-2; j != path && *j != '/'; j--)
296 memmove(j, i, strlen(i)+1);
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 "/".
312 path_clean(char *path)
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 */
324 /* 2. eliminate each . path name element */
325 for (i = path; *i; ++i) {
326 if ((i == path || *i == '/') && *(i+1) == '.' &&
328 /* move also the \0 */
329 memmove(i, i+2, strlen(i)-1);
333 if (!strcmp(path, ".") || !strcmp(path, "/.")) {
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))
343 /* 4. eliminate trailing ..*/
344 if ((i = strstr(path, "..")) != NULL)
345 if (!path_elide_dotdot(path, i, 2))
352 parse_query(struct parser *p)
354 p->parsed->query = p->uri;
358 while (UNRESERVED(*p->uri)
359 || SUB_DELIMITERS(*p->uri)
362 || parse_pct_encoded(p)
363 || valid_multibyte_utf8(p))
369 if (*p->uri != '\0' && *p->uri != '#') {
370 p->err = "illegal character in query";
374 if (*p->uri != '\0') {
382 /* don't even bother */
384 parse_fragment(struct parser *p)
386 p->parsed->fragment = p->uri;
390 /* XXX: is it too broad? */
393 parse_path(struct parser *p)
397 p->parsed->path = p->uri;
398 if (*p->uri == '\0') {
399 p->parsed->query = p->parsed->fragment = p->uri;
403 while (UNRESERVED(*p->uri)
404 || SUB_DELIMITERS(*p->uri)
406 || parse_pct_encoded(p)
407 || valid_multibyte_utf8(p))
413 if (*p->uri != '\0' && *p->uri != '?' && *p->uri != '#') {
414 p->err = "illegal character in path";
418 if (*p->uri != '\0') {
424 if (!parse_fragment(p))
427 if (!parse_query(p) || !parse_fragment(p))
431 if (!path_clean(p->parsed->path)) {
432 p->err = "illegal path";
440 parse_uri(char *uri, struct uri *ret, const char **err_ret)
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)) {
464 trim_req_uri(char *uri)
468 if ((i = strstr(uri, "\r\n")) == NULL)