2 * Copyright (c) 2020, 2022 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.
49 valid_pct_enc_string(char *s)
54 if (!isxdigit((unsigned char)s[1]) ||
55 !isxdigit((unsigned char)s[2]))
58 if (s[1] == '0' && s[2] == '0')
65 valid_pct_encoded(struct parser *p)
70 if (!valid_pct_enc_string(p->iri)) {
71 p->err = "illegal percent-encoding";
82 sscanf(s+1, "%2hhx", s);
83 memmove(s+1, s+3, strlen(s+3)+1);
87 parse_pct_encoded(struct parser *p)
92 if (!valid_pct_enc_string(p->iri)) {
93 p->err = "illegal percent-encoding";
98 if (*p->iri == '\0') {
99 p->err = "illegal percent-encoding";
106 /* ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) "://" */
108 parse_scheme(struct parser *p)
110 p->parsed->schema = p->iri;
112 if (!isalpha(*p->iri)) {
113 p->err = "illegal character in scheme";
119 * normalize the scheme (i.e. lowercase it)
121 * XXX: since we cannot have good things, tolower
122 * behaviour depends on the LC_CTYPE locale. The good
123 * news is that we're sure p->iri points to something
124 * that's in the ASCII range, so tolower can't
125 * mis-behave on some systems due to the locale.
127 *p->iri = tolower(*p->iri);
129 } while (isalnum(*p->iri)
134 if (*p->iri != ':') {
135 p->err = "illegal character in scheme";
140 if (p->iri[1] != '/' || p->iri[2] != '/') {
141 p->err = "invalid marker after scheme";
151 parse_port(struct parser *p)
155 p->parsed->port = p->iri;
157 for (; isdigit((unsigned char)*p->iri); p->iri++) {
158 i = i * 10 + *p->iri - '0';
159 if (i > UINT16_MAX) {
160 p->err = "port number too large";
165 if (*p->iri != '/' && *p->iri != '\0') {
166 p->err = "illegal character in port number";
170 p->parsed->port_no = i;
172 if (*p->iri != '\0') {
180 /* TODO: add support for ip-literal and ipv4addr ? */
181 /* *( unreserved / sub-delims / pct-encoded ) */
183 parse_authority(struct parser *p)
185 p->parsed->host = p->iri;
187 while (unreserved(*p->iri)
188 || sub_delimiters(*p->iri)
189 || parse_pct_encoded(p)
190 || valid_multibyte_utf8(p)) {
191 /* normalize the host name. */
193 *p->iri = tolower(*p->iri);
200 if (*p->iri == ':') {
203 return parse_port(p);
205 p->parsed->port_no = 1965;
207 if (*p->iri == '/') {
216 p->err = "illegal character in authority section";
221 * Routine for path_clean. Elide the pointed .. with the preceding
222 * element. Return 0 if it's not possible. incr is the length of
223 * the increment, 3 for ../ and 2 for ..
226 path_elide_dotdot(char *path, char *i, int incr)
232 for (j = i-2; j != path && *j != '/'; j--)
237 memmove(j, i, strlen(i)+1);
242 * Use an algorithm similar to the one implemented in go' path.Clean:
244 * 1. Replace multiple slashes with a single slash
245 * 2. Eliminate each . path name element
246 * 3. Eliminate each inner .. along with the non-.. element that precedes it
247 * 4. Eliminate trailing .. if possible or error (go would only discard)
249 * Unlike path.Clean, this function return the empty string if the
250 * original path is equivalent to "/".
253 path_clean(char *path)
257 /* 1. replace multiple slashes with a single one */
258 for (i = path; *i; ++i) {
259 if (*i == '/' && *(i+1) == '/') {
260 memmove(i, i+1, strlen(i)); /* move also the \0 */
265 /* 2. eliminate each . path name element */
266 for (i = path; *i; ++i) {
267 if ((i == path || *i == '/') &&
268 *i != '.' && i[1] == '.' && i[2] == '/') {
269 /* move also the \0 */
270 memmove(i, i+2, strlen(i)-1);
274 if (!strcmp(path, ".") || !strcmp(path, "/.")) {
279 /* 3. eliminate each inner .. along with the preceding non-.. */
280 for (i = strstr(path, "../"); i != NULL; i = strstr(path, "..")) {
281 /* break if we've found a trailing .. */
284 if (!path_elide_dotdot(path, i, 3))
288 /* 4. eliminate trailing ..*/
289 if ((i = strstr(path, "..")) != NULL)
290 if (!path_elide_dotdot(path, i, 2))
297 parse_query(struct parser *p)
299 p->parsed->query = p->iri;
303 while (unreserved(*p->iri)
304 || sub_delimiters(*p->iri)
309 || valid_pct_encoded(p)
310 || valid_multibyte_utf8(p))
316 if (*p->iri != '\0' && *p->iri != '#') {
317 p->err = "illegal character in query";
321 if (*p->iri != '\0') {
329 /* don't even bother */
331 parse_fragment(struct parser *p)
333 p->parsed->fragment = p->iri;
337 /* XXX: is it too broad? */
340 parse_path(struct parser *p)
344 /* trim initial slashes */
345 while (*p->iri == '/')
348 p->parsed->path = p->iri;
349 if (*p->iri == '\0') {
350 p->parsed->query = p->parsed->fragment = p->iri;
354 while (unreserved(*p->iri)
355 || sub_delimiters(*p->iri)
359 || parse_pct_encoded(p)
360 || valid_multibyte_utf8(p))
366 if (*p->iri != '\0' && *p->iri != '?' && *p->iri != '#') {
367 p->err = "illegal character in path";
371 if (*p->iri != '\0') {
377 if (!parse_fragment(p))
380 if (!parse_query(p) || !parse_fragment(p))
384 if (!path_clean(p->parsed->path)) {
385 p->err = "illegal path";
393 parse_iri(char *iri, struct iri *ret, const char **err_ret)
402 memset(ret, 0, sizeof(*ret));
404 /* initialize optional stuff to the empty string */
405 end = iri + strlen(iri);
406 p.parsed->host = end;
407 p.parsed->port = end;
408 p.parsed->path = end;
409 p.parsed->query = end;
410 p.parsed->fragment = end;
412 if (!parse_scheme(&p) || !parse_authority(&p) || !parse_path(&p)) {
422 serialize_iri(struct iri *i, char *buf, size_t len)
426 /* in ex.c we receive empty "" strings as NULL */
427 if (i->schema == NULL || i->host == NULL) {
432 strlcpy(buf, i->schema, len);
433 strlcat(buf, "://", len);
434 strlcat(buf, i->host, len);
435 strlcat(buf, "/", len);
438 l = strlcat(buf, i->path, len);
440 if (i->query != NULL && *i->query != '\0') {
441 strlcat(buf, "?", len);
442 l = strlcat(buf, i->query, len);
449 encode_path(char *buf, size_t len, const char *path)
455 while (*path != '\0') {
456 if (len == 1) /* NUL */
459 if (unreserved(*path) ||
460 sub_delimiters(*path) ||
469 a = (*path & 0xF0) >> 4;
473 p[1] = a <= 9 ? ('0' + a) : ('7' + a);
474 p[2] = b <= 9 ? ('0' + b) : ('7' + b);
486 pct_decode_str(char *s)
490 for (t = s; *t; ++t) {
491 if (*t == '%' && valid_pct_enc_string(t))