2 5e11c00c 2021-03-02 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 5e11c00c 2021-03-02 op * Permission to use, copy, modify, and distribute this software for any
5 5e11c00c 2021-03-02 op * purpose with or without fee is hereby granted, provided that the above
6 5e11c00c 2021-03-02 op * copyright notice and this permission notice appear in all copies.
8 5e11c00c 2021-03-02 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 5e11c00c 2021-03-02 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 5e11c00c 2021-03-02 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 5e11c00c 2021-03-02 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 5e11c00c 2021-03-02 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 5e11c00c 2021-03-02 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 5e11c00c 2021-03-02 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 5e11c00c 2021-03-02 op * This is mostly copied from gmid' iri.c, with minor changes. Bugs
19 5e11c00c 2021-03-02 op * fixed here should be ported to gmid and vice-versa.
22 5e11c00c 2021-03-02 op #include <config.h>
23 5e11c00c 2021-03-02 op #include <url.h>
25 5e11c00c 2021-03-02 op #include <ctype.h>
26 5e11c00c 2021-03-02 op #include <stdint.h>
27 5e11c00c 2021-03-02 op #include <stdio.h>
28 5e11c00c 2021-03-02 op #include <string.h>
30 5e11c00c 2021-03-02 op struct shallow_url {
34 5e11c00c 2021-03-02 op uint16_t port_no;
37 5e11c00c 2021-03-02 op char *fragment;
40 5e11c00c 2021-03-02 op struct parser {
41 5e11c00c 2021-03-02 op char buf[GEMINI_URL_LEN+1];
43 5e11c00c 2021-03-02 op struct shallow_url *parsed;
44 5e11c00c 2021-03-02 op const char *err;
47 5e11c00c 2021-03-02 op static inline int
48 5e11c00c 2021-03-02 op unreserved(int p)
50 5e11c00c 2021-03-02 op return isalnum(p)
57 5e11c00c 2021-03-02 op static inline int
58 5e11c00c 2021-03-02 op sub_delimiters(int p)
60 5e11c00c 2021-03-02 op return p == '!'
74 5e11c00c 2021-03-02 op valid_pct_enc_string(char *s)
76 5e11c00c 2021-03-02 op if (*s != '%')
79 5e11c00c 2021-03-02 op if (!isxdigit(s[1]) || !isxdigit(s[2]))
82 5e11c00c 2021-03-02 op if (s[1] == '0' && s[2] == '0')
89 5e11c00c 2021-03-02 op valid_pct_encoded(struct parser *p)
91 5e11c00c 2021-03-02 op if (p->iri[0] != '%')
94 5e11c00c 2021-03-02 op if (!valid_pct_enc_string(p->iri)) {
95 5e11c00c 2021-03-02 op p->err = "illegal percent-encoding";
104 5e11c00c 2021-03-02 op pct_decode(char *s)
106 5e11c00c 2021-03-02 op sscanf(s+1, "%2hhx", s);
107 5e11c00c 2021-03-02 op memmove(s+1, s+3, strlen(s+3)+1);
111 5e11c00c 2021-03-02 op parse_pct_encoded(struct parser *p)
113 5e11c00c 2021-03-02 op if (p->iri[0] != '%')
116 5e11c00c 2021-03-02 op if (!valid_pct_enc_string(p->iri)) {
117 5e11c00c 2021-03-02 op p->err = "illegal percent-encoding";
121 5e11c00c 2021-03-02 op pct_decode(p->iri);
122 5e11c00c 2021-03-02 op if (*p->iri == '\0') {
123 5e11c00c 2021-03-02 op p->err = "illegal percent-encoding";
131 ba714ce3 2021-04-01 op * ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) "://"
136 5e11c00c 2021-03-02 op parse_scheme(struct parser *p)
138 5e11c00c 2021-03-02 op p->parsed->scheme = p->iri;
140 ba714ce3 2021-04-01 op if (p->iri[0] == '/' && p->iri[1] == '/') {
141 ba714ce3 2021-04-01 op *p->iri = '\0';
146 5e11c00c 2021-03-02 op if (!isalpha(*p->iri)) {
147 5e11c00c 2021-03-02 op p->err = "illegal character in scheme";
152 5e11c00c 2021-03-02 op /* normalize the scheme (i.e. lowercase it)
154 5e11c00c 2021-03-02 op * XXX: since we cannot have good things, tolower
155 5e11c00c 2021-03-02 op * behaviour depends on the LC_CTYPE locale. The good
156 5e11c00c 2021-03-02 op * news is that we're sure p->iri points to something
157 5e11c00c 2021-03-02 op * that's in the ASCII range, so tolower can't
158 5e11c00c 2021-03-02 op * mis-behave on some systems due to the locale. */
159 5e11c00c 2021-03-02 op *p->iri = tolower(*p->iri);
161 5e11c00c 2021-03-02 op } while (isalnum(*p->iri)
162 5e11c00c 2021-03-02 op || *p->iri == '+'
163 5e11c00c 2021-03-02 op || *p->iri == '-'
164 5e11c00c 2021-03-02 op || *p->iri == '.');
166 5e11c00c 2021-03-02 op if (*p->iri != ':') {
167 5e11c00c 2021-03-02 op p->err = "illegal character in scheme";
171 5e11c00c 2021-03-02 op *p->iri = '\0';
172 5e11c00c 2021-03-02 op if (p->iri[1] != '/' || p->iri[2] != '/') {
173 5e11c00c 2021-03-02 op p->err = "invalid marker after scheme";
183 5e11c00c 2021-03-02 op parse_port(struct parser *p)
185 5e11c00c 2021-03-02 op uint32_t i = 0;
187 5e11c00c 2021-03-02 op p->parsed->port = p->iri;
189 5e11c00c 2021-03-02 op for (; isdigit(*p->iri); p->iri++) {
190 5e11c00c 2021-03-02 op i = i * 10 + *p->iri - '0';
191 5e11c00c 2021-03-02 op if (i > UINT16_MAX) {
192 5e11c00c 2021-03-02 op p->err = "port number too large";
197 5e11c00c 2021-03-02 op if (*p->iri != '/' && *p->iri != '\0') {
198 5e11c00c 2021-03-02 op p->err = "illegal character in port number";
202 5e11c00c 2021-03-02 op p->parsed->port_no = i;
204 5e11c00c 2021-03-02 op if (*p->iri != '\0') {
205 5e11c00c 2021-03-02 op *p->iri = '\0';
212 5e11c00c 2021-03-02 op /* TODO: add support for ip-literal and ipv4addr ? */
213 5e11c00c 2021-03-02 op /* *( unreserved / sub-delims / pct-encoded ) */
215 5e11c00c 2021-03-02 op parse_authority(struct parser *p)
217 5e11c00c 2021-03-02 op p->parsed->host = p->iri;
219 5e11c00c 2021-03-02 op while (unreserved(*p->iri)
220 5e11c00c 2021-03-02 op || sub_delimiters(*p->iri)
221 5e11c00c 2021-03-02 op || parse_pct_encoded(p)) {
222 5e11c00c 2021-03-02 op /* normalize the host name. */
223 5e11c00c 2021-03-02 op if (*p->iri < 0x7F)
224 5e11c00c 2021-03-02 op *p->iri = tolower(*p->iri);
228 5e11c00c 2021-03-02 op if (p->err != NULL)
231 5e11c00c 2021-03-02 op if (*p->iri == ':') {
232 5e11c00c 2021-03-02 op *p->iri = '\0';
234 5e11c00c 2021-03-02 op return parse_port(p);
236 5e11c00c 2021-03-02 op p->parsed->port_no = 1965;
238 5e11c00c 2021-03-02 op if (*p->iri == '/') {
239 5e11c00c 2021-03-02 op *p->iri = '\0';
244 5e11c00c 2021-03-02 op if (*p->iri == '\0')
247 5e11c00c 2021-03-02 op p->err = "illegal character in authority section";
251 5e11c00c 2021-03-02 op /* Routine for path_clean. Elide the pointed .. with the preceding
252 5e11c00c 2021-03-02 op * element. Return 0 if it's not possible. incr is the length of
253 5e11c00c 2021-03-02 op * the increment, 3 for ../ and 2 for .. */
255 5e11c00c 2021-03-02 op path_elide_dotdot(char *path, char *i, int incr)
259 5e11c00c 2021-03-02 op if (i == path)
261 5e11c00c 2021-03-02 op for (j = i-2; j != path && *j != '/'; j--)
263 5e11c00c 2021-03-02 op if (*j == '/')
266 5e11c00c 2021-03-02 op memmove(j, i, strlen(i)+1);
271 5e11c00c 2021-03-02 op * Use an algorithm similar to the one implemented in go' path.Clean:
273 5e11c00c 2021-03-02 op * 1. Replace multiple slashes with a single slash
274 5e11c00c 2021-03-02 op * 2. Eliminate each . path name element
275 5e11c00c 2021-03-02 op * 3. Eliminate each inner .. along with the non-.. element that precedes it
276 5e11c00c 2021-03-02 op * 4. Eliminate trailing .. if possible or error (go would only discard)
278 5e11c00c 2021-03-02 op * Unlike path.Clean, this function return the empty string if the
279 5e11c00c 2021-03-02 op * original path is equivalent to "/".
282 5e11c00c 2021-03-02 op path_clean(char *path)
286 5e11c00c 2021-03-02 op /* 1. replace multiple slashes with a single one */
287 5e11c00c 2021-03-02 op for (i = path; *i; ++i) {
288 5e11c00c 2021-03-02 op if (*i == '/' && *(i+1) == '/') {
289 5e11c00c 2021-03-02 op memmove(i, i+1, strlen(i)); /* move also the \0 */
294 5e11c00c 2021-03-02 op /* 2. eliminate each . path name element */
295 5e11c00c 2021-03-02 op for (i = path; *i; ++i) {
296 5e11c00c 2021-03-02 op if ((i == path || *i == '/') &&
297 5e11c00c 2021-03-02 op *i != '.' && i[1] == '.' && i[2] == '/') {
298 5e11c00c 2021-03-02 op /* move also the \0 */
299 5e11c00c 2021-03-02 op memmove(i, i+2, strlen(i)-1);
303 5e11c00c 2021-03-02 op if (!strcmp(path, ".") || !strcmp(path, "/.")) {
304 5e11c00c 2021-03-02 op *path = '\0';
308 5e11c00c 2021-03-02 op /* 3. eliminate each inner .. along with the preceding non-.. */
309 5e11c00c 2021-03-02 op for (i = strstr(path, "../"); i != NULL; i = strstr(path, ".."))
310 5e11c00c 2021-03-02 op if (!path_elide_dotdot(path, i, 3))
313 5e11c00c 2021-03-02 op /* 4. eliminate trailing ..*/
314 5e11c00c 2021-03-02 op if ((i = strstr(path, "..")) != NULL)
315 5e11c00c 2021-03-02 op if (!path_elide_dotdot(path, i, 2))
322 5e11c00c 2021-03-02 op parse_query(struct parser *p)
324 5e11c00c 2021-03-02 op p->parsed->query = p->iri;
325 5e11c00c 2021-03-02 op if (*p->iri == '\0')
328 5e11c00c 2021-03-02 op while (unreserved(*p->iri)
329 5e11c00c 2021-03-02 op || sub_delimiters(*p->iri)
330 5e11c00c 2021-03-02 op || *p->iri == '/'
331 5e11c00c 2021-03-02 op || *p->iri == '?'
332 5e11c00c 2021-03-02 op || *p->iri == ':'
333 5e11c00c 2021-03-02 op || *p->iri == '@'
334 5e11c00c 2021-03-02 op || valid_pct_encoded(p))
337 5e11c00c 2021-03-02 op if (p->err != NULL)
340 5e11c00c 2021-03-02 op if (*p->iri != '\0' && *p->iri != '#') {
341 5e11c00c 2021-03-02 op p->err = "illegal character in query";
345 5e11c00c 2021-03-02 op if (*p->iri != '\0') {
346 5e11c00c 2021-03-02 op *p->iri = '\0';
353 5e11c00c 2021-03-02 op /* don't even bother */
355 5e11c00c 2021-03-02 op parse_fragment(struct parser *p)
357 5e11c00c 2021-03-02 op p->parsed->fragment = p->iri;
361 5e11c00c 2021-03-02 op /* XXX: is it too broad? */
362 5e11c00c 2021-03-02 op /* *(pchar / "/") */
364 5e11c00c 2021-03-02 op parse_path(struct parser *p)
368 5e11c00c 2021-03-02 op /* trim initial slashes */
369 5e11c00c 2021-03-02 op while (*p->iri == '/')
372 5e11c00c 2021-03-02 op p->parsed->path = p->iri;
373 5e11c00c 2021-03-02 op if (*p->iri == '\0') {
374 5e11c00c 2021-03-02 op p->parsed->query = p->parsed->fragment = p->iri;
378 5e11c00c 2021-03-02 op while (unreserved(*p->iri)
379 5e11c00c 2021-03-02 op || sub_delimiters(*p->iri)
380 5e11c00c 2021-03-02 op || *p->iri == '/'
381 5e11c00c 2021-03-02 op || parse_pct_encoded(p))
384 5e11c00c 2021-03-02 op if (p->err != NULL)
387 5e11c00c 2021-03-02 op if (*p->iri != '\0' && *p->iri != '?' && *p->iri != '#') {
388 5e11c00c 2021-03-02 op p->err = "illegal character in path";
392 5e11c00c 2021-03-02 op if (*p->iri != '\0') {
394 5e11c00c 2021-03-02 op *p->iri = '\0';
397 5e11c00c 2021-03-02 op if (c == '#') {
398 5e11c00c 2021-03-02 op if (!parse_fragment(p))
401 5e11c00c 2021-03-02 op if (!parse_query(p) || !parse_fragment(p))
405 5e11c00c 2021-03-02 op if (!path_clean(p->parsed->path)) {
406 5e11c00c 2021-03-02 op p->err = "illegal path";
414 5e11c00c 2021-03-02 op url_parse(const char *data, struct url *url, const char **err)
416 5e11c00c 2021-03-02 op struct shallow_url u;
417 5e11c00c 2021-03-02 op struct parser p;
419 5e11c00c 2021-03-02 op memset(url, 0, sizeof(*url));
420 5e11c00c 2021-03-02 op memset(&p, 0, sizeof(p));
421 5e11c00c 2021-03-02 op memset(&u, 0, sizeof(u));
423 5e11c00c 2021-03-02 op strlcpy(p.buf, data, sizeof(p.buf));
424 5e11c00c 2021-03-02 op p.iri = p.buf;
425 5e11c00c 2021-03-02 op p.parsed = &u;
427 5e11c00c 2021-03-02 op if (!parse_scheme(&p) || !parse_authority(&p) || !parse_path(&p)) {
428 5e11c00c 2021-03-02 op *err = p.err;
432 ba714ce3 2021-04-01 op /* XXX: hack around our not complete compliance with RFC 3986 */
433 ba714ce3 2021-04-01 op /* if (u.scheme != NULL) */
434 ba714ce3 2021-04-01 op strlcpy(url->scheme, "gemini", sizeof(url->scheme));
436 5e11c00c 2021-03-02 op if (u.host != NULL)
437 5e11c00c 2021-03-02 op strlcpy(url->host, u.host, sizeof(url->host));
438 5e11c00c 2021-03-02 op if (u.port != NULL)
439 5e11c00c 2021-03-02 op strlcpy(url->port, u.port, sizeof(url->port));
440 5e11c00c 2021-03-02 op if (u.path != NULL)
441 5e11c00c 2021-03-02 op strlcpy(url->path, u.path, sizeof(url->path));
442 5e11c00c 2021-03-02 op if (u.query != NULL)
443 5e11c00c 2021-03-02 op strlcpy(url->query, u.query, sizeof(url->query));
444 5e11c00c 2021-03-02 op if (u.fragment != NULL)
445 5e11c00c 2021-03-02 op strlcpy(url->fragment, u.fragment, sizeof(url->fragment));
451 15dc7d08 2021-03-09 op url_resolve_from(struct url *url, const char *str, const char **err)
453 15dc7d08 2021-03-09 op char *marker, *query, *hash, *i;
455 ba714ce3 2021-04-01 op marker = strstr(str, "//");
456 15dc7d08 2021-03-09 op query = strchr(str, '?');
457 15dc7d08 2021-03-09 op hash = strchr(str, '#');
459 15dc7d08 2021-03-09 op /* full URL */
460 15dc7d08 2021-03-09 op if (marker != NULL
461 15dc7d08 2021-03-09 op && (query == NULL || marker < query)
462 15dc7d08 2021-03-09 op && (hash == NULL || marker < hash))
463 15dc7d08 2021-03-09 op return url_parse(str, url, err);
465 15dc7d08 2021-03-09 op /* TODO: reuse more of the above */
467 15dc7d08 2021-03-09 op /* absolute url */
468 15dc7d08 2021-03-09 op if (*str == '/') {
469 15dc7d08 2021-03-09 op strlcpy(url->path, str, sizeof(url->path));
471 401ec784 2021-03-12 op if ((hash = strchr(url->path, '#')) != NULL) {
472 15dc7d08 2021-03-09 op *hash = '\0';
474 15dc7d08 2021-03-09 op strlcpy(url->fragment, hash,
475 15dc7d08 2021-03-09 op sizeof(url->fragment));
478 15dc7d08 2021-03-09 op if ((query = strchr(url->path, '?')) != NULL) {
479 15dc7d08 2021-03-09 op *query = '\0';
481 15dc7d08 2021-03-09 op strlcpy(url->query, query,
482 15dc7d08 2021-03-09 op sizeof(url->query));
487 15dc7d08 2021-03-09 op /* local url */
488 15dc7d08 2021-03-09 op for (i = strchr(url->path, '\0'); i >= url->path; --i) {
489 15dc7d08 2021-03-09 op if (*i == '/')
493 ba20be69 2021-03-12 op if (*i == '/')
497 15dc7d08 2021-03-09 op strlcat(url->path, str, sizeof(url->path));
502 5cd2ebb1 2021-03-11 op url_set_query(struct url *url, const char *query)
504 5cd2ebb1 2021-03-11 op /* TODO: pct-encode! */
505 5cd2ebb1 2021-03-11 op memcpy(&url->query, query, strlen(query)+1);
510 4d3785b1 2021-03-09 op url_unparse(struct url *url, char *buf, size_t len)
512 4d3785b1 2021-03-09 op strlcpy(buf, url->scheme, len);
513 4d3785b1 2021-03-09 op strlcat(buf, "://", len);
514 4d3785b1 2021-03-09 op strlcat(buf, url->host, len);
516 4d3785b1 2021-03-09 op if (*url->path == '\0' && *url->query == '\0')
519 4d3785b1 2021-03-09 op strlcat(buf, "/", len);
521 4d3785b1 2021-03-09 op if (*url->path != '\0')
522 4d3785b1 2021-03-09 op strlcat(buf, url->path, len);
523 5cd2ebb1 2021-03-11 op if (*url->query != '\0') {
524 4d3785b1 2021-03-09 op strlcat(buf, "?", len);
525 4d3785b1 2021-03-09 op strlcat(buf, url->query, len);