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 parse_pct_encoded(struct parser *p)
50 {
51 if (p->iri[0] != '%')
52 return 0;
54 if (!isxdigit(p->iri[1]) || !isxdigit(p->iri[2])) {
55 p->err = "illegal percent-encoding";
56 return 0;
57 }
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";
63 return 0;
64 }
66 return 1;
67 }
69 /* ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) "://" */
70 static int
71 parse_scheme(struct parser *p)
72 {
73 p->parsed->schema = p->iri;
75 if (!isalpha(*p->iri)) {
76 p->err = "illegal character in scheme";
77 return 0;
78 }
80 do {
81 /* normalize the scheme (i.e. lowercase it)
82 *
83 * XXX: since we cannot have good things, tolower
84 * behaviour depends on the LC_CTYPE locale. The good
85 * news is that we're sure p->iri points to something
86 * that's in the ASCII range, so tolower can't
87 * mis-behave on some systems due to the locale. */
88 *p->iri = tolower(*p->iri);
89 p->iri++;
90 } while (isalnum(*p->iri)
91 || *p->iri == '+'
92 || *p->iri == '-'
93 || *p->iri == '.');
95 if (*p->iri != ':') {
96 p->err = "illegal character in scheme";
97 return 0;
98 }
100 *p->iri = '\0';
101 if (p->iri[1] != '/' || p->iri[2] != '/') {
102 p->err = "invalid marker after scheme";
103 return 0;
106 p->iri += 3;
107 return 1;
110 /* *DIGIT */
111 static int
112 parse_port(struct parser *p)
114 uint32_t i = 0;
116 p->parsed->port = p->iri;
118 for (; isdigit(*p->iri); p->iri++) {
119 i = i * 10 + *p->iri - '0';
120 if (i > UINT16_MAX) {
121 p->err = "port number too large";
122 return 0;
126 if (*p->iri != '/' && *p->iri != '\0') {
127 p->err = "illegal character in port number";
128 return 0;
131 p->parsed->port_no = i;
133 if (*p->iri != '\0') {
134 *p->iri = '\0';
135 p->iri++;
138 return 1;
141 /* TODO: add support for ip-literal and ipv4addr ? */
142 /* *( unreserved / sub-delims / pct-encoded ) */
143 static int
144 parse_authority(struct parser *p)
146 p->parsed->host = p->iri;
148 while (unreserved(*p->iri)
149 || sub_delimiters(*p->iri)
150 || parse_pct_encoded(p)
151 || valid_multibyte_utf8(p)) {
152 /* normalize the host name. */
153 if (*p->iri < 0x7F)
154 *p->iri = tolower(*p->iri);
155 p->iri++;
158 if (p->err != NULL)
159 return 0;
161 if (*p->iri == ':') {
162 *p->iri = '\0';
163 p->iri++;
164 return parse_port(p);
165 } else
166 p->parsed->port_no = 1965;
168 if (*p->iri == '/') {
169 *p->iri = '\0';
170 p->iri++;
171 return 1;
174 if (*p->iri == '\0')
175 return 1;
177 p->err = "illegal character in authority section";
178 return 0;
181 /* Routine for path_clean. Elide the pointed .. with the preceding
182 * element. Return 0 if it's not possible. incr is the length of
183 * the increment, 3 for ../ and 2 for .. */
184 static int
185 path_elide_dotdot(char *path, char *i, int incr)
187 char *j;
189 if (i == path)
190 return 0;
191 for (j = i-2; j != path && *j != '/'; j--)
192 /* noop */ ;
193 if (*j == '/')
194 j++;
195 i += incr;
196 memmove(j, i, strlen(i)+1);
197 return 1;
200 /*
201 * Use an algorithm similar to the one implemented in go' path.Clean:
203 * 1. Replace multiple slashes with a single slash
204 * 2. Eliminate each . path name element
205 * 3. Eliminate each inner .. along with the non-.. element that precedes it
206 * 4. Eliminate trailing .. if possible or error (go would only discard)
208 * Unlike path.Clean, this function return the empty string if the
209 * original path is equivalent to "/".
210 */
211 static int
212 path_clean(char *path)
214 char *i;
216 /* 1. replace multiple slashes with a single one */
217 for (i = path; *i; ++i) {
218 if (*i == '/' && *(i+1) == '/') {
219 memmove(i, i+1, strlen(i)); /* move also the \0 */
220 i--;
224 /* 2. eliminate each . path name element */
225 for (i = path; *i; ++i) {
226 if ((i == path || *i == '/') &&
227 *i != '.' && i[1] == '.' && i[2] == '/') {
228 /* move also the \0 */
229 memmove(i, i+2, strlen(i)-1);
230 i--;
233 if (!strcmp(path, ".") || !strcmp(path, "/.")) {
234 *path = '\0';
235 return 1;
238 /* 3. eliminate each inner .. along with the preceding non-.. */
239 for (i = strstr(path, "../"); i != NULL; i = strstr(path, ".."))
240 if (!path_elide_dotdot(path, i, 3))
241 return 0;
243 /* 4. eliminate trailing ..*/
244 if ((i = strstr(path, "..")) != NULL)
245 if (!path_elide_dotdot(path, i, 2))
246 return 0;
248 return 1;
251 static int
252 parse_query(struct parser *p)
254 p->parsed->query = p->iri;
255 if (*p->iri == '\0')
256 return 1;
258 while (unreserved(*p->iri)
259 || sub_delimiters(*p->iri)
260 || *p->iri == '/'
261 || *p->iri == '?'
262 || parse_pct_encoded(p)
263 || valid_multibyte_utf8(p))
264 p->iri++;
266 if (p->err != NULL)
267 return 0;
269 if (*p->iri != '\0' && *p->iri != '#') {
270 p->err = "illegal character in query";
271 return 0;
274 if (*p->iri != '\0') {
275 *p->iri = '\0';
276 p->iri++;
279 return 1;
282 /* don't even bother */
283 static int
284 parse_fragment(struct parser *p)
286 p->parsed->fragment = p->iri;
287 return 1;
290 /* XXX: is it too broad? */
291 /* *(pchar / "/") */
292 static int
293 parse_path(struct parser *p)
295 char c;
297 /* trim initial slashes */
298 while (*p->iri == '/')
299 p->iri++;
301 p->parsed->path = p->iri;
302 if (*p->iri == '\0') {
303 p->parsed->query = p->parsed->fragment = p->iri;
304 return 1;
307 while (unreserved(*p->iri)
308 || sub_delimiters(*p->iri)
309 || *p->iri == '/'
310 || parse_pct_encoded(p)
311 || valid_multibyte_utf8(p))
312 p->iri++;
314 if (p->err != NULL)
315 return 0;
317 if (*p->iri != '\0' && *p->iri != '?' && *p->iri != '#') {
318 p->err = "illegal character in path";
319 return 0;
322 if (*p->iri != '\0') {
323 c = *p->iri;
324 *p->iri = '\0';
325 p->iri++;
327 if (c == '#') {
328 if (!parse_fragment(p))
329 return 0;
330 } else
331 if (!parse_query(p) || !parse_fragment(p))
332 return 0;
335 if (!path_clean(p->parsed->path)) {
336 p->err = "illegal path";
337 return 0;
340 return 1;
343 int
344 parse_iri(char *iri, struct iri *ret, const char **err_ret)
346 char *end;
347 struct parser p = {iri, ret, NULL};
349 bzero(ret, sizeof(*ret));
351 /* initialize optional stuff to the empty string */
352 end = iri + strlen(iri);
353 p.parsed->host = end;
354 p.parsed->port = end;
355 p.parsed->path = end;
356 p.parsed->query = end;
357 p.parsed->fragment = end;
359 if (!parse_scheme(&p) || !parse_authority(&p) || !parse_path(&p)) {
360 *err_ret = p.err;
361 return 0;
364 *err_ret = NULL;
365 return 1;
368 int
369 trim_req_iri(char *iri, const char **err)
371 char *i;
373 if ((i = strstr(iri, "\r\n")) == NULL) {
374 *err = "missing CRLF";
375 return 0;
377 *i = '\0';
378 return 1;