2 3c1cf9d0 2021-01-11 op * Copyright (c) 2020 Omar Polo <op@omarpolo.com>
4 3c1cf9d0 2021-01-11 op * Permission to use, copy, modify, and distribute this software for any
5 3c1cf9d0 2021-01-11 op * purpose with or without fee is hereby granted, provided that the above
6 3c1cf9d0 2021-01-11 op * copyright notice and this permission notice appear in all copies.
8 3c1cf9d0 2021-01-11 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3c1cf9d0 2021-01-11 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3c1cf9d0 2021-01-11 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3c1cf9d0 2021-01-11 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3c1cf9d0 2021-01-11 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3c1cf9d0 2021-01-11 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3c1cf9d0 2021-01-11 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 52418c8d 2021-02-12 op #include "gmid.h"
19 3c1cf9d0 2021-01-11 op #include <ctype.h>
20 3c1cf9d0 2021-01-11 op #include <string.h>
22 3c1cf9d0 2021-01-11 op static inline int
23 3c1cf9d0 2021-01-11 op unreserved(int p)
25 3c1cf9d0 2021-01-11 op return isalnum(p)
32 3c1cf9d0 2021-01-11 op static inline int
33 3c1cf9d0 2021-01-11 op sub_delimiters(int p)
35 3c1cf9d0 2021-01-11 op return p == '!'
49 8404ec30 2021-02-05 op valid_pct_enc_string(char *s)
51 8404ec30 2021-02-05 op if (*s != '%')
54 8404ec30 2021-02-05 op if (!isxdigit(s[1]) || !isxdigit(s[2]))
57 8404ec30 2021-02-05 op if (s[1] == '0' && s[2] == '0')
64 8404ec30 2021-02-05 op valid_pct_encoded(struct parser *p)
66 8404ec30 2021-02-05 op if (p->iri[0] != '%')
69 8404ec30 2021-02-05 op if (!valid_pct_enc_string(p->iri)) {
70 8404ec30 2021-02-05 op p->err = "illegal percent-encoding";
79 9f006a21 2021-02-07 op pct_decode(char *s)
81 9f006a21 2021-02-07 op sscanf(s+1, "%2hhx", s);
82 9f006a21 2021-02-07 op memmove(s+1, s+3, strlen(s+3)+1);
86 3c1cf9d0 2021-01-11 op parse_pct_encoded(struct parser *p)
88 9a672b37 2021-01-28 op if (p->iri[0] != '%')
91 8404ec30 2021-02-05 op if (!valid_pct_enc_string(p->iri)) {
92 3c1cf9d0 2021-01-11 op p->err = "illegal percent-encoding";
96 9f006a21 2021-02-07 op pct_decode(p->iri);
97 3c1cf9d0 2021-01-11 op if (*p->iri == '\0') {
98 3c1cf9d0 2021-01-11 op p->err = "illegal percent-encoding";
105 3c1cf9d0 2021-01-11 op /* ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) "://" */
107 3c1cf9d0 2021-01-11 op parse_scheme(struct parser *p)
109 3c1cf9d0 2021-01-11 op p->parsed->schema = p->iri;
111 3c1cf9d0 2021-01-11 op if (!isalpha(*p->iri)) {
112 3c1cf9d0 2021-01-11 op p->err = "illegal character in scheme";
118 a8a1f439 2021-07-07 op * normalize the scheme (i.e. lowercase it)
120 de428fff 2021-01-13 op * XXX: since we cannot have good things, tolower
121 881dc835 2021-01-16 op * behaviour depends on the LC_CTYPE locale. The good
122 881dc835 2021-01-16 op * news is that we're sure p->iri points to something
123 881dc835 2021-01-16 op * that's in the ASCII range, so tolower can't
124 a8a1f439 2021-07-07 op * mis-behave on some systems due to the locale.
126 de428fff 2021-01-13 op *p->iri = tolower(*p->iri);
128 de428fff 2021-01-13 op } while (isalnum(*p->iri)
129 3c1cf9d0 2021-01-11 op || *p->iri == '+'
130 3c1cf9d0 2021-01-11 op || *p->iri == '-'
131 de428fff 2021-01-13 op || *p->iri == '.');
133 3c1cf9d0 2021-01-11 op if (*p->iri != ':') {
134 3c1cf9d0 2021-01-11 op p->err = "illegal character in scheme";
138 3c1cf9d0 2021-01-11 op *p->iri = '\0';
139 f7b816dc 2021-01-15 op if (p->iri[1] != '/' || p->iri[2] != '/') {
140 3c1cf9d0 2021-01-11 op p->err = "invalid marker after scheme";
150 3c1cf9d0 2021-01-11 op parse_port(struct parser *p)
152 3c1cf9d0 2021-01-11 op uint32_t i = 0;
154 3c1cf9d0 2021-01-11 op p->parsed->port = p->iri;
156 3c1cf9d0 2021-01-11 op for (; isdigit(*p->iri); p->iri++) {
157 3c1cf9d0 2021-01-11 op i = i * 10 + *p->iri - '0';
158 3c1cf9d0 2021-01-11 op if (i > UINT16_MAX) {
159 3c1cf9d0 2021-01-11 op p->err = "port number too large";
164 3c1cf9d0 2021-01-11 op if (*p->iri != '/' && *p->iri != '\0') {
165 3c1cf9d0 2021-01-11 op p->err = "illegal character in port number";
169 3c1cf9d0 2021-01-11 op p->parsed->port_no = i;
171 3c1cf9d0 2021-01-11 op if (*p->iri != '\0') {
172 3c1cf9d0 2021-01-11 op *p->iri = '\0';
179 3c1cf9d0 2021-01-11 op /* TODO: add support for ip-literal and ipv4addr ? */
180 3c1cf9d0 2021-01-11 op /* *( unreserved / sub-delims / pct-encoded ) */
182 3c1cf9d0 2021-01-11 op parse_authority(struct parser *p)
184 3c1cf9d0 2021-01-11 op p->parsed->host = p->iri;
186 3c1cf9d0 2021-01-11 op while (unreserved(*p->iri)
187 3c1cf9d0 2021-01-11 op || sub_delimiters(*p->iri)
188 117ac52c 2021-01-29 op || parse_pct_encoded(p)
189 117ac52c 2021-01-29 op || valid_multibyte_utf8(p)) {
190 e4d82bec 2021-01-15 op /* normalize the host name. */
191 e4d82bec 2021-01-15 op if (*p->iri < 0x7F)
192 e4d82bec 2021-01-15 op *p->iri = tolower(*p->iri);
196 3c1cf9d0 2021-01-11 op if (p->err != NULL)
199 3c1cf9d0 2021-01-11 op if (*p->iri == ':') {
200 3c1cf9d0 2021-01-11 op *p->iri = '\0';
202 3c1cf9d0 2021-01-11 op return parse_port(p);
204 b777bf4b 2021-01-15 op p->parsed->port_no = 1965;
206 3c1cf9d0 2021-01-11 op if (*p->iri == '/') {
207 3c1cf9d0 2021-01-11 op *p->iri = '\0';
212 3c1cf9d0 2021-01-11 op if (*p->iri == '\0')
215 3c1cf9d0 2021-01-11 op p->err = "illegal character in authority section";
220 a8a1f439 2021-07-07 op * Routine for path_clean. Elide the pointed .. with the preceding
221 3c1cf9d0 2021-01-11 op * element. Return 0 if it's not possible. incr is the length of
222 a8a1f439 2021-07-07 op * the increment, 3 for ../ and 2 for ..
225 3c1cf9d0 2021-01-11 op path_elide_dotdot(char *path, char *i, int incr)
229 3c1cf9d0 2021-01-11 op if (i == path)
231 3c1cf9d0 2021-01-11 op for (j = i-2; j != path && *j != '/'; j--)
233 3c1cf9d0 2021-01-11 op if (*j == '/')
236 3c1cf9d0 2021-01-11 op memmove(j, i, strlen(i)+1);
241 3c1cf9d0 2021-01-11 op * Use an algorithm similar to the one implemented in go' path.Clean:
243 3c1cf9d0 2021-01-11 op * 1. Replace multiple slashes with a single slash
244 3c1cf9d0 2021-01-11 op * 2. Eliminate each . path name element
245 3c1cf9d0 2021-01-11 op * 3. Eliminate each inner .. along with the non-.. element that precedes it
246 3c1cf9d0 2021-01-11 op * 4. Eliminate trailing .. if possible or error (go would only discard)
248 3c1cf9d0 2021-01-11 op * Unlike path.Clean, this function return the empty string if the
249 3c1cf9d0 2021-01-11 op * original path is equivalent to "/".
252 3c1cf9d0 2021-01-11 op path_clean(char *path)
256 3c1cf9d0 2021-01-11 op /* 1. replace multiple slashes with a single one */
257 3c1cf9d0 2021-01-11 op for (i = path; *i; ++i) {
258 3c1cf9d0 2021-01-11 op if (*i == '/' && *(i+1) == '/') {
259 3c1cf9d0 2021-01-11 op memmove(i, i+1, strlen(i)); /* move also the \0 */
264 3c1cf9d0 2021-01-11 op /* 2. eliminate each . path name element */
265 3c1cf9d0 2021-01-11 op for (i = path; *i; ++i) {
266 6a9ae707 2021-01-11 op if ((i == path || *i == '/') &&
267 6a9ae707 2021-01-11 op *i != '.' && i[1] == '.' && i[2] == '/') {
268 3c1cf9d0 2021-01-11 op /* move also the \0 */
269 3c1cf9d0 2021-01-11 op memmove(i, i+2, strlen(i)-1);
273 3c1cf9d0 2021-01-11 op if (!strcmp(path, ".") || !strcmp(path, "/.")) {
274 3c1cf9d0 2021-01-11 op *path = '\0';
278 3c1cf9d0 2021-01-11 op /* 3. eliminate each inner .. along with the preceding non-.. */
279 9d092b60 2021-04-12 op for (i = strstr(path, "../"); i != NULL; i = strstr(path, "..")) {
280 9d092b60 2021-04-12 op /* break if we've found a trailing .. */
281 9d092b60 2021-04-12 op if (i[2] == '\0')
283 3c1cf9d0 2021-01-11 op if (!path_elide_dotdot(path, i, 3))
287 3c1cf9d0 2021-01-11 op /* 4. eliminate trailing ..*/
288 3c1cf9d0 2021-01-11 op if ((i = strstr(path, "..")) != NULL)
289 3c1cf9d0 2021-01-11 op if (!path_elide_dotdot(path, i, 2))
296 3c1cf9d0 2021-01-11 op parse_query(struct parser *p)
298 3c1cf9d0 2021-01-11 op p->parsed->query = p->iri;
299 3c1cf9d0 2021-01-11 op if (*p->iri == '\0')
302 3c1cf9d0 2021-01-11 op while (unreserved(*p->iri)
303 3c1cf9d0 2021-01-11 op || sub_delimiters(*p->iri)
304 3c1cf9d0 2021-01-11 op || *p->iri == '/'
305 3c1cf9d0 2021-01-11 op || *p->iri == '?'
306 19e7bd00 2021-02-06 op || *p->iri == ':'
307 19e7bd00 2021-02-06 op || *p->iri == '@'
308 8404ec30 2021-02-05 op || valid_pct_encoded(p)
309 3c1cf9d0 2021-01-11 op || valid_multibyte_utf8(p))
312 3c1cf9d0 2021-01-11 op if (p->err != NULL)
315 3c1cf9d0 2021-01-11 op if (*p->iri != '\0' && *p->iri != '#') {
316 3c1cf9d0 2021-01-11 op p->err = "illegal character in query";
320 3c1cf9d0 2021-01-11 op if (*p->iri != '\0') {
321 3c1cf9d0 2021-01-11 op *p->iri = '\0';
328 3c1cf9d0 2021-01-11 op /* don't even bother */
330 3c1cf9d0 2021-01-11 op parse_fragment(struct parser *p)
332 3c1cf9d0 2021-01-11 op p->parsed->fragment = p->iri;
336 3c1cf9d0 2021-01-11 op /* XXX: is it too broad? */
337 3c1cf9d0 2021-01-11 op /* *(pchar / "/") */
339 3c1cf9d0 2021-01-11 op parse_path(struct parser *p)
343 42bbdc79 2021-01-21 op /* trim initial slashes */
344 42bbdc79 2021-01-21 op while (*p->iri == '/')
347 3c1cf9d0 2021-01-11 op p->parsed->path = p->iri;
348 3c1cf9d0 2021-01-11 op if (*p->iri == '\0') {
349 3c1cf9d0 2021-01-11 op p->parsed->query = p->parsed->fragment = p->iri;
353 3c1cf9d0 2021-01-11 op while (unreserved(*p->iri)
354 3c1cf9d0 2021-01-11 op || sub_delimiters(*p->iri)
355 3c1cf9d0 2021-01-11 op || *p->iri == '/'
356 3c1cf9d0 2021-01-11 op || parse_pct_encoded(p)
357 3c1cf9d0 2021-01-11 op || valid_multibyte_utf8(p))
360 3c1cf9d0 2021-01-11 op if (p->err != NULL)
363 3c1cf9d0 2021-01-11 op if (*p->iri != '\0' && *p->iri != '?' && *p->iri != '#') {
364 3c1cf9d0 2021-01-11 op p->err = "illegal character in path";
368 3c1cf9d0 2021-01-11 op if (*p->iri != '\0') {
370 3c1cf9d0 2021-01-11 op *p->iri = '\0';
373 3c1cf9d0 2021-01-11 op if (c == '#') {
374 3c1cf9d0 2021-01-11 op if (!parse_fragment(p))
377 3c1cf9d0 2021-01-11 op if (!parse_query(p) || !parse_fragment(p))
381 3c1cf9d0 2021-01-11 op if (!path_clean(p->parsed->path)) {
382 3c1cf9d0 2021-01-11 op p->err = "illegal path";
390 3c1cf9d0 2021-01-11 op parse_iri(char *iri, struct iri *ret, const char **err_ret)
393 3c1cf9d0 2021-01-11 op struct parser p = {iri, ret, NULL};
395 3c1cf9d0 2021-01-11 op bzero(ret, sizeof(*ret));
397 3c1cf9d0 2021-01-11 op /* initialize optional stuff to the empty string */
398 3c1cf9d0 2021-01-11 op end = iri + strlen(iri);
399 57d0d0ad 2021-01-31 op p.parsed->host = end;
400 3c1cf9d0 2021-01-11 op p.parsed->port = end;
401 3c1cf9d0 2021-01-11 op p.parsed->path = end;
402 3c1cf9d0 2021-01-11 op p.parsed->query = end;
403 3c1cf9d0 2021-01-11 op p.parsed->fragment = end;
405 3c1cf9d0 2021-01-11 op if (!parse_scheme(&p) || !parse_authority(&p) || !parse_path(&p)) {
406 3c1cf9d0 2021-01-11 op *err_ret = p.err;
410 3c1cf9d0 2021-01-11 op *err_ret = NULL;
415 c4f682f8 2021-01-27 op trim_req_iri(char *iri, const char **err)
419 c4f682f8 2021-01-27 op if ((i = strstr(iri, "\r\n")) == NULL) {
420 c4f682f8 2021-01-27 op *err = "missing CRLF";
429 2fafa2d2 2021-02-01 op serialize_iri(struct iri *i, char *buf, size_t len)
431 80fbf1e9 2021-06-16 op size_t l = 0;
433 2fafa2d2 2021-02-01 op /* in ex.c we receive empty "" strings as NULL */
434 2fafa2d2 2021-02-01 op if (i->schema == NULL || i->host == NULL) {
435 2fafa2d2 2021-02-01 op memset(buf, 0, len);
439 2fafa2d2 2021-02-01 op strlcpy(buf, i->schema, len);
440 2fafa2d2 2021-02-01 op strlcat(buf, "://", len);
441 2fafa2d2 2021-02-01 op strlcat(buf, i->host, len);
442 2fafa2d2 2021-02-01 op strlcat(buf, "/", len);
444 2fafa2d2 2021-02-01 op if (i->path != NULL)
445 2fafa2d2 2021-02-01 op l = strlcat(buf, i->path, len);
447 2fafa2d2 2021-02-01 op if (i->query != NULL && *i->query != '\0') {
448 2fafa2d2 2021-02-01 op strlcat(buf, "?", len);
449 2fafa2d2 2021-02-01 op l = strlcat(buf, i->query, len);
452 2fafa2d2 2021-02-01 op return l < len;
456 9f006a21 2021-02-07 op pct_decode_str(char *s)
460 9f006a21 2021-02-07 op for (t = s; *t; ++t) {
461 9f006a21 2021-02-07 op if (*t == '%' && valid_pct_enc_string(t))
462 9f006a21 2021-02-07 op pct_decode(t);