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 while (isalnum(*p->iri)
88 p->err = "illegal character in scheme";
93 if (*(++p->iri) != '/' || *(++p->iri) != '/') {
94 p->err = "invalid marker after scheme";
104 parse_port(struct parser *p)
108 p->parsed->port = p->iri;
110 for (; isdigit(*p->iri); p->iri++) {
111 i = i * 10 + *p->iri - '0';
112 if (i > UINT16_MAX) {
113 p->err = "port number too large";
118 if (*p->iri != '/' && *p->iri != '\0') {
119 p->err = "illegal character in port number";
123 p->parsed->port_no = i;
125 if (*p->iri != '\0') {
133 /* TODO: add support for ip-literal and ipv4addr ? */
134 /* *( unreserved / sub-delims / pct-encoded ) */
136 parse_authority(struct parser *p)
138 p->parsed->host = p->iri;
140 while (unreserved(*p->iri)
141 || sub_delimiters(*p->iri)
142 || parse_pct_encoded(p))
148 if (*p->iri == ':') {
151 return parse_port(p);
154 if (*p->iri == '/') {
163 p->err = "illegal character in authority section";
167 /* Routine for path_clean. Elide the pointed .. with the preceding
168 * element. Return 0 if it's not possible. incr is the length of
169 * the increment, 3 for ../ and 2 for .. */
171 path_elide_dotdot(char *path, char *i, int incr)
177 for (j = i-2; j != path && *j != '/'; j--)
182 memmove(j, i, strlen(i)+1);
187 * Use an algorithm similar to the one implemented in go' path.Clean:
189 * 1. Replace multiple slashes with a single slash
190 * 2. Eliminate each . path name element
191 * 3. Eliminate each inner .. along with the non-.. element that precedes it
192 * 4. Eliminate trailing .. if possible or error (go would only discard)
194 * Unlike path.Clean, this function return the empty string if the
195 * original path is equivalent to "/".
198 path_clean(char *path)
202 /* 1. replace multiple slashes with a single one */
203 for (i = path; *i; ++i) {
204 if (*i == '/' && *(i+1) == '/') {
205 memmove(i, i+1, strlen(i)); /* move also the \0 */
210 /* 2. eliminate each . path name element */
211 for (i = path; *i; ++i) {
212 if ((i == path || *i == '/') && *(i+1) == '.' &&
214 /* move also the \0 */
215 memmove(i, i+2, strlen(i)-1);
219 if (!strcmp(path, ".") || !strcmp(path, "/.")) {
224 /* 3. eliminate each inner .. along with the preceding non-.. */
225 for (i = strstr(path, "../"); i != NULL; i = strstr(path, ".."))
226 if (!path_elide_dotdot(path, i, 3))
229 /* 4. eliminate trailing ..*/
230 if ((i = strstr(path, "..")) != NULL)
231 if (!path_elide_dotdot(path, i, 2))
238 parse_query(struct parser *p)
240 p->parsed->query = p->iri;
244 while (unreserved(*p->iri)
245 || sub_delimiters(*p->iri)
248 || parse_pct_encoded(p)
249 || valid_multibyte_utf8(p))
255 if (*p->iri != '\0' && *p->iri != '#') {
256 p->err = "illegal character in query";
260 if (*p->iri != '\0') {
268 /* don't even bother */
270 parse_fragment(struct parser *p)
272 p->parsed->fragment = p->iri;
276 /* XXX: is it too broad? */
279 parse_path(struct parser *p)
283 p->parsed->path = p->iri;
284 if (*p->iri == '\0') {
285 p->parsed->query = p->parsed->fragment = p->iri;
289 while (unreserved(*p->iri)
290 || sub_delimiters(*p->iri)
292 || parse_pct_encoded(p)
293 || valid_multibyte_utf8(p))
299 if (*p->iri != '\0' && *p->iri != '?' && *p->iri != '#') {
300 p->err = "illegal character in path";
304 if (*p->iri != '\0') {
310 if (!parse_fragment(p))
313 if (!parse_query(p) || !parse_fragment(p))
317 if (!path_clean(p->parsed->path)) {
318 p->err = "illegal path";
326 parse_iri(char *iri, struct iri *ret, const char **err_ret)
329 struct parser p = {iri, ret, NULL};
331 bzero(ret, sizeof(*ret));
333 /* initialize optional stuff to the empty string */
334 end = iri + strlen(iri);
335 p.parsed->port = end;
336 p.parsed->path = end;
337 p.parsed->query = end;
338 p.parsed->fragment = end;
340 if (!parse_scheme(&p) || !parse_authority(&p) || !parse_path(&p)) {
350 trim_req_iri(char *iri)
354 if ((i = strstr(iri, "\r\n")) == NULL)