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>
10 #include "lib/chartype.h"
19 static uns url_ignore_spaces;
20 static uns url_ignore_underflow;
21 static byte *url_component_separators = "";
22 static uns url_min_repeat_count = 0x7fffffff;
23 static uns url_max_repeat_length = 0;
25 static struct cfitem url_config[] = {
26 { "URL", CT_SECTION, NULL },
27 { "IgnoreSpaces", CT_INT, &url_ignore_spaces },
28 { "IgnoreUnderflow", CT_INT, &url_ignore_underflow },
29 { "ComponentSeparators", CT_STRING, &url_component_separators },
30 { "MinRepeatCount", CT_INT, &url_min_repeat_count },
31 { "MaxRepeatLength", CT_INT, &url_max_repeat_length },
32 { NULL, CT_STOP, NULL }
35 static void CONSTRUCTOR url_init_config(void)
37 cf_register(url_config);
40 /* Escaping and de-escaping */
45 return (x<10) ? (x + '0') : (x - 10 + 'A');
49 url_deescape(byte *s, byte *d)
52 byte *end = d + MAX_URL_SIZE - 10;
56 return URL_ERR_TOO_LONG;
60 if (!Cxdigit(s[1]) || !Cxdigit(s[2]))
61 return URL_ERR_INVALID_ESCAPE;
62 val = Cxvalue(s[1])*16 + Cxvalue(s[2]);
64 return URL_ERR_INVALID_ESCAPED_CHAR;
68 val = NCC_SEMICOLON; break;
70 val = NCC_SLASH; break;
72 val = NCC_QUEST; break;
74 val = NCC_COLON; break;
78 val = NCC_EQUAL; break;
82 val = NCC_HASH; break;
94 if (!url_ignore_spaces || !(!*s || d == dstart))
99 return URL_ERR_TOO_LONG;
105 return URL_ERR_INVALID_CHAR;
112 url_enescape(byte *s, byte *d)
114 byte *end = d + MAX_URL_SIZE - 10;
120 return URL_ERR_TOO_LONG;
121 if (Calnum(c) || /* RFC 1738(2.2): Only alphanumerics ... */
122 c == '$' || c == '-' || c == '_' || c == '.' || c == '+' || /* ... and several other exceptions ... */
123 c == '!' || c == '*' || c == '\'' || c == '(' || c == ')' ||
125 c == '/' || c == '?' || c == ':' || c == '@' || /* ... and reserved chars used for reserved purpose */
126 c == '=' || c == '&' || c == '#' || c == ';')
130 uns val = (*s < NCC_MAX) ? NCC_CHARS[*s] : *s;
132 *d++ = enhex(val >> 4);
133 *d++ = enhex(val & 0x0f);
141 /* Split an URL (several parts may be copied to the destination buffer) */
143 byte *url_proto_names[URL_PROTO_MAX] = URL_PNAMES;
144 static int url_proto_path_flags[URL_PROTO_MAX] = URL_PATH_FLAGS;
147 identify_protocol(byte *p)
151 for(i=1; i<URL_PROTO_MAX; i++)
152 if (!strcasecmp(p, url_proto_names[i]))
154 return URL_PROTO_UNKNOWN;
158 url_split(byte *s, struct url *u, byte *d)
160 bzero(u, sizeof(struct url));
162 u->bufend = d + MAX_URL_SIZE - 10;
164 if (s[0] != '/') /* Seek for "protocol:" */
167 while (*p && Calnum(*p))
169 if (p != s && *p == ':')
175 u->protoid = identify_protocol(u->protocol);
177 if (url_proto_path_flags[u->protoid] && (s[0] != '/' || s[1] != '/'))
179 /* The protocol requires complete host spec, but it's missing -> treat as a relative path instead */
180 int len = d - u->protocol;
189 if (s[0] == '/') /* Host spec or absolute path */
191 if (s[1] == '/') /* Host spec */
198 while (*s && *s != '/') /* Copy user:passwd@host:port */
202 if (w) /* user:passwd present */
210 if (e) /* host:port present */
214 p = strtoul(e, &ep, 10);
215 if (ep && *ep || p > 65535)
216 return URL_ERR_INVALID_PORT;
217 else if (p) /* Port 0 (e.g. in :/) is treated as default port */
229 /* Normalization according to given base URL */
231 static uns std_ports[] = URL_DEFPORTS; /* Default port numbers */
234 relpath_merge(struct url *u, struct url *b)
242 if (a[0] == '/') /* Absolute path => OK */
245 return URL_PATH_UNDERFLOW;
247 if (!a[0]) /* Empty URL -> inherit everything */
253 u->rest = d; /* We know we'll need to copy the path somewhere else */
255 if (a[0] == '#') /* Another fragment */
257 for(p=o; *p && *p != '#'; p++)
261 if (a[0] == '?') /* New query */
263 for(p=o; *p && *p != '#' && *p != '?'; p++)
267 if (a[0] == ';') /* Change parameters */
269 for(p=o; *p && *p != ';' && *p != '?' && *p != '#'; p++)
274 p = NULL; /* Copy original path and find the last slash */
275 while (*o && *o != ';' && *o != '?' && *o != '#')
278 return URL_ERR_TOO_LONG;
279 if ((*d++ = *o++) == '/')
283 return URL_ERR_REL_NOTHING;
290 if (a[1] == '/' || !a[1]) /* Skip "./" and ".$" */
297 else if (a[1] == '.' && (a[2] == '/' || !a[2])) /* "../" */
305 * RFC 1808 says we should leave ".." as a path segment, but
306 * we intentionally break the rule and refuse the URL.
308 if (!url_ignore_underflow)
309 return URL_PATH_UNDERFLOW;
313 d--; /* Discard trailing slash */
320 while (a[0] && a[0] != '/')
323 return URL_ERR_TOO_LONG;
335 copy: /* Combine part of old URL with the new one */
340 return URL_ERR_TOO_LONG;
345 return URL_ERR_TOO_LONG;
350 url_normalize(struct url *u, struct url *b)
355 if (url_proto_path_flags[u->protoid] && !u->host ||
356 u->host && !*u->host ||
357 !u->host && u->user ||
359 return URL_SYNTAX_ERROR;
363 /* Now we know it's a relative URL. Do we have any base? */
364 if (!b || !url_proto_path_flags[b->protoid])
365 return URL_ERR_REL_NOTHING;
366 u->protocol = b->protocol;
367 u->protoid = b->protoid;
369 /* Reference to the same host */
375 if (err = relpath_merge(u, b))
380 /* Fill in missing info */
382 u->port = std_ports[u->protoid];
387 /* Name canonicalization */
395 if (*b >= 'A' && *b <= 'Z')
402 kill_end_dot(byte *b)
408 k = b + strlen(b) - 1;
409 if (k > b && *k == '.')
415 url_canonicalize(struct url *u)
419 lowercase(u->protocol);
421 kill_end_dot(u->host);
422 if ((!u->rest || !*u->rest) && url_proto_path_flags[u->protoid])
424 if (u->rest && (c = strchr(u->rest, '#'))) /* Kill fragment reference */
429 /* Pack a broken-down URL */
432 append(byte *d, byte *s, byte *e)
445 url_pack(struct url *u, byte *d)
447 byte *e = d + MAX_URL_SIZE - 10;
451 d = append(d, u->protocol, e);
452 d = append(d, ":", e);
453 u->protoid = identify_protocol(u->protocol);
457 d = append(d, "//", e);
460 d = append(d, u->user, e);
461 d = append(d, "@", e);
463 d = append(d, u->host, e);
464 if (u->port != std_ports[u->protoid] && u->port != ~0U)
467 sprintf(z, "%d", u->port);
468 d = append(d, ":", e);
473 d = append(d, u->rest, e);
475 return URL_ERR_TOO_LONG;
482 static char *errmsg[] = {
483 "Something is wrong",
487 "Invalid escaped character",
488 "Invalid port number",
489 "Relative URL not allowed",
498 if (err >= sizeof(errmsg) / sizeof(char *))
503 /* Standard cookbook recipes */
506 url_canon_split(byte *u, byte *buf1, byte *buf2, struct url *url)
510 if (err = url_deescape(u, buf1))
512 if (err = url_split(buf1, url, buf2))
514 if (err = url_normalize(url, NULL))
516 return url_canonicalize(url);
520 url_auto_canonicalize(byte *src, byte *dst)
522 byte buf1[MAX_URL_SIZE], buf2[MAX_URL_SIZE], buf3[MAX_URL_SIZE];
526 (void)((err = url_canon_split(src, buf1, buf2, &ur)) ||
527 (err = url_pack(&ur, buf3)) ||
528 (err = url_enescape(buf3, dst)));
536 int main(int argc, char **argv)
538 char buf1[MAX_URL_SIZE], buf2[MAX_URL_SIZE], buf3[MAX_URL_SIZE], buf4[MAX_URL_SIZE];
540 struct url url, url0;
544 if (err = url_deescape(argv[1], buf1))
546 printf("deesc: error %d\n", err);
549 printf("deesc: %s\n", buf1);
550 if (err = url_split(buf1, &url, buf2))
552 printf("split: error %d\n", err);
555 printf("split: @%s@%s@%s@%d@%s\n", url.protocol, url.user, url.host, url.port, url.rest);
556 if (err = url_split("http://mj@www.hell.org/123/sub_dir/index.html;param?query&zzz/subquery#fragment", &url0, buf3))
558 printf("split base: error %d\n", err);
561 if (err = url_normalize(&url0, NULL))
563 printf("normalize base: error %d\n", err);
566 printf("base: @%s@%s@%s@%d@%s\n", url0.protocol, url0.user, url0.host, url0.port, url0.rest);
567 if (err = url_normalize(&url, &url0))
569 printf("normalize: error %d\n", err);
572 printf("normalize: @%s@%s@%s@%d@%s\n", url.protocol, url.user, url.host, url.port, url.rest);
573 if (err = url_canonicalize(&url))
575 printf("canonicalize: error %d\n", err);
578 printf("canonicalize: @%s@%s@%s@%d@%s\n", url.protocol, url.user, url.host, url.port, url.rest);
579 if (err = url_pack(&url, buf4))
581 printf("pack: error %d\n", err);
584 printf("pack: %s\n", buf4);
585 if (err = url_enescape(buf4, buf2))
587 printf("enesc: error %d\n", err);
590 printf("enesc: %s\n", buf2);
603 hashf(byte *start, int length)
607 hf = (hf << 8 | hf >> 24) ^ *start++;
612 repeat_count(struct component *comp, uns count, uns len)
614 struct component *orig_comp = comp;
624 for (i=0; i<len; i++)
625 if (comp[i].hash != orig_comp[i].hash
626 || comp[i].length != orig_comp[i].length
627 || memcmp(comp[i].start, orig_comp[i].start, comp[i].length))
633 url_has_repeated_component(byte *url)
635 struct component *comp;
636 uns comps, comp_len, rep_prefix;
640 for (comps=0, c=url; c; comps++)
642 c = strpbrk(c, url_component_separators);
646 if (comps < url_min_repeat_count)
648 comp = alloca(comps * sizeof(struct component));
649 for (i=0, c=url; c; i++)
652 c = strpbrk(c, url_component_separators);
655 comp[i].length = c - comp[i].start;
659 comp[i].length = strlen(comp[i].start);
662 for (i=0; i<comps; i++)
663 comp[i].hash = hashf(comp[i].start, comp[i].length);
664 for (comp_len = 1; comp_len <= url_max_repeat_length && comp_len <= comps; comp_len++)
665 for (rep_prefix = 0; rep_prefix <= comps - comp_len; rep_prefix++)
666 if (repeat_count(comp + rep_prefix, comps - rep_prefix, comp_len) >= url_min_repeat_count)