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";
130 /* ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) "://" */
132 parse_scheme(struct parser *p)
134 p->parsed->scheme = p->iri;
136 if (!isalpha(*p->iri)) {
137 p->err = "illegal character in scheme";
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);
151 } while (isalnum(*p->iri)
156 if (*p->iri != ':') {
157 p->err = "illegal character in scheme";
162 if (p->iri[1] != '/' || p->iri[2] != '/') {
163 p->err = "invalid marker after scheme";
173 parse_port(struct parser *p)
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";
187 if (*p->iri != '/' && *p->iri != '\0') {
188 p->err = "illegal character in port number";
192 p->parsed->port_no = i;
194 if (*p->iri != '\0') {
202 /* TODO: add support for ip-literal and ipv4addr ? */
203 /* *( unreserved / sub-delims / pct-encoded ) */
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. */
214 *p->iri = tolower(*p->iri);
221 if (*p->iri == ':') {
224 return parse_port(p);
226 p->parsed->port_no = 1965;
228 if (*p->iri == '/') {
237 p->err = "illegal character in authority section";
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 .. */
245 path_elide_dotdot(char *path, char *i, int incr)
251 for (j = i-2; j != path && *j != '/'; j--)
256 memmove(j, i, strlen(i)+1);
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 "/".
272 path_clean(char *path)
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 */
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);
293 if (!strcmp(path, ".") || !strcmp(path, "/.")) {
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))
303 /* 4. eliminate trailing ..*/
304 if ((i = strstr(path, "..")) != NULL)
305 if (!path_elide_dotdot(path, i, 2))
312 parse_query(struct parser *p)
314 p->parsed->query = p->iri;
318 while (unreserved(*p->iri)
319 || sub_delimiters(*p->iri)
324 || valid_pct_encoded(p))
330 if (*p->iri != '\0' && *p->iri != '#') {
331 p->err = "illegal character in query";
335 if (*p->iri != '\0') {
343 /* don't even bother */
345 parse_fragment(struct parser *p)
347 p->parsed->fragment = p->iri;
351 /* XXX: is it too broad? */
354 parse_path(struct parser *p)
358 /* trim initial slashes */
359 while (*p->iri == '/')
362 p->parsed->path = p->iri;
363 if (*p->iri == '\0') {
364 p->parsed->query = p->parsed->fragment = p->iri;
368 while (unreserved(*p->iri)
369 || sub_delimiters(*p->iri)
371 || parse_pct_encoded(p))
377 if (*p->iri != '\0' && *p->iri != '?' && *p->iri != '#') {
378 p->err = "illegal character in path";
382 if (*p->iri != '\0') {
388 if (!parse_fragment(p))
391 if (!parse_query(p) || !parse_fragment(p))
395 if (!path_clean(p->parsed->path)) {
396 p->err = "illegal path";
404 url_parse(const char *data, struct url *url, const char **err)
406 struct shallow_url u;
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));
417 if (!parse_scheme(&p) || !parse_authority(&p) || !parse_path(&p)) {
422 if (u.scheme != NULL)
423 strlcpy(url->scheme, u.scheme, sizeof(url->scheme));
425 strlcpy(url->host, u.host, sizeof(url->host));
427 strlcpy(url->port, u.port, sizeof(url->port));
429 strlcpy(url->path, u.path, sizeof(url->path));
431 strlcpy(url->query, u.query, sizeof(url->query));
432 if (u.fragment != NULL)
433 strlcpy(url->fragment, u.fragment, sizeof(url->fragment));
439 url_resolve_from(struct url *url, const char *str, const char **err)
441 char *marker, *query, *hash, *i;
443 marker = strstr(str, "://");
444 query = strchr(str, '?');
445 hash = strchr(str, '#');
449 && (query == NULL || marker < query)
450 && (hash == NULL || marker < hash))
451 return url_parse(str, url, err);
453 /* TODO: reuse more of the above */
457 strlcpy(url->path, str, sizeof(url->path));
459 if ((hash = strchr(url->path, '#')) != NULL) {
462 strlcpy(url->fragment, hash,
463 sizeof(url->fragment));
466 if ((query = strchr(url->path, '?')) != NULL) {
469 strlcpy(url->query, query,
476 for (i = strchr(url->path, '\0'); i >= url->path; --i) {
485 strlcat(url->path, str, sizeof(url->path));
490 url_set_query(struct url *url, const char *query)
492 /* TODO: pct-encode! */
493 memcpy(&url->query, query, strlen(query)+1);
498 url_unparse(struct url *url, char *buf, size_t len)
500 strlcpy(buf, url->scheme, len);
501 strlcat(buf, "://", len);
502 strlcat(buf, url->host, len);
504 if (*url->path == '\0' && *url->query == '\0')
507 strlcat(buf, "/", len);
509 if (*url->path != '\0')
510 strlcat(buf, url->path, len);
511 if (*url->query != '\0') {
512 strlcat(buf, "?", len);
513 strlcat(buf, url->query, len);