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 static inline int
91 unreserved(int p)
92 {
93 return isalnum(p)
94 || p == '-'
95 || p == '.'
96 || p == '_'
97 || p == '~';
98 }
100 static inline int
101 sub_delimiters(int p)
103 return p == '!'
104 || p == '$'
105 || p == '&'
106 || p == '\''
107 || p == '('
108 || p == ')'
109 || p == '*'
110 || p == '+'
111 || p == ','
112 || p == ';'
113 || p == '=';
116 static int
117 parse_pct_encoded(struct parser *p)
119 if (*p->uri != '%')
120 return 0;
122 if (!isxdigit(*(p->uri+1)) || !isxdigit(*(p->uri+2))) {
123 p->err = "illegal percent-encoding";
124 return 0;
127 sscanf(p->uri+1, "%2hhx", p->uri);
128 memmove(p->uri+1, p->uri+3, strlen(p->uri+3)+1);
129 if (*p->uri == '\0') {
130 p->err = "illegal percent-encoding";
131 return 0;
134 return 1;
137 /* ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) "://" */
138 static int
139 parse_scheme(struct parser *p)
141 p->parsed->schema = p->uri;
143 if (!isalpha(*p->uri)) {
144 p->err = "illegal character in scheme";
145 return 0;
148 p->uri++;
149 while (isalnum(*p->uri)
150 || *p->uri == '+'
151 || *p->uri == '-'
152 || *p->uri == '.')
153 p->uri++;
155 if (*p->uri != ':') {
156 p->err = "illegal character in scheme";
157 return 0;
160 *p->uri = '\0';
161 if (*(++p->uri) != '/' || *(++p->uri) != '/') {
162 p->err = "invalid marker after scheme";
163 return 0;
166 p->uri++;
167 return 1;
170 /* *DIGIT */
171 static int
172 parse_port(struct parser *p)
174 uint32_t i = 0;
176 p->parsed->port = p->uri;
178 for (; isdigit(*p->uri); p->uri++) {
179 i = i * 10 + *p->uri - '0';
180 if (i > UINT16_MAX) {
181 p->err = "port number too large";
182 return 0;
186 if (*p->uri != '/' && *p->uri != '\0') {
187 p->err = "illegal character in port number";
188 return 0;
191 p->parsed->port_no = i;
193 if (*p->uri != '\0') {
194 *p->uri = '\0';
195 p->uri++;
198 return 1;
201 /* TODO: add support for ip-literal and ipv4addr ? */
202 /* *( unreserved / sub-delims / pct-encoded ) */
203 static int
204 parse_authority(struct parser *p)
206 p->parsed->host = p->uri;
208 while (unreserved(*p->uri)
209 || sub_delimiters(*p->uri)
210 || parse_pct_encoded(p))
211 p->uri++;
213 if (p->err != NULL)
214 return 0;
216 if (*p->uri == ':') {
217 *p->uri = '\0';
218 p->uri++;
219 return parse_port(p);
222 if (*p->uri == '/') {
223 *p->uri = '\0';
224 p->uri++;
225 return 1;
228 if (*p->uri == '\0')
229 return 1;
231 p->err = "illegal character in authority section";
232 return 0;
235 /* Routine for path_clean. Elide the pointed .. with the preceding
236 * element. Return 0 if it's not possible. incr is the length of
237 * the increment, 3 for ../ and 2 for .. */
238 static int
239 path_elide_dotdot(char *path, char *i, int incr)
241 char *j;
243 if (i == path)
244 return 0;
245 for (j = i-2; j != path && *j != '/'; j--)
246 /* noop */ ;
247 if (*j == '/')
248 j++;
249 i += incr;
250 memmove(j, i, strlen(i)+1);
251 return 1;
254 /*
255 * Use an algorithm similar to the one implemented in go' path.Clean:
257 * 1. Replace multiple slashes with a single slash
258 * 2. Eliminate each . path name element
259 * 3. Eliminate each inner .. along with the non-.. element that precedes it
260 * 4. Eliminate trailing .. if possible or error (go would only discard)
262 * Unlike path.Clean, this function return the empty string if the
263 * original path is equivalent to "/".
264 */
265 static int
266 path_clean(char *path)
268 char *i;
270 /* 1. replace multiple slashes with a single one */
271 for (i = path; *i; ++i) {
272 if (*i == '/' && *(i+1) == '/') {
273 memmove(i, i+1, strlen(i)); /* move also the \0 */
274 i--;
278 /* 2. eliminate each . path name element */
279 for (i = path; *i; ++i) {
280 if ((i == path || *i == '/') && *(i+1) == '.' &&
281 *(i+2) == '/') {
282 /* move also the \0 */
283 memmove(i, i+2, strlen(i)-1);
284 i--;
287 if (!strcmp(path, ".") || !strcmp(path, "/.")) {
288 *path = '\0';
289 return 1;
292 /* 3. eliminate each inner .. along with the preceding non-.. */
293 for (i = strstr(path, "../"); i != NULL; i = strstr(path, ".."))
294 if (!path_elide_dotdot(path, i, 3))
295 return 0;
297 /* 4. eliminate trailing ..*/
298 if ((i = strstr(path, "..")) != NULL)
299 if (!path_elide_dotdot(path, i, 2))
300 return 0;
302 return 1;
305 static int
306 parse_query(struct parser *p)
308 p->parsed->query = p->uri;
309 if (*p->uri == '\0')
310 return 1;
312 while (unreserved(*p->uri)
313 || sub_delimiters(*p->uri)
314 || *p->uri == '/'
315 || *p->uri == '?'
316 || parse_pct_encoded(p)
317 || valid_multibyte_utf8(p))
318 p->uri++;
320 if (p->err != NULL)
321 return 0;
323 if (*p->uri != '\0' && *p->uri != '#') {
324 p->err = "illegal character in query";
325 return 0;
328 if (*p->uri != '\0') {
329 *p->uri = '\0';
330 p->uri++;
333 return 1;
336 /* don't even bother */
337 static int
338 parse_fragment(struct parser *p)
340 p->parsed->fragment = p->uri;
341 return 1;
344 /* XXX: is it too broad? */
345 /* *(pchar / "/") */
346 static int
347 parse_path(struct parser *p)
349 char c;
351 p->parsed->path = p->uri;
352 if (*p->uri == '\0') {
353 p->parsed->query = p->parsed->fragment = p->uri;
354 return 1;
357 while (unreserved(*p->uri)
358 || sub_delimiters(*p->uri)
359 || *p->uri == '/'
360 || parse_pct_encoded(p)
361 || valid_multibyte_utf8(p))
362 p->uri++;
364 if (p->err != NULL)
365 return 0;
367 if (*p->uri != '\0' && *p->uri != '?' && *p->uri != '#') {
368 p->err = "illegal character in path";
369 return 0;
372 if (*p->uri != '\0') {
373 c = *p->uri;
374 *p->uri = '\0';
375 p->uri++;
377 if (c == '#') {
378 if (!parse_fragment(p))
379 return 0;
380 } else
381 if (!parse_query(p) || !parse_fragment(p))
382 return 0;
385 if (!path_clean(p->parsed->path)) {
386 p->err = "illegal path";
387 return 0;
390 return 1;
393 int
394 parse_uri(char *uri, struct uri *ret, const char **err_ret)
396 char *end;
397 struct parser p = {uri, ret, NULL};
399 bzero(ret, sizeof(*ret));
401 /* initialize optional stuff to the empty string */
402 end = uri + strlen(uri);
403 p.parsed->port = end;
404 p.parsed->path = end;
405 p.parsed->query = end;
406 p.parsed->fragment = end;
408 if (!parse_scheme(&p) || !parse_authority(&p) || !parse_path(&p)) {
409 *err_ret = p.err;
410 return 0;
413 *err_ret = NULL;
414 return 1;
417 int
418 trim_req_uri(char *uri)
420 char *i;
422 if ((i = strstr(uri, "\r\n")) == NULL)
423 return 0;
424 *i = '\0';
425 return 1;