2 * Copyright (c) 2020 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(s[1]) || !isxdigit(s[2]))
57 if (s[1] == '0' && s[2] == '0')
64 valid_pct_encoded(struct parser *p)
69 if (!valid_pct_enc_string(p->iri)) {
70 p->err = "illegal percent-encoding";
79 parse_pct_encoded(struct parser *p)
84 if (!valid_pct_enc_string(p->iri)) {
85 p->err = "illegal percent-encoding";
89 sscanf(p->iri+1, "%2hhx", p->iri);
90 memmove(p->iri+1, p->iri+3, strlen(p->iri+3)+1);
91 if (*p->iri == '\0') {
92 p->err = "illegal percent-encoding";
99 /* ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) "://" */
101 parse_scheme(struct parser *p)
103 p->parsed->schema = p->iri;
105 if (!isalpha(*p->iri)) {
106 p->err = "illegal character in scheme";
111 /* normalize the scheme (i.e. lowercase it)
113 * XXX: since we cannot have good things, tolower
114 * behaviour depends on the LC_CTYPE locale. The good
115 * news is that we're sure p->iri points to something
116 * that's in the ASCII range, so tolower can't
117 * mis-behave on some systems due to the locale. */
118 *p->iri = tolower(*p->iri);
120 } while (isalnum(*p->iri)
125 if (*p->iri != ':') {
126 p->err = "illegal character in scheme";
131 if (p->iri[1] != '/' || p->iri[2] != '/') {
132 p->err = "invalid marker after scheme";
142 parse_port(struct parser *p)
146 p->parsed->port = p->iri;
148 for (; isdigit(*p->iri); p->iri++) {
149 i = i * 10 + *p->iri - '0';
150 if (i > UINT16_MAX) {
151 p->err = "port number too large";
156 if (*p->iri != '/' && *p->iri != '\0') {
157 p->err = "illegal character in port number";
161 p->parsed->port_no = i;
163 if (*p->iri != '\0') {
171 /* TODO: add support for ip-literal and ipv4addr ? */
172 /* *( unreserved / sub-delims / pct-encoded ) */
174 parse_authority(struct parser *p)
176 p->parsed->host = p->iri;
178 while (unreserved(*p->iri)
179 || sub_delimiters(*p->iri)
180 || parse_pct_encoded(p)
181 || valid_multibyte_utf8(p)) {
182 /* normalize the host name. */
184 *p->iri = tolower(*p->iri);
191 if (*p->iri == ':') {
194 return parse_port(p);
196 p->parsed->port_no = 1965;
198 if (*p->iri == '/') {
207 p->err = "illegal character in authority section";
211 /* Routine for path_clean. Elide the pointed .. with the preceding
212 * element. Return 0 if it's not possible. incr is the length of
213 * the increment, 3 for ../ and 2 for .. */
215 path_elide_dotdot(char *path, char *i, int incr)
221 for (j = i-2; j != path && *j != '/'; j--)
226 memmove(j, i, strlen(i)+1);
231 * Use an algorithm similar to the one implemented in go' path.Clean:
233 * 1. Replace multiple slashes with a single slash
234 * 2. Eliminate each . path name element
235 * 3. Eliminate each inner .. along with the non-.. element that precedes it
236 * 4. Eliminate trailing .. if possible or error (go would only discard)
238 * Unlike path.Clean, this function return the empty string if the
239 * original path is equivalent to "/".
242 path_clean(char *path)
246 /* 1. replace multiple slashes with a single one */
247 for (i = path; *i; ++i) {
248 if (*i == '/' && *(i+1) == '/') {
249 memmove(i, i+1, strlen(i)); /* move also the \0 */
254 /* 2. eliminate each . path name element */
255 for (i = path; *i; ++i) {
256 if ((i == path || *i == '/') &&
257 *i != '.' && i[1] == '.' && i[2] == '/') {
258 /* move also the \0 */
259 memmove(i, i+2, strlen(i)-1);
263 if (!strcmp(path, ".") || !strcmp(path, "/.")) {
268 /* 3. eliminate each inner .. along with the preceding non-.. */
269 for (i = strstr(path, "../"); i != NULL; i = strstr(path, ".."))
270 if (!path_elide_dotdot(path, i, 3))
273 /* 4. eliminate trailing ..*/
274 if ((i = strstr(path, "..")) != NULL)
275 if (!path_elide_dotdot(path, i, 2))
282 parse_query(struct parser *p)
284 p->parsed->query = p->iri;
288 while (unreserved(*p->iri)
289 || sub_delimiters(*p->iri)
294 || valid_pct_encoded(p)
295 || valid_multibyte_utf8(p))
301 if (*p->iri != '\0' && *p->iri != '#') {
302 p->err = "illegal character in query";
306 if (*p->iri != '\0') {
314 /* don't even bother */
316 parse_fragment(struct parser *p)
318 p->parsed->fragment = p->iri;
322 /* XXX: is it too broad? */
325 parse_path(struct parser *p)
329 /* trim initial slashes */
330 while (*p->iri == '/')
333 p->parsed->path = p->iri;
334 if (*p->iri == '\0') {
335 p->parsed->query = p->parsed->fragment = p->iri;
339 while (unreserved(*p->iri)
340 || sub_delimiters(*p->iri)
342 || parse_pct_encoded(p)
343 || valid_multibyte_utf8(p))
349 if (*p->iri != '\0' && *p->iri != '?' && *p->iri != '#') {
350 p->err = "illegal character in path";
354 if (*p->iri != '\0') {
360 if (!parse_fragment(p))
363 if (!parse_query(p) || !parse_fragment(p))
367 if (!path_clean(p->parsed->path)) {
368 p->err = "illegal path";
376 parse_iri(char *iri, struct iri *ret, const char **err_ret)
379 struct parser p = {iri, ret, NULL};
381 bzero(ret, sizeof(*ret));
383 /* initialize optional stuff to the empty string */
384 end = iri + strlen(iri);
385 p.parsed->host = end;
386 p.parsed->port = end;
387 p.parsed->path = end;
388 p.parsed->query = end;
389 p.parsed->fragment = end;
391 if (!parse_scheme(&p) || !parse_authority(&p) || !parse_path(&p)) {
401 trim_req_iri(char *iri, const char **err)
405 if ((i = strstr(iri, "\r\n")) == NULL) {
406 *err = "missing CRLF";
415 serialize_iri(struct iri *i, char *buf, size_t len)
419 /* in ex.c we receive empty "" strings as NULL */
420 if (i->schema == NULL || i->host == NULL) {
425 strlcpy(buf, i->schema, len);
426 strlcat(buf, "://", len);
427 strlcat(buf, i->host, len);
428 strlcat(buf, "/", len);
431 l = strlcat(buf, i->path, len);
433 if (i->query != NULL && *i->query != '\0') {
434 strlcat(buf, "?", len);
435 l = strlcat(buf, i->query, len);