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 parse_pct_encoded(struct parser *p)
54 if (!isxdigit(*(p->iri+1)) || !isxdigit(*(p->iri+2))) {
55 p->err = "illegal percent-encoding";
59 sscanf(p->iri+1, "%2hhx", p->iri);
60 memmove(p->iri+1, p->iri+3, strlen(p->iri+3)+1);
61 if (*p->iri == '\0') {
62 p->err = "illegal percent-encoding";
69 /* ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) "://" */
71 parse_scheme(struct parser *p)
73 p->parsed->schema = p->iri;
75 if (!isalpha(*p->iri)) {
76 p->err = "illegal character in scheme";
81 /* normalize the scheme (i.e. lowercase it)
83 * XXX: since we cannot have good things, tolower
84 * behaviour depends on the LC_CTYPE locale. The good
85 * news is that we're sure p->iri points to something
86 * that's in the ASCII range, so tolower can't
87 * mis-behave on some systems due to the locale. */
88 *p->iri = tolower(*p->iri);
90 } while (isalnum(*p->iri)
96 p->err = "illegal character in scheme";
101 if (p->iri[1] != '/' || p->iri[2] != '/') {
102 p->err = "invalid marker after scheme";
112 parse_port(struct parser *p)
116 p->parsed->port = p->iri;
118 for (; isdigit(*p->iri); p->iri++) {
119 i = i * 10 + *p->iri - '0';
120 if (i > UINT16_MAX) {
121 p->err = "port number too large";
126 if (*p->iri != '/' && *p->iri != '\0') {
127 p->err = "illegal character in port number";
131 p->parsed->port_no = i;
133 if (*p->iri != '\0') {
141 /* TODO: add support for ip-literal and ipv4addr ? */
142 /* *( unreserved / sub-delims / pct-encoded ) */
144 parse_authority(struct parser *p)
146 p->parsed->host = p->iri;
148 while (unreserved(*p->iri)
149 || sub_delimiters(*p->iri)
150 || parse_pct_encoded(p)) {
151 /* normalize the host name. */
153 *p->iri = tolower(*p->iri);
160 if (*p->iri == ':') {
163 return parse_port(p);
165 p->parsed->port_no = 1965;
167 if (*p->iri == '/') {
176 p->err = "illegal character in authority section";
180 /* Routine for path_clean. Elide the pointed .. with the preceding
181 * element. Return 0 if it's not possible. incr is the length of
182 * the increment, 3 for ../ and 2 for .. */
184 path_elide_dotdot(char *path, char *i, int incr)
190 for (j = i-2; j != path && *j != '/'; j--)
195 memmove(j, i, strlen(i)+1);
200 * Use an algorithm similar to the one implemented in go' path.Clean:
202 * 1. Replace multiple slashes with a single slash
203 * 2. Eliminate each . path name element
204 * 3. Eliminate each inner .. along with the non-.. element that precedes it
205 * 4. Eliminate trailing .. if possible or error (go would only discard)
207 * Unlike path.Clean, this function return the empty string if the
208 * original path is equivalent to "/".
211 path_clean(char *path)
215 /* 1. replace multiple slashes with a single one */
216 for (i = path; *i; ++i) {
217 if (*i == '/' && *(i+1) == '/') {
218 memmove(i, i+1, strlen(i)); /* move also the \0 */
223 /* 2. eliminate each . path name element */
224 for (i = path; *i; ++i) {
225 if ((i == path || *i == '/') &&
226 *i != '.' && i[1] == '.' && i[2] == '/') {
227 /* move also the \0 */
228 memmove(i, i+2, strlen(i)-1);
232 if (!strcmp(path, ".") || !strcmp(path, "/.")) {
237 /* 3. eliminate each inner .. along with the preceding non-.. */
238 for (i = strstr(path, "../"); i != NULL; i = strstr(path, ".."))
239 if (!path_elide_dotdot(path, i, 3))
242 /* 4. eliminate trailing ..*/
243 if ((i = strstr(path, "..")) != NULL)
244 if (!path_elide_dotdot(path, i, 2))
251 parse_query(struct parser *p)
253 p->parsed->query = p->iri;
257 while (unreserved(*p->iri)
258 || sub_delimiters(*p->iri)
261 || parse_pct_encoded(p)
262 || valid_multibyte_utf8(p))
268 if (*p->iri != '\0' && *p->iri != '#') {
269 p->err = "illegal character in query";
273 if (*p->iri != '\0') {
281 /* don't even bother */
283 parse_fragment(struct parser *p)
285 p->parsed->fragment = p->iri;
289 /* XXX: is it too broad? */
292 parse_path(struct parser *p)
296 /* trim initial slashes */
297 while (*p->iri == '/')
300 p->parsed->path = p->iri;
301 if (*p->iri == '\0') {
302 p->parsed->query = p->parsed->fragment = p->iri;
306 while (unreserved(*p->iri)
307 || sub_delimiters(*p->iri)
309 || parse_pct_encoded(p)
310 || valid_multibyte_utf8(p))
316 if (*p->iri != '\0' && *p->iri != '?' && *p->iri != '#') {
317 p->err = "illegal character in path";
321 if (*p->iri != '\0') {
327 if (!parse_fragment(p))
330 if (!parse_query(p) || !parse_fragment(p))
334 if (!path_clean(p->parsed->path)) {
335 p->err = "illegal path";
343 parse_iri(char *iri, struct iri *ret, const char **err_ret)
346 struct parser p = {iri, ret, NULL};
348 bzero(ret, sizeof(*ret));
350 /* initialize optional stuff to the empty string */
351 end = iri + strlen(iri);
352 p.parsed->port = end;
353 p.parsed->path = end;
354 p.parsed->query = end;
355 p.parsed->fragment = end;
357 if (!parse_scheme(&p) || !parse_authority(&p) || !parse_path(&p)) {
367 trim_req_iri(char *iri, const char **err)
371 if ((i = strstr(iri, "\r\n")) == NULL) {
372 *err = "missing CRLF";