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 "gmid.h"
19 #include <ctype.h>
20 #include <string.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 void
79 pct_decode(char *s)
80 {
81 sscanf(s+1, "%2hhx", s);
82 memmove(s+1, s+3, strlen(s+3)+1);
83 }
85 static int
86 parse_pct_encoded(struct parser *p)
87 {
88 if (p->iri[0] != '%')
89 return 0;
91 if (!valid_pct_enc_string(p->iri)) {
92 p->err = "illegal percent-encoding";
93 return 0;
94 }
96 pct_decode(p->iri);
97 if (*p->iri == '\0') {
98 p->err = "illegal percent-encoding";
99 return 0;
102 return 1;
105 /* ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) "://" */
106 static int
107 parse_scheme(struct parser *p)
109 p->parsed->schema = p->iri;
111 if (!isalpha(*p->iri)) {
112 p->err = "illegal character in scheme";
113 return 0;
116 do {
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);
125 p->iri++;
126 } while (isalnum(*p->iri)
127 || *p->iri == '+'
128 || *p->iri == '-'
129 || *p->iri == '.');
131 if (*p->iri != ':') {
132 p->err = "illegal character in scheme";
133 return 0;
136 *p->iri = '\0';
137 if (p->iri[1] != '/' || p->iri[2] != '/') {
138 p->err = "invalid marker after scheme";
139 return 0;
142 p->iri += 3;
143 return 1;
146 /* *DIGIT */
147 static int
148 parse_port(struct parser *p)
150 uint32_t i = 0;
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";
158 return 0;
162 if (*p->iri != '/' && *p->iri != '\0') {
163 p->err = "illegal character in port number";
164 return 0;
167 p->parsed->port_no = i;
169 if (*p->iri != '\0') {
170 *p->iri = '\0';
171 p->iri++;
174 return 1;
177 /* TODO: add support for ip-literal and ipv4addr ? */
178 /* *( unreserved / sub-delims / pct-encoded ) */
179 static int
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. */
189 if (*p->iri < 0x7F)
190 *p->iri = tolower(*p->iri);
191 p->iri++;
194 if (p->err != NULL)
195 return 0;
197 if (*p->iri == ':') {
198 *p->iri = '\0';
199 p->iri++;
200 return parse_port(p);
201 } else
202 p->parsed->port_no = 1965;
204 if (*p->iri == '/') {
205 *p->iri = '\0';
206 p->iri++;
207 return 1;
210 if (*p->iri == '\0')
211 return 1;
213 p->err = "illegal character in authority section";
214 return 0;
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 .. */
220 static int
221 path_elide_dotdot(char *path, char *i, int incr)
223 char *j;
225 if (i == path)
226 return 0;
227 for (j = i-2; j != path && *j != '/'; j--)
228 /* noop */ ;
229 if (*j == '/')
230 j++;
231 i += incr;
232 memmove(j, i, strlen(i)+1);
233 return 1;
236 /*
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 "/".
246 */
247 static int
248 path_clean(char *path)
250 char *i;
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 */
256 i--;
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);
266 i--;
269 if (!strcmp(path, ".") || !strcmp(path, "/.")) {
270 *path = '\0';
271 return 1;
274 /* 3. eliminate each inner .. along with the preceding non-.. */
275 for (i = strstr(path, "../"); i != NULL; i = strstr(path, ".."))
276 if (!path_elide_dotdot(path, i, 3))
277 return 0;
279 /* 4. eliminate trailing ..*/
280 if ((i = strstr(path, "..")) != NULL)
281 if (!path_elide_dotdot(path, i, 2))
282 return 0;
284 return 1;
287 static int
288 parse_query(struct parser *p)
290 p->parsed->query = p->iri;
291 if (*p->iri == '\0')
292 return 1;
294 while (unreserved(*p->iri)
295 || sub_delimiters(*p->iri)
296 || *p->iri == '/'
297 || *p->iri == '?'
298 || *p->iri == ':'
299 || *p->iri == '@'
300 || valid_pct_encoded(p)
301 || valid_multibyte_utf8(p))
302 p->iri++;
304 if (p->err != NULL)
305 return 0;
307 if (*p->iri != '\0' && *p->iri != '#') {
308 p->err = "illegal character in query";
309 return 0;
312 if (*p->iri != '\0') {
313 *p->iri = '\0';
314 p->iri++;
317 return 1;
320 /* don't even bother */
321 static int
322 parse_fragment(struct parser *p)
324 p->parsed->fragment = p->iri;
325 return 1;
328 /* XXX: is it too broad? */
329 /* *(pchar / "/") */
330 static int
331 parse_path(struct parser *p)
333 char c;
335 /* trim initial slashes */
336 while (*p->iri == '/')
337 p->iri++;
339 p->parsed->path = p->iri;
340 if (*p->iri == '\0') {
341 p->parsed->query = p->parsed->fragment = p->iri;
342 return 1;
345 while (unreserved(*p->iri)
346 || sub_delimiters(*p->iri)
347 || *p->iri == '/'
348 || parse_pct_encoded(p)
349 || valid_multibyte_utf8(p))
350 p->iri++;
352 if (p->err != NULL)
353 return 0;
355 if (*p->iri != '\0' && *p->iri != '?' && *p->iri != '#') {
356 p->err = "illegal character in path";
357 return 0;
360 if (*p->iri != '\0') {
361 c = *p->iri;
362 *p->iri = '\0';
363 p->iri++;
365 if (c == '#') {
366 if (!parse_fragment(p))
367 return 0;
368 } else
369 if (!parse_query(p) || !parse_fragment(p))
370 return 0;
373 if (!path_clean(p->parsed->path)) {
374 p->err = "illegal path";
375 return 0;
378 return 1;
381 int
382 parse_iri(char *iri, struct iri *ret, const char **err_ret)
384 char *end;
385 struct parser p = {iri, ret, NULL};
387 bzero(ret, sizeof(*ret));
389 /* initialize optional stuff to the empty string */
390 end = iri + strlen(iri);
391 p.parsed->host = end;
392 p.parsed->port = end;
393 p.parsed->path = end;
394 p.parsed->query = end;
395 p.parsed->fragment = end;
397 if (!parse_scheme(&p) || !parse_authority(&p) || !parse_path(&p)) {
398 *err_ret = p.err;
399 return 0;
402 *err_ret = NULL;
403 return 1;
406 int
407 trim_req_iri(char *iri, const char **err)
409 char *i;
411 if ((i = strstr(iri, "\r\n")) == NULL) {
412 *err = "missing CRLF";
413 return 0;
415 *i = '\0';
416 return 1;
420 int
421 serialize_iri(struct iri *i, char *buf, size_t len)
423 size_t l;
425 /* in ex.c we receive empty "" strings as NULL */
426 if (i->schema == NULL || i->host == NULL) {
427 memset(buf, 0, len);
428 return 0;
431 strlcpy(buf, i->schema, len);
432 strlcat(buf, "://", len);
433 strlcat(buf, i->host, len);
434 strlcat(buf, "/", len);
436 if (i->path != NULL)
437 l = strlcat(buf, i->path, len);
439 if (i->query != NULL && *i->query != '\0') {
440 strlcat(buf, "?", len);
441 l = strlcat(buf, i->query, len);
444 return l < len;
447 char *
448 pct_decode_str(char *s)
450 char *t;
452 for (t = s; *t; ++t) {
453 if (*t == '%' && valid_pct_enc_string(t))
454 pct_decode(t);
457 return s;