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 /* XXX: these macros will expand multiple times their argument */
92 #define UNRESERVED(p) \
93 (isalnum(p) \
94 || p == '-' \
95 || p == '.' \
96 || p == '_' \
97 || p == '~')
99 #define SUB_DELIMITERS(p) \
100 (p == '!' \
101 || p == '$' \
102 || p == '&' \
103 || p == '\'' \
104 || p == '(' \
105 || p == ')' \
106 || p == '*' \
107 || p == '+' \
108 || p == ',' \
109 || p == ';' \
110 || p == '=')
112 static int
113 parse_pct_encoded(struct parser *p)
115 if (*p->uri != '%')
116 return 0;
118 if (!isxdigit(*(p->uri+1)) || !isxdigit(*(p->uri+2))) {
119 p->err = "illegal percent-encoding";
120 return 0;
123 sscanf(p->uri+1, "%2hhx", p->uri);
124 memmove(p->uri+1, p->uri+3, strlen(p->uri+3)+1);
125 if (*p->uri == '\0') {
126 p->err = "illegal percent-encoding";
127 return 0;
130 return 1;
133 /* ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) "://" */
134 static int
135 parse_scheme(struct parser *p)
137 p->parsed->schema = p->uri;
139 if (!isalpha(*p->uri)) {
140 p->err = "illegal character in scheme";
141 return 0;
144 p->uri++;
145 while (isalnum(*p->uri)
146 || *p->uri == '+'
147 || *p->uri == '-'
148 || *p->uri == '.')
149 p->uri++;
151 if (*p->uri != ':') {
152 p->err = "illegal character in scheme";
153 return 0;
156 *p->uri = '\0';
157 if (*(++p->uri) != '/' || *(++p->uri) != '/') {
158 p->err = "invalid marker after scheme";
159 return 0;
162 p->uri++;
163 return 1;
166 /* *DIGIT */
167 static int
168 parse_port(struct parser *p)
170 uint32_t i = 0;
172 p->parsed->port = p->uri;
174 for (; isdigit(*p->uri); p->uri++) {
175 i = i * 10 + *p->uri - '0';
176 if (i > UINT16_MAX) {
177 p->err = "port number too large";
178 return 0;
182 if (*p->uri != '/' && *p->uri != '\0') {
183 p->err = "illegal character in port number";
184 return 0;
187 p->parsed->port_no = i;
189 if (*p->uri != '\0') {
190 *p->uri = '\0';
191 p->uri++;
194 return 1;
197 /* TODO: add support for ip-literal and ipv4addr ? */
198 /* *( unreserved / sub-delims / pct-encoded ) */
199 static int
200 parse_authority(struct parser *p)
202 p->parsed->host = p->uri;
204 while (UNRESERVED(*p->uri)
205 || SUB_DELIMITERS(*p->uri)
206 || parse_pct_encoded(p))
207 p->uri++;
209 if (p->err != NULL)
210 return 0;
212 if (*p->uri == ':') {
213 *p->uri = '\0';
214 p->uri++;
215 return parse_port(p);
218 if (*p->uri == '/') {
219 *p->uri = '\0';
220 p->uri++;
221 return 1;
224 if (*p->uri == '\0')
225 return 1;
227 p->err = "illegal character in authority section";
228 return 0;
231 /* Routine for path_clean. Elide the pointed .. with the preceding
232 * element. Return 0 if it's not possible. incr is the length of
233 * the increment, 3 for ../ and 2 for .. */
234 static int
235 path_elide_dotdot(char *path, char *i, int incr)
237 char *j;
239 if (i == path)
240 return 0;
241 for (j = i-2; j != path && *j != '/'; j--)
242 /* noop */ ;
243 if (*j == '/')
244 j++;
245 i += incr;
246 memmove(j, i, strlen(i)+1);
247 return 1;
250 /*
251 * Use an algorithm similar to the one implemented in go' path.Clean:
253 * 1. Replace multiple slashes with a single slash
254 * 2. Eliminate each . path name element
255 * 3. Eliminate each inner .. along with the non-.. element that precedes it
256 * 4. Eliminate trailing .. if possible or error (go would only discard)
258 * Unlike path.Clean, this function return the empty string if the
259 * original path is equivalent to "/".
260 */
261 static int
262 path_clean(char *path)
264 char *i;
266 /* 1. replace multiple slashes with a single one */
267 for (i = path; *i; ++i) {
268 if (*i == '/' && *(i+1) == '/') {
269 memmove(i, i+1, strlen(i)); /* move also the \0 */
270 i--;
274 /* 2. eliminate each . path name element */
275 for (i = path; *i; ++i) {
276 if ((i == path || *i == '/') && *(i+1) == '.' &&
277 *(i+2) == '/') {
278 /* move also the \0 */
279 memmove(i, i+2, strlen(i)-1);
280 i--;
283 if (!strcmp(path, ".") || !strcmp(path, "/.")) {
284 *path = '\0';
285 return 1;
288 /* 3. eliminate each inner .. along with the preceding non-.. */
289 for (i = strstr(path, "../"); i != NULL; i = strstr(path, ".."))
290 if (!path_elide_dotdot(path, i, 3))
291 return 0;
293 /* 4. eliminate trailing ..*/
294 if ((i = strstr(path, "..")) != NULL)
295 if (!path_elide_dotdot(path, i, 2))
296 return 0;
298 return 1;
301 static int
302 parse_query(struct parser *p)
304 p->parsed->query = p->uri;
305 if (*p->uri == '\0')
306 return 1;
308 while (UNRESERVED(*p->uri)
309 || SUB_DELIMITERS(*p->uri)
310 || *p->uri == '/'
311 || *p->uri == '?'
312 || parse_pct_encoded(p)
313 || valid_multibyte_utf8(p))
314 p->uri++;
316 if (p->err != NULL)
317 return 0;
319 if (*p->uri != '\0' && *p->uri != '#') {
320 p->err = "illegal character in query";
321 return 0;
324 if (*p->uri != '\0') {
325 *p->uri = '\0';
326 p->uri++;
329 return 1;
332 /* don't even bother */
333 static int
334 parse_fragment(struct parser *p)
336 p->parsed->fragment = p->uri;
337 return 1;
340 /* XXX: is it too broad? */
341 /* *(pchar / "/") */
342 static int
343 parse_path(struct parser *p)
345 char c;
347 p->parsed->path = p->uri;
348 if (*p->uri == '\0') {
349 p->parsed->query = p->parsed->fragment = p->uri;
350 return 1;
353 while (UNRESERVED(*p->uri)
354 || SUB_DELIMITERS(*p->uri)
355 || *p->uri == '/'
356 || parse_pct_encoded(p)
357 || valid_multibyte_utf8(p))
358 p->uri++;
360 if (p->err != NULL)
361 return 0;
363 if (*p->uri != '\0' && *p->uri != '?' && *p->uri != '#') {
364 p->err = "illegal character in path";
365 return 0;
368 if (*p->uri != '\0') {
369 c = *p->uri;
370 *p->uri = '\0';
371 p->uri++;
373 if (c == '#') {
374 if (!parse_fragment(p))
375 return 0;
376 } else
377 if (!parse_query(p) || !parse_fragment(p))
378 return 0;
381 if (!path_clean(p->parsed->path)) {
382 p->err = "illegal path";
383 return 0;
386 return 1;
389 int
390 parse_uri(char *uri, struct uri *ret, const char **err_ret)
392 char *end;
393 struct parser p = {uri, ret, NULL};
395 bzero(ret, sizeof(*ret));
397 /* initialize optional stuff to the empty string */
398 end = uri + strlen(uri);
399 p.parsed->port = end;
400 p.parsed->path = end;
401 p.parsed->query = end;
402 p.parsed->fragment = end;
404 if (!parse_scheme(&p) || !parse_authority(&p) || !parse_path(&p)) {
405 *err_ret = p.err;
406 return 0;
409 *err_ret = NULL;
410 return 1;
413 int
414 trim_req_uri(char *uri)
416 char *i;
418 if ((i = strstr(uri, "\r\n")) == NULL)
419 return 0;
420 *i = '\0';
421 return 1;