2 5546fdd5 2022-12-23 op * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
4 5546fdd5 2022-12-23 op * Permission to use, copy, modify, and distribute this software for any
5 5546fdd5 2022-12-23 op * purpose with or without fee is hereby granted, provided that the above
6 5546fdd5 2022-12-23 op * copyright notice and this permission notice appear in all copies.
8 5546fdd5 2022-12-23 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 5546fdd5 2022-12-23 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 5546fdd5 2022-12-23 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 5546fdd5 2022-12-23 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 5546fdd5 2022-12-23 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 5546fdd5 2022-12-23 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 5546fdd5 2022-12-23 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 5546fdd5 2022-12-23 op #include <ctype.h>
18 5546fdd5 2022-12-23 op #include <stdint.h>
19 5546fdd5 2022-12-23 op #include <stdlib.h>
20 5546fdd5 2022-12-23 op #include <string.h>
22 5546fdd5 2022-12-23 op #include "iri.h"
24 5546fdd5 2022-12-23 op /* TODO: URI -> IRI. accept IRI but emit always URI */
26 5546fdd5 2022-12-23 op static inline int
27 5546fdd5 2022-12-23 op cpstr(const char *start, const char *till, char *buf, size_t len)
29 5546fdd5 2022-12-23 op size_t slen = till - start;
31 5546fdd5 2022-12-23 op if (slen + 1 >= len)
33 5546fdd5 2022-12-23 op memcpy(buf, start, slen);
34 5546fdd5 2022-12-23 op buf[slen] = '\0';
38 5546fdd5 2022-12-23 op static inline int
39 5546fdd5 2022-12-23 op unreserved(int c)
41 5546fdd5 2022-12-23 op return (isalnum((unsigned char)c) ||
48 5546fdd5 2022-12-23 op static inline int
49 5546fdd5 2022-12-23 op pctenc(const char *s)
51 5546fdd5 2022-12-23 op const char *t = s;
53 5546fdd5 2022-12-23 op return (t[0] == '%' &&
54 5546fdd5 2022-12-23 op isxdigit((unsigned char)t[1]) &&
55 5546fdd5 2022-12-23 op isxdigit((unsigned char)t[2]));
58 5546fdd5 2022-12-23 op static inline int
59 5546fdd5 2022-12-23 op sub_delims(int c)
61 5546fdd5 2022-12-23 op return (c == '!' || c == '$' || c == '&' || c == '\'' ||
62 5546fdd5 2022-12-23 op c == '(' || c == ')' || c == '*' || c == '+' || c == ',' ||
63 5546fdd5 2022-12-23 op c == ';' || c == '=');
66 5546fdd5 2022-12-23 op static inline const char *
67 5546fdd5 2022-12-23 op advance_pchar(const char *s)
69 5546fdd5 2022-12-23 op if (unreserved(*s) || sub_delims(*s) || *s == ':' || *s == '@')
70 5546fdd5 2022-12-23 op return (s + 1);
71 5546fdd5 2022-12-23 op if (pctenc(s))
72 5546fdd5 2022-12-23 op return (s + 3);
73 5546fdd5 2022-12-23 op return (NULL);
76 5546fdd5 2022-12-23 op static inline const char *
77 5546fdd5 2022-12-23 op advance_segment(const char *s)
79 5546fdd5 2022-12-23 op const char *t = s;
81 5546fdd5 2022-12-23 op while ((t = advance_pchar(s)) != NULL)
86 5546fdd5 2022-12-23 op static inline const char *
87 5546fdd5 2022-12-23 op advance_segment_nz(const char *s)
89 5546fdd5 2022-12-23 op const char *t;
91 5546fdd5 2022-12-23 op if ((t = advance_pchar(s)) == NULL)
92 5546fdd5 2022-12-23 op return (NULL);
93 5546fdd5 2022-12-23 op return (advance_segment(t));
96 5546fdd5 2022-12-23 op static inline const char *
97 5546fdd5 2022-12-23 op advance_segment_nz_nc(const char *s)
99 5546fdd5 2022-12-23 op const char *t = s;
102 5546fdd5 2022-12-23 op if (unreserved(*t) || sub_delims(*t) || *t == '@')
104 5546fdd5 2022-12-23 op else if (pctenc(t))
110 5546fdd5 2022-12-23 op return (t != s ? t : NULL);
113 5546fdd5 2022-12-23 op static const char *
114 5546fdd5 2022-12-23 op parse_scheme(const char *s, struct iri *iri)
116 5546fdd5 2022-12-23 op const char *t = s;
118 5546fdd5 2022-12-23 op if (!isalpha((unsigned char)*t))
119 5546fdd5 2022-12-23 op return (NULL);
121 5546fdd5 2022-12-23 op while (isalnum((unsigned char)*t) ||
127 5546fdd5 2022-12-23 op if (cpstr(s, t, iri->iri_scheme, sizeof(iri->iri_scheme)) == -1)
128 5546fdd5 2022-12-23 op return (NULL);
130 5546fdd5 2022-12-23 op iri->iri_flags |= IH_SCHEME;
134 5546fdd5 2022-12-23 op /* userinfo is always optional */
135 5546fdd5 2022-12-23 op static const char *
136 5546fdd5 2022-12-23 op parse_uinfo(const char *s, struct iri *iri)
138 5546fdd5 2022-12-23 op const char *t = s;
141 5546fdd5 2022-12-23 op if (unreserved(*t) || sub_delims(*t) || *t == ':')
143 5546fdd5 2022-12-23 op else if (pctenc(t))
149 5546fdd5 2022-12-23 op if (*t != '@')
152 5546fdd5 2022-12-23 op if (cpstr(s, t, iri->iri_uinfo, sizeof(iri->iri_uinfo)) == -1)
153 5546fdd5 2022-12-23 op return (NULL);
154 5546fdd5 2022-12-23 op iri->iri_flags |= IH_UINFO;
155 5546fdd5 2022-12-23 op return (t + 1);
158 5546fdd5 2022-12-23 op static const char *
159 5546fdd5 2022-12-23 op parse_host(const char *s, struct iri *iri)
161 5546fdd5 2022-12-23 op const char *t = s;
164 5546fdd5 2022-12-23 op * cheating a bit by relaxing and merging the rule for
165 5546fdd5 2022-12-23 op * IPv6address and IPvFuture and by merging IPv4address and
169 5546fdd5 2022-12-23 op if (*t == '[') {
170 5546fdd5 2022-12-23 op while (*t && *t != ']')
172 5546fdd5 2022-12-23 op if (*t == '\0')
173 5546fdd5 2022-12-23 op return (NULL);
175 5546fdd5 2022-12-23 op if (cpstr(s, t, iri->iri_host, sizeof(iri->iri_host)) == -1)
176 5546fdd5 2022-12-23 op return (NULL);
177 5546fdd5 2022-12-23 op iri->iri_flags |= IH_HOST;
182 5546fdd5 2022-12-23 op if (unreserved(*t) || sub_delims(*t))
184 5546fdd5 2022-12-23 op else if (pctenc(t))
190 5546fdd5 2022-12-23 op if (cpstr(s, t, iri->iri_host, sizeof(iri->iri_host)) == -1)
191 5546fdd5 2022-12-23 op return (NULL);
192 5546fdd5 2022-12-23 op iri->iri_flags |= IH_HOST;
196 5546fdd5 2022-12-23 op static const char *
197 5546fdd5 2022-12-23 op parse_port(const char *s, struct iri *iri)
199 5546fdd5 2022-12-23 op const char *t = s;
200 5546fdd5 2022-12-23 op const char *errstr;
202 5546fdd5 2022-12-23 op while (isdigit((unsigned char)*t))
204 5546fdd5 2022-12-23 op if (cpstr(s, t, iri->iri_portstr, sizeof(iri->iri_portstr)) == -1)
205 5546fdd5 2022-12-23 op return (NULL);
206 5546fdd5 2022-12-23 op iri->iri_port = strtonum(iri->iri_portstr, 1, UINT16_MAX, &errstr);
208 5546fdd5 2022-12-23 op return (NULL);
209 5546fdd5 2022-12-23 op iri->iri_flags |= IH_PORT;
213 5546fdd5 2022-12-23 op static const char *
214 5546fdd5 2022-12-23 op parse_authority(const char *s, struct iri *iri)
216 5546fdd5 2022-12-23 op const char *t;
218 5546fdd5 2022-12-23 op if ((t = parse_uinfo(s, iri)) == NULL)
219 5546fdd5 2022-12-23 op return (NULL);
221 5546fdd5 2022-12-23 op if ((t = parse_host(t, iri)) == NULL)
222 5546fdd5 2022-12-23 op return (NULL);
224 5546fdd5 2022-12-23 op if (*t == ':')
225 5546fdd5 2022-12-23 op return (parse_port(t, iri));
230 5546fdd5 2022-12-23 op static const char *
231 5546fdd5 2022-12-23 op parse_path_abempty(const char *s, struct iri *iri)
233 5546fdd5 2022-12-23 op const char *t = s;
235 5546fdd5 2022-12-23 op while (*t == '/')
236 5546fdd5 2022-12-23 op t = advance_segment(t + 1);
238 5546fdd5 2022-12-23 op if (cpstr(s, t, iri->iri_path, sizeof(iri->iri_path)) == -1)
239 5546fdd5 2022-12-23 op return (NULL);
240 5546fdd5 2022-12-23 op iri->iri_flags |= IH_PATH;
244 5546fdd5 2022-12-23 op static const char *
245 5546fdd5 2022-12-23 op parse_path_absolute(const char *s, struct iri *iri)
247 5546fdd5 2022-12-23 op const char *t;
249 5546fdd5 2022-12-23 op if (*s != '/')
250 5546fdd5 2022-12-23 op return (NULL);
252 5546fdd5 2022-12-23 op if ((t = advance_segment_nz(s + 1)) == NULL)
253 5546fdd5 2022-12-23 op return (s + 1);
255 5546fdd5 2022-12-23 op while (*t == '/')
256 5546fdd5 2022-12-23 op t = advance_segment(t + 1);
258 5546fdd5 2022-12-23 op if (cpstr(s, t, iri->iri_path, sizeof(iri->iri_path)) == -1)
259 5546fdd5 2022-12-23 op return (NULL);
260 5546fdd5 2022-12-23 op iri->iri_flags |= IH_PATH;
264 5546fdd5 2022-12-23 op static const char *
265 5546fdd5 2022-12-23 op parse_path_rootless(const char *s, struct iri *iri)
267 5546fdd5 2022-12-23 op const char *t;
269 5546fdd5 2022-12-23 op if ((t = advance_segment_nz(s)) == NULL)
270 5546fdd5 2022-12-23 op return (NULL);
272 5546fdd5 2022-12-23 op while (*t == '/')
273 5546fdd5 2022-12-23 op t = advance_segment(t + 1);
275 5546fdd5 2022-12-23 op if (cpstr(s, t, iri->iri_path, sizeof(iri->iri_path)) == -1)
276 5546fdd5 2022-12-23 op return (NULL);
277 5546fdd5 2022-12-23 op iri->iri_flags |= IH_PATH;
281 5546fdd5 2022-12-23 op static const char *
282 5546fdd5 2022-12-23 op parse_path_noscheme(const char *s, struct iri *iri)
284 5546fdd5 2022-12-23 op const char *t;
286 5546fdd5 2022-12-23 op if ((t = advance_segment_nz_nc(s)) == NULL)
287 5546fdd5 2022-12-23 op return (NULL);
289 5546fdd5 2022-12-23 op while (*t == '/')
290 5546fdd5 2022-12-23 op t = advance_segment(t + 1);
292 5546fdd5 2022-12-23 op if (cpstr(s, t, iri->iri_path, sizeof(iri->iri_path)) == -1)
293 5546fdd5 2022-12-23 op return (NULL);
294 5546fdd5 2022-12-23 op iri->iri_flags |= IH_PATH;
298 5546fdd5 2022-12-23 op static const char *
299 5546fdd5 2022-12-23 op parse_path_empty(const char *s, struct iri *iri)
301 5546fdd5 2022-12-23 op iri->iri_path[0] = '\0';
302 5546fdd5 2022-12-23 op iri->iri_flags |= IH_PATH;
306 5546fdd5 2022-12-23 op static const char *
307 5546fdd5 2022-12-23 op parse_hier(const char *s, struct iri *iri)
309 5546fdd5 2022-12-23 op const char *t;
311 5546fdd5 2022-12-23 op if (!strncmp(s, "//", 2)) {
312 5546fdd5 2022-12-23 op if ((t = parse_authority(s + 2, iri)) == NULL)
313 5546fdd5 2022-12-23 op return (NULL);
314 5546fdd5 2022-12-23 op return (parse_path_abempty(t, iri));
317 5546fdd5 2022-12-23 op if ((t = parse_path_absolute(s, iri)) != NULL)
320 5546fdd5 2022-12-23 op if ((t = parse_path_rootless(s, iri)) != NULL)
323 5546fdd5 2022-12-23 op return (parse_path_empty(s, iri));
326 5546fdd5 2022-12-23 op static const char *
327 5546fdd5 2022-12-23 op parse_relative(const char *s, struct iri *iri)
329 5546fdd5 2022-12-23 op const char *t = s;
331 5546fdd5 2022-12-23 op if (!strncmp(s, "//", 2)) {
332 5546fdd5 2022-12-23 op if ((t = parse_authority(s + 2, iri)) == NULL)
333 5546fdd5 2022-12-23 op return (NULL);
334 5546fdd5 2022-12-23 op return (parse_path_abempty(t, iri));
337 5546fdd5 2022-12-23 op if ((t = parse_path_absolute(s, iri)) != NULL)
340 5546fdd5 2022-12-23 op if ((t = parse_path_noscheme(s, iri)) != NULL)
343 5546fdd5 2022-12-23 op return (parse_path_empty(s, iri));
346 5546fdd5 2022-12-23 op static const char *
347 5546fdd5 2022-12-23 op parse_query(const char *s, struct iri *iri)
349 5546fdd5 2022-12-23 op const char *n, *t = s;
352 5546fdd5 2022-12-23 op if ((n = advance_pchar(t)) != NULL)
354 5546fdd5 2022-12-23 op else if (*t == '/' || *t == '?')
360 5546fdd5 2022-12-23 op if (cpstr(s, t, iri->iri_query, sizeof(iri->iri_query)) == -1)
361 5546fdd5 2022-12-23 op return (NULL);
362 5546fdd5 2022-12-23 op iri->iri_flags |= IH_QUERY;
367 5546fdd5 2022-12-23 op parse_uri(const char *s, struct iri *iri)
369 5546fdd5 2022-12-23 op if ((s = parse_scheme(s, iri)) == NULL)
372 5546fdd5 2022-12-23 op if (*s != ':')
375 5546fdd5 2022-12-23 op if ((s = parse_hier(s + 1, iri)) == NULL)
378 5546fdd5 2022-12-23 op if (*s == '?' && (s = parse_query(s + 1, iri)) == NULL)
381 5546fdd5 2022-12-23 op /* skip fragments */
382 5546fdd5 2022-12-23 op if (*s == '#' || *s == '\0')
389 5546fdd5 2022-12-23 op parse_relative_ref(const char *s, struct iri *iri)
391 5546fdd5 2022-12-23 op if ((s = parse_relative(s, iri)) == NULL)
394 5546fdd5 2022-12-23 op if (*s == '?' && (s = parse_query(s + 1, iri)) == NULL)
397 5546fdd5 2022-12-23 op /* skip fragments */
398 5546fdd5 2022-12-23 op if (*s == '#' || *s == '\0')
405 5546fdd5 2022-12-23 op parse(const char *s, struct iri *iri)
407 5546fdd5 2022-12-23 op iri->iri_flags = 0;
409 5546fdd5 2022-12-23 op if (s == NULL)
412 5546fdd5 2022-12-23 op if (parse_uri(s, iri) == -1) {
413 5546fdd5 2022-12-23 op iri->iri_flags = 0;
414 5546fdd5 2022-12-23 op if (parse_relative_ref(s, iri) == -1)
422 5546fdd5 2022-12-23 op cpfields(struct iri *dest, const struct iri *src, int flags)
424 5546fdd5 2022-12-23 op if (flags & IH_SCHEME) {
425 5546fdd5 2022-12-23 op dest->iri_flags |= IH_SCHEME;
426 5546fdd5 2022-12-23 op if (src->iri_flags & IH_SCHEME)
427 5546fdd5 2022-12-23 op memcpy(dest->iri_scheme, src->iri_scheme,
428 5546fdd5 2022-12-23 op sizeof(dest->iri_scheme));
430 5546fdd5 2022-12-23 op if (flags & IH_UINFO) {
431 5546fdd5 2022-12-23 op dest->iri_flags |= IH_UINFO;
432 5546fdd5 2022-12-23 op if (src->iri_flags & IH_UINFO)
433 5546fdd5 2022-12-23 op memcpy(dest->iri_uinfo, src->iri_uinfo,
434 5546fdd5 2022-12-23 op sizeof(dest->iri_uinfo));
436 5546fdd5 2022-12-23 op if (flags & IH_HOST) {
437 5546fdd5 2022-12-23 op dest->iri_flags |= IH_HOST;
438 5546fdd5 2022-12-23 op if (src->iri_flags & IH_HOST)
439 5546fdd5 2022-12-23 op memcpy(dest->iri_host, src->iri_host,
440 5546fdd5 2022-12-23 op sizeof(dest->iri_host));
442 5546fdd5 2022-12-23 op if (flags & IH_PORT) {
443 5546fdd5 2022-12-23 op dest->iri_flags |= IH_PORT;
444 5546fdd5 2022-12-23 op if (src->iri_flags & IH_PORT)
445 5546fdd5 2022-12-23 op dest->iri_port = src->iri_port;
447 5546fdd5 2022-12-23 op if (flags & IH_PATH) {
448 5546fdd5 2022-12-23 op dest->iri_flags |= IH_PATH;
449 5546fdd5 2022-12-23 op if (src->iri_flags & IH_PATH)
450 5546fdd5 2022-12-23 op memcpy(dest->iri_path, src->iri_path,
451 5546fdd5 2022-12-23 op sizeof(dest->iri_path));
453 5546fdd5 2022-12-23 op if (flags & IH_QUERY) {
454 5546fdd5 2022-12-23 op dest->iri_flags |= IH_QUERY;
455 5546fdd5 2022-12-23 op if (src->iri_flags & IH_QUERY)
456 5546fdd5 2022-12-23 op memcpy(dest->iri_query, src->iri_query,
457 5546fdd5 2022-12-23 op sizeof(dest->iri_query));
461 5546fdd5 2022-12-23 op static inline void
462 5546fdd5 2022-12-23 op remove_dot_segments(struct iri *iri)
464 5546fdd5 2022-12-23 op /* TODO: fixup iri->iri_path */
468 5546fdd5 2022-12-23 op static inline void
469 5546fdd5 2022-12-23 op mergepath(char *out, size_t len, const char *a, const char *b)
471 5546fdd5 2022-12-23 op /* TODO: compute into out path `b' resolved from `a' */
472 5546fdd5 2022-12-23 op memset(out, 0, len);
477 5546fdd5 2022-12-23 op iri_parse(const char *base, const char *str, struct iri *iri)
479 5546fdd5 2022-12-23 op static struct iri ibase, iparsed;
481 5546fdd5 2022-12-23 op memset(iri, 0, sizeof(*iri));
483 5546fdd5 2022-12-23 op if (base == NULL) {
484 5546fdd5 2022-12-23 op ibase.iri_flags = 0;
485 5546fdd5 2022-12-23 op if (parse_uri(str, &iparsed) == -1)
488 5546fdd5 2022-12-23 op if (parse_uri(base, &ibase) == -1)
490 5546fdd5 2022-12-23 op if (parse(str, &iparsed) == -1)
494 5546fdd5 2022-12-23 op if (iparsed.iri_flags & IH_SCHEME) {
495 5546fdd5 2022-12-23 op cpfields(iri, &iparsed, iparsed.iri_flags);
496 5546fdd5 2022-12-23 op remove_dot_segments(iri);
500 5546fdd5 2022-12-23 op /* if fragments are supported, copy iparsed fragment to iri */
502 5546fdd5 2022-12-23 op cpfields(iri, &ibase, IH_SCHEME);
504 5546fdd5 2022-12-23 op if (iparsed.iri_flags & IH_HOST) {
505 5546fdd5 2022-12-23 op cpfields(iri, &iparsed, IH_AUTHORITY|IH_PATH|IH_QUERY);
506 5546fdd5 2022-12-23 op remove_dot_segments(iri);
510 5546fdd5 2022-12-23 op cpfields(iri, &ibase, IH_AUTHORITY);
512 5546fdd5 2022-12-23 op if ((iparsed.iri_flags & IH_PATH) && *iparsed.iri_path == '\0') {
513 5546fdd5 2022-12-23 op cpfields(iri, &ibase, IH_PATH);
514 5546fdd5 2022-12-23 op if (iparsed.iri_flags & IH_QUERY)
515 5546fdd5 2022-12-23 op cpfields(iri, &iparsed, IH_QUERY);
517 5546fdd5 2022-12-23 op cpfields(iri, &ibase, IH_QUERY);
521 5546fdd5 2022-12-23 op cpfields(iri, &iparsed, IH_QUERY);
522 5546fdd5 2022-12-23 op if ((iparsed.iri_flags & IH_PATH) && !strcmp(iparsed.iri_path, "/"))
523 5546fdd5 2022-12-23 op cpfields(iri, &iparsed, IH_PATH);
525 5546fdd5 2022-12-23 op if (!(ibase.iri_flags & IH_PATH))
526 5546fdd5 2022-12-23 op ibase.iri_path[0] = '\0';
527 5546fdd5 2022-12-23 op if (!(iparsed.iri_flags & IH_PATH))
528 5546fdd5 2022-12-23 op iparsed.iri_path[0] = '\0';
529 5546fdd5 2022-12-23 op mergepath(iri->iri_path, sizeof(iri->iri_path),
530 5546fdd5 2022-12-23 op ibase.iri_path, iparsed.iri_path);
532 5546fdd5 2022-12-23 op remove_dot_segments(iri);
533 5546fdd5 2022-12-23 op cpfields(iri, &ibase, IH_QUERY);
538 5546fdd5 2022-12-23 op iri_unparse(const struct iri *iri, char *buf, size_t buflen)
540 5546fdd5 2022-12-23 op memset(buf, 0, buflen);
545 5546fdd5 2022-12-23 op iri_human(const struct iri *iri, char *buf, size_t buflen)
547 5546fdd5 2022-12-23 op memset(buf, 0, buflen);
552 5546fdd5 2022-12-23 op iri_setquery(struct iri *iri, const char *text)