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 3c1cf9d0 2021-01-11 op #include <ctype.h>
18 3c1cf9d0 2021-01-11 op #include <string.h>
20 3c1cf9d0 2021-01-11 op #include "gmid.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 3c1cf9d0 2021-01-11 op parse_pct_encoded(struct parser *p)
51 9a672b37 2021-01-28 op if (p->iri[0] != '%')
54 9a672b37 2021-01-28 op if (!isxdigit(p->iri[1]) || !isxdigit(p->iri[2])) {
55 3c1cf9d0 2021-01-11 op p->err = "illegal percent-encoding";
59 3c1cf9d0 2021-01-11 op sscanf(p->iri+1, "%2hhx", p->iri);
60 3c1cf9d0 2021-01-11 op memmove(p->iri+1, p->iri+3, strlen(p->iri+3)+1);
61 3c1cf9d0 2021-01-11 op if (*p->iri == '\0') {
62 3c1cf9d0 2021-01-11 op p->err = "illegal percent-encoding";
69 3c1cf9d0 2021-01-11 op /* ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) "://" */
71 3c1cf9d0 2021-01-11 op parse_scheme(struct parser *p)
73 3c1cf9d0 2021-01-11 op p->parsed->schema = p->iri;
75 3c1cf9d0 2021-01-11 op if (!isalpha(*p->iri)) {
76 3c1cf9d0 2021-01-11 op p->err = "illegal character in scheme";
81 de428fff 2021-01-13 op /* normalize the scheme (i.e. lowercase it)
83 de428fff 2021-01-13 op * XXX: since we cannot have good things, tolower
84 881dc835 2021-01-16 op * behaviour depends on the LC_CTYPE locale. The good
85 881dc835 2021-01-16 op * news is that we're sure p->iri points to something
86 881dc835 2021-01-16 op * that's in the ASCII range, so tolower can't
87 881dc835 2021-01-16 op * mis-behave on some systems due to the locale. */
88 de428fff 2021-01-13 op *p->iri = tolower(*p->iri);
90 de428fff 2021-01-13 op } while (isalnum(*p->iri)
91 3c1cf9d0 2021-01-11 op || *p->iri == '+'
92 3c1cf9d0 2021-01-11 op || *p->iri == '-'
93 de428fff 2021-01-13 op || *p->iri == '.');
95 3c1cf9d0 2021-01-11 op if (*p->iri != ':') {
96 3c1cf9d0 2021-01-11 op p->err = "illegal character in scheme";
100 3c1cf9d0 2021-01-11 op *p->iri = '\0';
101 f7b816dc 2021-01-15 op if (p->iri[1] != '/' || p->iri[2] != '/') {
102 3c1cf9d0 2021-01-11 op p->err = "invalid marker after scheme";
112 3c1cf9d0 2021-01-11 op parse_port(struct parser *p)
114 3c1cf9d0 2021-01-11 op uint32_t i = 0;
116 3c1cf9d0 2021-01-11 op p->parsed->port = p->iri;
118 3c1cf9d0 2021-01-11 op for (; isdigit(*p->iri); p->iri++) {
119 3c1cf9d0 2021-01-11 op i = i * 10 + *p->iri - '0';
120 3c1cf9d0 2021-01-11 op if (i > UINT16_MAX) {
121 3c1cf9d0 2021-01-11 op p->err = "port number too large";
126 3c1cf9d0 2021-01-11 op if (*p->iri != '/' && *p->iri != '\0') {
127 3c1cf9d0 2021-01-11 op p->err = "illegal character in port number";
131 3c1cf9d0 2021-01-11 op p->parsed->port_no = i;
133 3c1cf9d0 2021-01-11 op if (*p->iri != '\0') {
134 3c1cf9d0 2021-01-11 op *p->iri = '\0';
141 3c1cf9d0 2021-01-11 op /* TODO: add support for ip-literal and ipv4addr ? */
142 3c1cf9d0 2021-01-11 op /* *( unreserved / sub-delims / pct-encoded ) */
144 3c1cf9d0 2021-01-11 op parse_authority(struct parser *p)
146 3c1cf9d0 2021-01-11 op p->parsed->host = p->iri;
148 3c1cf9d0 2021-01-11 op while (unreserved(*p->iri)
149 3c1cf9d0 2021-01-11 op || sub_delimiters(*p->iri)
150 117ac52c 2021-01-29 op || parse_pct_encoded(p)
151 117ac52c 2021-01-29 op || valid_multibyte_utf8(p)) {
152 e4d82bec 2021-01-15 op /* normalize the host name. */
153 e4d82bec 2021-01-15 op if (*p->iri < 0x7F)
154 e4d82bec 2021-01-15 op *p->iri = tolower(*p->iri);
158 3c1cf9d0 2021-01-11 op if (p->err != NULL)
161 3c1cf9d0 2021-01-11 op if (*p->iri == ':') {
162 3c1cf9d0 2021-01-11 op *p->iri = '\0';
164 3c1cf9d0 2021-01-11 op return parse_port(p);
166 b777bf4b 2021-01-15 op p->parsed->port_no = 1965;
168 3c1cf9d0 2021-01-11 op if (*p->iri == '/') {
169 3c1cf9d0 2021-01-11 op *p->iri = '\0';
174 3c1cf9d0 2021-01-11 op if (*p->iri == '\0')
177 3c1cf9d0 2021-01-11 op p->err = "illegal character in authority section";
181 3c1cf9d0 2021-01-11 op /* Routine for path_clean. Elide the pointed .. with the preceding
182 3c1cf9d0 2021-01-11 op * element. Return 0 if it's not possible. incr is the length of
183 3c1cf9d0 2021-01-11 op * the increment, 3 for ../ and 2 for .. */
185 3c1cf9d0 2021-01-11 op path_elide_dotdot(char *path, char *i, int incr)
189 3c1cf9d0 2021-01-11 op if (i == path)
191 3c1cf9d0 2021-01-11 op for (j = i-2; j != path && *j != '/'; j--)
193 3c1cf9d0 2021-01-11 op if (*j == '/')
196 3c1cf9d0 2021-01-11 op memmove(j, i, strlen(i)+1);
201 3c1cf9d0 2021-01-11 op * Use an algorithm similar to the one implemented in go' path.Clean:
203 3c1cf9d0 2021-01-11 op * 1. Replace multiple slashes with a single slash
204 3c1cf9d0 2021-01-11 op * 2. Eliminate each . path name element
205 3c1cf9d0 2021-01-11 op * 3. Eliminate each inner .. along with the non-.. element that precedes it
206 3c1cf9d0 2021-01-11 op * 4. Eliminate trailing .. if possible or error (go would only discard)
208 3c1cf9d0 2021-01-11 op * Unlike path.Clean, this function return the empty string if the
209 3c1cf9d0 2021-01-11 op * original path is equivalent to "/".
212 3c1cf9d0 2021-01-11 op path_clean(char *path)
216 3c1cf9d0 2021-01-11 op /* 1. replace multiple slashes with a single one */
217 3c1cf9d0 2021-01-11 op for (i = path; *i; ++i) {
218 3c1cf9d0 2021-01-11 op if (*i == '/' && *(i+1) == '/') {
219 3c1cf9d0 2021-01-11 op memmove(i, i+1, strlen(i)); /* move also the \0 */
224 3c1cf9d0 2021-01-11 op /* 2. eliminate each . path name element */
225 3c1cf9d0 2021-01-11 op for (i = path; *i; ++i) {
226 6a9ae707 2021-01-11 op if ((i == path || *i == '/') &&
227 6a9ae707 2021-01-11 op *i != '.' && i[1] == '.' && i[2] == '/') {
228 3c1cf9d0 2021-01-11 op /* move also the \0 */
229 3c1cf9d0 2021-01-11 op memmove(i, i+2, strlen(i)-1);
233 3c1cf9d0 2021-01-11 op if (!strcmp(path, ".") || !strcmp(path, "/.")) {
234 3c1cf9d0 2021-01-11 op *path = '\0';
238 3c1cf9d0 2021-01-11 op /* 3. eliminate each inner .. along with the preceding non-.. */
239 3c1cf9d0 2021-01-11 op for (i = strstr(path, "../"); i != NULL; i = strstr(path, ".."))
240 3c1cf9d0 2021-01-11 op if (!path_elide_dotdot(path, i, 3))
243 3c1cf9d0 2021-01-11 op /* 4. eliminate trailing ..*/
244 3c1cf9d0 2021-01-11 op if ((i = strstr(path, "..")) != NULL)
245 3c1cf9d0 2021-01-11 op if (!path_elide_dotdot(path, i, 2))
252 3c1cf9d0 2021-01-11 op parse_query(struct parser *p)
254 3c1cf9d0 2021-01-11 op p->parsed->query = p->iri;
255 3c1cf9d0 2021-01-11 op if (*p->iri == '\0')
258 3c1cf9d0 2021-01-11 op while (unreserved(*p->iri)
259 3c1cf9d0 2021-01-11 op || sub_delimiters(*p->iri)
260 3c1cf9d0 2021-01-11 op || *p->iri == '/'
261 3c1cf9d0 2021-01-11 op || *p->iri == '?'
262 3c1cf9d0 2021-01-11 op || parse_pct_encoded(p)
263 3c1cf9d0 2021-01-11 op || valid_multibyte_utf8(p))
266 3c1cf9d0 2021-01-11 op if (p->err != NULL)
269 3c1cf9d0 2021-01-11 op if (*p->iri != '\0' && *p->iri != '#') {
270 3c1cf9d0 2021-01-11 op p->err = "illegal character in query";
274 3c1cf9d0 2021-01-11 op if (*p->iri != '\0') {
275 3c1cf9d0 2021-01-11 op *p->iri = '\0';
282 3c1cf9d0 2021-01-11 op /* don't even bother */
284 3c1cf9d0 2021-01-11 op parse_fragment(struct parser *p)
286 3c1cf9d0 2021-01-11 op p->parsed->fragment = p->iri;
290 3c1cf9d0 2021-01-11 op /* XXX: is it too broad? */
291 3c1cf9d0 2021-01-11 op /* *(pchar / "/") */
293 3c1cf9d0 2021-01-11 op parse_path(struct parser *p)
297 42bbdc79 2021-01-21 op /* trim initial slashes */
298 42bbdc79 2021-01-21 op while (*p->iri == '/')
301 3c1cf9d0 2021-01-11 op p->parsed->path = p->iri;
302 3c1cf9d0 2021-01-11 op if (*p->iri == '\0') {
303 3c1cf9d0 2021-01-11 op p->parsed->query = p->parsed->fragment = p->iri;
307 3c1cf9d0 2021-01-11 op while (unreserved(*p->iri)
308 3c1cf9d0 2021-01-11 op || sub_delimiters(*p->iri)
309 3c1cf9d0 2021-01-11 op || *p->iri == '/'
310 3c1cf9d0 2021-01-11 op || parse_pct_encoded(p)
311 3c1cf9d0 2021-01-11 op || valid_multibyte_utf8(p))
314 3c1cf9d0 2021-01-11 op if (p->err != NULL)
317 3c1cf9d0 2021-01-11 op if (*p->iri != '\0' && *p->iri != '?' && *p->iri != '#') {
318 3c1cf9d0 2021-01-11 op p->err = "illegal character in path";
322 3c1cf9d0 2021-01-11 op if (*p->iri != '\0') {
324 3c1cf9d0 2021-01-11 op *p->iri = '\0';
327 3c1cf9d0 2021-01-11 op if (c == '#') {
328 3c1cf9d0 2021-01-11 op if (!parse_fragment(p))
331 3c1cf9d0 2021-01-11 op if (!parse_query(p) || !parse_fragment(p))
335 3c1cf9d0 2021-01-11 op if (!path_clean(p->parsed->path)) {
336 3c1cf9d0 2021-01-11 op p->err = "illegal path";
344 3c1cf9d0 2021-01-11 op parse_iri(char *iri, struct iri *ret, const char **err_ret)
347 3c1cf9d0 2021-01-11 op struct parser p = {iri, ret, NULL};
349 3c1cf9d0 2021-01-11 op bzero(ret, sizeof(*ret));
351 3c1cf9d0 2021-01-11 op /* initialize optional stuff to the empty string */
352 3c1cf9d0 2021-01-11 op end = iri + strlen(iri);
353 57d0d0ad 2021-01-31 op p.parsed->host = end;
354 3c1cf9d0 2021-01-11 op p.parsed->port = end;
355 3c1cf9d0 2021-01-11 op p.parsed->path = end;
356 3c1cf9d0 2021-01-11 op p.parsed->query = end;
357 3c1cf9d0 2021-01-11 op p.parsed->fragment = end;
359 3c1cf9d0 2021-01-11 op if (!parse_scheme(&p) || !parse_authority(&p) || !parse_path(&p)) {
360 3c1cf9d0 2021-01-11 op *err_ret = p.err;
364 3c1cf9d0 2021-01-11 op *err_ret = NULL;
369 c4f682f8 2021-01-27 op trim_req_iri(char *iri, const char **err)
373 c4f682f8 2021-01-27 op if ((i = strstr(iri, "\r\n")) == NULL) {
374 c4f682f8 2021-01-27 op *err = "missing CRLF";
383 2fafa2d2 2021-02-01 op serialize_iri(struct iri *i, char *buf, size_t len)
387 2fafa2d2 2021-02-01 op /* in ex.c we receive empty "" strings as NULL */
388 2fafa2d2 2021-02-01 op if (i->schema == NULL || i->host == NULL) {
389 2fafa2d2 2021-02-01 op memset(buf, 0, len);
393 2fafa2d2 2021-02-01 op strlcpy(buf, i->schema, len);
394 2fafa2d2 2021-02-01 op strlcat(buf, "://", len);
395 2fafa2d2 2021-02-01 op strlcat(buf, i->host, len);
396 2fafa2d2 2021-02-01 op strlcat(buf, "/", len);
398 2fafa2d2 2021-02-01 op if (i->path != NULL)
399 2fafa2d2 2021-02-01 op l = strlcat(buf, i->path, len);
401 2fafa2d2 2021-02-01 op if (i->query != NULL && *i->query != '\0') {
402 2fafa2d2 2021-02-01 op strlcat(buf, "?", len);
403 2fafa2d2 2021-02-01 op l = strlcat(buf, i->query, len);
406 2fafa2d2 2021-02-01 op return l < len;