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 != '%')
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 * depends on the LC_CTYPE locale. The good things is
85 * that we're sure p->iri points to something in the
86 * ASCII range, so it shouldn't do weird stuff. */
87 *p->iri = tolower(*p->iri);
88 p->iri++;
89 } while (isalnum(*p->iri)
90 || *p->iri == '+'
91 || *p->iri == '-'
92 || *p->iri == '.');
94 if (*p->iri != ':') {
95 p->err = "illegal character in scheme";
96 return 0;
97 }
99 *p->iri = '\0';
100 if (p->iri[1] != '/' || p->iri[2] != '/') {
101 p->err = "invalid marker after scheme";
102 return 0;
105 p->iri += 3;
106 return 1;
109 /* *DIGIT */
110 static int
111 parse_port(struct parser *p)
113 uint32_t i = 0;
115 p->parsed->port = p->iri;
117 for (; isdigit(*p->iri); p->iri++) {
118 i = i * 10 + *p->iri - '0';
119 if (i > UINT16_MAX) {
120 p->err = "port number too large";
121 return 0;
125 if (*p->iri != '/' && *p->iri != '\0') {
126 p->err = "illegal character in port number";
127 return 0;
130 p->parsed->port_no = i;
132 if (*p->iri != '\0') {
133 *p->iri = '\0';
134 p->iri++;
137 return 1;
140 /* TODO: add support for ip-literal and ipv4addr ? */
141 /* *( unreserved / sub-delims / pct-encoded ) */
142 static int
143 parse_authority(struct parser *p)
145 p->parsed->host = p->iri;
147 while (unreserved(*p->iri)
148 || sub_delimiters(*p->iri)
149 || parse_pct_encoded(p)) {
150 /* normalize the host name. */
151 if (*p->iri < 0x7F)
152 *p->iri = tolower(*p->iri);
153 p->iri++;
156 if (p->err != NULL)
157 return 0;
159 if (*p->iri == ':') {
160 *p->iri = '\0';
161 p->iri++;
162 return parse_port(p);
163 } else
164 p->parsed->port_no = 1965;
166 if (*p->iri == '/') {
167 *p->iri = '\0';
168 p->iri++;
169 return 1;
172 if (*p->iri == '\0')
173 return 1;
175 p->err = "illegal character in authority section";
176 return 0;
179 /* Routine for path_clean. Elide the pointed .. with the preceding
180 * element. Return 0 if it's not possible. incr is the length of
181 * the increment, 3 for ../ and 2 for .. */
182 static int
183 path_elide_dotdot(char *path, char *i, int incr)
185 char *j;
187 if (i == path)
188 return 0;
189 for (j = i-2; j != path && *j != '/'; j--)
190 /* noop */ ;
191 if (*j == '/')
192 j++;
193 i += incr;
194 memmove(j, i, strlen(i)+1);
195 return 1;
198 /*
199 * Use an algorithm similar to the one implemented in go' path.Clean:
201 * 1. Replace multiple slashes with a single slash
202 * 2. Eliminate each . path name element
203 * 3. Eliminate each inner .. along with the non-.. element that precedes it
204 * 4. Eliminate trailing .. if possible or error (go would only discard)
206 * Unlike path.Clean, this function return the empty string if the
207 * original path is equivalent to "/".
208 */
209 static int
210 path_clean(char *path)
212 char *i;
214 /* 1. replace multiple slashes with a single one */
215 for (i = path; *i; ++i) {
216 if (*i == '/' && *(i+1) == '/') {
217 memmove(i, i+1, strlen(i)); /* move also the \0 */
218 i--;
222 /* 2. eliminate each . path name element */
223 for (i = path; *i; ++i) {
224 if ((i == path || *i == '/') &&
225 *i != '.' && i[1] == '.' && i[2] == '/') {
226 /* move also the \0 */
227 memmove(i, i+2, strlen(i)-1);
228 i--;
231 if (!strcmp(path, ".") || !strcmp(path, "/.")) {
232 *path = '\0';
233 return 1;
236 /* 3. eliminate each inner .. along with the preceding non-.. */
237 for (i = strstr(path, "../"); i != NULL; i = strstr(path, ".."))
238 if (!path_elide_dotdot(path, i, 3))
239 return 0;
241 /* 4. eliminate trailing ..*/
242 if ((i = strstr(path, "..")) != NULL)
243 if (!path_elide_dotdot(path, i, 2))
244 return 0;
246 return 1;
249 static int
250 parse_query(struct parser *p)
252 p->parsed->query = p->iri;
253 if (*p->iri == '\0')
254 return 1;
256 while (unreserved(*p->iri)
257 || sub_delimiters(*p->iri)
258 || *p->iri == '/'
259 || *p->iri == '?'
260 || parse_pct_encoded(p)
261 || valid_multibyte_utf8(p))
262 p->iri++;
264 if (p->err != NULL)
265 return 0;
267 if (*p->iri != '\0' && *p->iri != '#') {
268 p->err = "illegal character in query";
269 return 0;
272 if (*p->iri != '\0') {
273 *p->iri = '\0';
274 p->iri++;
277 return 1;
280 /* don't even bother */
281 static int
282 parse_fragment(struct parser *p)
284 p->parsed->fragment = p->iri;
285 return 1;
288 /* XXX: is it too broad? */
289 /* *(pchar / "/") */
290 static int
291 parse_path(struct parser *p)
293 char c;
295 p->parsed->path = p->iri;
296 if (*p->iri == '\0') {
297 p->parsed->query = p->parsed->fragment = p->iri;
298 return 1;
301 while (unreserved(*p->iri)
302 || sub_delimiters(*p->iri)
303 || *p->iri == '/'
304 || parse_pct_encoded(p)
305 || valid_multibyte_utf8(p))
306 p->iri++;
308 if (p->err != NULL)
309 return 0;
311 if (*p->iri != '\0' && *p->iri != '?' && *p->iri != '#') {
312 p->err = "illegal character in path";
313 return 0;
316 if (*p->iri != '\0') {
317 c = *p->iri;
318 *p->iri = '\0';
319 p->iri++;
321 if (c == '#') {
322 if (!parse_fragment(p))
323 return 0;
324 } else
325 if (!parse_query(p) || !parse_fragment(p))
326 return 0;
329 if (!path_clean(p->parsed->path)) {
330 p->err = "illegal path";
331 return 0;
334 return 1;
337 int
338 parse_iri(char *iri, struct iri *ret, const char **err_ret)
340 char *end;
341 struct parser p = {iri, ret, NULL};
343 bzero(ret, sizeof(*ret));
345 /* initialize optional stuff to the empty string */
346 end = iri + strlen(iri);
347 p.parsed->port = end;
348 p.parsed->path = end;
349 p.parsed->query = end;
350 p.parsed->fragment = end;
352 if (!parse_scheme(&p) || !parse_authority(&p) || !parse_path(&p)) {
353 *err_ret = p.err;
354 return 0;
357 *err_ret = NULL;
358 return 1;
361 int
362 trim_req_iri(char *iri)
364 char *i;
366 if ((i = strstr(iri, "\r\n")) == NULL)
367 return 0;
368 *i = '\0';
369 return 1;