]> git.ipfire.org Git - thirdparty/squid.git/blob - src/url.cc
Merge from upstream.
[thirdparty/squid.git] / src / url.cc
1
2 /*
3 * $Id: url.cc,v 1.165 2008/02/03 10:00:30 amosjeffries Exp $
4 *
5 * DEBUG: section 23 URL Parsing
6 * AUTHOR: Duane Wessels
7 *
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
10 *
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 *
34 */
35
36 #include "URL.h"
37 #include "HttpRequest.h"
38 #include "URLScheme.h"
39
40 static HttpRequest *urnParse(const HttpRequestMethod& method, char *urn);
41 static const char valid_hostname_chars_u[] =
42 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
43 "abcdefghijklmnopqrstuvwxyz"
44 "0123456789-._"
45 #if USE_IPV6
46 "[:]"
47 #endif
48 ;
49 static const char valid_hostname_chars[] =
50 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
51 "abcdefghijklmnopqrstuvwxyz"
52 "0123456789-."
53 #if USE_IPV6
54 "[:]"
55 #endif
56 ;
57
58 void
59 urlInitialize(void)
60 {
61 debugs(23, 5, "urlInitialize: Initializing...");
62 /* this ensures that the number of protocol strings is the same as
63 * the enum slots allocated because the last enum is always 'TOTAL'.
64 */
65 assert(strcmp(ProtocolStr[PROTO_MAX], "TOTAL") == 0);
66 /*
67 * These test that our matchDomainName() function works the
68 * way we expect it to.
69 */
70 assert(0 == matchDomainName("foo.com", "foo.com"));
71 assert(0 == matchDomainName(".foo.com", "foo.com"));
72 assert(0 == matchDomainName("foo.com", ".foo.com"));
73 assert(0 == matchDomainName(".foo.com", ".foo.com"));
74 assert(0 == matchDomainName("x.foo.com", ".foo.com"));
75 assert(0 != matchDomainName("x.foo.com", "foo.com"));
76 assert(0 != matchDomainName("foo.com", "x.foo.com"));
77 assert(0 != matchDomainName("bar.com", "foo.com"));
78 assert(0 != matchDomainName(".bar.com", "foo.com"));
79 assert(0 != matchDomainName(".bar.com", ".foo.com"));
80 assert(0 != matchDomainName("bar.com", ".foo.com"));
81 assert(0 < matchDomainName("zzz.com", "foo.com"));
82 assert(0 > matchDomainName("aaa.com", "foo.com"));
83 assert(0 == matchDomainName("FOO.com", "foo.COM"));
84 assert(0 < matchDomainName("bfoo.com", "afoo.com"));
85 assert(0 > matchDomainName("afoo.com", "bfoo.com"));
86 assert(0 < matchDomainName("x-foo.com", ".foo.com"));
87 /* more cases? */
88 }
89
90 /**
91 * urlParseProtocol() takes begin (b) and end (e) pointers, but for
92 * backwards compatibility, e defaults to NULL, in which case we
93 * assume b is NULL-terminated.
94 */
95 protocol_t
96 urlParseProtocol(const char *b, const char *e)
97 {
98 /*
99 * if e is NULL, b must be NULL terminated and we
100 * make e point to the first whitespace character
101 * after b.
102 */
103
104 if (NULL == e)
105 e = b + strcspn(b, ":");
106
107 int len = e - b;
108
109 /* test common stuff first */
110
111 if (strncasecmp(b, "http", len) == 0)
112 return PROTO_HTTP;
113
114 if (strncasecmp(b, "ftp", len) == 0)
115 return PROTO_FTP;
116
117 if (strncasecmp(b, "https", len) == 0)
118 return PROTO_HTTPS;
119
120 if (strncasecmp(b, "file", len) == 0)
121 return PROTO_FTP;
122
123 if (strncasecmp(b, "gopher", len) == 0)
124 return PROTO_GOPHER;
125
126 if (strncasecmp(b, "wais", len) == 0)
127 return PROTO_WAIS;
128
129 if (strncasecmp(b, "cache_object", len) == 0)
130 return PROTO_CACHEOBJ;
131
132 if (strncasecmp(b, "urn", len) == 0)
133 return PROTO_URN;
134
135 if (strncasecmp(b, "whois", len) == 0)
136 return PROTO_WHOIS;
137
138 if (strncasecmp(b, "internal", len) == 0)
139 return PROTO_INTERNAL;
140
141 return PROTO_NONE;
142 }
143
144 int
145 urlDefaultPort(protocol_t p)
146 {
147 switch (p) {
148
149 case PROTO_HTTP:
150 return 80;
151
152 case PROTO_HTTPS:
153 return 443;
154
155 case PROTO_FTP:
156 return 21;
157
158 case PROTO_GOPHER:
159 return 70;
160
161 case PROTO_WAIS:
162 return 210;
163
164 case PROTO_CACHEOBJ:
165
166 case PROTO_INTERNAL:
167 return CACHE_HTTP_PORT;
168
169 case PROTO_WHOIS:
170 return 43;
171
172 default:
173 return 0;
174 }
175 }
176
177 /*
178 * Parse a URI/URL.
179 *
180 * If the 'request' arg is non-NULL, put parsed values there instead
181 * of allocating a new HttpRequest.
182 *
183 * This abuses HttpRequest as a way of representing the parsed url
184 * and its components.
185 * method is used to switch parsers and to init the HttpRequest.
186 * If method is METHOD_CONNECT, then rather than a URL a hostname:port is
187 * looked for.
188 * The url is non const so that if its too long we can NULL-terminate it in place.
189 */
190
191 /*
192 * This routine parses a URL. Its assumed that the URL is complete -
193 * ie, the end of the string is the end of the URL. Don't pass a partial
194 * URL here as this routine doesn't have any way of knowing whether
195 * its partial or not (ie, it handles the case of no trailing slash as
196 * being "end of host with implied path of /".
197 */
198 HttpRequest *
199 urlParse(const HttpRequestMethod& method, char *url, HttpRequest *request)
200 {
201 LOCAL_ARRAY(char, proto, MAX_URL);
202 LOCAL_ARRAY(char, login, MAX_URL);
203 LOCAL_ARRAY(char, host, MAX_URL);
204 LOCAL_ARRAY(char, urlpath, MAX_URL);
205 char *t = NULL;
206 char *q = NULL;
207 int port;
208 protocol_t protocol = PROTO_NONE;
209 int l;
210 int i;
211 const char *src;
212 char *dst;
213 proto[0] = host[0] = urlpath[0] = login[0] = '\0';
214
215 if ((l = strlen(url)) + Config.appendDomainLen > (MAX_URL - 1)) {
216 /* terminate so it doesn't overflow other buffers */
217 *(url + (MAX_URL >> 1)) = '\0';
218 debugs(23, 1, "urlParse: URL too large (" << l << " bytes)");
219 return NULL;
220 }
221 if (method == METHOD_CONNECT) {
222 port = CONNECT_PORT;
223
224 if (sscanf(url, "[%[^]]]:%d", host, &port) < 1)
225 if (sscanf(url, "%[^:]:%d", host, &port) < 1)
226 return NULL;
227
228 } else if (!strncmp(url, "urn:", 4)) {
229 return urnParse(method, url);
230 } else {
231 /* Parse the URL: */
232 src = url;
233 i = 0;
234 /* Find first : - everything before is protocol */
235 for (i = 0, dst = proto; i < l && *src != ':'; i++, src++, dst++) {
236 *dst = *src;
237 }
238 if (i >= l)
239 return NULL;
240 *dst = '\0';
241
242 /* Then its :// */
243 /* (XXX yah, I'm not checking we've got enough data left before checking the array..) */
244 if (*src != ':' || *(src + 1) != '/' || *(src + 2) != '/')
245 return NULL;
246 i += 3;
247 src += 3;
248
249 /* Then everything until first /; thats host (and port; which we'll look for here later) */
250 /* bug 1881: If we don't get a "/" then we imply it was there */
251 for (dst = host; i < l && *src != '/' && src != '\0'; i++, src++, dst++) {
252 *dst = *src;
253 }
254
255 /*
256 * We can't check for "i >= l" here because we could be at the end of the line
257 * and have a perfectly valid URL w/ no trailing '/'. In this case we assume we've
258 * been -given- a valid URL and the path is just '/'.
259 */
260 if (i > l)
261 return NULL;
262 *dst = '\0';
263
264 /* Then everything from / (inclusive) until \r\n or \0 - thats urlpath */
265 for (dst = urlpath; i < l && *src != '\r' && *src != '\n' && *src != '\0'; i++, src++, dst++) {
266 *dst = *src;
267 }
268
269 /* We -could- be at the end of the buffer here */
270 if (i > l)
271 return NULL;
272 /* If the URL path is empty we set it to be "/" */
273 if (dst == urlpath) {
274 *(dst++) = '/';
275 }
276 *dst = '\0';
277
278 protocol = urlParseProtocol(proto);
279 port = urlDefaultPort(protocol);
280
281 /* Is there any login information? (we should eventually parse it above) */
282 if ((t = strrchr(host, '@'))) {
283 strcpy((char *) login, (char *) host);
284 t = strrchr(login, '@');
285 *t = 0;
286 strcpy((char *) host, t + 1);
287 }
288
289 /* Is there any host information? (we should eventually parse it above) */
290 if(*host == '[') {
291 /* strip any IPA brackets. valid under IPv6. */
292 dst = host;
293 #if USE_IPV6
294 /* only for IPv6 sadly, pre-IPv6/URL code can't handle the clean result properly anyway. */
295 src = host; src++;
296 l = strlen(host);
297 i = 1;
298 for (; i < l && *src != ']' && *src != '\0'; i++, src++, dst++) {
299 *dst = *src;
300 }
301
302 /* we moved in-place, so truncate the actual hostname found */
303 *(dst++) = '\0';
304 #else
305 /* IPv4-pure needs to skip the whole hostname to ']' inclusive for now */
306 while(*dst != '\0' && *dst != ']') dst++;
307 #endif
308
309 /* skip ahead to either start of port, or original EOS */
310 while(*dst != '\0' && *dst != ':') dst++;
311 t = dst;
312 } else {
313 t = strrchr(host, ':');
314
315 if(t != strchr(host,':') ) {
316 /* RFC 2732 states IPv6 "SHOULD" be bracketed. allowing for times when its not. */
317 /* RFC 3986 'update' simply modifies this to an "is" with no emphasis at all! */
318 /* therefore we MUST accept the case where they are not bracketed at all. */
319 t = NULL;
320 }
321 }
322
323 if (t && *t == ':') {
324 *t = '\0'; t++;
325 port = atoi(t);
326 }
327 }
328
329 for (t = host; *t; t++)
330 *t = xtolower(*t);
331
332 if (stringHasWhitespace(host)) {
333 if (URI_WHITESPACE_STRIP == Config.uri_whitespace) {
334 t = q = host;
335 while (*t) {
336 if (!xisspace(*t))
337 *q++ = *t;
338 t++;
339 }
340 *q = '\0';
341 }
342 }
343
344 debugs(23, 3, "urlParse: Split URL '" << url << "' into proto='" << proto << "', host='" << host << "', port='" << port << "', path='" << urlpath << "'");
345
346 if (Config.onoff.check_hostnames && strspn(host, Config.onoff.allow_underscore ? valid_hostname_chars_u : valid_hostname_chars) != strlen(host)) {
347 debugs(23, 1, "urlParse: Illegal character in hostname '" << host << "'");
348 return NULL;
349 }
350
351 if (Config.appendDomain && !strchr(host, '.'))
352 strncat(host, Config.appendDomain, SQUIDHOSTNAMELEN - strlen(host) - 1);
353
354 /* remove trailing dots from hostnames */
355 while ((l = strlen(host)) > 0 && host[--l] == '.')
356 host[l] = '\0';
357
358 /* reject duplicate or leading dots */
359 if (strstr(host, "..") || *host == '.') {
360 debug(23, 1) ("urlParse: Illegal hostname '%s'\n", host);
361 return NULL;
362 }
363
364 if (port < 1 || port > 65535) {
365 debugs(23, 3, "urlParse: Invalid port '" << port << "'");
366 return NULL;
367 }
368
369 #ifdef HARDCODE_DENY_PORTS
370 /* These ports are filtered in the default squid.conf, but
371 * maybe someone wants them hardcoded... */
372 if (port == 7 || port == 9 || port == 19) {
373 debugs(23, 0, "urlParse: Deny access to port " << port);
374 return NULL;
375 }
376 #endif
377
378 if (stringHasWhitespace(urlpath)) {
379 debugs(23, 2, "urlParse: URI has whitespace: {" << url << "}");
380
381 switch (Config.uri_whitespace) {
382
383 case URI_WHITESPACE_DENY:
384 return NULL;
385
386 case URI_WHITESPACE_ALLOW:
387 break;
388
389 case URI_WHITESPACE_ENCODE:
390 t = rfc1738_escape_unescaped(urlpath);
391 xstrncpy(urlpath, t, MAX_URL);
392 break;
393
394 case URI_WHITESPACE_CHOP:
395 *(urlpath + strcspn(urlpath, w_space)) = '\0';
396 break;
397
398 case URI_WHITESPACE_STRIP:
399 default:
400 t = q = urlpath;
401 while (*t) {
402 if (!xisspace(*t))
403 *q++ = *t;
404 t++;
405 }
406 *q = '\0';
407 }
408 }
409
410 if (NULL == request)
411 request = new HttpRequest(method, protocol, urlpath);
412 else {
413 request->initHTTP(method, protocol, urlpath);
414 }
415
416 request->SetHost(host);
417 xstrncpy(request->login, login, MAX_LOGIN_SZ);
418 request->port = (u_short) port;
419 return request;
420 }
421
422 static HttpRequest *
423 urnParse(const HttpRequestMethod& method, char *urn)
424 {
425 debugs(50, 5, "urnParse: " << urn);
426 return new HttpRequest(method, PROTO_URN, urn + 4);
427 }
428
429 const char *
430 urlCanonical(HttpRequest * request)
431 {
432 LOCAL_ARRAY(char, portbuf, 32);
433 /// \todo AYJ: Performance: making this a ptr and allocating when needed will be better than a write and future xstrdup().
434 LOCAL_ARRAY(char, urlbuf, MAX_URL);
435
436 if (request->canonical)
437 return request->canonical;
438
439 if (request->protocol == PROTO_URN) {
440 snprintf(urlbuf, MAX_URL, "urn:%s", request->urlpath.buf());
441 } else {
442 /// \todo AYJ: this could use "if..else and method == METHOD_CONNECT" easier.
443 switch (request->method.id()) {
444
445 case METHOD_CONNECT:
446 snprintf(urlbuf, MAX_URL, "%s:%d", request->GetHost(), request->port);
447 break;
448
449 default:
450 portbuf[0] = '\0';
451
452 if (request->port != urlDefaultPort(request->protocol))
453 snprintf(portbuf, 32, ":%d", request->port);
454
455 snprintf(urlbuf, MAX_URL, "%s://%s%s%s%s%s",
456 ProtocolStr[request->protocol],
457 request->login,
458 *request->login ? "@" : null_string,
459 request->GetHost(),
460 portbuf,
461 request->urlpath.buf());
462
463 break;
464 }
465 }
466
467 return (request->canonical = xstrdup(urlbuf));
468 }
469
470 /** \todo AYJ: Performance: This is an *almost* duplicate of urlCanoncical. But elides the query-string.
471 * After copying it on in the first place! Would be less code to merge the two with a flag parameter.
472 * and never copy the query-string part in the first place
473 */
474 char *
475 urlCanonicalClean(const HttpRequest * request)
476 {
477 LOCAL_ARRAY(char, buf, MAX_URL);
478 LOCAL_ARRAY(char, portbuf, 32);
479 LOCAL_ARRAY(char, loginbuf, MAX_LOGIN_SZ + 1);
480 char *t;
481
482 if (request->protocol == PROTO_URN) {
483 snprintf(buf, MAX_URL, "urn:%s", request->urlpath.buf());
484 } else {
485 /// \todo AYJ: this could use "if..else and method == METHOD_CONNECT" easier.
486 switch (request->method.id()) {
487
488 case METHOD_CONNECT:
489 snprintf(buf, MAX_URL, "%s:%d",
490 request->GetHost(),
491 request->port);
492 break;
493
494 default:
495 portbuf[0] = '\0';
496
497 if (request->port != urlDefaultPort(request->protocol))
498 snprintf(portbuf, 32, ":%d", request->port);
499
500 loginbuf[0] = '\0';
501
502 if ((int) strlen(request->login) > 0) {
503 strcpy(loginbuf, request->login);
504
505 if ((t = strchr(loginbuf, ':')))
506 *t = '\0';
507
508 strcat(loginbuf, "@");
509 }
510
511 snprintf(buf, MAX_URL, "%s://%s%s%s%s",
512 ProtocolStr[request->protocol],
513 loginbuf,
514 request->GetHost(),
515 portbuf,
516 request->urlpath.buf());
517 /*
518 * strip arguments AFTER a question-mark
519 */
520
521 if (Config.onoff.strip_query_terms)
522 if ((t = strchr(buf, '?')))
523 *(++t) = '\0';
524
525 break;
526 }
527 }
528
529 if (stringHasCntl(buf))
530 xstrncpy(buf, rfc1738_escape_unescaped(buf), MAX_URL);
531
532 return buf;
533 }
534
535 const char *
536 urlAbsolute(const HttpRequest * req, const char *relUrl)
537 {
538 LOCAL_ARRAY(char, portbuf, 32);
539 LOCAL_ARRAY(char, urlbuf, MAX_URL);
540 char *path, *last_slash;
541
542 if (relUrl == NULL) {
543 return (NULL);
544 }
545 if (req->method.id() == METHOD_CONNECT) {
546 return (NULL);
547 }
548 if (strchr(relUrl, ':') != NULL) {
549 return (NULL);
550 }
551 if (req->protocol == PROTO_URN) {
552 snprintf(urlbuf, MAX_URL, "urn:%s", req->urlpath.buf());
553 } else {
554 portbuf[0] = '\0';
555 if (req->port != urlDefaultPort(req->protocol)) {
556 snprintf(portbuf, 32, ":%d", req->port);
557 }
558 if (relUrl[0] == '/') {
559 snprintf(urlbuf, MAX_URL, "%s://%s%s%s%s%s",
560 ProtocolStr[req->protocol],
561 req->login,
562 *req->login ? "@" : null_string,
563 req->GetHost(),
564 portbuf,
565 relUrl
566 );
567 } else {
568 path = xstrdup(req->urlpath.buf());
569 last_slash = strrchr(path, '/');
570 if (last_slash == NULL) {
571 snprintf(urlbuf, MAX_URL, "%s://%s%s%s%s/%s",
572 ProtocolStr[req->protocol],
573 req->login,
574 *req->login ? "@" : null_string,
575 req->GetHost(),
576 portbuf,
577 relUrl
578 );
579 } else {
580 last_slash++;
581 *last_slash = '\0';
582 snprintf(urlbuf, MAX_URL, "%s://%s%s%s%s%s%s",
583 ProtocolStr[req->protocol],
584 req->login,
585 *req->login ? "@" : null_string,
586 req->GetHost(),
587 portbuf,
588 path,
589 relUrl
590 );
591 }
592 xfree(path);
593 }
594 }
595
596 return (xstrdup(urlbuf));
597 }
598
599 /*
600 * matchDomainName() compares a hostname with a domainname according
601 * to the following rules:
602 *
603 * HOST DOMAIN MATCH?
604 * ------------- ------------- ------
605 * foo.com foo.com YES
606 * .foo.com foo.com YES
607 * x.foo.com foo.com NO
608 * foo.com .foo.com YES
609 * .foo.com .foo.com YES
610 * x.foo.com .foo.com YES
611 *
612 * We strip leading dots on hosts (but not domains!) so that
613 * ".foo.com" is is always the same as "foo.com".
614 *
615 * Return values:
616 * 0 means the host matches the domain
617 * 1 means the host is greater than the domain
618 * -1 means the host is less than the domain
619 */
620
621 int
622 matchDomainName(const char *h, const char *d)
623 {
624 int dl;
625 int hl;
626
627 while ('.' == *h)
628 h++;
629
630 hl = strlen(h);
631
632 dl = strlen(d);
633
634 /*
635 * Start at the ends of the two strings and work towards the
636 * beginning.
637 */
638 while (xtolower(h[--hl]) == xtolower(d[--dl])) {
639 if (hl == 0 && dl == 0) {
640 /*
641 * We made it all the way to the beginning of both
642 * strings without finding any difference.
643 */
644 return 0;
645 }
646
647 if (0 == hl) {
648 /*
649 * The host string is shorter than the domain string.
650 * There is only one case when this can be a match.
651 * If the domain is just one character longer, and if
652 * that character is a leading '.' then we call it a
653 * match.
654 */
655
656 if (1 == dl && '.' == d[0])
657 return 0;
658 else
659 return -1;
660 }
661
662 if (0 == dl) {
663 /*
664 * The domain string is shorter than the host string.
665 * This is a match only if the first domain character
666 * is a leading '.'.
667 */
668
669 if ('.' == d[0])
670 return 0;
671 else
672 return 1;
673 }
674 }
675
676 /*
677 * We found different characters in the same position (from the end).
678 */
679 /*
680 * If one of those character is '.' then its special. In order
681 * for splay tree sorting to work properly, "x-foo.com" must
682 * be greater than ".foo.com" even though '-' is less than '.'.
683 */
684 if ('.' == d[dl])
685 return 1;
686
687 if ('.' == h[hl])
688 return -1;
689
690 return (xtolower(h[hl]) - xtolower(d[dl]));
691 }
692
693
694 /*
695 * return true if we can serve requests for this method.
696 */
697 int
698 urlCheckRequest(const HttpRequest * r)
699 {
700 int rc = 0;
701 /* protocol "independent" methods
702 *
703 * actually these methods are specific to HTTP:
704 * they are methods we recieve on our HTTP port,
705 * and if we had a FTP listener would not be relevant
706 * there.
707 *
708 * So, we should delegate them to HTTP. The problem is that we
709 * do not have a default protocol from the client side of HTTP.
710 */
711
712 if (r->method == METHOD_CONNECT)
713 return 1;
714
715 if (r->method == METHOD_TRACE)
716 return 1;
717
718 if (r->method == METHOD_PURGE)
719 return 1;
720
721 /* does method match the protocol? */
722 switch (r->protocol) {
723
724 case PROTO_URN:
725
726 case PROTO_HTTP:
727
728 case PROTO_CACHEOBJ:
729 rc = 1;
730 break;
731
732 case PROTO_FTP:
733
734 if (r->method == METHOD_PUT)
735 rc = 1;
736
737 case PROTO_GOPHER:
738
739 case PROTO_WAIS:
740
741 case PROTO_WHOIS:
742 if (r->method == METHOD_GET)
743 rc = 1;
744 else if (r->method == METHOD_HEAD)
745 rc = 1;
746
747 break;
748
749 case PROTO_HTTPS:
750 #ifdef USE_SSL
751
752 rc = 1;
753
754 break;
755
756 #else
757 /*
758 * Squid can't originate an SSL connection, so it should
759 * never receive an "https:" URL. It should always be
760 * CONNECT instead.
761 */
762 rc = 0;
763
764 #endif
765
766 default:
767 break;
768 }
769
770 return rc;
771 }
772
773 /*
774 * Quick-n-dirty host extraction from a URL. Steps:
775 * Look for a colon
776 * Skip any '/' after the colon
777 * Copy the next SQUID_MAXHOSTNAMELEN bytes to host[]
778 * Look for an ending '/' or ':' and terminate
779 * Look for login info preceeded by '@'
780 */
781
782 class URLHostName
783 {
784
785 public:
786 char * extract(char const *url);
787
788 private:
789 static char Host [SQUIDHOSTNAMELEN];
790 void init(char const *);
791 void findHostStart();
792 void trimTrailingChars();
793 void trimAuth();
794 char const *hostStart;
795 char const *url;
796 };
797
798 char *
799 urlHostname(const char *url)
800 {
801 return URLHostName().extract(url);
802 }
803
804 char URLHostName::Host[SQUIDHOSTNAMELEN];
805
806 void
807 URLHostName::init(char const *aUrl)
808 {
809 Host[0] = '\0';
810 url = url;
811 }
812
813 void
814 URLHostName::findHostStart()
815 {
816 if (NULL == (hostStart = strchr(url, ':')))
817 return;
818
819 ++hostStart;
820
821 while (*hostStart != '\0' && *hostStart == '/')
822 ++hostStart;
823
824 #if USE_IPV6
825 if (*hostStart == ']')
826 ++hostStart;
827 #endif
828
829 }
830
831 void
832 URLHostName::trimTrailingChars()
833 {
834 char *t;
835
836 if ((t = strchr(Host, '/')))
837 *t = '\0';
838
839 if ((t = strrchr(Host, ':')))
840 *t = '\0';
841
842 #if USE_IPV6
843 if ((t = strchr(Host, ']')))
844 *t = '\0';
845 #endif
846
847 }
848
849 void
850 URLHostName::trimAuth()
851 {
852 char *t;
853
854 if ((t = strrchr(Host, '@'))) {
855 t++;
856 xmemmove(Host, t, strlen(t) + 1);
857 }
858 }
859
860 char *
861 URLHostName::extract(char const *aUrl)
862 {
863 init(aUrl);
864 findHostStart();
865
866 if (hostStart == NULL)
867 return NULL;
868
869 xstrncpy(Host, hostStart, SQUIDHOSTNAMELEN);
870
871 trimTrailingChars();
872
873 trimAuth();
874
875 return Host;
876 }
877
878 URL::URL() : scheme()
879 {}
880
881 URL::URL(URLScheme const &aScheme): scheme(aScheme)
882 {}