2 * Sherlock Library -- URL Functions (according to RFC 1738 and 1808)
4 * (c) 1997--2001 Martin Mares <mj@ucw.cz>
5 * (c) 2001 Robert Spalek <robert@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
13 #include "lib/chartype.h"
22 static uns url_ignore_spaces;
23 static uns url_ignore_underflow;
24 static byte *url_component_separators = "";
25 static uns url_min_repeat_count = 0x7fffffff;
26 static uns url_max_repeat_length = 0;
28 static struct cfitem url_config[] = {
29 { "URL", CT_SECTION, NULL },
30 { "IgnoreSpaces", CT_INT, &url_ignore_spaces },
31 { "IgnoreUnderflow", CT_INT, &url_ignore_underflow },
32 { "ComponentSeparators", CT_STRING, &url_component_separators },
33 { "MinRepeatCount", CT_INT, &url_min_repeat_count },
34 { "MaxRepeatLength", CT_INT, &url_max_repeat_length },
35 { NULL, CT_STOP, NULL }
38 static void CONSTRUCTOR url_init_config(void)
40 cf_register(url_config);
43 /* Escaping and de-escaping */
48 return (x<10) ? (x + '0') : (x - 10 + 'A');
52 url_deescape(byte *s, byte *d)
55 byte *end = d + MAX_URL_SIZE - 10;
59 return URL_ERR_TOO_LONG;
63 if (!Cxdigit(s[1]) || !Cxdigit(s[2]))
64 return URL_ERR_INVALID_ESCAPE;
65 val = Cxvalue(s[1])*16 + Cxvalue(s[2]);
67 return URL_ERR_INVALID_ESCAPED_CHAR;
71 val = NCC_SEMICOLON; break;
73 val = NCC_SLASH; break;
75 val = NCC_QUEST; break;
77 val = NCC_COLON; break;
81 val = NCC_EQUAL; break;
85 val = NCC_HASH; break;
97 if (!url_ignore_spaces || !(!*s || d == dstart))
102 return URL_ERR_TOO_LONG;
108 return URL_ERR_INVALID_CHAR;
115 url_enescape(byte *s, byte *d)
117 byte *end = d + MAX_URL_SIZE - 10;
123 return URL_ERR_TOO_LONG;
124 if (Calnum(c) || /* RFC 1738(2.2): Only alphanumerics ... */
125 c == '$' || c == '-' || c == '_' || c == '.' || c == '+' || /* ... and several other exceptions ... */
126 c == '!' || c == '*' || c == '\'' || c == '(' || c == ')' ||
128 c == '/' || c == '?' || c == ':' || c == '@' || /* ... and reserved chars used for reserved purpose */
129 c == '=' || c == '&' || c == '#' || c == ';')
133 uns val = (*s < NCC_MAX) ? NCC_CHARS[*s] : *s;
135 *d++ = enhex(val >> 4);
136 *d++ = enhex(val & 0x0f);
144 /* Split an URL (several parts may be copied to the destination buffer) */
146 byte *url_proto_names[URL_PROTO_MAX] = URL_PNAMES;
147 static int url_proto_path_flags[URL_PROTO_MAX] = URL_PATH_FLAGS;
150 identify_protocol(byte *p)
154 for(i=1; i<URL_PROTO_MAX; i++)
155 if (!strcasecmp(p, url_proto_names[i]))
157 return URL_PROTO_UNKNOWN;
161 url_split(byte *s, struct url *u, byte *d)
163 bzero(u, sizeof(struct url));
165 u->bufend = d + MAX_URL_SIZE - 10;
167 if (s[0] != '/') /* Seek for "protocol:" */
170 while (*p && Calnum(*p))
172 if (p != s && *p == ':')
178 u->protoid = identify_protocol(u->protocol);
180 if (url_proto_path_flags[u->protoid] && (s[0] != '/' || s[1] != '/'))
182 /* The protocol requires complete host spec, but it's missing -> treat as a relative path instead */
183 int len = d - u->protocol;
192 if (s[0] == '/') /* Host spec or absolute path */
194 if (s[1] == '/') /* Host spec */
201 while (*s && *s != '/') /* Copy user:passwd@host:port */
205 if (w) /* user:passwd present */
213 if (e) /* host:port present */
217 p = strtoul(e, &ep, 10);
218 if (ep && *ep || p > 65535)
219 return URL_ERR_INVALID_PORT;
220 else if (p) /* Port 0 (e.g. in :/) is treated as default port */
232 /* Normalization according to given base URL */
234 static uns std_ports[] = URL_DEFPORTS; /* Default port numbers */
237 relpath_merge(struct url *u, struct url *b)
245 if (a[0] == '/') /* Absolute path => OK */
248 return URL_PATH_UNDERFLOW;
250 if (!a[0]) /* Empty URL -> inherit everything */
256 u->rest = d; /* We know we'll need to copy the path somewhere else */
258 if (a[0] == '#') /* Another fragment */
260 for(p=o; *p && *p != '#'; p++)
264 if (a[0] == '?') /* New query */
266 for(p=o; *p && *p != '#' && *p != '?'; p++)
270 if (a[0] == ';') /* Change parameters */
272 for(p=o; *p && *p != ';' && *p != '?' && *p != '#'; p++)
277 p = NULL; /* Copy original path and find the last slash */
278 while (*o && *o != ';' && *o != '?' && *o != '#')
281 return URL_ERR_TOO_LONG;
282 if ((*d++ = *o++) == '/')
286 return URL_ERR_REL_NOTHING;
293 if (a[1] == '/' || !a[1]) /* Skip "./" and ".$" */
300 else if (a[1] == '.' && (a[2] == '/' || !a[2])) /* "../" */
308 * RFC 1808 says we should leave ".." as a path segment, but
309 * we intentionally break the rule and refuse the URL.
311 if (!url_ignore_underflow)
312 return URL_PATH_UNDERFLOW;
316 d--; /* Discard trailing slash */
323 while (a[0] && a[0] != '/')
326 return URL_ERR_TOO_LONG;
338 copy: /* Combine part of old URL with the new one */
343 return URL_ERR_TOO_LONG;
348 return URL_ERR_TOO_LONG;
353 url_normalize(struct url *u, struct url *b)
358 if (url_proto_path_flags[u->protoid] && !u->host ||
359 u->host && !*u->host ||
360 !u->host && u->user ||
362 return URL_SYNTAX_ERROR;
366 /* Now we know it's a relative URL. Do we have any base? */
367 if (!b || !url_proto_path_flags[b->protoid])
368 return URL_ERR_REL_NOTHING;
369 u->protocol = b->protocol;
370 u->protoid = b->protoid;
372 /* Reference to the same host */
378 if (err = relpath_merge(u, b))
383 /* Fill in missing info */
385 u->port = std_ports[u->protoid];
390 /* Name canonicalization */
398 if (*b >= 'A' && *b <= 'Z')
405 kill_end_dot(byte *b)
411 k = b + strlen(b) - 1;
412 if (k > b && *k == '.')
418 url_canonicalize(struct url *u)
422 lowercase(u->protocol);
424 kill_end_dot(u->host);
425 if ((!u->rest || !*u->rest) && url_proto_path_flags[u->protoid])
427 if (u->rest && (c = strchr(u->rest, '#'))) /* Kill fragment reference */
432 /* Pack a broken-down URL */
435 append(byte *d, byte *s, byte *e)
448 url_pack(struct url *u, byte *d)
450 byte *e = d + MAX_URL_SIZE - 10;
454 d = append(d, u->protocol, e);
455 d = append(d, ":", e);
456 u->protoid = identify_protocol(u->protocol);
460 d = append(d, "//", e);
463 d = append(d, u->user, e);
464 d = append(d, "@", e);
466 d = append(d, u->host, e);
467 if (u->port != std_ports[u->protoid] && u->port != ~0U)
470 sprintf(z, "%d", u->port);
471 d = append(d, ":", e);
476 d = append(d, u->rest, e);
478 return URL_ERR_TOO_LONG;
485 static char *errmsg[] = {
486 "Something is wrong",
490 "Invalid escaped character",
491 "Invalid port number",
492 "Relative URL not allowed",
501 if (err >= sizeof(errmsg) / sizeof(char *))
506 /* Standard cookbook recipes */
509 url_canon_split(byte *u, byte *buf1, byte *buf2, struct url *url)
513 if (err = url_deescape(u, buf1))
515 if (err = url_split(buf1, url, buf2))
517 if (err = url_normalize(url, NULL))
519 return url_canonicalize(url);
523 url_auto_canonicalize(byte *src, byte *dst)
525 byte buf1[MAX_URL_SIZE], buf2[MAX_URL_SIZE], buf3[MAX_URL_SIZE];
529 (void)((err = url_canon_split(src, buf1, buf2, &ur)) ||
530 (err = url_pack(&ur, buf3)) ||
531 (err = url_enescape(buf3, dst)));
539 int main(int argc, char **argv)
541 char buf1[MAX_URL_SIZE], buf2[MAX_URL_SIZE], buf3[MAX_URL_SIZE], buf4[MAX_URL_SIZE];
543 struct url url, url0;
547 if (err = url_deescape(argv[1], buf1))
549 printf("deesc: error %d\n", err);
552 printf("deesc: %s\n", buf1);
553 if (err = url_split(buf1, &url, buf2))
555 printf("split: error %d\n", err);
558 printf("split: @%s@%s@%s@%d@%s\n", url.protocol, url.user, url.host, url.port, url.rest);
559 if (err = url_split("http://mj@www.hell.org/123/sub_dir/index.html;param?query&zzz/subquery#fragment", &url0, buf3))
561 printf("split base: error %d\n", err);
564 if (err = url_normalize(&url0, NULL))
566 printf("normalize base: error %d\n", err);
569 printf("base: @%s@%s@%s@%d@%s\n", url0.protocol, url0.user, url0.host, url0.port, url0.rest);
570 if (err = url_normalize(&url, &url0))
572 printf("normalize: error %d\n", err);
575 printf("normalize: @%s@%s@%s@%d@%s\n", url.protocol, url.user, url.host, url.port, url.rest);
576 if (err = url_canonicalize(&url))
578 printf("canonicalize: error %d\n", err);
581 printf("canonicalize: @%s@%s@%s@%d@%s\n", url.protocol, url.user, url.host, url.port, url.rest);
582 if (err = url_pack(&url, buf4))
584 printf("pack: error %d\n", err);
587 printf("pack: %s\n", buf4);
588 if (err = url_enescape(buf4, buf2))
590 printf("enesc: error %d\n", err);
593 printf("enesc: %s\n", buf2);
606 hashf(byte *start, int length)
610 hf = (hf << 8 | hf >> 24) ^ *start++;
615 repeat_count(struct component *comp, uns count, uns len)
617 struct component *orig_comp = comp;
627 for (i=0; i<len; i++)
628 if (comp[i].hash != orig_comp[i].hash
629 || comp[i].length != orig_comp[i].length
630 || memcmp(comp[i].start, orig_comp[i].start, comp[i].length))
636 url_has_repeated_component(byte *url)
638 struct component *comp;
639 uns comps, comp_len, rep_prefix;
643 for (comps=0, c=url; c; comps++)
645 c = strpbrk(c, url_component_separators);
649 if (comps < url_min_repeat_count)
651 comp = alloca(comps * sizeof(struct component));
652 for (i=0, c=url; c; i++)
655 c = strpbrk(c, url_component_separators);
658 comp[i].length = c - comp[i].start;
662 comp[i].length = strlen(comp[i].start);
665 for (i=0; i<comps; i++)
666 comp[i].hash = hashf(comp[i].start, comp[i].length);
667 for (comp_len = 1; comp_len <= url_max_repeat_length && comp_len <= comps; comp_len++)
668 for (rep_prefix = 0; rep_prefix <= comps - comp_len; rep_prefix++)
669 if (repeat_count(comp + rep_prefix, comps - rep_prefix, comp_len) >= url_min_repeat_count)