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 /* XXX: these macros will expand multiple times their argument */
98 #define UNRESERVED(p) \
99 (isalnum(p) \
100 || p == '-' \
101 || p == '.' \
102 || p == '_' \
103 || p == '~')
105 #define SUB_DELIMITERS(p) \
106 (p == '!' \
107 || p == '$' \
108 || p == '&' \
109 || p == '\'' \
110 || p == '(' \
111 || p == ')' \
112 || p == '*' \
113 || p == '+' \
114 || p == ',' \
115 || p == ';' \
116 || p == '=')
118 static int
119 parse_pct_encoded(struct parser *p)
121 if (*p->uri != '%')
122 return 0;
124 if (!isxdigit(*(p->uri+1)) || !isxdigit(*(p->uri+2))) {
125 p->err = "illegal percent-encoding";
126 return 0;
129 sscanf(p->uri+1, "%2hhx", p->uri);
130 memmove(p->uri+1, p->uri+3, strlen(p->uri+3)+1);
132 return 1;
135 /* ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) "://" */
136 static int
137 parse_scheme(struct parser *p)
139 p->parsed->schema = p->uri;
141 if (!isalpha(*p->uri)) {
142 p->err = "illegal character in scheme";
143 return 0;
146 p->uri++;
147 while (isalnum(*p->uri)
148 || *p->uri == '+'
149 || *p->uri == '-'
150 || *p->uri == '.')
151 p->uri++;
153 if (*p->uri != ':') {
154 p->err = "illegal character in scheme";
155 return 0;
158 *p->uri = '\0';
159 if (*(++p->uri) != '/' || *(++p->uri) != '/') {
160 p->err = "invalid marker after scheme";
161 return 0;
164 p->uri++;
165 return 1;
168 /* *DIGIT */
169 static int
170 parse_port(struct parser *p)
172 uint32_t i = 0;
174 p->parsed->port = p->uri;
176 for (; isdigit(*p->uri); p->uri++) {
177 i = i * 10 + *p->uri - '0';
178 if (i > UINT16_MAX) {
179 p->err = "port number too large";
180 return 0;
184 if (*p->uri != '/' && *p->uri != '\0') {
185 p->err = "illegal character in port number";
186 return 0;
189 p->parsed->port_no = i;
191 if (*p->uri != '\0') {
192 *p->uri = '\0';
193 p->uri++;
196 return 1;
199 /* TODO: add support for ip-literal and ipv4addr ? */
200 /* *( unreserved / sub-delims / pct-encoded ) */
201 static int
202 parse_authority(struct parser *p)
204 p->parsed->host = p->uri;
206 while (UNRESERVED(*p->uri)
207 || SUB_DELIMITERS(*p->uri)
208 || parse_pct_encoded(p))
209 p->uri++;
211 if (*p->uri == ':') {
212 *p->uri = '\0';
213 p->uri++;
214 return parse_port(p);
217 if (*p->uri == '/') {
218 *p->uri = '\0';
219 p->uri++;
220 return 1;
223 if (*p->uri == '\0')
224 return 1;
226 p->err = "illegal character in authority section";
227 return 0;
230 /* Routine for path_clean. Elide the pointed .. with the preceding
231 * element. Return 0 if it's not possible. incr is the length of
232 * the increment, 3 for ../ and 2 for .. */
233 static int
234 path_elide_dotdot(char *path, char *i, int incr)
236 char *j;
238 if (i == path)
239 return 0;
240 for (j = i-2; j != path && *j != '/'; j--)
241 /* noop */ ;
242 if (*j == '/')
243 j++;
244 i += incr;
245 memmove(j, i, strlen(i)+1);
246 return 1;
249 /*
250 * Use an algorithm similar to the one implemented in go' path.Clean:
252 * 1. Replace multiple slashes with a single slash
253 * 2. Eliminate each . path name element
254 * 3. Eliminate each inner .. along with the non-.. element that precedes it
255 * 4. Eliminate trailing .. if possible or error (go would only discard)
257 * Unlike path.Clean, this function return the empty string if the
258 * original path is equivalent to "/".
259 */
260 static int
261 path_clean(char *path)
263 char *i;
265 /* 1. replace multiple slashes with a single one */
266 for (i = path; *i; ++i) {
267 if (*i == '/' && *(i+1) == '/') {
268 memmove(i, i+1, strlen(i)); /* move also the \0 */
269 i--;
273 /* 2. eliminate each . path name element */
274 for (i = path; *i; ++i) {
275 if ((i == path || *i == '/') && *(i+1) == '.' &&
276 *(i+2) == '/') {
277 /* move also the \0 */
278 memmove(i, i+2, strlen(i)-1);
279 i--;
282 if (!strcmp(path, ".") || !strcmp(path, "/.")) {
283 *path = '\0';
284 return 1;
287 /* 3. eliminate each inner .. along with the preceding non-.. */
288 for (i = strstr(path, "../"); i != NULL; i = strstr(path, ".."))
289 if (!path_elide_dotdot(path, i, 3))
290 return 0;
292 /* 4. eliminate trailing ..*/
293 if ((i = strstr(path, "..")) != NULL)
294 if (!path_elide_dotdot(path, i, 2))
295 return 0;
297 return 1;
300 static int
301 parse_query(struct parser *p)
303 p->parsed->query = p->uri;
304 if (*p->uri == '\0')
305 return 1;
307 while (UNRESERVED(*p->uri)
308 || SUB_DELIMITERS(*p->uri)
309 || *p->uri == '/'
310 || *p->uri == '?'
311 || parse_pct_encoded(p))
312 p->uri++;
314 if (*p->uri != '\0' && *p->uri != '#') {
315 p->err = "illegal character in query";
316 return 0;
319 if (*p->uri != '\0') {
320 *p->uri = '\0';
321 p->uri++;
324 return 1;
327 /* don't even bother */
328 static int
329 parse_fragment(struct parser *p)
331 p->parsed->fragment = p->uri;
332 return 1;
335 /* XXX: is it too broad? */
336 /* *(pchar / "/") */
337 static int
338 parse_path(struct parser *p)
340 char c;
342 p->parsed->path = p->uri;
343 if (*p->uri == '\0') {
344 p->parsed->query = p->parsed->fragment = p->uri;
345 return 1;
348 while (UNRESERVED(*p->uri)
349 || SUB_DELIMITERS(*p->uri)
350 || *p->uri == '/'
351 || parse_pct_encoded(p))
352 p->uri++;
354 if (*p->uri != '\0' && *p->uri != '?' && *p->uri != '#') {
355 p->err = "illegal character in path";
356 return 0;
359 if (*p->uri != '\0') {
360 c = *p->uri;
361 *p->uri = '\0';
362 p->uri++;
364 if (c == '#') {
365 if (!parse_fragment(p))
366 return 0;
367 } else
368 if (!parse_query(p) || !parse_fragment(p))
369 return 0;
372 if (!path_clean(p->parsed->path)) {
373 p->err = "illegal path";
374 return 0;
377 return 1;
380 int
381 parse_uri(char *uri, struct uri *ret, const char **err_ret)
383 char *end;
384 struct parser p = {uri, ret, NULL};
386 bzero(ret, sizeof(*ret));
388 /* initialize optional stuff to the empty string */
389 end = uri + strlen(uri);
390 p.parsed->port = end;
391 p.parsed->path = end;
392 p.parsed->query = end;
393 p.parsed->fragment = end;
395 if (!parse_scheme(&p) || !parse_authority(&p) || !parse_path(&p)) {
396 *err_ret = p.err;
397 return 0;
400 *err_ret = NULL;
401 return 1;
404 int
405 trim_req_uri(char *uri)
407 char *i;
409 if ((i = strstr(uri, "\r\n")) == NULL)
410 return 0;
411 *i = '\0';
412 return 1;