Blob


1 /*
2 * Copyright (c) 2021 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 /*
18 * This is mostly copied from gmid' iri.c, with minor changes. Bugs
19 * fixed here should be ported to gmid and vice-versa.
20 */
22 #include <config.h>
23 #include <url.h>
25 #include <ctype.h>
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <string.h>
30 struct shallow_url {
31 char *scheme;
32 char *host;
33 char *port;
34 uint16_t port_no;
35 char *path;
36 char *query;
37 char *fragment;
38 };
40 struct parser {
41 char buf[GEMINI_URL_LEN+1];
42 char *iri;
43 struct shallow_url *parsed;
44 const char *err;
45 };
47 static inline int
48 unreserved(int p)
49 {
50 return isalnum(p)
51 || p == '-'
52 || p == '.'
53 || p == '_'
54 || p == '~';
55 }
57 static inline int
58 sub_delimiters(int p)
59 {
60 return p == '!'
61 || p == '$'
62 || p == '&'
63 || p == '\''
64 || p == '('
65 || p == ')'
66 || p == '*'
67 || p == '+'
68 || p == ','
69 || p == ';'
70 || p == '=';
71 }
73 static int
74 valid_pct_enc_string(char *s)
75 {
76 if (*s != '%')
77 return 1;
79 if (!isxdigit(s[1]) || !isxdigit(s[2]))
80 return 0;
82 if (s[1] == '0' && s[2] == '0')
83 return 0;
85 return 1;
86 }
88 static int
89 valid_pct_encoded(struct parser *p)
90 {
91 if (p->iri[0] != '%')
92 return 0;
94 if (!valid_pct_enc_string(p->iri)) {
95 p->err = "illegal percent-encoding";
96 return 0;
97 }
99 p->iri += 2;
100 return 1;
103 static void
104 pct_decode(char *s)
106 sscanf(s+1, "%2hhx", s);
107 memmove(s+1, s+3, strlen(s+3)+1);
110 static int
111 parse_pct_encoded(struct parser *p)
113 if (p->iri[0] != '%')
114 return 0;
116 if (!valid_pct_enc_string(p->iri)) {
117 p->err = "illegal percent-encoding";
118 return 0;
121 pct_decode(p->iri);
122 if (*p->iri == '\0') {
123 p->err = "illegal percent-encoding";
124 return 0;
127 return 1;
130 /* ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) "://" */
131 static int
132 parse_scheme(struct parser *p)
134 p->parsed->scheme = p->iri;
136 if (!isalpha(*p->iri)) {
137 p->err = "illegal character in scheme";
138 return 0;
141 do {
142 /* normalize the scheme (i.e. lowercase it)
144 * XXX: since we cannot have good things, tolower
145 * behaviour depends on the LC_CTYPE locale. The good
146 * news is that we're sure p->iri points to something
147 * that's in the ASCII range, so tolower can't
148 * mis-behave on some systems due to the locale. */
149 *p->iri = tolower(*p->iri);
150 p->iri++;
151 } while (isalnum(*p->iri)
152 || *p->iri == '+'
153 || *p->iri == '-'
154 || *p->iri == '.');
156 if (*p->iri != ':') {
157 p->err = "illegal character in scheme";
158 return 0;
161 *p->iri = '\0';
162 if (p->iri[1] != '/' || p->iri[2] != '/') {
163 p->err = "invalid marker after scheme";
164 return 0;
167 p->iri += 3;
168 return 1;
171 /* *DIGIT */
172 static int
173 parse_port(struct parser *p)
175 uint32_t i = 0;
177 p->parsed->port = p->iri;
179 for (; isdigit(*p->iri); p->iri++) {
180 i = i * 10 + *p->iri - '0';
181 if (i > UINT16_MAX) {
182 p->err = "port number too large";
183 return 0;
187 if (*p->iri != '/' && *p->iri != '\0') {
188 p->err = "illegal character in port number";
189 return 0;
192 p->parsed->port_no = i;
194 if (*p->iri != '\0') {
195 *p->iri = '\0';
196 p->iri++;
199 return 1;
202 /* TODO: add support for ip-literal and ipv4addr ? */
203 /* *( unreserved / sub-delims / pct-encoded ) */
204 static int
205 parse_authority(struct parser *p)
207 p->parsed->host = p->iri;
209 while (unreserved(*p->iri)
210 || sub_delimiters(*p->iri)
211 || parse_pct_encoded(p)) {
212 /* normalize the host name. */
213 if (*p->iri < 0x7F)
214 *p->iri = tolower(*p->iri);
215 p->iri++;
218 if (p->err != NULL)
219 return 0;
221 if (*p->iri == ':') {
222 *p->iri = '\0';
223 p->iri++;
224 return parse_port(p);
225 } else
226 p->parsed->port_no = 1965;
228 if (*p->iri == '/') {
229 *p->iri = '\0';
230 p->iri++;
231 return 1;
234 if (*p->iri == '\0')
235 return 1;
237 p->err = "illegal character in authority section";
238 return 0;
241 /* Routine for path_clean. Elide the pointed .. with the preceding
242 * element. Return 0 if it's not possible. incr is the length of
243 * the increment, 3 for ../ and 2 for .. */
244 static int
245 path_elide_dotdot(char *path, char *i, int incr)
247 char *j;
249 if (i == path)
250 return 0;
251 for (j = i-2; j != path && *j != '/'; j--)
252 /* noop */ ;
253 if (*j == '/')
254 j++;
255 i += incr;
256 memmove(j, i, strlen(i)+1);
257 return 1;
260 /*
261 * Use an algorithm similar to the one implemented in go' path.Clean:
263 * 1. Replace multiple slashes with a single slash
264 * 2. Eliminate each . path name element
265 * 3. Eliminate each inner .. along with the non-.. element that precedes it
266 * 4. Eliminate trailing .. if possible or error (go would only discard)
268 * Unlike path.Clean, this function return the empty string if the
269 * original path is equivalent to "/".
270 */
271 static int
272 path_clean(char *path)
274 char *i;
276 /* 1. replace multiple slashes with a single one */
277 for (i = path; *i; ++i) {
278 if (*i == '/' && *(i+1) == '/') {
279 memmove(i, i+1, strlen(i)); /* move also the \0 */
280 i--;
284 /* 2. eliminate each . path name element */
285 for (i = path; *i; ++i) {
286 if ((i == path || *i == '/') &&
287 *i != '.' && i[1] == '.' && i[2] == '/') {
288 /* move also the \0 */
289 memmove(i, i+2, strlen(i)-1);
290 i--;
293 if (!strcmp(path, ".") || !strcmp(path, "/.")) {
294 *path = '\0';
295 return 1;
298 /* 3. eliminate each inner .. along with the preceding non-.. */
299 for (i = strstr(path, "../"); i != NULL; i = strstr(path, ".."))
300 if (!path_elide_dotdot(path, i, 3))
301 return 0;
303 /* 4. eliminate trailing ..*/
304 if ((i = strstr(path, "..")) != NULL)
305 if (!path_elide_dotdot(path, i, 2))
306 return 0;
308 return 1;
311 static int
312 parse_query(struct parser *p)
314 p->parsed->query = p->iri;
315 if (*p->iri == '\0')
316 return 1;
318 while (unreserved(*p->iri)
319 || sub_delimiters(*p->iri)
320 || *p->iri == '/'
321 || *p->iri == '?'
322 || *p->iri == ':'
323 || *p->iri == '@'
324 || valid_pct_encoded(p))
325 p->iri++;
327 if (p->err != NULL)
328 return 0;
330 if (*p->iri != '\0' && *p->iri != '#') {
331 p->err = "illegal character in query";
332 return 0;
335 if (*p->iri != '\0') {
336 *p->iri = '\0';
337 p->iri++;
340 return 1;
343 /* don't even bother */
344 static int
345 parse_fragment(struct parser *p)
347 p->parsed->fragment = p->iri;
348 return 1;
351 /* XXX: is it too broad? */
352 /* *(pchar / "/") */
353 static int
354 parse_path(struct parser *p)
356 char c;
358 /* trim initial slashes */
359 while (*p->iri == '/')
360 p->iri++;
362 p->parsed->path = p->iri;
363 if (*p->iri == '\0') {
364 p->parsed->query = p->parsed->fragment = p->iri;
365 return 1;
368 while (unreserved(*p->iri)
369 || sub_delimiters(*p->iri)
370 || *p->iri == '/'
371 || parse_pct_encoded(p))
372 p->iri++;
374 if (p->err != NULL)
375 return 0;
377 if (*p->iri != '\0' && *p->iri != '?' && *p->iri != '#') {
378 p->err = "illegal character in path";
379 return 0;
382 if (*p->iri != '\0') {
383 c = *p->iri;
384 *p->iri = '\0';
385 p->iri++;
387 if (c == '#') {
388 if (!parse_fragment(p))
389 return 0;
390 } else
391 if (!parse_query(p) || !parse_fragment(p))
392 return 0;
395 if (!path_clean(p->parsed->path)) {
396 p->err = "illegal path";
397 return 0;
400 return 1;
403 int
404 url_parse(const char *data, struct url *url, const char **err)
406 struct shallow_url u;
407 struct parser p;
409 memset(url, 0, sizeof(*url));
410 memset(&p, 0, sizeof(p));
411 memset(&u, 0, sizeof(u));
413 strlcpy(p.buf, data, sizeof(p.buf));
414 p.iri = p.buf;
415 p.parsed = &u;
417 if (!parse_scheme(&p) || !parse_authority(&p) || !parse_path(&p)) {
418 *err = p.err;
419 return 0;
422 if (u.scheme != NULL)
423 strlcpy(url->scheme, u.scheme, sizeof(url->scheme));
424 if (u.host != NULL)
425 strlcpy(url->host, u.host, sizeof(url->host));
426 if (u.port != NULL)
427 strlcpy(url->port, u.port, sizeof(url->port));
428 if (u.path != NULL)
429 strlcpy(url->path, u.path, sizeof(url->path));
430 if (u.query != NULL)
431 strlcpy(url->query, u.query, sizeof(url->query));
432 if (u.fragment != NULL)
433 strlcpy(url->fragment, u.fragment, sizeof(url->fragment));
435 return 1;