2 * Sherlock Library -- URL Functions (according to RFC 1738 and 1808)
4 * (c) 1997--2001 Martin Mares <mj@ucw.cz>
9 #include "lib/chartype.h"
18 static uns url_ignore_spaces;
19 static uns url_ignore_underflow;
21 static struct cfitem url_config[] = {
22 { "URL", CT_SECTION, NULL },
23 { "IgnoreSpaces", CT_INT, &url_ignore_spaces },
24 { "IgnoreUnderflow", CT_INT, &url_ignore_underflow },
25 { NULL, CT_STOP, NULL }
28 static void CONSTRUCTOR url_init_config(void)
30 cf_register(url_config);
33 /* Escaping and de-escaping */
38 return (x<10) ? (x + '0') : (x - 10 + 'A');
42 url_deescape(byte *s, byte *d)
45 byte *end = d + MAX_URL_SIZE - 10;
49 return URL_ERR_TOO_LONG;
53 if (!Cxdigit(s[1]) || !Cxdigit(s[2]))
54 return URL_ERR_INVALID_ESCAPE;
55 val = Cxvalue(s[1])*16 + Cxvalue(s[2]);
57 return URL_ERR_INVALID_ESCAPED_CHAR;
61 val = NCC_SEMICOLON; break;
63 val = NCC_SLASH; break;
65 val = NCC_QUEST; break;
67 val = NCC_COLON; break;
71 val = NCC_EQUAL; break;
75 val = NCC_HASH; break;
87 if (!url_ignore_spaces || !(!*s || d == dstart))
92 return URL_ERR_TOO_LONG;
98 return URL_ERR_INVALID_CHAR;
105 url_enescape(byte *s, byte *d)
107 byte *end = d + MAX_URL_SIZE - 10;
113 return URL_ERR_TOO_LONG;
114 if (Calnum(c) || /* RFC 1738(2.2): Only alphanumerics ... */
115 c == '$' || c == '-' || c == '_' || c == '.' || c == '+' || /* ... and several other exceptions ... */
116 c == '!' || c == '*' || c == '\'' || c == '(' || c == ')' ||
118 c == '/' || c == '?' || c == ':' || c == '@' || /* ... and reserved chars used for reserved purpose */
119 c == '=' || c == '&' || c == '#' || c == ';')
123 uns val = (*s < NCC_MAX) ? NCC_CHARS[*s] : *s;
125 *d++ = enhex(val >> 4);
126 *d++ = enhex(val & 0x0f);
134 /* Split an URL (several parts may be copied to the destination buffer) */
136 byte *url_proto_names[URL_PROTO_MAX] = URL_PNAMES;
137 static int url_proto_path_flags[URL_PROTO_MAX] = URL_PATH_FLAGS;
140 identify_protocol(byte *p)
144 for(i=1; i<URL_PROTO_MAX; i++)
145 if (!strcasecmp(p, url_proto_names[i]))
147 return URL_PROTO_UNKNOWN;
151 url_split(byte *s, struct url *u, byte *d)
153 bzero(u, sizeof(struct url));
155 u->bufend = d + MAX_URL_SIZE - 10;
157 if (s[0] != '/') /* Seek for "protocol:" */
160 while (*p && Calnum(*p))
162 if (p != s && *p == ':')
168 u->protoid = identify_protocol(u->protocol);
170 if (url_proto_path_flags[u->protoid] && (s[0] != '/' || s[1] != '/'))
172 /* The protocol requires complete host spec, but it's missing -> treat as a relative path instead */
173 int len = d - u->protocol;
182 if (s[0] == '/') /* Host spec or absolute path */
184 if (s[1] == '/') /* Host spec */
191 while (*s && *s != '/') /* Copy user:passwd@host:port */
195 if (w) /* user:passwd present */
203 if (e) /* host:port present */
207 p = strtoul(e, &ep, 10);
208 if (ep && *ep || p > 65535)
209 return URL_ERR_INVALID_PORT;
210 else if (p) /* Port 0 (e.g. in :/) is treated as default port */
222 /* Normalization according to given base URL */
224 static uns std_ports[] = URL_DEFPORTS; /* Default port numbers */
227 relpath_merge(struct url *u, struct url *b)
235 if (a[0] == '/') /* Absolute path => OK */
238 return URL_PATH_UNDERFLOW;
240 if (!a[0]) /* Empty URL -> inherit everything */
246 u->rest = d; /* We know we'll need to copy the path somewhere else */
248 if (a[0] == '#') /* Another fragment */
250 for(p=o; *p && *p != '#'; p++)
254 if (a[0] == '?') /* New query */
256 for(p=o; *p && *p != '#' && *p != '?'; p++)
260 if (a[0] == ';') /* Change parameters */
262 for(p=o; *p && *p != ';' && *p != '?' && *p != '#'; p++)
267 p = NULL; /* Copy original path and find the last slash */
268 while (*o && *o != ';' && *o != '?' && *o != '#')
271 return URL_ERR_TOO_LONG;
272 if ((*d++ = *o++) == '/')
276 return URL_ERR_REL_NOTHING;
283 if (a[1] == '/' || !a[1]) /* Skip "./" and ".$" */
290 else if (a[1] == '.' && (a[2] == '/' || !a[2])) /* "../" */
298 * RFC 1808 says we should leave ".." as a path segment, but
299 * we intentionally break the rule and refuse the URL.
301 if (!url_ignore_underflow)
302 return URL_PATH_UNDERFLOW;
306 d--; /* Discard trailing slash */
313 while (a[0] && a[0] != '/')
316 return URL_ERR_TOO_LONG;
328 copy: /* Combine part of old URL with the new one */
333 return URL_ERR_TOO_LONG;
338 return URL_ERR_TOO_LONG;
343 url_normalize(struct url *u, struct url *b)
348 if (url_proto_path_flags[u->protoid] && !u->host ||
349 u->host && !*u->host ||
350 !u->host && u->user ||
352 return URL_SYNTAX_ERROR;
356 /* Now we know it's a relative URL. Do we have any base? */
357 if (!b || !url_proto_path_flags[b->protoid])
358 return URL_ERR_REL_NOTHING;
359 u->protocol = b->protocol;
360 u->protoid = b->protoid;
362 /* Reference to the same host */
368 if (err = relpath_merge(u, b))
373 /* Fill in missing info */
375 u->port = std_ports[u->protoid];
380 /* Name canonicalization */
388 if (*b >= 'A' && *b <= 'Z')
395 kill_end_dot(byte *b)
401 k = b + strlen(b) - 1;
402 if (k > b && *k == '.')
408 url_canonicalize(struct url *u)
412 lowercase(u->protocol);
414 kill_end_dot(u->host);
415 if ((!u->rest || !*u->rest) && url_proto_path_flags[u->protoid])
417 if (u->rest && (c = strchr(u->rest, '#'))) /* Kill fragment reference */
422 /* Pack a broken-down URL */
425 append(byte *d, byte *s, byte *e)
438 url_pack(struct url *u, byte *d)
440 byte *e = d + MAX_URL_SIZE - 10;
444 d = append(d, u->protocol, e);
445 d = append(d, ":", e);
446 u->protoid = identify_protocol(u->protocol);
450 d = append(d, "//", e);
453 d = append(d, u->user, e);
454 d = append(d, "@", e);
456 d = append(d, u->host, e);
457 if (u->port != std_ports[u->protoid] && u->port != ~0U)
460 sprintf(z, "%d", u->port);
461 d = append(d, ":", e);
466 d = append(d, u->rest, e);
468 return URL_ERR_TOO_LONG;
475 static char *errmsg[] = {
476 "Something is wrong",
480 "Invalid escaped character",
481 "Invalid port number",
482 "Relative URL not allowed",
491 if (err >= sizeof(errmsg) / sizeof(char *))
496 /* A "macro" for canonical split */
499 url_canon_split(byte *u, byte *buf1, byte *buf2, struct url *url)
503 if (err = url_deescape(u, buf1))
505 if (err = url_split(buf1, url, buf2))
507 if (err = url_normalize(url, NULL))
509 return url_canonicalize(url);
516 int main(int argc, char **argv)
518 char buf1[MAX_URL_SIZE], buf2[MAX_URL_SIZE], buf3[MAX_URL_SIZE], buf4[MAX_URL_SIZE];
520 struct url url, url0;
524 if (err = url_deescape(argv[1], buf1))
526 printf("deesc: error %d\n", err);
529 printf("deesc: %s\n", buf1);
530 if (err = url_split(buf1, &url, buf2))
532 printf("split: error %d\n", err);
535 printf("split: @%s@%s@%s@%d@%s\n", url.protocol, url.user, url.host, url.port, url.rest);
536 if (err = url_split("http://mj@www.hell.org/123/sub_dir/index.html;param?query&zzz/subquery#fragment", &url0, buf3))
538 printf("split base: error %d\n", err);
541 if (err = url_normalize(&url0, NULL))
543 printf("normalize base: error %d\n", err);
546 printf("base: @%s@%s@%s@%d@%s\n", url0.protocol, url0.user, url0.host, url0.port, url0.rest);
547 if (err = url_normalize(&url, &url0))
549 printf("normalize: error %d\n", err);
552 printf("normalize: @%s@%s@%s@%d@%s\n", url.protocol, url.user, url.host, url.port, url.rest);
553 if (err = url_canonicalize(&url))
555 printf("canonicalize: error %d\n", err);
558 printf("canonicalize: @%s@%s@%s@%d@%s\n", url.protocol, url.user, url.host, url.port, url.rest);
559 if (err = url_pack(&url, buf4))
561 printf("pack: error %d\n", err);
564 printf("pack: %s\n", buf4);
565 if (err = url_enescape(buf4, buf2))
567 printf("enesc: error %d\n", err);
570 printf("enesc: %s\n", buf2);