]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/url.cc
3 * $Id: url.cc,v 1.161 2007/05/23 21:10:07 hno Exp $
5 * DEBUG: section 23 URL Parsing
6 * AUTHOR: Duane Wessels
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
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.
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.
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.
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.
37 #include "HttpRequest.h"
38 #include "URLScheme.h"
40 static HttpRequest
*urnParse(method_t method
, char *urn
);
41 static const char valid_hostname_chars_u
[] =
42 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
43 "abcdefghijklmnopqrstuvwxyz"
46 static const char valid_hostname_chars
[] =
47 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
48 "abcdefghijklmnopqrstuvwxyz"
55 debugs(23, 5, "urlInitialize: Initializing...");
56 /* this ensures that the number of protocol strings is the same as
57 * the enum slots allocated because the last enum is always 'TOTAL'.
59 assert(strcmp(ProtocolStr
[PROTO_MAX
], "TOTAL") == 0);
61 * These test that our matchDomainName() function works the
62 * way we expect it to.
64 assert(0 == matchDomainName("foo.com", "foo.com"));
65 assert(0 == matchDomainName(".foo.com", "foo.com"));
66 assert(0 == matchDomainName("foo.com", ".foo.com"));
67 assert(0 == matchDomainName(".foo.com", ".foo.com"));
68 assert(0 == matchDomainName("x.foo.com", ".foo.com"));
69 assert(0 != matchDomainName("x.foo.com", "foo.com"));
70 assert(0 != matchDomainName("foo.com", "x.foo.com"));
71 assert(0 != matchDomainName("bar.com", "foo.com"));
72 assert(0 != matchDomainName(".bar.com", "foo.com"));
73 assert(0 != matchDomainName(".bar.com", ".foo.com"));
74 assert(0 != matchDomainName("bar.com", ".foo.com"));
75 assert(0 < matchDomainName("zzz.com", "foo.com"));
76 assert(0 > matchDomainName("aaa.com", "foo.com"));
77 assert(0 == matchDomainName("FOO.com", "foo.COM"));
78 assert(0 < matchDomainName("bfoo.com", "afoo.com"));
79 assert(0 > matchDomainName("afoo.com", "bfoo.com"));
80 assert(0 < matchDomainName("x-foo.com", ".foo.com"));
85 * urlParseProtocol() takes begin (b) and end (e) pointers, but for
86 * backwards compatibility, e defaults to NULL, in which case we
87 * assume b is NULL-terminated.
90 urlParseProtocol(const char *b
, const char *e
)
93 * if e is NULL, b must be NULL terminated and we
94 * make e point to the first whitespace character
99 e
= b
+ strcspn(b
, ":");
103 /* test common stuff first */
105 if (strncasecmp(b
, "http", len
) == 0)
108 if (strncasecmp(b
, "ftp", len
) == 0)
111 if (strncasecmp(b
, "https", len
) == 0)
114 if (strncasecmp(b
, "file", len
) == 0)
117 if (strncasecmp(b
, "gopher", len
) == 0)
120 if (strncasecmp(b
, "wais", len
) == 0)
123 if (strncasecmp(b
, "cache_object", len
) == 0)
124 return PROTO_CACHEOBJ
;
126 if (strncasecmp(b
, "urn", len
) == 0)
129 if (strncasecmp(b
, "whois", len
) == 0)
132 if (strncasecmp(b
, "internal", len
) == 0)
133 return PROTO_INTERNAL
;
139 urlDefaultPort(protocol_t p
)
161 return CACHE_HTTP_PORT
;
174 * If the 'request' arg is non-NULL, put parsed values there instead
175 * of allocating a new HttpRequest.
177 * This abuses HttpRequest as a way of representing the parsed url
178 * and its components.
179 * method is used to switch parsers and to init the HttpRequest.
180 * If method is METHOD_CONNECT, then rather than a URL a hostname:port is
182 * The url is non const so that if its too long we can NULL-terminate it in place.
185 urlParse(method_t method
, char *url
, HttpRequest
*request
)
187 LOCAL_ARRAY(char, proto
, MAX_URL
);
188 LOCAL_ARRAY(char, login
, MAX_URL
);
189 LOCAL_ARRAY(char, host
, MAX_URL
);
190 LOCAL_ARRAY(char, urlpath
, MAX_URL
);
194 protocol_t protocol
= PROTO_NONE
;
196 proto
[0] = host
[0] = urlpath
[0] = login
[0] = '\0';
198 if ((l
= strlen(url
)) + Config
.appendDomainLen
> (MAX_URL
- 1)) {
199 /* terminate so it doesn't overflow other buffers */
200 *(url
+ (MAX_URL
>> 1)) = '\0';
201 debugs(23, 1, "urlParse: URL too large (" << l
<< " bytes)");
205 if (method
== METHOD_CONNECT
) {
208 if (sscanf(url
, "%[^:]:%d", host
, &port
) < 1)
210 } else if (!strncmp(url
, "urn:", 4)) {
211 return urnParse(method
, url
);
213 if (sscanf(url
, "%[^:]://%[^/]%[^\r\n]", proto
, host
, urlpath
) < 2)
216 protocol
= urlParseProtocol(proto
);
218 port
= urlDefaultPort(protocol
);
220 /* Is there any login informaiton? */
221 if ((t
= strrchr(host
, '@'))) {
222 strcpy((char *) login
, (char *) host
);
223 t
= strrchr(login
, '@');
225 strcpy((char *) host
, t
+ 1);
228 if ((t
= strrchr(host
, ':'))) {
236 for (t
= host
; *t
; t
++)
239 if (strpbrk(host
, w_space
) != NULL
) {
240 if (URI_WHITESPACE_STRIP
== Config
.uri_whitespace
) {
254 if (Config
.onoff
.check_hostnames
&& strspn(host
, Config
.onoff
.allow_underscore
? valid_hostname_chars_u
: valid_hostname_chars
) != strlen(host
)) {
255 debugs(23, 1, "urlParse: Illegal character in hostname '" << host
<< "'");
259 #if DONT_DO_THIS_IT_BREAKS_SEMANTIC_TRANSPARENCY
260 /* remove trailing dots from hostnames */
261 while ((l
= strlen(host
)) > 0 && host
[--l
] == '.')
264 /* remove duplicate dots */
265 while ((t
= strstr(host
, "..")))
266 xmemmove(t
, t
+ 1, strlen(t
));
270 if (Config
.appendDomain
&& !strchr(host
, '.'))
271 strncat(host
, Config
.appendDomain
, SQUIDHOSTNAMELEN
- strlen(host
) - 1);
273 if (port
< 1 || port
> 65535) {
274 debugs(23, 3, "urlParse: Invalid port '" << port
<< "'");
278 #ifdef HARDCODE_DENY_PORTS
279 /* These ports are filtered in the default squid.conf, but
280 * maybe someone wants them hardcoded... */
281 if (port
== 7 || port
== 9 || port
== 19) {
282 debugs(23, 0, "urlParse: Deny access to port " << port
);
287 if (strpbrk(urlpath
, w_space
) != NULL
) {
288 debugs(23, 2, "urlParse: URI has whitespace: {" << url
<< "}");
290 switch (Config
.uri_whitespace
) {
292 case URI_WHITESPACE_DENY
:
295 case URI_WHITESPACE_ALLOW
:
298 case URI_WHITESPACE_ENCODE
:
299 t
= rfc1738_escape_unescaped(urlpath
);
300 xstrncpy(urlpath
, t
, MAX_URL
);
303 case URI_WHITESPACE_CHOP
:
304 *(urlpath
+ strcspn(urlpath
, w_space
)) = '\0';
307 case URI_WHITESPACE_STRIP
:
324 request
= new HttpRequest(method
, protocol
, urlpath
);
326 request
->initHTTP(method
, protocol
, urlpath
);
329 xstrncpy(request
->host
, host
, SQUIDHOSTNAMELEN
);
330 xstrncpy(request
->login
, login
, MAX_LOGIN_SZ
);
331 request
->port
= (u_short
) port
;
336 urnParse(method_t method
, char *urn
)
338 debugs(50, 5, "urnParse: " << urn
);
339 return new HttpRequest(method
, PROTO_URN
, urn
+ 4);
343 urlCanonical(HttpRequest
* request
)
345 LOCAL_ARRAY(char, portbuf
, 32);
346 LOCAL_ARRAY(char, urlbuf
, MAX_URL
);
348 if (request
->canonical
)
349 return request
->canonical
;
351 if (request
->protocol
== PROTO_URN
) {
352 snprintf(urlbuf
, MAX_URL
, "urn:%s", request
->urlpath
.c_str());
354 switch (request
->method
) {
357 snprintf(urlbuf
, MAX_URL
, "%s:%d", request
->host
, request
->port
);
363 if (request
->port
!= urlDefaultPort(request
->protocol
))
364 snprintf(portbuf
, 32, ":%d", request
->port
);
366 snprintf(urlbuf
, MAX_URL
, "%s://%s%s%s%s%s",
367 ProtocolStr
[request
->protocol
],
369 *request
->login
? "@" : null_string
,
372 request
->urlpath
.c_str());
378 return (request
->canonical
= xstrdup(urlbuf
));
382 stringHasCntl(const char *s
)
386 while ((c
= (unsigned char) *s
++) != '\0') {
390 if (c
>= 0x7f && c
<= 0x9f)
398 urlCanonicalClean(const HttpRequest
* request
)
400 LOCAL_ARRAY(char, buf
, MAX_URL
);
401 LOCAL_ARRAY(char, portbuf
, 32);
402 LOCAL_ARRAY(char, loginbuf
, MAX_LOGIN_SZ
+ 1);
405 if (request
->protocol
== PROTO_URN
) {
406 snprintf(buf
, MAX_URL
, "urn:%s", request
->urlpath
.c_str());
408 switch (request
->method
) {
411 snprintf(buf
, MAX_URL
, "%s:%d", request
->host
, request
->port
);
417 if (request
->port
!= urlDefaultPort(request
->protocol
))
418 snprintf(portbuf
, 32, ":%d", request
->port
);
422 if ((int) strlen(request
->login
) > 0) {
423 strcpy(loginbuf
, request
->login
);
425 if ((t
= strchr(loginbuf
, ':')))
428 strcat(loginbuf
, "@");
431 snprintf(buf
, MAX_URL
, "%s://%s%s%s%s",
432 ProtocolStr
[request
->protocol
],
436 request
->urlpath
.c_str());
438 * strip arguments AFTER a question-mark
441 if (Config
.onoff
.strip_query_terms
)
442 if ((t
= strchr(buf
, '?')))
449 if (stringHasCntl(buf
))
450 xstrncpy(buf
, rfc1738_escape_unescaped(buf
), MAX_URL
);
456 * matchDomainName() compares a hostname with a domainname according
457 * to the following rules:
460 * ------------- ------------- ------
461 * foo.com foo.com YES
462 * .foo.com foo.com YES
463 * x.foo.com foo.com NO
464 * foo.com .foo.com YES
465 * .foo.com .foo.com YES
466 * x.foo.com .foo.com YES
468 * We strip leading dots on hosts (but not domains!) so that
469 * ".foo.com" is is always the same as "foo.com".
472 * 0 means the host matches the domain
473 * 1 means the host is greater than the domain
474 * -1 means the host is less than the domain
478 matchDomainName(const char *h
, const char *d
)
491 * Start at the ends of the two strings and work towards the
494 while (xtolower(h
[--hl
]) == xtolower(d
[--dl
])) {
495 if (hl
== 0 && dl
== 0) {
497 * We made it all the way to the beginning of both
498 * strings without finding any difference.
505 * The host string is shorter than the domain string.
506 * There is only one case when this can be a match.
507 * If the domain is just one character longer, and if
508 * that character is a leading '.' then we call it a
512 if (1 == dl
&& '.' == d
[0])
520 * The domain string is shorter than the host string.
521 * This is a match only if the first domain character
533 * We found different characters in the same position (from the end).
536 * If one of those character is '.' then its special. In order
537 * for splay tree sorting to work properly, "x-foo.com" must
538 * be greater than ".foo.com" even though '-' is less than '.'.
546 return (xtolower(h
[hl
]) - xtolower(d
[dl
]));
551 * return true if we can serve requests for this method.
554 urlCheckRequest(const HttpRequest
* r
)
557 /* protocol "independent" methods
559 * actually these methods are specific to HTTP:
560 * they are methods we recieve on our HTTP port,
561 * and if we had a FTP listener would not be relevant
564 * So, we should delegate them to HTTP. The problem is that we
565 * do not have a default protocol from the client side of HTTP.
568 if (r
->method
== METHOD_CONNECT
)
571 if (r
->method
== METHOD_TRACE
)
574 if (r
->method
== METHOD_PURGE
)
577 /* does method match the protocol? */
578 switch (r
->protocol
) {
590 if (r
->method
== METHOD_PUT
)
598 if (r
->method
== METHOD_GET
)
600 else if (r
->method
== METHOD_HEAD
)
614 * Squid can't originate an SSL connection, so it should
615 * never receive an "https:" URL. It should always be
630 * Quick-n-dirty host extraction from a URL. Steps:
632 * Skip any '/' after the colon
633 * Copy the next SQUID_MAXHOSTNAMELEN bytes to host[]
634 * Look for an ending '/' or ':' and terminate
635 * Look for login info preceeded by '@'
642 char * extract(char const *url
);
645 static char Host
[SQUIDHOSTNAMELEN
];
646 void init(char const *);
647 void findHostStart();
648 void trimTrailingChars();
650 char const *hostStart
;
655 urlHostname(const char *url
)
657 return URLHostName().extract(url
);
660 char URLHostName::Host
[SQUIDHOSTNAMELEN
];
663 URLHostName::init(char const *aUrl
)
670 URLHostName::findHostStart()
672 if (NULL
== (hostStart
= strchr(url
, ':')))
677 while (*hostStart
!= '\0' && *hostStart
== '/')
682 URLHostName::trimTrailingChars()
686 if ((t
= strchr(Host
, '/')))
689 if ((t
= strchr(Host
, ':')))
694 URLHostName::trimAuth()
698 if ((t
= strrchr(Host
, '@'))) {
700 xmemmove(Host
, t
, strlen(t
) + 1);
705 URLHostName::extract(char const *aUrl
)
710 if (hostStart
== NULL
)
713 xstrncpy(Host
, hostStart
, SQUIDHOSTNAMELEN
);
722 URL::URL() : scheme()
725 URL::URL(URLScheme
const &aScheme
): scheme(aScheme
)