2 * Sherlock Library -- URL Functions (according to RFC 1738 and 1808)
4 * (c) 1997 Martin Mares, <mj@atrey.karlin.mff.cuni.cz>
15 /* Escaping and de-escaping */
20 return (x<10) ? (x + '0') : (x - 10 + 'A');
24 url_deescape(byte *s, byte *d)
26 byte *end = d + MAX_URL_SIZE - 10;
30 return URL_ERR_TOO_LONG;
34 if (!Cxdigit(s[1]) || !Cxdigit(s[2]))
35 return URL_ERR_INVALID_ESCAPE;
36 val = Cxvalue(s[1])*16 + Cxvalue(s[2]);
38 return URL_ERR_INVALID_ESCAPED_CHAR;
42 val = NCC_SEMICOLON; break;
44 val = NCC_SLASH; break;
46 val = NCC_QUEST; break;
48 val = NCC_COLON; break;
52 val = NCC_EQUAL; break;
59 else if (*s >= 0x20 && *s <= 0x7e || *s >= 0xa0)
62 return URL_ERR_INVALID_CHAR;
69 url_enescape(byte *s, byte *d)
71 byte *end = d + MAX_URL_SIZE - 10;
76 return URL_ERR_TOO_LONG;
77 if ( *s >= 'A' && *s <= 'Z'
78 || *s >= 'a' && *s <= 'z'
79 || *s >= '0' && *s <= '9'
80 || *s == '$' || *s == '-' || *s == '.' || *s == '+'
81 || *s == '!' || *s == '*' || *s == '\'' || *s == '('
82 || *s == ')' || *s == '_' || *s == ';' || *s == '/'
83 || *s == '?' || *s == ':' || *s == '@' || *s == '='
88 uns val = (*s < NCC_MAX) ? ";/?:@=&"[*s] : *s;
90 *d++ = enhex(val >> 4);
91 *d++ = enhex(val & 0x0f);
99 /* Split an URL (several parts may be copied to the destination buffer) */
102 identify_protocol(byte *p)
104 if (!strcasecmp(p, "http"))
105 return URL_PROTO_HTTP;
106 if (!strcasecmp(p, "ftp"))
107 return URL_PROTO_FTP;
112 url_split(byte *s, struct url *u, byte *d)
114 bzero(u, sizeof(struct url));
116 u->bufend = d + MAX_URL_SIZE - 10;
118 if (s[0] != '/') /* Seek for "protocol:" */
121 while (*p && Calnum(*p))
123 if (p != s && *p == ':')
129 u->protoid = identify_protocol(u->protocol);
134 if (s[0] == '/') /* Host spec or absolute path */
136 if (s[1] == '/') /* Host spec */
143 while (*s && *s != '/') /* Copy user:passwd@host:port */
147 if (w) /* user:passwd present */
155 if (e) /* host:port present */
159 p = strtoul(e, &ep, 10);
160 if (ep && *ep || p > 65535)
161 return URL_ERR_INVALID_PORT;
162 else if (p) /* Port 0 (e.g. in :/) is treated as default port */
174 /* Normalization according to given base URL */
176 static uns std_ports[] = { ~0, 80, 21 }; /* Default port numbers */
179 relpath_merge(struct url *u, struct url *b)
187 if (a[0] == '/') /* Absolute path => OK */
190 return URL_PATH_UNDERFLOW;
192 if (!a[0]) /* Empty relative URL is a special case */
199 p = strrchr(o, '/'); /* Must be found! */
200 while (o <= p) /* Copy original path */
203 return URL_ERR_TOO_LONG;
211 if (a[1] == '/' || !a[1]) /* Skip "./" and ".$" */
218 else if (a[1] == '.' && (a[2] == '/' || !a[2])) /* "../" */
222 return URL_PATH_UNDERFLOW;
223 d--; /* Discard trailing slash */
231 while (a[0] && a[0] != '/')
234 return URL_ERR_TOO_LONG;
247 url_normalize(struct url *u, struct url *b)
251 if (u->protocol && !u->protoid)
254 if ((u->protoid == URL_PROTO_HTTP || (!u->protoid && b && b->protoid == URL_PROTO_HTTP))
255 && u->rest && (k = strchr(u->rest, '#')))
256 *k = 0; /* Kill fragment reference */
259 u->port = std_ports[u->protoid];
261 if ( u->protocol && !u->host
262 || u->host && !*u->host
263 || !u->host && u->user
265 return URL_SYNTAX_ERROR;
267 if (u->protocol) /* Absolute URL */
270 if (!b) /* Relative to something? */
271 return URL_ERR_REL_NOTHING;
273 return URL_ERR_UNKNOWN_PROTOCOL;
277 u->protocol = b->protocol;
278 u->protoid = b->protoid;
286 return relpath_merge(u, b);
292 /* Name canonicalization */
300 if (*b >= 'A' && *b <= 'Z')
307 kill_end_dot(byte *b)
313 k = b + strlen(b) - 1;
314 if (k > b && *k == '.')
320 url_canonicalize(struct url *u)
322 lowercase(u->protocol);
324 kill_end_dot(u->host);
325 if ((!u->rest || !*u->rest) && (u->protoid == URL_PROTO_HTTP || u->protoid == URL_PROTO_FTP))
330 /* Pack a broken-down URL */
333 append(byte *d, byte *s, byte *e)
346 url_pack(struct url *u, byte *d)
348 byte *e = d + MAX_URL_SIZE - 10;
352 d = append(d, u->protocol, e);
353 d = append(d, ":", e);
354 u->protoid = identify_protocol(u->protocol);
358 d = append(d, "//", e);
361 d = append(d, u->user, e);
362 d = append(d, "@", e);
364 d = append(d, u->host, e);
365 if (u->port != std_ports[u->protoid] && u->port != ~0)
368 sprintf(z, "%d", u->port);
369 d = append(d, ":", e);
374 d = append(d, u->rest, e);
376 return URL_ERR_TOO_LONG;
383 static char *errmsg[] = {
384 "Something is wrong",
388 "Invalid escaped character",
389 "Invalid port number",
390 "Relative URL not allowed",
399 if (err >= sizeof(errmsg) / sizeof(char *))
404 /* A "macro" for canonical split */
407 url_canon_split(byte *u, byte *buf1, byte *buf2, struct url *url)
411 if (err = url_deescape(u, buf1))
413 if (err = url_split(buf1, url, buf2))
415 if (err = url_normalize(url, NULL))
417 return url_canonicalize(url);
424 int main(int argc, char **argv)
426 char buf1[MAX_URL_SIZE], buf2[MAX_URL_SIZE], buf3[MAX_URL_SIZE], buf4[MAX_URL_SIZE];
428 struct url url, url0;
432 if (err = url_deescape(argv[1], buf1))
434 printf("deesc: error %d\n", err);
437 printf("deesc: %s\n", buf1);
438 if (err = url_split(buf1, &url, buf2))
440 printf("split: error %d\n", err);
443 printf("split: @%s@%s@%s@%d@%s\n", url.protocol, url.user, url.host, url.port, url.rest);
444 if (err = url_split("http://mj@www.hell.org/123/sub_dir/index.html", &url0, buf3))
446 printf("split base: error %d\n", err);
449 if (err = url_normalize(&url0, NULL))
451 printf("normalize base: error %d\n", err);
454 printf("base: @%s@%s@%s@%d@%s\n", url0.protocol, url0.user, url0.host, url0.port, url0.rest);
455 if (err = url_normalize(&url, &url0))
457 printf("normalize: error %d\n", err);
460 printf("normalize: @%s@%s@%s@%d@%s\n", url.protocol, url.user, url.host, url.port, url.rest);
461 if (err = url_canonicalize(&url))
463 printf("canonicalize: error %d\n", err);
466 printf("canonicalize: @%s@%s@%s@%d@%s\n", url.protocol, url.user, url.host, url.port, url.rest);
467 if (err = url_pack(&url, buf4))
469 printf("pack: error %d\n", err);
472 printf("pack: %s\n", buf1);
473 if (err = url_enescape(buf4, buf2))
475 printf("enesc: error %d\n", err);
478 printf("enesc: %s\n", buf2);