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";
81 sscanf(s+1, "%2hhx", s);
82 memmove(s+1, s+3, strlen(s+3)+1);
86 parse_pct_encoded(struct parser *p)
91 if (!valid_pct_enc_string(p->iri)) {
92 p->err = "illegal percent-encoding";
97 if (*p->iri == '\0') {
98 p->err = "illegal percent-encoding";
105 /* ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) "://" */
107 parse_scheme(struct parser *p)
109 p->parsed->schema = p->iri;
111 if (!isalpha(*p->iri)) {
112 p->err = "illegal character in scheme";
117 /* normalize the scheme (i.e. lowercase it)
119 * XXX: since we cannot have good things, tolower
120 * behaviour depends on the LC_CTYPE locale. The good
121 * news is that we're sure p->iri points to something
122 * that's in the ASCII range, so tolower can't
123 * mis-behave on some systems due to the locale. */
124 *p->iri = tolower(*p->iri);
126 } while (isalnum(*p->iri)
131 if (*p->iri != ':') {
132 p->err = "illegal character in scheme";
137 if (p->iri[1] != '/' || p->iri[2] != '/') {
138 p->err = "invalid marker after scheme";
148 parse_port(struct parser *p)
152 p->parsed->port = p->iri;
154 for (; isdigit(*p->iri); p->iri++) {
155 i = i * 10 + *p->iri - '0';
156 if (i > UINT16_MAX) {
157 p->err = "port number too large";
162 if (*p->iri != '/' && *p->iri != '\0') {
163 p->err = "illegal character in port number";
167 p->parsed->port_no = i;
169 if (*p->iri != '\0') {
177 /* TODO: add support for ip-literal and ipv4addr ? */
178 /* *( unreserved / sub-delims / pct-encoded ) */
180 parse_authority(struct parser *p)
182 p->parsed->host = p->iri;
184 while (unreserved(*p->iri)
185 || sub_delimiters(*p->iri)
186 || parse_pct_encoded(p)
187 || valid_multibyte_utf8(p)) {
188 /* normalize the host name. */
190 *p->iri = tolower(*p->iri);
197 if (*p->iri == ':') {
200 return parse_port(p);
202 p->parsed->port_no = 1965;
204 if (*p->iri == '/') {
213 p->err = "illegal character in authority section";
217 /* Routine for path_clean. Elide the pointed .. with the preceding
218 * element. Return 0 if it's not possible. incr is the length of
219 * the increment, 3 for ../ and 2 for .. */
221 path_elide_dotdot(char *path, char *i, int incr)
227 for (j = i-2; j != path && *j != '/'; j--)
232 memmove(j, i, strlen(i)+1);
237 * Use an algorithm similar to the one implemented in go' path.Clean:
239 * 1. Replace multiple slashes with a single slash
240 * 2. Eliminate each . path name element
241 * 3. Eliminate each inner .. along with the non-.. element that precedes it
242 * 4. Eliminate trailing .. if possible or error (go would only discard)
244 * Unlike path.Clean, this function return the empty string if the
245 * original path is equivalent to "/".
248 path_clean(char *path)
252 /* 1. replace multiple slashes with a single one */
253 for (i = path; *i; ++i) {
254 if (*i == '/' && *(i+1) == '/') {
255 memmove(i, i+1, strlen(i)); /* move also the \0 */
260 /* 2. eliminate each . path name element */
261 for (i = path; *i; ++i) {
262 if ((i == path || *i == '/') &&
263 *i != '.' && i[1] == '.' && i[2] == '/') {
264 /* move also the \0 */
265 memmove(i, i+2, strlen(i)-1);
269 if (!strcmp(path, ".") || !strcmp(path, "/.")) {
274 /* 3. eliminate each inner .. along with the preceding non-.. */
275 for (i = strstr(path, "../"); i != NULL; i = strstr(path, "..")) {
276 /* break if we've found a trailing .. */
279 if (!path_elide_dotdot(path, i, 3))
283 /* 4. eliminate trailing ..*/
284 if ((i = strstr(path, "..")) != NULL)
285 if (!path_elide_dotdot(path, i, 2))
292 parse_query(struct parser *p)
294 p->parsed->query = p->iri;
298 while (unreserved(*p->iri)
299 || sub_delimiters(*p->iri)
304 || valid_pct_encoded(p)
305 || valid_multibyte_utf8(p))
311 if (*p->iri != '\0' && *p->iri != '#') {
312 p->err = "illegal character in query";
316 if (*p->iri != '\0') {
324 /* don't even bother */
326 parse_fragment(struct parser *p)
328 p->parsed->fragment = p->iri;
332 /* XXX: is it too broad? */
335 parse_path(struct parser *p)
339 /* trim initial slashes */
340 while (*p->iri == '/')
343 p->parsed->path = p->iri;
344 if (*p->iri == '\0') {
345 p->parsed->query = p->parsed->fragment = p->iri;
349 while (unreserved(*p->iri)
350 || sub_delimiters(*p->iri)
352 || parse_pct_encoded(p)
353 || valid_multibyte_utf8(p))
359 if (*p->iri != '\0' && *p->iri != '?' && *p->iri != '#') {
360 p->err = "illegal character in path";
364 if (*p->iri != '\0') {
370 if (!parse_fragment(p))
373 if (!parse_query(p) || !parse_fragment(p))
377 if (!path_clean(p->parsed->path)) {
378 p->err = "illegal path";
386 parse_iri(char *iri, struct iri *ret, const char **err_ret)
389 struct parser p = {iri, ret, NULL};
391 bzero(ret, sizeof(*ret));
393 /* initialize optional stuff to the empty string */
394 end = iri + strlen(iri);
395 p.parsed->host = end;
396 p.parsed->port = end;
397 p.parsed->path = end;
398 p.parsed->query = end;
399 p.parsed->fragment = end;
401 if (!parse_scheme(&p) || !parse_authority(&p) || !parse_path(&p)) {
411 trim_req_iri(char *iri, const char **err)
415 if ((i = strstr(iri, "\r\n")) == NULL) {
416 *err = "missing CRLF";
425 serialize_iri(struct iri *i, char *buf, size_t len)
429 /* in ex.c we receive empty "" strings as NULL */
430 if (i->schema == NULL || i->host == NULL) {
435 strlcpy(buf, i->schema, len);
436 strlcat(buf, "://", len);
437 strlcat(buf, i->host, len);
438 strlcat(buf, "/", len);
441 l = strlcat(buf, i->path, len);
443 if (i->query != NULL && *i->query != '\0') {
444 strlcat(buf, "?", len);
445 l = strlcat(buf, i->query, len);
452 pct_decode_str(char *s)
456 for (t = s; *t; ++t) {
457 if (*t == '%' && valid_pct_enc_string(t))