]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/url.cc
3 * $Id: url.cc,v 1.154 2006/05/08 23:38:33 robertc 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
);
42 static const char *const valid_hostname_chars
=
43 #if ALLOW_HOSTNAME_UNDERSCORES
44 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
45 "abcdefghijklmnopqrstuvwxyz"
48 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
49 "abcdefghijklmnopqrstuvwxyz"
53 #endif /* CHECK_HOSTNAMES */
55 /* convert %xx in url string to a character
56 * Allocate a new string and return a pointer to converted string */
59 url_convert_hex(char *org_url
, int allocate
)
61 static char code
[] = "00";
65 url
= allocate
? (char *) xstrdup(org_url
) : org_url
;
67 if ((int) strlen(url
) < 3 || !strchr(url
, '%'))
70 for (s
= t
= url
; *s
; s
++) {
71 if (*s
== '%' && *(s
+ 1) && *(s
+ 2)) {
74 *t
++ = (char) strtol(code
, NULL
, 16);
90 debug(23, 5) ("urlInitialize: Initializing...\n");
91 /* this ensures that the number of protocol strings is the same as
92 * the enum slots allocated because the last enum is always 'TOTAL'.
94 assert(strcmp(ProtocolStr
[PROTO_MAX
], "TOTAL") == 0);
96 * These test that our matchDomainName() function works the
97 * way we expect it to.
99 assert(0 == matchDomainName("foo.com", "foo.com"));
100 assert(0 == matchDomainName(".foo.com", "foo.com"));
101 assert(0 == matchDomainName("foo.com", ".foo.com"));
102 assert(0 == matchDomainName(".foo.com", ".foo.com"));
103 assert(0 == matchDomainName("x.foo.com", ".foo.com"));
104 assert(0 != matchDomainName("x.foo.com", "foo.com"));
105 assert(0 != matchDomainName("foo.com", "x.foo.com"));
106 assert(0 != matchDomainName("bar.com", "foo.com"));
107 assert(0 != matchDomainName(".bar.com", "foo.com"));
108 assert(0 != matchDomainName(".bar.com", ".foo.com"));
109 assert(0 != matchDomainName("bar.com", ".foo.com"));
110 assert(0 < matchDomainName("zzz.com", "foo.com"));
111 assert(0 > matchDomainName("aaa.com", "foo.com"));
112 assert(0 == matchDomainName("FOO.com", "foo.COM"));
113 assert(0 < matchDomainName("bfoo.com", "afoo.com"));
114 assert(0 > matchDomainName("afoo.com", "bfoo.com"));
115 assert(0 < matchDomainName("x-foo.com", ".foo.com"));
120 * urlParseProtocol() takes begin (b) and end (e) pointers, but for
121 * backwards compatibility, e defaults to NULL, in which case we
122 * assume b is NULL-terminated.
125 urlParseProtocol(const char *b
, const char *e
)
128 * if e is NULL, b must be NULL terminated and we
129 * make e point to the first whitespace character
134 e
= b
+ strcspn(b
, ":");
138 /* test common stuff first */
140 if (strncasecmp(b
, "http", len
) == 0)
143 if (strncasecmp(b
, "ftp", len
) == 0)
146 if (strncasecmp(b
, "https", len
) == 0)
149 if (strncasecmp(b
, "file", len
) == 0)
152 if (strncasecmp(b
, "gopher", len
) == 0)
155 if (strncasecmp(b
, "wais", len
) == 0)
158 if (strncasecmp(b
, "cache_object", len
) == 0)
159 return PROTO_CACHEOBJ
;
161 if (strncasecmp(b
, "urn", len
) == 0)
164 if (strncasecmp(b
, "whois", len
) == 0)
167 if (strncasecmp(b
, "internal", len
) == 0)
168 return PROTO_INTERNAL
;
174 urlDefaultPort(protocol_t p
)
196 return CACHE_HTTP_PORT
;
209 * If the 'request' arg is non-NULL, put parsed values there instead
210 * of allocating a new HttpRequest.
212 * This abuses HttpRequest as a way of representing the parsed url
213 * and its components.
214 * method is used to switch parsers and to init the HttpRequest.
215 * If method is METHOD_CONNECT, then rather than a URL a hostname:port is
217 * The url is non const so that if its too long we can NULL-terminate it in place.
220 urlParse(method_t method
, char *url
, HttpRequest
*request
)
222 LOCAL_ARRAY(char, proto
, MAX_URL
);
223 LOCAL_ARRAY(char, login
, MAX_URL
);
224 LOCAL_ARRAY(char, host
, MAX_URL
);
225 LOCAL_ARRAY(char, urlpath
, MAX_URL
);
229 protocol_t protocol
= PROTO_NONE
;
231 proto
[0] = host
[0] = urlpath
[0] = login
[0] = '\0';
233 if ((l
= strlen(url
)) + Config
.appendDomainLen
> (MAX_URL
- 1)) {
234 /* terminate so it doesn't overflow other buffers */
235 *(url
+ (MAX_URL
>> 1)) = '\0';
236 debug(23, 1) ("urlParse: URL too large (%d bytes)\n", l
);
240 if (method
== METHOD_CONNECT
) {
243 if (sscanf(url
, "%[^:]:%d", host
, &port
) < 1)
245 } else if (!strncmp(url
, "urn:", 4)) {
246 return urnParse(method
, url
);
248 if (sscanf(url
, "%[^:]://%[^/]%[^\r\n]", proto
, host
, urlpath
) < 2)
251 protocol
= urlParseProtocol(proto
);
253 port
= urlDefaultPort(protocol
);
255 /* Is there any login informaiton? */
256 if ((t
= strrchr(host
, '@'))) {
257 strcpy((char *) login
, (char *) host
);
258 t
= strrchr(login
, '@');
260 strcpy((char *) host
, t
+ 1);
263 if ((t
= strrchr(host
, ':'))) {
271 for (t
= host
; *t
; t
++)
274 if (stringHasWhitespace(host
)) {
275 if (URI_WHITESPACE_STRIP
== Config
.uri_whitespace
) {
290 if (Config
.onoff
.check_hostnames
&& strspn(host
, valid_hostname_chars
) != strlen(host
)) {
291 debug(23, 1) ("urlParse: Illegal character in hostname '%s'\n", host
);
296 #if DONT_DO_THIS_IT_BREAKS_SEMANTIC_TRANSPARENCY
297 /* remove trailing dots from hostnames */
298 while ((l
= strlen(host
)) > 0 && host
[--l
] == '.')
301 /* remove duplicate dots */
302 while ((t
= strstr(host
, "..")))
303 xmemmove(t
, t
+ 1, strlen(t
));
307 if (Config
.appendDomain
&& !strchr(host
, '.'))
308 strncat(host
, Config
.appendDomain
, SQUIDHOSTNAMELEN
- strlen(host
) - 1);
310 if (port
< 1 || port
> 65535) {
311 debug(23, 3) ("urlParse: Invalid port '%d'\n", port
);
315 #ifdef HARDCODE_DENY_PORTS
316 /* These ports are filtered in the default squid.conf, but
317 * maybe someone wants them hardcoded... */
318 if (port
== 7 || port
== 9 || port
== 19) {
319 debug(23, 0) ("urlParse: Deny access to port %d\n", port
);
324 if (stringHasWhitespace(urlpath
)) {
325 debug(23, 2) ("urlParse: URI has whitespace: {%s}\n", url
);
327 switch (Config
.uri_whitespace
) {
329 case URI_WHITESPACE_DENY
:
332 case URI_WHITESPACE_ALLOW
:
335 case URI_WHITESPACE_ENCODE
:
336 t
= rfc1738_escape_unescaped(urlpath
);
337 xstrncpy(urlpath
, t
, MAX_URL
);
340 case URI_WHITESPACE_CHOP
:
341 *(urlpath
+ strcspn(urlpath
, w_space
)) = '\0';
344 case URI_WHITESPACE_STRIP
:
361 request
= new HttpRequest(method
, protocol
, urlpath
);
363 request
->initHTTP(method
, protocol
, urlpath
);
366 xstrncpy(request
->host
, host
, SQUIDHOSTNAMELEN
);
367 xstrncpy(request
->login
, login
, MAX_LOGIN_SZ
);
368 request
->port
= (u_short
) port
;
373 urnParse(method_t method
, char *urn
)
375 debug(50, 5) ("urnParse: %s\n", urn
);
376 return new HttpRequest(method
, PROTO_URN
, urn
+ 4);
380 urlCanonical(HttpRequest
* request
)
382 LOCAL_ARRAY(char, portbuf
, 32);
383 LOCAL_ARRAY(char, urlbuf
, MAX_URL
);
385 if (request
->canonical
)
386 return request
->canonical
;
388 if (request
->protocol
== PROTO_URN
) {
389 snprintf(urlbuf
, MAX_URL
, "urn:%s", request
->urlpath
.buf());
391 switch (request
->method
) {
394 snprintf(urlbuf
, MAX_URL
, "%s:%d", request
->host
, request
->port
);
400 if (request
->port
!= urlDefaultPort(request
->protocol
))
401 snprintf(portbuf
, 32, ":%d", request
->port
);
403 snprintf(urlbuf
, MAX_URL
, "%s://%s%s%s%s%s",
404 ProtocolStr
[request
->protocol
],
406 *request
->login
? "@" : null_string
,
409 request
->urlpath
.buf());
415 return (request
->canonical
= xstrdup(urlbuf
));
419 urlCanonicalClean(const HttpRequest
* request
)
421 LOCAL_ARRAY(char, buf
, MAX_URL
);
422 LOCAL_ARRAY(char, portbuf
, 32);
423 LOCAL_ARRAY(char, loginbuf
, MAX_LOGIN_SZ
+ 1);
426 if (request
->protocol
== PROTO_URN
) {
427 snprintf(buf
, MAX_URL
, "urn:%s", request
->urlpath
.buf());
429 switch (request
->method
) {
432 snprintf(buf
, MAX_URL
, "%s:%d", request
->host
, request
->port
);
438 if (request
->port
!= urlDefaultPort(request
->protocol
))
439 snprintf(portbuf
, 32, ":%d", request
->port
);
443 if ((int) strlen(request
->login
) > 0) {
444 strcpy(loginbuf
, request
->login
);
446 if ((t
= strchr(loginbuf
, ':')))
449 strcat(loginbuf
, "@");
452 snprintf(buf
, MAX_URL
, "%s://%s%s%s%s",
453 ProtocolStr
[request
->protocol
],
457 request
->urlpath
.buf());
459 * strip arguments AFTER a question-mark
462 if (Config
.onoff
.strip_query_terms
)
463 if ((t
= strchr(buf
, '?')))
470 if (stringHasCntl(buf
))
471 xstrncpy(buf
, rfc1738_escape_unescaped(buf
), MAX_URL
);
477 * matchDomainName() compares a hostname with a domainname according
478 * to the following rules:
481 * ------------- ------------- ------
482 * foo.com foo.com YES
483 * .foo.com foo.com YES
484 * x.foo.com foo.com NO
485 * foo.com .foo.com YES
486 * .foo.com .foo.com YES
487 * x.foo.com .foo.com YES
489 * We strip leading dots on hosts (but not domains!) so that
490 * ".foo.com" is is always the same as "foo.com".
493 * 0 means the host matches the domain
494 * 1 means the host is greater than the domain
495 * -1 means the host is less than the domain
499 matchDomainName(const char *h
, const char *d
)
512 * Start at the ends of the two strings and work towards the
515 while (xtolower(h
[--hl
]) == xtolower(d
[--dl
])) {
516 if (hl
== 0 && dl
== 0) {
518 * We made it all the way to the beginning of both
519 * strings without finding any difference.
526 * The host string is shorter than the domain string.
527 * There is only one case when this can be a match.
528 * If the domain is just one character longer, and if
529 * that character is a leading '.' then we call it a
533 if (1 == dl
&& '.' == d
[0])
541 * The domain string is shorter than the host string.
542 * This is a match only if the first domain character
554 * We found different characters in the same position (from the end).
557 * If one of those character is '.' then its special. In order
558 * for splay tree sorting to work properly, "x-foo.com" must
559 * be greater than ".foo.com" even though '-' is less than '.'.
567 return (xtolower(h
[hl
]) - xtolower(d
[dl
]));
572 * what does the return code of this mean ?
575 urlCheckRequest(const HttpRequest
* r
)
578 /* protocol "independent" methods */
580 if (r
->method
== METHOD_CONNECT
)
583 if (r
->method
== METHOD_TRACE
)
586 if (r
->method
== METHOD_PURGE
)
589 /* does method match the protocol? */
590 switch (r
->protocol
) {
602 if (r
->method
== METHOD_PUT
)
610 if (r
->method
== METHOD_GET
)
612 else if (r
->method
== METHOD_HEAD
)
626 * Squid can't originate an SSL connection, so it should
627 * never receive an "https:" URL. It should always be
642 * Quick-n-dirty host extraction from a URL. Steps:
644 * Skip any '/' after the colon
645 * Copy the next SQUID_MAXHOSTNAMELEN bytes to host[]
646 * Look for an ending '/' or ':' and terminate
647 * Look for login info preceeded by '@'
654 char * extract(char const *url
);
657 static char Host
[SQUIDHOSTNAMELEN
];
658 void init(char const *);
659 void findHostStart();
660 void trimTrailingChars();
662 char const *hostStart
;
667 urlHostname(const char *url
)
669 return URLHostName().extract(url
);
672 char URLHostName::Host
[SQUIDHOSTNAMELEN
];
675 URLHostName::init(char const *aUrl
)
682 URLHostName::findHostStart()
684 if (NULL
== (hostStart
= strchr(url
, ':')))
689 while (*hostStart
!= '\0' && *hostStart
== '/')
694 URLHostName::trimTrailingChars()
698 if ((t
= strchr(Host
, '/')))
701 if ((t
= strchr(Host
, ':')))
706 URLHostName::trimAuth()
710 if ((t
= strrchr(Host
, '@'))) {
712 xmemmove(Host
, t
, strlen(t
) + 1);
717 URLHostName::extract(char const *aUrl
)
722 if (hostStart
== NULL
)
725 xstrncpy(Host
, hostStart
, SQUIDHOSTNAMELEN
);
734 URL::URL() : scheme()
737 URL::URL(URLScheme
const &aScheme
): scheme(aScheme
)