Blame


1 3c1cf9d0 2021-01-11 op /*
2 a555e0d6 2022-07-04 op * Copyright (c) 2020, 2022 Omar Polo <op@omarpolo.com>
3 3c1cf9d0 2021-01-11 op *
4 3c1cf9d0 2021-01-11 op * Permission to use, copy, modify, and distribute this software for any
5 3c1cf9d0 2021-01-11 op * purpose with or without fee is hereby granted, provided that the above
6 3c1cf9d0 2021-01-11 op * copyright notice and this permission notice appear in all copies.
7 3c1cf9d0 2021-01-11 op *
8 3c1cf9d0 2021-01-11 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3c1cf9d0 2021-01-11 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3c1cf9d0 2021-01-11 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3c1cf9d0 2021-01-11 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3c1cf9d0 2021-01-11 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3c1cf9d0 2021-01-11 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3c1cf9d0 2021-01-11 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 3c1cf9d0 2021-01-11 op */
16 52418c8d 2021-02-12 op
17 52418c8d 2021-02-12 op #include "gmid.h"
18 3c1cf9d0 2021-01-11 op
19 3c1cf9d0 2021-01-11 op #include <ctype.h>
20 3c1cf9d0 2021-01-11 op #include <string.h>
21 3c1cf9d0 2021-01-11 op
22 3c1cf9d0 2021-01-11 op static inline int
23 3c1cf9d0 2021-01-11 op unreserved(int p)
24 3c1cf9d0 2021-01-11 op {
25 06035a02 2022-11-29 op return isalnum((unsigned char)p)
26 3c1cf9d0 2021-01-11 op || p == '-'
27 3c1cf9d0 2021-01-11 op || p == '.'
28 3c1cf9d0 2021-01-11 op || p == '_'
29 3c1cf9d0 2021-01-11 op || p == '~';
30 3c1cf9d0 2021-01-11 op }
31 3c1cf9d0 2021-01-11 op
32 3c1cf9d0 2021-01-11 op static inline int
33 3c1cf9d0 2021-01-11 op sub_delimiters(int p)
34 3c1cf9d0 2021-01-11 op {
35 3c1cf9d0 2021-01-11 op return p == '!'
36 3c1cf9d0 2021-01-11 op || p == '$'
37 3c1cf9d0 2021-01-11 op || p == '&'
38 3c1cf9d0 2021-01-11 op || p == '\''
39 3c1cf9d0 2021-01-11 op || p == '('
40 3c1cf9d0 2021-01-11 op || p == ')'
41 3c1cf9d0 2021-01-11 op || p == '*'
42 3c1cf9d0 2021-01-11 op || p == '+'
43 3c1cf9d0 2021-01-11 op || p == ','
44 3c1cf9d0 2021-01-11 op || p == ';'
45 3c1cf9d0 2021-01-11 op || p == '=';
46 8404ec30 2021-02-05 op }
47 8404ec30 2021-02-05 op
48 8404ec30 2021-02-05 op static int
49 8404ec30 2021-02-05 op valid_pct_enc_string(char *s)
50 8404ec30 2021-02-05 op {
51 8404ec30 2021-02-05 op if (*s != '%')
52 8404ec30 2021-02-05 op return 1;
53 8404ec30 2021-02-05 op
54 6130e0ee 2022-11-17 op if (!isxdigit((unsigned char)s[1]) ||
55 6130e0ee 2022-11-17 op !isxdigit((unsigned char)s[2]))
56 8404ec30 2021-02-05 op return 0;
57 8404ec30 2021-02-05 op
58 8404ec30 2021-02-05 op if (s[1] == '0' && s[2] == '0')
59 8404ec30 2021-02-05 op return 0;
60 8404ec30 2021-02-05 op
61 8404ec30 2021-02-05 op return 1;
62 8404ec30 2021-02-05 op }
63 8404ec30 2021-02-05 op
64 8404ec30 2021-02-05 op static int
65 8404ec30 2021-02-05 op valid_pct_encoded(struct parser *p)
66 8404ec30 2021-02-05 op {
67 8404ec30 2021-02-05 op if (p->iri[0] != '%')
68 8404ec30 2021-02-05 op return 0;
69 8404ec30 2021-02-05 op
70 8404ec30 2021-02-05 op if (!valid_pct_enc_string(p->iri)) {
71 8404ec30 2021-02-05 op p->err = "illegal percent-encoding";
72 8404ec30 2021-02-05 op return 0;
73 8404ec30 2021-02-05 op }
74 8404ec30 2021-02-05 op
75 8404ec30 2021-02-05 op p->iri += 2;
76 8404ec30 2021-02-05 op return 1;
77 3c1cf9d0 2021-01-11 op }
78 3c1cf9d0 2021-01-11 op
79 9f006a21 2021-02-07 op static void
80 9f006a21 2021-02-07 op pct_decode(char *s)
81 9f006a21 2021-02-07 op {
82 9f006a21 2021-02-07 op sscanf(s+1, "%2hhx", s);
83 9f006a21 2021-02-07 op memmove(s+1, s+3, strlen(s+3)+1);
84 9f006a21 2021-02-07 op }
85 9f006a21 2021-02-07 op
86 3c1cf9d0 2021-01-11 op static int
87 3c1cf9d0 2021-01-11 op parse_pct_encoded(struct parser *p)
88 3c1cf9d0 2021-01-11 op {
89 9a672b37 2021-01-28 op if (p->iri[0] != '%')
90 3c1cf9d0 2021-01-11 op return 0;
91 3c1cf9d0 2021-01-11 op
92 8404ec30 2021-02-05 op if (!valid_pct_enc_string(p->iri)) {
93 3c1cf9d0 2021-01-11 op p->err = "illegal percent-encoding";
94 3c1cf9d0 2021-01-11 op return 0;
95 3c1cf9d0 2021-01-11 op }
96 3c1cf9d0 2021-01-11 op
97 9f006a21 2021-02-07 op pct_decode(p->iri);
98 3c1cf9d0 2021-01-11 op if (*p->iri == '\0') {
99 3c1cf9d0 2021-01-11 op p->err = "illegal percent-encoding";
100 3c1cf9d0 2021-01-11 op return 0;
101 3c1cf9d0 2021-01-11 op }
102 3c1cf9d0 2021-01-11 op
103 3c1cf9d0 2021-01-11 op return 1;
104 3c1cf9d0 2021-01-11 op }
105 3c1cf9d0 2021-01-11 op
106 3c1cf9d0 2021-01-11 op /* ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) "://" */
107 3c1cf9d0 2021-01-11 op static int
108 3c1cf9d0 2021-01-11 op parse_scheme(struct parser *p)
109 3c1cf9d0 2021-01-11 op {
110 3c1cf9d0 2021-01-11 op p->parsed->schema = p->iri;
111 3c1cf9d0 2021-01-11 op
112 06035a02 2022-11-29 op if (!isalpha((unsigned char)*p->iri)) {
113 3c1cf9d0 2021-01-11 op p->err = "illegal character in scheme";
114 3c1cf9d0 2021-01-11 op return 0;
115 3c1cf9d0 2021-01-11 op }
116 3c1cf9d0 2021-01-11 op
117 de428fff 2021-01-13 op do {
118 a8a1f439 2021-07-07 op /*
119 a8a1f439 2021-07-07 op * normalize the scheme (i.e. lowercase it)
120 de428fff 2021-01-13 op *
121 de428fff 2021-01-13 op * XXX: since we cannot have good things, tolower
122 881dc835 2021-01-16 op * behaviour depends on the LC_CTYPE locale. The good
123 881dc835 2021-01-16 op * news is that we're sure p->iri points to something
124 881dc835 2021-01-16 op * that's in the ASCII range, so tolower can't
125 a8a1f439 2021-07-07 op * mis-behave on some systems due to the locale.
126 a8a1f439 2021-07-07 op */
127 de428fff 2021-01-13 op *p->iri = tolower(*p->iri);
128 de428fff 2021-01-13 op p->iri++;
129 06035a02 2022-11-29 op } while (isalnum((unsigned char)*p->iri)
130 3c1cf9d0 2021-01-11 op || *p->iri == '+'
131 3c1cf9d0 2021-01-11 op || *p->iri == '-'
132 de428fff 2021-01-13 op || *p->iri == '.');
133 3c1cf9d0 2021-01-11 op
134 3c1cf9d0 2021-01-11 op if (*p->iri != ':') {
135 3c1cf9d0 2021-01-11 op p->err = "illegal character in scheme";
136 3c1cf9d0 2021-01-11 op return 0;
137 3c1cf9d0 2021-01-11 op }
138 3c1cf9d0 2021-01-11 op
139 3c1cf9d0 2021-01-11 op *p->iri = '\0';
140 f7b816dc 2021-01-15 op if (p->iri[1] != '/' || p->iri[2] != '/') {
141 3c1cf9d0 2021-01-11 op p->err = "invalid marker after scheme";
142 3c1cf9d0 2021-01-11 op return 0;
143 3c1cf9d0 2021-01-11 op }
144 3c1cf9d0 2021-01-11 op
145 f7b816dc 2021-01-15 op p->iri += 3;
146 3c1cf9d0 2021-01-11 op return 1;
147 3c1cf9d0 2021-01-11 op }
148 3c1cf9d0 2021-01-11 op
149 3c1cf9d0 2021-01-11 op /* *DIGIT */
150 3c1cf9d0 2021-01-11 op static int
151 3c1cf9d0 2021-01-11 op parse_port(struct parser *p)
152 3c1cf9d0 2021-01-11 op {
153 3c1cf9d0 2021-01-11 op uint32_t i = 0;
154 3c1cf9d0 2021-01-11 op
155 3c1cf9d0 2021-01-11 op p->parsed->port = p->iri;
156 3c1cf9d0 2021-01-11 op
157 6130e0ee 2022-11-17 op for (; isdigit((unsigned char)*p->iri); p->iri++) {
158 3c1cf9d0 2021-01-11 op i = i * 10 + *p->iri - '0';
159 3c1cf9d0 2021-01-11 op if (i > UINT16_MAX) {
160 3c1cf9d0 2021-01-11 op p->err = "port number too large";
161 3c1cf9d0 2021-01-11 op return 0;
162 3c1cf9d0 2021-01-11 op }
163 3c1cf9d0 2021-01-11 op }
164 3c1cf9d0 2021-01-11 op
165 3c1cf9d0 2021-01-11 op if (*p->iri != '/' && *p->iri != '\0') {
166 3c1cf9d0 2021-01-11 op p->err = "illegal character in port number";
167 3c1cf9d0 2021-01-11 op return 0;
168 3c1cf9d0 2021-01-11 op }
169 3c1cf9d0 2021-01-11 op
170 3c1cf9d0 2021-01-11 op p->parsed->port_no = i;
171 3c1cf9d0 2021-01-11 op
172 3c1cf9d0 2021-01-11 op if (*p->iri != '\0') {
173 3c1cf9d0 2021-01-11 op *p->iri = '\0';
174 3c1cf9d0 2021-01-11 op p->iri++;
175 3c1cf9d0 2021-01-11 op }
176 3c1cf9d0 2021-01-11 op
177 3c1cf9d0 2021-01-11 op return 1;
178 3c1cf9d0 2021-01-11 op }
179 3c1cf9d0 2021-01-11 op
180 3c1cf9d0 2021-01-11 op /* TODO: add support for ip-literal and ipv4addr ? */
181 3c1cf9d0 2021-01-11 op /* *( unreserved / sub-delims / pct-encoded ) */
182 3c1cf9d0 2021-01-11 op static int
183 3c1cf9d0 2021-01-11 op parse_authority(struct parser *p)
184 3c1cf9d0 2021-01-11 op {
185 3c1cf9d0 2021-01-11 op p->parsed->host = p->iri;
186 3c1cf9d0 2021-01-11 op
187 3c1cf9d0 2021-01-11 op while (unreserved(*p->iri)
188 3c1cf9d0 2021-01-11 op || sub_delimiters(*p->iri)
189 117ac52c 2021-01-29 op || parse_pct_encoded(p)
190 117ac52c 2021-01-29 op || valid_multibyte_utf8(p)) {
191 e4d82bec 2021-01-15 op /* normalize the host name. */
192 e4d82bec 2021-01-15 op if (*p->iri < 0x7F)
193 e4d82bec 2021-01-15 op *p->iri = tolower(*p->iri);
194 3c1cf9d0 2021-01-11 op p->iri++;
195 e4d82bec 2021-01-15 op }
196 3c1cf9d0 2021-01-11 op
197 3c1cf9d0 2021-01-11 op if (p->err != NULL)
198 3c1cf9d0 2021-01-11 op return 0;
199 3c1cf9d0 2021-01-11 op
200 3c1cf9d0 2021-01-11 op if (*p->iri == ':') {
201 3c1cf9d0 2021-01-11 op *p->iri = '\0';
202 3c1cf9d0 2021-01-11 op p->iri++;
203 3c1cf9d0 2021-01-11 op return parse_port(p);
204 b777bf4b 2021-01-15 op } else
205 b777bf4b 2021-01-15 op p->parsed->port_no = 1965;
206 3c1cf9d0 2021-01-11 op
207 3c1cf9d0 2021-01-11 op if (*p->iri == '/') {
208 3c1cf9d0 2021-01-11 op *p->iri = '\0';
209 3c1cf9d0 2021-01-11 op p->iri++;
210 3c1cf9d0 2021-01-11 op return 1;
211 3c1cf9d0 2021-01-11 op }
212 3c1cf9d0 2021-01-11 op
213 3c1cf9d0 2021-01-11 op if (*p->iri == '\0')
214 3c1cf9d0 2021-01-11 op return 1;
215 3c1cf9d0 2021-01-11 op
216 3c1cf9d0 2021-01-11 op p->err = "illegal character in authority section";
217 3c1cf9d0 2021-01-11 op return 0;
218 3c1cf9d0 2021-01-11 op }
219 3c1cf9d0 2021-01-11 op
220 a8a1f439 2021-07-07 op /*
221 a8a1f439 2021-07-07 op * Routine for path_clean. Elide the pointed .. with the preceding
222 3c1cf9d0 2021-01-11 op * element. Return 0 if it's not possible. incr is the length of
223 a8a1f439 2021-07-07 op * the increment, 3 for ../ and 2 for ..
224 a8a1f439 2021-07-07 op */
225 3c1cf9d0 2021-01-11 op static int
226 3c1cf9d0 2021-01-11 op path_elide_dotdot(char *path, char *i, int incr)
227 3c1cf9d0 2021-01-11 op {
228 3c1cf9d0 2021-01-11 op char *j;
229 3c1cf9d0 2021-01-11 op
230 3c1cf9d0 2021-01-11 op if (i == path)
231 3c1cf9d0 2021-01-11 op return 0;
232 3c1cf9d0 2021-01-11 op for (j = i-2; j != path && *j != '/'; j--)
233 4842c72d 2021-10-18 op /* noop */ ;
234 3c1cf9d0 2021-01-11 op if (*j == '/')
235 3c1cf9d0 2021-01-11 op j++;
236 3c1cf9d0 2021-01-11 op i += incr;
237 3c1cf9d0 2021-01-11 op memmove(j, i, strlen(i)+1);
238 3c1cf9d0 2021-01-11 op return 1;
239 3c1cf9d0 2021-01-11 op }
240 3c1cf9d0 2021-01-11 op
241 3c1cf9d0 2021-01-11 op /*
242 3c1cf9d0 2021-01-11 op * Use an algorithm similar to the one implemented in go' path.Clean:
243 3c1cf9d0 2021-01-11 op *
244 3c1cf9d0 2021-01-11 op * 1. Replace multiple slashes with a single slash
245 3c1cf9d0 2021-01-11 op * 2. Eliminate each . path name element
246 3c1cf9d0 2021-01-11 op * 3. Eliminate each inner .. along with the non-.. element that precedes it
247 3c1cf9d0 2021-01-11 op * 4. Eliminate trailing .. if possible or error (go would only discard)
248 3c1cf9d0 2021-01-11 op *
249 3c1cf9d0 2021-01-11 op * Unlike path.Clean, this function return the empty string if the
250 3c1cf9d0 2021-01-11 op * original path is equivalent to "/".
251 3c1cf9d0 2021-01-11 op */
252 3c1cf9d0 2021-01-11 op static int
253 3c1cf9d0 2021-01-11 op path_clean(char *path)
254 3c1cf9d0 2021-01-11 op {
255 3c1cf9d0 2021-01-11 op char *i;
256 3c1cf9d0 2021-01-11 op
257 3c1cf9d0 2021-01-11 op /* 1. replace multiple slashes with a single one */
258 3c1cf9d0 2021-01-11 op for (i = path; *i; ++i) {
259 3c1cf9d0 2021-01-11 op if (*i == '/' && *(i+1) == '/') {
260 3c1cf9d0 2021-01-11 op memmove(i, i+1, strlen(i)); /* move also the \0 */
261 3c1cf9d0 2021-01-11 op i--;
262 3c1cf9d0 2021-01-11 op }
263 3c1cf9d0 2021-01-11 op }
264 3c1cf9d0 2021-01-11 op
265 3c1cf9d0 2021-01-11 op /* 2. eliminate each . path name element */
266 3c1cf9d0 2021-01-11 op for (i = path; *i; ++i) {
267 6a9ae707 2021-01-11 op if ((i == path || *i == '/') &&
268 6a9ae707 2021-01-11 op *i != '.' && i[1] == '.' && i[2] == '/') {
269 3c1cf9d0 2021-01-11 op /* move also the \0 */
270 3c1cf9d0 2021-01-11 op memmove(i, i+2, strlen(i)-1);
271 3c1cf9d0 2021-01-11 op i--;
272 3c1cf9d0 2021-01-11 op }
273 3c1cf9d0 2021-01-11 op }
274 3c1cf9d0 2021-01-11 op if (!strcmp(path, ".") || !strcmp(path, "/.")) {
275 3c1cf9d0 2021-01-11 op *path = '\0';
276 3c1cf9d0 2021-01-11 op return 1;
277 3c1cf9d0 2021-01-11 op }
278 3c1cf9d0 2021-01-11 op
279 3c1cf9d0 2021-01-11 op /* 3. eliminate each inner .. along with the preceding non-.. */
280 9d092b60 2021-04-12 op for (i = strstr(path, "../"); i != NULL; i = strstr(path, "..")) {
281 9d092b60 2021-04-12 op /* break if we've found a trailing .. */
282 9d092b60 2021-04-12 op if (i[2] == '\0')
283 9d092b60 2021-04-12 op break;
284 3c1cf9d0 2021-01-11 op if (!path_elide_dotdot(path, i, 3))
285 3c1cf9d0 2021-01-11 op return 0;
286 9d092b60 2021-04-12 op }
287 3c1cf9d0 2021-01-11 op
288 3c1cf9d0 2021-01-11 op /* 4. eliminate trailing ..*/
289 3c1cf9d0 2021-01-11 op if ((i = strstr(path, "..")) != NULL)
290 3c1cf9d0 2021-01-11 op if (!path_elide_dotdot(path, i, 2))
291 3c1cf9d0 2021-01-11 op return 0;
292 3c1cf9d0 2021-01-11 op
293 3c1cf9d0 2021-01-11 op return 1;
294 3c1cf9d0 2021-01-11 op }
295 3c1cf9d0 2021-01-11 op
296 3c1cf9d0 2021-01-11 op static int
297 3c1cf9d0 2021-01-11 op parse_query(struct parser *p)
298 3c1cf9d0 2021-01-11 op {
299 3c1cf9d0 2021-01-11 op p->parsed->query = p->iri;
300 3c1cf9d0 2021-01-11 op if (*p->iri == '\0')
301 3c1cf9d0 2021-01-11 op return 1;
302 3c1cf9d0 2021-01-11 op
303 3c1cf9d0 2021-01-11 op while (unreserved(*p->iri)
304 3c1cf9d0 2021-01-11 op || sub_delimiters(*p->iri)
305 3c1cf9d0 2021-01-11 op || *p->iri == '/'
306 3c1cf9d0 2021-01-11 op || *p->iri == '?'
307 19e7bd00 2021-02-06 op || *p->iri == ':'
308 19e7bd00 2021-02-06 op || *p->iri == '@'
309 8404ec30 2021-02-05 op || valid_pct_encoded(p)
310 3c1cf9d0 2021-01-11 op || valid_multibyte_utf8(p))
311 3c1cf9d0 2021-01-11 op p->iri++;
312 3c1cf9d0 2021-01-11 op
313 3c1cf9d0 2021-01-11 op if (p->err != NULL)
314 3c1cf9d0 2021-01-11 op return 0;
315 3c1cf9d0 2021-01-11 op
316 3c1cf9d0 2021-01-11 op if (*p->iri != '\0' && *p->iri != '#') {
317 3c1cf9d0 2021-01-11 op p->err = "illegal character in query";
318 3c1cf9d0 2021-01-11 op return 0;
319 3c1cf9d0 2021-01-11 op }
320 3c1cf9d0 2021-01-11 op
321 3c1cf9d0 2021-01-11 op if (*p->iri != '\0') {
322 3c1cf9d0 2021-01-11 op *p->iri = '\0';
323 3c1cf9d0 2021-01-11 op p->iri++;
324 3c1cf9d0 2021-01-11 op }
325 3c1cf9d0 2021-01-11 op
326 3c1cf9d0 2021-01-11 op return 1;
327 3c1cf9d0 2021-01-11 op }
328 3c1cf9d0 2021-01-11 op
329 3c1cf9d0 2021-01-11 op /* don't even bother */
330 3c1cf9d0 2021-01-11 op static int
331 3c1cf9d0 2021-01-11 op parse_fragment(struct parser *p)
332 3c1cf9d0 2021-01-11 op {
333 3c1cf9d0 2021-01-11 op p->parsed->fragment = p->iri;
334 3c1cf9d0 2021-01-11 op return 1;
335 3c1cf9d0 2021-01-11 op }
336 3c1cf9d0 2021-01-11 op
337 3c1cf9d0 2021-01-11 op /* XXX: is it too broad? */
338 3c1cf9d0 2021-01-11 op /* *(pchar / "/") */
339 3c1cf9d0 2021-01-11 op static int
340 3c1cf9d0 2021-01-11 op parse_path(struct parser *p)
341 3c1cf9d0 2021-01-11 op {
342 3c1cf9d0 2021-01-11 op char c;
343 42bbdc79 2021-01-21 op
344 42bbdc79 2021-01-21 op /* trim initial slashes */
345 42bbdc79 2021-01-21 op while (*p->iri == '/')
346 42bbdc79 2021-01-21 op p->iri++;
347 3c1cf9d0 2021-01-11 op
348 3c1cf9d0 2021-01-11 op p->parsed->path = p->iri;
349 3c1cf9d0 2021-01-11 op if (*p->iri == '\0') {
350 3c1cf9d0 2021-01-11 op p->parsed->query = p->parsed->fragment = p->iri;
351 3c1cf9d0 2021-01-11 op return 1;
352 3c1cf9d0 2021-01-11 op }
353 3c1cf9d0 2021-01-11 op
354 3c1cf9d0 2021-01-11 op while (unreserved(*p->iri)
355 3c1cf9d0 2021-01-11 op || sub_delimiters(*p->iri)
356 5e41063f 2022-07-04 op || *p->iri == '@'
357 5e41063f 2022-07-04 op || *p->iri == ':'
358 3c1cf9d0 2021-01-11 op || *p->iri == '/'
359 3c1cf9d0 2021-01-11 op || parse_pct_encoded(p)
360 3c1cf9d0 2021-01-11 op || valid_multibyte_utf8(p))
361 3c1cf9d0 2021-01-11 op p->iri++;
362 3c1cf9d0 2021-01-11 op
363 3c1cf9d0 2021-01-11 op if (p->err != NULL)
364 3c1cf9d0 2021-01-11 op return 0;
365 3c1cf9d0 2021-01-11 op
366 3c1cf9d0 2021-01-11 op if (*p->iri != '\0' && *p->iri != '?' && *p->iri != '#') {
367 3c1cf9d0 2021-01-11 op p->err = "illegal character in path";
368 3c1cf9d0 2021-01-11 op return 0;
369 3c1cf9d0 2021-01-11 op }
370 3c1cf9d0 2021-01-11 op
371 3c1cf9d0 2021-01-11 op if (*p->iri != '\0') {
372 3c1cf9d0 2021-01-11 op c = *p->iri;
373 3c1cf9d0 2021-01-11 op *p->iri = '\0';
374 3c1cf9d0 2021-01-11 op p->iri++;
375 3c1cf9d0 2021-01-11 op
376 3c1cf9d0 2021-01-11 op if (c == '#') {
377 3c1cf9d0 2021-01-11 op if (!parse_fragment(p))
378 3c1cf9d0 2021-01-11 op return 0;
379 3c1cf9d0 2021-01-11 op } else
380 3c1cf9d0 2021-01-11 op if (!parse_query(p) || !parse_fragment(p))
381 3c1cf9d0 2021-01-11 op return 0;
382 3c1cf9d0 2021-01-11 op }
383 3c1cf9d0 2021-01-11 op
384 3c1cf9d0 2021-01-11 op if (!path_clean(p->parsed->path)) {
385 3c1cf9d0 2021-01-11 op p->err = "illegal path";
386 3c1cf9d0 2021-01-11 op return 0;
387 3c1cf9d0 2021-01-11 op }
388 3c1cf9d0 2021-01-11 op
389 3c1cf9d0 2021-01-11 op return 1;
390 3c1cf9d0 2021-01-11 op }
391 3c1cf9d0 2021-01-11 op
392 3c1cf9d0 2021-01-11 op int
393 3c1cf9d0 2021-01-11 op parse_iri(char *iri, struct iri *ret, const char **err_ret)
394 3c1cf9d0 2021-01-11 op {
395 3c1cf9d0 2021-01-11 op char *end;
396 e15fc957 2021-09-24 op struct parser p = {
397 e15fc957 2021-09-24 op .iri = iri,
398 e15fc957 2021-09-24 op .parsed = ret,
399 e15fc957 2021-09-24 op .err = NULL,
400 e15fc957 2021-09-24 op };
401 3c1cf9d0 2021-01-11 op
402 df0c2926 2021-09-24 op memset(ret, 0, sizeof(*ret));
403 3c1cf9d0 2021-01-11 op
404 3c1cf9d0 2021-01-11 op /* initialize optional stuff to the empty string */
405 3c1cf9d0 2021-01-11 op end = iri + strlen(iri);
406 57d0d0ad 2021-01-31 op p.parsed->host = end;
407 3c1cf9d0 2021-01-11 op p.parsed->port = end;
408 3c1cf9d0 2021-01-11 op p.parsed->path = end;
409 3c1cf9d0 2021-01-11 op p.parsed->query = end;
410 3c1cf9d0 2021-01-11 op p.parsed->fragment = end;
411 3c1cf9d0 2021-01-11 op
412 3c1cf9d0 2021-01-11 op if (!parse_scheme(&p) || !parse_authority(&p) || !parse_path(&p)) {
413 3c1cf9d0 2021-01-11 op *err_ret = p.err;
414 3c1cf9d0 2021-01-11 op return 0;
415 3c1cf9d0 2021-01-11 op }
416 3c1cf9d0 2021-01-11 op
417 3c1cf9d0 2021-01-11 op *err_ret = NULL;
418 3c1cf9d0 2021-01-11 op return 1;
419 3c1cf9d0 2021-01-11 op }
420 3c1cf9d0 2021-01-11 op
421 3c1cf9d0 2021-01-11 op int
422 2fafa2d2 2021-02-01 op serialize_iri(struct iri *i, char *buf, size_t len)
423 2fafa2d2 2021-02-01 op {
424 80fbf1e9 2021-06-16 op size_t l = 0;
425 2fafa2d2 2021-02-01 op
426 2fafa2d2 2021-02-01 op /* in ex.c we receive empty "" strings as NULL */
427 2fafa2d2 2021-02-01 op if (i->schema == NULL || i->host == NULL) {
428 2fafa2d2 2021-02-01 op memset(buf, 0, len);
429 2fafa2d2 2021-02-01 op return 0;
430 2fafa2d2 2021-02-01 op }
431 2fafa2d2 2021-02-01 op
432 2fafa2d2 2021-02-01 op strlcpy(buf, i->schema, len);
433 2fafa2d2 2021-02-01 op strlcat(buf, "://", len);
434 4842c72d 2021-10-18 op strlcat(buf, i->host, len);
435 2fafa2d2 2021-02-01 op strlcat(buf, "/", len);
436 2fafa2d2 2021-02-01 op
437 2fafa2d2 2021-02-01 op if (i->path != NULL)
438 2fafa2d2 2021-02-01 op l = strlcat(buf, i->path, len);
439 2fafa2d2 2021-02-01 op
440 2fafa2d2 2021-02-01 op if (i->query != NULL && *i->query != '\0') {
441 2fafa2d2 2021-02-01 op strlcat(buf, "?", len);
442 2fafa2d2 2021-02-01 op l = strlcat(buf, i->query, len);
443 2fafa2d2 2021-02-01 op }
444 2fafa2d2 2021-02-01 op
445 2fafa2d2 2021-02-01 op return l < len;
446 3c1cf9d0 2021-01-11 op }
447 f2f8eb35 2022-07-04 op
448 f2f8eb35 2022-07-04 op int
449 f2f8eb35 2022-07-04 op encode_path(char *buf, size_t len, const char *path)
450 f2f8eb35 2022-07-04 op {
451 f2f8eb35 2022-07-04 op char *p = buf;
452 f2f8eb35 2022-07-04 op int a, b;
453 f2f8eb35 2022-07-04 op
454 f2f8eb35 2022-07-04 op memset(buf, 0, len);
455 f2f8eb35 2022-07-04 op while (*path != '\0') {
456 f2f8eb35 2022-07-04 op if (len == 1) /* NUL */
457 f2f8eb35 2022-07-04 op return -1;
458 9f006a21 2021-02-07 op
459 f2f8eb35 2022-07-04 op if (unreserved(*path) ||
460 f2f8eb35 2022-07-04 op sub_delimiters(*path) ||
461 f2f8eb35 2022-07-04 op *path == '@' ||
462 f2f8eb35 2022-07-04 op *path == ':' ||
463 f2f8eb35 2022-07-04 op *path == '/') {
464 f2f8eb35 2022-07-04 op *p++ = *path++;
465 f2f8eb35 2022-07-04 op len--;
466 f2f8eb35 2022-07-04 op } else if (len < 4)
467 f2f8eb35 2022-07-04 op return -1;
468 f2f8eb35 2022-07-04 op else {
469 f2f8eb35 2022-07-04 op a = (*path & 0xF0) >> 4;
470 f2f8eb35 2022-07-04 op b = (*path & 0x0F);
471 f2f8eb35 2022-07-04 op
472 f2f8eb35 2022-07-04 op p[0] = '%';
473 f2f8eb35 2022-07-04 op p[1] = a <= 9 ? ('0' + a) : ('7' + a);
474 f2f8eb35 2022-07-04 op p[2] = b <= 9 ? ('0' + b) : ('7' + b);
475 f2f8eb35 2022-07-04 op
476 f2f8eb35 2022-07-04 op path++;
477 f2f8eb35 2022-07-04 op p += 3;
478 f2f8eb35 2022-07-04 op len -= 3;
479 f2f8eb35 2022-07-04 op }
480 f2f8eb35 2022-07-04 op }
481 f2f8eb35 2022-07-04 op
482 f2f8eb35 2022-07-04 op return 0;
483 f2f8eb35 2022-07-04 op }
484 f2f8eb35 2022-07-04 op
485 9f006a21 2021-02-07 op char *
486 9f006a21 2021-02-07 op pct_decode_str(char *s)
487 9f006a21 2021-02-07 op {
488 9f006a21 2021-02-07 op char *t;
489 9f006a21 2021-02-07 op
490 9f006a21 2021-02-07 op for (t = s; *t; ++t) {
491 97b306cb 2022-11-27 op if (*t == '+')
492 97b306cb 2022-11-27 op *t = ' ';
493 97b306cb 2022-11-27 op else if (*t == '%' && valid_pct_enc_string(t))
494 9f006a21 2021-02-07 op pct_decode(t);
495 9f006a21 2021-02-07 op }
496 9f006a21 2021-02-07 op
497 9f006a21 2021-02-07 op return s;
498 9f006a21 2021-02-07 op }