Blob


1 /*
2 * Copyright (c) 2020 Omar Polo <op@omarpolo.com>
3 *
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.
7 *
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.
15 */
17 #include <ctype.h>
18 #include <string.h>
20 #include "gmid.h"
22 static inline int
23 unreserved(int p)
24 {
25 return isalnum(p)
26 || p == '-'
27 || p == '.'
28 || p == '_'
29 || p == '~';
30 }
32 static inline int
33 sub_delimiters(int p)
34 {
35 return p == '!'
36 || p == '$'
37 || p == '&'
38 || p == '\''
39 || p == '('
40 || p == ')'
41 || p == '*'
42 || p == '+'
43 || p == ','
44 || p == ';'
45 || p == '=';
46 }
48 static int
49 valid_pct_enc_string(char *s)
50 {
51 if (*s != '%')
52 return 1;
54 if (!isxdigit(s[1]) || !isxdigit(s[2]))
55 return 0;
57 if (s[1] == '0' && s[2] == '0')
58 return 0;
60 return 1;
61 }
63 static int
64 valid_pct_encoded(struct parser *p)
65 {
66 if (p->iri[0] != '%')
67 return 0;
69 if (!valid_pct_enc_string(p->iri)) {
70 p->err = "illegal percent-encoding";
71 return 0;
72 }
74 p->iri += 2;
75 return 1;
76 }
78 static int
79 parse_pct_encoded(struct parser *p)
80 {
81 if (p->iri[0] != '%')
82 return 0;
84 if (!valid_pct_enc_string(p->iri)) {
85 p->err = "illegal percent-encoding";
86 return 0;
87 }
89 sscanf(p->iri+1, "%2hhx", p->iri);
90 memmove(p->iri+1, p->iri+3, strlen(p->iri+3)+1);
91 if (*p->iri == '\0') {
92 p->err = "illegal percent-encoding";
93 return 0;
94 }
96 return 1;
97 }
99 /* ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) "://" */
100 static int
101 parse_scheme(struct parser *p)
103 p->parsed->schema = p->iri;
105 if (!isalpha(*p->iri)) {
106 p->err = "illegal character in scheme";
107 return 0;
110 do {
111 /* normalize the scheme (i.e. lowercase it)
113 * XXX: since we cannot have good things, tolower
114 * behaviour depends on the LC_CTYPE locale. The good
115 * news is that we're sure p->iri points to something
116 * that's in the ASCII range, so tolower can't
117 * mis-behave on some systems due to the locale. */
118 *p->iri = tolower(*p->iri);
119 p->iri++;
120 } while (isalnum(*p->iri)
121 || *p->iri == '+'
122 || *p->iri == '-'
123 || *p->iri == '.');
125 if (*p->iri != ':') {
126 p->err = "illegal character in scheme";
127 return 0;
130 *p->iri = '\0';
131 if (p->iri[1] != '/' || p->iri[2] != '/') {
132 p->err = "invalid marker after scheme";
133 return 0;
136 p->iri += 3;
137 return 1;
140 /* *DIGIT */
141 static int
142 parse_port(struct parser *p)
144 uint32_t i = 0;
146 p->parsed->port = p->iri;
148 for (; isdigit(*p->iri); p->iri++) {
149 i = i * 10 + *p->iri - '0';
150 if (i > UINT16_MAX) {
151 p->err = "port number too large";
152 return 0;
156 if (*p->iri != '/' && *p->iri != '\0') {
157 p->err = "illegal character in port number";
158 return 0;
161 p->parsed->port_no = i;
163 if (*p->iri != '\0') {
164 *p->iri = '\0';
165 p->iri++;
168 return 1;
171 /* TODO: add support for ip-literal and ipv4addr ? */
172 /* *( unreserved / sub-delims / pct-encoded ) */
173 static int
174 parse_authority(struct parser *p)
176 p->parsed->host = p->iri;
178 while (unreserved(*p->iri)
179 || sub_delimiters(*p->iri)
180 || parse_pct_encoded(p)
181 || valid_multibyte_utf8(p)) {
182 /* normalize the host name. */
183 if (*p->iri < 0x7F)
184 *p->iri = tolower(*p->iri);
185 p->iri++;
188 if (p->err != NULL)
189 return 0;
191 if (*p->iri == ':') {
192 *p->iri = '\0';
193 p->iri++;
194 return parse_port(p);
195 } else
196 p->parsed->port_no = 1965;
198 if (*p->iri == '/') {
199 *p->iri = '\0';
200 p->iri++;
201 return 1;
204 if (*p->iri == '\0')
205 return 1;
207 p->err = "illegal character in authority section";
208 return 0;
211 /* Routine for path_clean. Elide the pointed .. with the preceding
212 * element. Return 0 if it's not possible. incr is the length of
213 * the increment, 3 for ../ and 2 for .. */
214 static int
215 path_elide_dotdot(char *path, char *i, int incr)
217 char *j;
219 if (i == path)
220 return 0;
221 for (j = i-2; j != path && *j != '/'; j--)
222 /* noop */ ;
223 if (*j == '/')
224 j++;
225 i += incr;
226 memmove(j, i, strlen(i)+1);
227 return 1;
230 /*
231 * Use an algorithm similar to the one implemented in go' path.Clean:
233 * 1. Replace multiple slashes with a single slash
234 * 2. Eliminate each . path name element
235 * 3. Eliminate each inner .. along with the non-.. element that precedes it
236 * 4. Eliminate trailing .. if possible or error (go would only discard)
238 * Unlike path.Clean, this function return the empty string if the
239 * original path is equivalent to "/".
240 */
241 static int
242 path_clean(char *path)
244 char *i;
246 /* 1. replace multiple slashes with a single one */
247 for (i = path; *i; ++i) {
248 if (*i == '/' && *(i+1) == '/') {
249 memmove(i, i+1, strlen(i)); /* move also the \0 */
250 i--;
254 /* 2. eliminate each . path name element */
255 for (i = path; *i; ++i) {
256 if ((i == path || *i == '/') &&
257 *i != '.' && i[1] == '.' && i[2] == '/') {
258 /* move also the \0 */
259 memmove(i, i+2, strlen(i)-1);
260 i--;
263 if (!strcmp(path, ".") || !strcmp(path, "/.")) {
264 *path = '\0';
265 return 1;
268 /* 3. eliminate each inner .. along with the preceding non-.. */
269 for (i = strstr(path, "../"); i != NULL; i = strstr(path, ".."))
270 if (!path_elide_dotdot(path, i, 3))
271 return 0;
273 /* 4. eliminate trailing ..*/
274 if ((i = strstr(path, "..")) != NULL)
275 if (!path_elide_dotdot(path, i, 2))
276 return 0;
278 return 1;
281 static int
282 parse_query(struct parser *p)
284 p->parsed->query = p->iri;
285 if (*p->iri == '\0')
286 return 1;
288 while (unreserved(*p->iri)
289 || sub_delimiters(*p->iri)
290 || *p->iri == '/'
291 || *p->iri == '?'
292 || *p->iri == ':'
293 || *p->iri == '@'
294 || valid_pct_encoded(p)
295 || valid_multibyte_utf8(p))
296 p->iri++;
298 if (p->err != NULL)
299 return 0;
301 if (*p->iri != '\0' && *p->iri != '#') {
302 p->err = "illegal character in query";
303 return 0;
306 if (*p->iri != '\0') {
307 *p->iri = '\0';
308 p->iri++;
311 return 1;
314 /* don't even bother */
315 static int
316 parse_fragment(struct parser *p)
318 p->parsed->fragment = p->iri;
319 return 1;
322 /* XXX: is it too broad? */
323 /* *(pchar / "/") */
324 static int
325 parse_path(struct parser *p)
327 char c;
329 /* trim initial slashes */
330 while (*p->iri == '/')
331 p->iri++;
333 p->parsed->path = p->iri;
334 if (*p->iri == '\0') {
335 p->parsed->query = p->parsed->fragment = p->iri;
336 return 1;
339 while (unreserved(*p->iri)
340 || sub_delimiters(*p->iri)
341 || *p->iri == '/'
342 || parse_pct_encoded(p)
343 || valid_multibyte_utf8(p))
344 p->iri++;
346 if (p->err != NULL)
347 return 0;
349 if (*p->iri != '\0' && *p->iri != '?' && *p->iri != '#') {
350 p->err = "illegal character in path";
351 return 0;
354 if (*p->iri != '\0') {
355 c = *p->iri;
356 *p->iri = '\0';
357 p->iri++;
359 if (c == '#') {
360 if (!parse_fragment(p))
361 return 0;
362 } else
363 if (!parse_query(p) || !parse_fragment(p))
364 return 0;
367 if (!path_clean(p->parsed->path)) {
368 p->err = "illegal path";
369 return 0;
372 return 1;
375 int
376 parse_iri(char *iri, struct iri *ret, const char **err_ret)
378 char *end;
379 struct parser p = {iri, ret, NULL};
381 bzero(ret, sizeof(*ret));
383 /* initialize optional stuff to the empty string */
384 end = iri + strlen(iri);
385 p.parsed->host = end;
386 p.parsed->port = end;
387 p.parsed->path = end;
388 p.parsed->query = end;
389 p.parsed->fragment = end;
391 if (!parse_scheme(&p) || !parse_authority(&p) || !parse_path(&p)) {
392 *err_ret = p.err;
393 return 0;
396 *err_ret = NULL;
397 return 1;
400 int
401 trim_req_iri(char *iri, const char **err)
403 char *i;
405 if ((i = strstr(iri, "\r\n")) == NULL) {
406 *err = "missing CRLF";
407 return 0;
409 *i = '\0';
410 return 1;
414 int
415 serialize_iri(struct iri *i, char *buf, size_t len)
417 size_t l;
419 /* in ex.c we receive empty "" strings as NULL */
420 if (i->schema == NULL || i->host == NULL) {
421 memset(buf, 0, len);
422 return 0;
425 strlcpy(buf, i->schema, len);
426 strlcat(buf, "://", len);
427 strlcat(buf, i->host, len);
428 strlcat(buf, "/", len);
430 if (i->path != NULL)
431 l = strlcat(buf, i->path, len);
433 if (i->query != NULL && *i->query != '\0') {
434 strlcat(buf, "?", len);
435 l = strlcat(buf, i->query, len);
438 return l < len;