2 * Sherlock Library -- URL Functions
4 * (c) 1997--2004 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.
10 * The URL syntax corresponds to RFC 2396 with several exceptions:
12 * o Escaping of special characters still follows RFC 1738.
13 * o Interpretation of path parameters follows RFC 1808.
15 * XXX: The buffer handling in this module is really horrible, but it works.
20 #include "lib/chartype.h"
30 static uns url_ignore_spaces;
31 static uns url_ignore_underflow;
32 static byte *url_component_separators = "";
33 static uns url_min_repeat_count = 0x7fffffff;
34 static uns url_max_repeat_length = 0;
36 static struct cfitem url_config[] = {
37 { "URL", CT_SECTION, NULL },
38 { "IgnoreSpaces", CT_INT, &url_ignore_spaces },
39 { "IgnoreUnderflow", CT_INT, &url_ignore_underflow },
40 { "ComponentSeparators", CT_STRING, &url_component_separators },
41 { "MinRepeatCount", CT_INT, &url_min_repeat_count },
42 { "MaxRepeatLength", CT_INT, &url_max_repeat_length },
43 { NULL, CT_STOP, NULL }
46 static void CONSTRUCTOR url_init_config(void)
48 cf_register(url_config);
51 /* Escaping and de-escaping */
56 return (x<10) ? (x + '0') : (x - 10 + 'A');
60 url_deescape(byte *s, byte *d)
63 byte *end = d + MAX_URL_SIZE - 10;
67 return URL_ERR_TOO_LONG;
71 if (!Cxdigit(s[1]) || !Cxdigit(s[2]))
72 return URL_ERR_INVALID_ESCAPE;
73 val = Cxvalue(s[1])*16 + Cxvalue(s[2]);
75 return URL_ERR_INVALID_ESCAPED_CHAR;
79 val = NCC_SEMICOLON; break;
81 val = NCC_SLASH; break;
83 val = NCC_QUEST; break;
85 val = NCC_COLON; break;
89 val = NCC_EQUAL; break;
93 val = NCC_HASH; break;
105 if (!url_ignore_spaces || !(!*s || d == dstart))
110 return URL_ERR_TOO_LONG;
116 return URL_ERR_INVALID_CHAR;
123 url_enescape(byte *s, byte *d)
125 byte *end = d + MAX_URL_SIZE - 10;
131 return URL_ERR_TOO_LONG;
132 if (Calnum(c) || /* RFC 1738(2.2): Only alphanumerics ... */
133 c == '$' || c == '-' || c == '_' || c == '.' || c == '+' || /* ... and several other exceptions ... */
134 c == '!' || c == '*' || c == '\'' || c == '(' || c == ')' ||
136 c == '/' || c == '?' || c == ':' || c == '@' || /* ... and reserved chars used for reserved purpose */
137 c == '=' || c == '&' || c == '#' || c == ';')
141 uns val = (*s < NCC_MAX) ? NCC_CHARS[*s] : *s;
143 *d++ = enhex(val >> 4);
144 *d++ = enhex(val & 0x0f);
152 /* Split an URL (several parts may be copied to the destination buffer) */
154 byte *url_proto_names[URL_PROTO_MAX] = URL_PNAMES;
155 static int url_proto_path_flags[URL_PROTO_MAX] = URL_PATH_FLAGS;
158 identify_protocol(byte *p)
162 for(i=1; i<URL_PROTO_MAX; i++)
163 if (!strcasecmp(p, url_proto_names[i]))
165 return URL_PROTO_UNKNOWN;
169 url_split(byte *s, struct url *u, byte *d)
171 bzero(u, sizeof(struct url));
173 u->bufend = d + MAX_URL_SIZE - 10;
175 if (s[0] != '/') /* Seek for "protocol:" */
178 while (*p && Calnum(*p))
180 if (p != s && *p == ':')
186 u->protoid = identify_protocol(u->protocol);
188 if (url_proto_path_flags[u->protoid] && (s[0] != '/' || s[1] != '/'))
190 /* The protocol requires complete host spec, but it's missing -> treat as a relative path instead */
191 int len = d - u->protocol;
200 if (s[0] == '/') /* Host spec or absolute path */
202 if (s[1] == '/') /* Host spec */
209 while (*s && *s != '/' && *s != '?') /* Copy user:passwd@host:port */
213 if (w) /* user:passwd present */
217 if (e = strchr(q, ':'))
226 if (e) /* host:port present */
230 p = strtoul(e, &ep, 10);
231 if (ep && *ep || p > 65535)
232 return URL_ERR_INVALID_PORT;
233 else if (p) /* Port 0 (e.g. in :/) is treated as default port */
245 /* Normalization according to given base URL */
247 static uns std_ports[] = URL_DEFPORTS; /* Default port numbers */
250 relpath_merge(struct url *u, struct url *b)
258 if (a[0] == '/') /* Absolute path => OK */
260 if (o[0] != '/' && o[0] != '?')
261 return URL_PATH_UNDERFLOW;
263 if (!a[0]) /* Empty URL -> inherit everything */
269 u->rest = d; /* We know we'll need to copy the path somewhere else */
271 if (a[0] == '#') /* Another fragment */
273 for(p=o; *p && *p != '#'; p++)
277 if (a[0] == '?') /* New query */
279 for(p=o; *p && *p != '#' && *p != '?'; p++)
283 if (a[0] == ';') /* Change parameters */
285 for(p=o; *p && *p != ';' && *p != '?' && *p != '#'; p++)
290 p = NULL; /* Copy original path and find the last slash */
291 while (*o && *o != ';' && *o != '?' && *o != '#')
294 return URL_ERR_TOO_LONG;
295 if ((*d++ = *o++) == '/')
299 return URL_ERR_REL_NOTHING;
306 if (a[1] == '/' || !a[1]) /* Skip "./" and ".$" */
313 else if (a[1] == '.' && (a[2] == '/' || !a[2])) /* "../" */
321 * RFC 1808 says we should leave ".." as a path segment, but
322 * we intentionally break the rule and refuse the URL.
324 if (!url_ignore_underflow)
325 return URL_PATH_UNDERFLOW;
329 d--; /* Discard trailing slash */
336 while (a[0] && a[0] != '/')
339 return URL_ERR_TOO_LONG;
351 copy: /* Combine part of old URL with the new one */
356 return URL_ERR_TOO_LONG;
361 return URL_ERR_TOO_LONG;
366 url_normalize(struct url *u, struct url *b)
371 if (url_proto_path_flags[u->protoid] && (!u->host || !*u->host) ||
372 !u->host && u->user ||
373 !u->user && u->pass ||
375 return URL_SYNTAX_ERROR;
379 /* Now we know it's a relative URL. Do we have any base? */
380 if (!b || !url_proto_path_flags[b->protoid])
381 return URL_ERR_REL_NOTHING;
382 u->protocol = b->protocol;
383 u->protoid = b->protoid;
385 /* Reference to the same host */
392 if (err = relpath_merge(u, b))
397 /* Change path "?" to "/?" because it's the true meaning */
398 if (u->rest[0] == '?')
400 int l = strlen(u->rest);
401 if (u->bufend - u->buf < l+1)
402 return URL_ERR_TOO_LONG;
404 memcpy(u->buf+1, u->rest, l+1);
409 /* Fill in missing info */
411 u->port = std_ports[u->protoid];
416 /* Name canonicalization */
424 if (*b >= 'A' && *b <= 'Z')
431 kill_end_dot(byte *b)
437 k = b + strlen(b) - 1;
438 while (k > b && *k == '.')
444 url_canonicalize(struct url *u)
448 lowercase(u->protocol);
450 kill_end_dot(u->host);
451 if ((!u->rest || !*u->rest) && url_proto_path_flags[u->protoid])
453 if (u->rest && (c = strchr(u->rest, '#'))) /* Kill fragment reference */
458 /* Pack a broken-down URL */
461 append(byte *d, byte *s, byte *e)
474 url_pack(struct url *u, byte *d)
476 byte *e = d + MAX_URL_SIZE - 10;
480 d = append(d, u->protocol, e);
481 d = append(d, ":", e);
482 u->protoid = identify_protocol(u->protocol);
486 d = append(d, "//", e);
489 d = append(d, u->user, e);
492 d = append(d, ":", e);
493 d = append(d, u->pass, e);
495 d = append(d, "@", e);
497 d = append(d, u->host, e);
498 if (u->port != std_ports[u->protoid] && u->port != ~0U)
501 sprintf(z, "%d", u->port);
502 d = append(d, ":", e);
507 d = append(d, u->rest, e);
509 return URL_ERR_TOO_LONG;
516 static char *errmsg[] = {
517 "Something is wrong",
521 "Invalid escaped character",
522 "Invalid port number",
523 "Relative URL not allowed",
532 if (err >= sizeof(errmsg) / sizeof(char *))
537 /* Standard cookbook recipes */
540 url_canon_split(byte *u, byte *buf1, byte *buf2, struct url *url)
544 if (err = url_deescape(u, buf1))
546 if (err = url_split(buf1, url, buf2))
548 if (err = url_normalize(url, NULL))
550 return url_canonicalize(url);
554 url_auto_canonicalize(byte *src, byte *dst)
556 byte buf1[MAX_URL_SIZE], buf2[MAX_URL_SIZE], buf3[MAX_URL_SIZE];
560 (void)((err = url_canon_split(src, buf1, buf2, &ur)) ||
561 (err = url_pack(&ur, buf3)) ||
562 (err = url_enescape(buf3, dst)));
570 int main(int argc, char **argv)
572 char buf1[MAX_URL_SIZE], buf2[MAX_URL_SIZE], buf3[MAX_URL_SIZE], buf4[MAX_URL_SIZE];
574 struct url url, url0;
575 char *base = "http://mj@www.hell.org/123/sub_dir/index.html;param?query&zzz/subquery#fragment";
577 if (argc != 2 && argc != 3)
581 if (err = url_deescape(argv[1], buf1))
583 printf("deesc: error %d\n", err);
586 printf("deesc: %s\n", buf1);
587 if (err = url_split(buf1, &url, buf2))
589 printf("split: error %d\n", err);
592 printf("split: @%s@%s@%s@%s@%d@%s\n", url.protocol, url.user, url.pass, url.host, url.port, url.rest);
593 if (err = url_split(base, &url0, buf3))
595 printf("split base: error %d\n", err);
598 if (err = url_normalize(&url0, NULL))
600 printf("normalize base: error %d\n", err);
603 printf("base: @%s@%s@%s@%s@%d@%s\n", url0.protocol, url0.user, url0.pass, url0.host, url0.port, url0.rest);
604 if (err = url_normalize(&url, &url0))
606 printf("normalize: error %d\n", err);
609 printf("normalize: @%s@%s@%s@%s@%d@%s\n", url.protocol, url.user, url.pass, url.host, url.port, url.rest);
610 if (err = url_canonicalize(&url))
612 printf("canonicalize: error %d\n", err);
615 printf("canonicalize: @%s@%s@%s@%s@%d@%s\n", url.protocol, url.user, url.pass, url.host, url.port, url.rest);
616 if (err = url_pack(&url, buf4))
618 printf("pack: error %d\n", err);
621 printf("pack: %s\n", buf4);
622 if (err = url_enescape(buf4, buf2))
624 printf("enesc: error %d\n", err);
627 printf("enesc: %s\n", buf2);
640 hashf(byte *start, int length)
644 hf = (hf << 8 | hf >> 24) ^ *start++;
649 repeat_count(struct component *comp, uns count, uns len)
651 struct component *orig_comp = comp;
661 for (i=0; i<len; i++)
662 if (comp[i].hash != orig_comp[i].hash
663 || comp[i].length != orig_comp[i].length
664 || memcmp(comp[i].start, orig_comp[i].start, comp[i].length))
670 url_has_repeated_component(byte *url)
672 struct component *comp;
673 uns comps, comp_len, rep_prefix;
677 for (comps=0, c=url; c; comps++)
679 c = strpbrk(c, url_component_separators);
683 if (comps < url_min_repeat_count)
685 comp = alloca(comps * sizeof(struct component));
686 for (i=0, c=url; c; i++)
689 c = strpbrk(c, url_component_separators);
692 comp[i].length = c - comp[i].start;
696 comp[i].length = strlen(comp[i].start);
699 for (i=0; i<comps; i++)
700 comp[i].hash = hashf(comp[i].start, comp[i].length);
701 for (comp_len = 1; comp_len <= url_max_repeat_length && comp_len <= comps; comp_len++)
702 for (rep_prefix = 0; rep_prefix <= comps - comp_len; rep_prefix++)
703 if (repeat_count(comp + rep_prefix, comps - rep_prefix, comp_len) >= url_min_repeat_count)