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 p->iri++;
81 while (isalnum(*p->iri)
82 || *p->iri == '+'
83 || *p->iri == '-'
84 || *p->iri == '.')
85 p->iri++;
87 if (*p->iri != ':') {
88 p->err = "illegal character in scheme";
89 return 0;
90 }
92 *p->iri = '\0';
93 if (*(++p->iri) != '/' || *(++p->iri) != '/') {
94 p->err = "invalid marker after scheme";
95 return 0;
96 }
98 p->iri++;
99 return 1;
102 /* *DIGIT */
103 static int
104 parse_port(struct parser *p)
106 uint32_t i = 0;
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";
114 return 0;
118 if (*p->iri != '/' && *p->iri != '\0') {
119 p->err = "illegal character in port number";
120 return 0;
123 p->parsed->port_no = i;
125 if (*p->iri != '\0') {
126 *p->iri = '\0';
127 p->iri++;
130 return 1;
133 /* TODO: add support for ip-literal and ipv4addr ? */
134 /* *( unreserved / sub-delims / pct-encoded ) */
135 static int
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))
143 p->iri++;
145 if (p->err != NULL)
146 return 0;
148 if (*p->iri == ':') {
149 *p->iri = '\0';
150 p->iri++;
151 return parse_port(p);
154 if (*p->iri == '/') {
155 *p->iri = '\0';
156 p->iri++;
157 return 1;
160 if (*p->iri == '\0')
161 return 1;
163 p->err = "illegal character in authority section";
164 return 0;
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 .. */
170 static int
171 path_elide_dotdot(char *path, char *i, int incr)
173 char *j;
175 if (i == path)
176 return 0;
177 for (j = i-2; j != path && *j != '/'; j--)
178 /* noop */ ;
179 if (*j == '/')
180 j++;
181 i += incr;
182 memmove(j, i, strlen(i)+1);
183 return 1;
186 /*
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 "/".
196 */
197 static int
198 path_clean(char *path)
200 char *i;
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 */
206 i--;
210 /* 2. eliminate each . path name element */
211 for (i = path; *i; ++i) {
212 if ((i == path || *i == '/') && *(i+1) == '.' &&
213 *(i+2) == '/') {
214 /* move also the \0 */
215 memmove(i, i+2, strlen(i)-1);
216 i--;
219 if (!strcmp(path, ".") || !strcmp(path, "/.")) {
220 *path = '\0';
221 return 1;
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))
227 return 0;
229 /* 4. eliminate trailing ..*/
230 if ((i = strstr(path, "..")) != NULL)
231 if (!path_elide_dotdot(path, i, 2))
232 return 0;
234 return 1;
237 static int
238 parse_query(struct parser *p)
240 p->parsed->query = p->iri;
241 if (*p->iri == '\0')
242 return 1;
244 while (unreserved(*p->iri)
245 || sub_delimiters(*p->iri)
246 || *p->iri == '/'
247 || *p->iri == '?'
248 || parse_pct_encoded(p)
249 || valid_multibyte_utf8(p))
250 p->iri++;
252 if (p->err != NULL)
253 return 0;
255 if (*p->iri != '\0' && *p->iri != '#') {
256 p->err = "illegal character in query";
257 return 0;
260 if (*p->iri != '\0') {
261 *p->iri = '\0';
262 p->iri++;
265 return 1;
268 /* don't even bother */
269 static int
270 parse_fragment(struct parser *p)
272 p->parsed->fragment = p->iri;
273 return 1;
276 /* XXX: is it too broad? */
277 /* *(pchar / "/") */
278 static int
279 parse_path(struct parser *p)
281 char c;
283 p->parsed->path = p->iri;
284 if (*p->iri == '\0') {
285 p->parsed->query = p->parsed->fragment = p->iri;
286 return 1;
289 while (unreserved(*p->iri)
290 || sub_delimiters(*p->iri)
291 || *p->iri == '/'
292 || parse_pct_encoded(p)
293 || valid_multibyte_utf8(p))
294 p->iri++;
296 if (p->err != NULL)
297 return 0;
299 if (*p->iri != '\0' && *p->iri != '?' && *p->iri != '#') {
300 p->err = "illegal character in path";
301 return 0;
304 if (*p->iri != '\0') {
305 c = *p->iri;
306 *p->iri = '\0';
307 p->iri++;
309 if (c == '#') {
310 if (!parse_fragment(p))
311 return 0;
312 } else
313 if (!parse_query(p) || !parse_fragment(p))
314 return 0;
317 if (!path_clean(p->parsed->path)) {
318 p->err = "illegal path";
319 return 0;
322 return 1;
325 int
326 parse_iri(char *iri, struct iri *ret, const char **err_ret)
328 char *end;
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)) {
341 *err_ret = p.err;
342 return 0;
345 *err_ret = NULL;
346 return 1;
349 int
350 trim_req_iri(char *iri)
352 char *i;
354 if ((i = strstr(iri, "\r\n")) == NULL)
355 return 0;
356 *i = '\0';
357 return 1;