2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
18 * This is mostly copied from gmid' iri.c, with minor changes. Bugs
19 * fixed here should be ported to gmid and vice-versa.
41 char buf[GEMINI_URL_LEN+1];
43 struct shallow_url *parsed;
74 valid_pct_enc_string(char *s)
79 if (!isxdigit(s[1]) || !isxdigit(s[2]))
82 if (s[1] == '0' && s[2] == '0')
89 valid_pct_encoded(struct parser *p)
94 if (!valid_pct_enc_string(p->iri)) {
95 p->err = "illegal percent-encoding";
106 sscanf(s+1, "%2hhx", s);
107 memmove(s+1, s+3, strlen(s+3)+1);
111 parse_pct_encoded(struct parser *p)
113 if (p->iri[0] != '%')
116 if (!valid_pct_enc_string(p->iri)) {
117 p->err = "illegal percent-encoding";
122 if (*p->iri == '\0') {
123 p->err = "illegal percent-encoding";
131 * ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) "://"
136 parse_scheme(struct parser *p)
138 p->parsed->scheme = p->iri;
140 if (p->iri[0] == '/' && p->iri[1] == '/') {
146 if (!isalpha(*p->iri)) {
147 p->err = "illegal character in scheme";
152 /* normalize the scheme (i.e. lowercase it)
154 * XXX: since we cannot have good things, tolower
155 * behaviour depends on the LC_CTYPE locale. The good
156 * news is that we're sure p->iri points to something
157 * that's in the ASCII range, so tolower can't
158 * mis-behave on some systems due to the locale. */
159 *p->iri = tolower(*p->iri);
161 } while (isalnum(*p->iri)
166 if (*p->iri != ':') {
167 p->err = "illegal character in scheme";
172 if (p->iri[1] != '/' || p->iri[2] != '/') {
173 p->err = "invalid marker after scheme";
183 parse_port(struct parser *p)
187 p->parsed->port = p->iri;
189 for (; isdigit(*p->iri); p->iri++) {
190 i = i * 10 + *p->iri - '0';
191 if (i > UINT16_MAX) {
192 p->err = "port number too large";
197 if (*p->iri != '/' && *p->iri != '\0') {
198 p->err = "illegal character in port number";
202 p->parsed->port_no = i;
204 if (*p->iri != '\0') {
212 /* TODO: add support for ip-literal and ipv4addr ? */
213 /* *( unreserved / sub-delims / pct-encoded ) */
215 parse_authority(struct parser *p)
217 p->parsed->host = p->iri;
219 while (unreserved(*p->iri)
220 || sub_delimiters(*p->iri)
221 || parse_pct_encoded(p)) {
222 /* normalize the host name. */
224 *p->iri = tolower(*p->iri);
231 if (*p->iri == ':') {
234 return parse_port(p);
236 p->parsed->port_no = 1965;
238 if (*p->iri == '/') {
247 p->err = "illegal character in authority section";
251 /* Routine for path_clean. Elide the pointed .. with the preceding
252 * element. Return 0 if it's not possible. incr is the length of
253 * the increment, 3 for ../ and 2 for .. */
255 path_elide_dotdot(char *path, char *i, int incr)
261 for (j = i-2; j != path && *j != '/'; j--)
266 memmove(j, i, strlen(i)+1);
271 * Use an algorithm similar to the one implemented in go' path.Clean:
273 * 1. Replace multiple slashes with a single slash
274 * 2. Eliminate each . path name element
275 * 3. Eliminate each inner .. along with the non-.. element that precedes it
276 * 4. Eliminate trailing .. if possible or error (go would only discard)
278 * Unlike path.Clean, this function return the empty string if the
279 * original path is equivalent to "/".
282 path_clean(char *path)
286 /* 1. replace multiple slashes with a single one */
287 for (i = path; *i; ++i) {
288 if (*i == '/' && *(i+1) == '/') {
289 memmove(i, i+1, strlen(i)); /* move also the \0 */
294 /* 2. eliminate each . path name element */
295 for (i = path; *i; ++i) {
296 if ((i == path || *i == '/') &&
297 *i != '.' && i[1] == '.' && i[2] == '/') {
298 /* move also the \0 */
299 memmove(i, i+2, strlen(i)-1);
303 if (!strcmp(path, ".") || !strcmp(path, "/.")) {
308 /* 3. eliminate each inner .. along with the preceding non-.. */
309 for (i = strstr(path, "../"); i != NULL; i = strstr(path, ".."))
310 if (!path_elide_dotdot(path, i, 3))
313 /* 4. eliminate trailing ..*/
314 if ((i = strstr(path, "..")) != NULL)
315 if (!path_elide_dotdot(path, i, 2))
322 parse_query(struct parser *p)
324 p->parsed->query = p->iri;
328 while (unreserved(*p->iri)
329 || sub_delimiters(*p->iri)
334 || valid_pct_encoded(p))
340 if (*p->iri != '\0' && *p->iri != '#') {
341 p->err = "illegal character in query";
345 if (*p->iri != '\0') {
353 /* don't even bother */
355 parse_fragment(struct parser *p)
357 p->parsed->fragment = p->iri;
361 /* XXX: is it too broad? */
364 parse_path(struct parser *p)
368 /* trim initial slashes */
369 while (*p->iri == '/')
372 p->parsed->path = p->iri;
373 if (*p->iri == '\0') {
374 p->parsed->query = p->parsed->fragment = p->iri;
378 while (unreserved(*p->iri)
379 || sub_delimiters(*p->iri)
381 || parse_pct_encoded(p))
387 if (*p->iri != '\0' && *p->iri != '?' && *p->iri != '#') {
388 p->err = "illegal character in path";
392 if (*p->iri != '\0') {
398 if (!parse_fragment(p))
401 if (!parse_query(p) || !parse_fragment(p))
405 if (!path_clean(p->parsed->path)) {
406 p->err = "illegal path";
414 url_parse(const char *data, struct url *url, const char **err)
416 struct shallow_url u;
419 memset(url, 0, sizeof(*url));
420 memset(&p, 0, sizeof(p));
421 memset(&u, 0, sizeof(u));
423 strlcpy(p.buf, data, sizeof(p.buf));
427 if (!parse_scheme(&p) || !parse_authority(&p) || !parse_path(&p)) {
432 /* XXX: hack around our not complete compliance with RFC 3986 */
433 /* if (u.scheme != NULL) */
434 strlcpy(url->scheme, "gemini", sizeof(url->scheme));
437 strlcpy(url->host, u.host, sizeof(url->host));
439 strlcpy(url->port, u.port, sizeof(url->port));
441 strlcpy(url->path, u.path, sizeof(url->path));
443 strlcpy(url->query, u.query, sizeof(url->query));
444 if (u.fragment != NULL)
445 strlcpy(url->fragment, u.fragment, sizeof(url->fragment));
451 url_resolve_from(struct url *url, const char *str, const char **err)
453 char *marker, *query, *hash, *i;
455 marker = strstr(str, "//");
456 query = strchr(str, '?');
457 hash = strchr(str, '#');
461 && (query == NULL || marker < query)
462 && (hash == NULL || marker < hash))
463 return url_parse(str, url, err);
465 /* TODO: reuse more of the above */
469 strlcpy(url->path, str, sizeof(url->path));
471 if ((hash = strchr(url->path, '#')) != NULL) {
474 strlcpy(url->fragment, hash,
475 sizeof(url->fragment));
478 if ((query = strchr(url->path, '?')) != NULL) {
481 strlcpy(url->query, query,
488 for (i = strchr(url->path, '\0'); i >= url->path; --i) {
497 strlcat(url->path, str, sizeof(url->path));
502 url_set_query(struct url *url, const char *query)
504 /* TODO: pct-encode! */
505 memcpy(&url->query, query, strlen(query)+1);
510 url_unparse(struct url *url, char *buf, size_t len)
512 strlcpy(buf, url->scheme, len);
513 strlcat(buf, "://", len);
514 strlcat(buf, url->host, len);
516 if (*url->path == '\0' && *url->query == '\0')
519 strlcat(buf, "/", len);
521 if (*url->path != '\0')
522 strlcat(buf, url->path, len);
523 if (*url->query != '\0') {
524 strlcat(buf, "?", len);
525 strlcat(buf, url->query, len);