]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/url.cc
3 * $Id: url.cc,v 1.152 2006/04/23 11:10:32 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"
40 const char *RequestMethodStr
[] =
92 const char *ProtocolStr
[] =
111 static HttpRequest
*urnParse(method_t method
, char *urn
);
113 static const char *const valid_hostname_chars
=
114 #if ALLOW_HOSTNAME_UNDERSCORES
115 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
116 "abcdefghijklmnopqrstuvwxyz"
119 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
120 "abcdefghijklmnopqrstuvwxyz"
124 #endif /* CHECK_HOSTNAMES */
126 /* convert %xx in url string to a character
127 * Allocate a new string and return a pointer to converted string */
130 url_convert_hex(char *org_url
, int allocate
)
132 static char code
[] = "00";
136 url
= allocate
? (char *) xstrdup(org_url
) : org_url
;
138 if ((int) strlen(url
) < 3 || !strchr(url
, '%'))
141 for (s
= t
= url
; *s
; s
++) {
142 if (*s
== '%' && *(s
+ 1) && *(s
+ 2)) {
145 *t
++ = (char) strtol(code
, NULL
, 16);
161 debug(23, 5) ("urlInitialize: Initializing...\n");
162 assert(sizeof(ProtocolStr
) == (PROTO_MAX
+ 1) * sizeof(char *));
164 * These test that our matchDomainName() function works the
165 * way we expect it to.
167 assert(0 == matchDomainName("foo.com", "foo.com"));
168 assert(0 == matchDomainName(".foo.com", "foo.com"));
169 assert(0 == matchDomainName("foo.com", ".foo.com"));
170 assert(0 == matchDomainName(".foo.com", ".foo.com"));
171 assert(0 == matchDomainName("x.foo.com", ".foo.com"));
172 assert(0 != matchDomainName("x.foo.com", "foo.com"));
173 assert(0 != matchDomainName("foo.com", "x.foo.com"));
174 assert(0 != matchDomainName("bar.com", "foo.com"));
175 assert(0 != matchDomainName(".bar.com", "foo.com"));
176 assert(0 != matchDomainName(".bar.com", ".foo.com"));
177 assert(0 != matchDomainName("bar.com", ".foo.com"));
178 assert(0 < matchDomainName("zzz.com", "foo.com"));
179 assert(0 > matchDomainName("aaa.com", "foo.com"));
180 assert(0 == matchDomainName("FOO.com", "foo.COM"));
181 assert(0 < matchDomainName("bfoo.com", "afoo.com"));
182 assert(0 > matchDomainName("afoo.com", "bfoo.com"));
183 assert(0 < matchDomainName("x-foo.com", ".foo.com"));
187 method_t
&operator++ (method_t
&aMethod
)
189 int tmp
= (int)aMethod
;
190 aMethod
= (method_t
)(++tmp
);
195 * urlParseMethod() takes begin and end pointers, but for backwards
196 * compatibility, end defaults to NULL, in which case we assume begin
197 * is NULL-terminated.
200 urlParseMethod(const char *b
, const char *e
)
202 method_t method
= METHOD_NONE
;
204 * This check for '%' makes sure that we don't
205 * match one of the extension method placeholders,
206 * which have the form %EXT[0-9][0-9]
213 * if e is NULL, b must be NULL terminated and we
214 * make e point to the first whitespace character
218 e
= b
+ strcspn(b
, w_space
);
220 for (++method
; method
< METHOD_ENUM_END
; ++method
) {
221 if (0 == strncasecmp(b
, RequestMethodStr
[method
], e
-b
))
229 * urlParseProtocol() takes begin (b) and end (e) pointers, but for
230 * backwards compatibility, e defaults to NULL, in which case we
231 * assume b is NULL-terminated.
234 urlParseProtocol(const char *b
, const char *e
)
237 * if e is NULL, b must be NULL terminated and we
238 * make e point to the first whitespace character
243 e
= b
+ strcspn(b
, ":");
247 /* test common stuff first */
249 if (strncasecmp(b
, "http", len
) == 0)
252 if (strncasecmp(b
, "ftp", len
) == 0)
255 if (strncasecmp(b
, "https", len
) == 0)
258 if (strncasecmp(b
, "file", len
) == 0)
261 if (strncasecmp(b
, "gopher", len
) == 0)
264 if (strncasecmp(b
, "wais", len
) == 0)
267 if (strncasecmp(b
, "cache_object", len
) == 0)
268 return PROTO_CACHEOBJ
;
270 if (strncasecmp(b
, "urn", len
) == 0)
273 if (strncasecmp(b
, "whois", len
) == 0)
276 if (strncasecmp(b
, "internal", len
) == 0)
277 return PROTO_INTERNAL
;
283 urlDefaultPort(protocol_t p
)
305 return CACHE_HTTP_PORT
;
318 * If the 'request' arg is non-NULL, put parsed values there instead
319 * of allocating a new HttpRequest.
322 urlParse(method_t method
, char *url
, HttpRequest
*request
)
324 LOCAL_ARRAY(char, proto
, MAX_URL
);
325 LOCAL_ARRAY(char, login
, MAX_URL
);
326 LOCAL_ARRAY(char, host
, MAX_URL
);
327 LOCAL_ARRAY(char, urlpath
, MAX_URL
);
331 protocol_t protocol
= PROTO_NONE
;
333 proto
[0] = host
[0] = urlpath
[0] = login
[0] = '\0';
335 if ((l
= strlen(url
)) + Config
.appendDomainLen
> (MAX_URL
- 1)) {
336 /* terminate so it doesn't overflow other buffers */
337 *(url
+ (MAX_URL
>> 1)) = '\0';
338 debug(23, 1) ("urlParse: URL too large (%d bytes)\n", l
);
342 if (method
== METHOD_CONNECT
) {
345 if (sscanf(url
, "%[^:]:%d", host
, &port
) < 1)
347 } else if (!strncmp(url
, "urn:", 4)) {
348 return urnParse(method
, url
);
350 if (sscanf(url
, "%[^:]://%[^/]%[^\r\n]", proto
, host
, urlpath
) < 2)
353 protocol
= urlParseProtocol(proto
);
355 port
= urlDefaultPort(protocol
);
357 /* Is there any login informaiton? */
358 if ((t
= strrchr(host
, '@'))) {
359 strcpy((char *) login
, (char *) host
);
360 t
= strrchr(login
, '@');
362 strcpy((char *) host
, t
+ 1);
365 if ((t
= strrchr(host
, ':'))) {
373 for (t
= host
; *t
; t
++)
376 if (stringHasWhitespace(host
)) {
377 if (URI_WHITESPACE_STRIP
== Config
.uri_whitespace
) {
392 if (Config
.onoff
.check_hostnames
&& strspn(host
, valid_hostname_chars
) != strlen(host
)) {
393 debug(23, 1) ("urlParse: Illegal character in hostname '%s'\n", host
);
398 #if DONT_DO_THIS_IT_BREAKS_SEMANTIC_TRANSPARENCY
399 /* remove trailing dots from hostnames */
400 while ((l
= strlen(host
)) > 0 && host
[--l
] == '.')
403 /* remove duplicate dots */
404 while ((t
= strstr(host
, "..")))
405 xmemmove(t
, t
+ 1, strlen(t
));
409 if (Config
.appendDomain
&& !strchr(host
, '.'))
410 strncat(host
, Config
.appendDomain
, SQUIDHOSTNAMELEN
- strlen(host
) - 1);
412 if (port
< 1 || port
> 65535) {
413 debug(23, 3) ("urlParse: Invalid port '%d'\n", port
);
417 #ifdef HARDCODE_DENY_PORTS
418 /* These ports are filtered in the default squid.conf, but
419 * maybe someone wants them hardcoded... */
420 if (port
== 7 || port
== 9 || port
== 19) {
421 debug(23, 0) ("urlParse: Deny access to port %d\n", port
);
426 if (stringHasWhitespace(urlpath
)) {
427 debug(23, 2) ("urlParse: URI has whitespace: {%s}\n", url
);
429 switch (Config
.uri_whitespace
) {
431 case URI_WHITESPACE_DENY
:
434 case URI_WHITESPACE_ALLOW
:
437 case URI_WHITESPACE_ENCODE
:
438 t
= rfc1738_escape_unescaped(urlpath
);
439 xstrncpy(urlpath
, t
, MAX_URL
);
442 case URI_WHITESPACE_CHOP
:
443 *(urlpath
+ strcspn(urlpath
, w_space
)) = '\0';
446 case URI_WHITESPACE_STRIP
:
463 request
= new HttpRequest(method
, protocol
, urlpath
);
465 request
->initHTTP(method
, protocol
, urlpath
);
468 xstrncpy(request
->host
, host
, SQUIDHOSTNAMELEN
);
469 xstrncpy(request
->login
, login
, MAX_LOGIN_SZ
);
470 request
->port
= (u_short
) port
;
475 urnParse(method_t method
, char *urn
)
477 debug(50, 5) ("urnParse: %s\n", urn
);
478 return new HttpRequest(method
, PROTO_URN
, urn
+ 4);
482 urlCanonical(HttpRequest
* request
)
484 LOCAL_ARRAY(char, portbuf
, 32);
485 LOCAL_ARRAY(char, urlbuf
, MAX_URL
);
487 if (request
->canonical
)
488 return request
->canonical
;
490 if (request
->protocol
== PROTO_URN
) {
491 snprintf(urlbuf
, MAX_URL
, "urn:%s", request
->urlpath
.buf());
493 switch (request
->method
) {
496 snprintf(urlbuf
, MAX_URL
, "%s:%d", request
->host
, request
->port
);
502 if (request
->port
!= urlDefaultPort(request
->protocol
))
503 snprintf(portbuf
, 32, ":%d", request
->port
);
505 snprintf(urlbuf
, MAX_URL
, "%s://%s%s%s%s%s",
506 ProtocolStr
[request
->protocol
],
508 *request
->login
? "@" : null_string
,
511 request
->urlpath
.buf());
517 return (request
->canonical
= xstrdup(urlbuf
));
521 urlCanonicalClean(const HttpRequest
* request
)
523 LOCAL_ARRAY(char, buf
, MAX_URL
);
524 LOCAL_ARRAY(char, portbuf
, 32);
525 LOCAL_ARRAY(char, loginbuf
, MAX_LOGIN_SZ
+ 1);
528 if (request
->protocol
== PROTO_URN
) {
529 snprintf(buf
, MAX_URL
, "urn:%s", request
->urlpath
.buf());
531 switch (request
->method
) {
534 snprintf(buf
, MAX_URL
, "%s:%d", request
->host
, request
->port
);
540 if (request
->port
!= urlDefaultPort(request
->protocol
))
541 snprintf(portbuf
, 32, ":%d", request
->port
);
545 if ((int) strlen(request
->login
) > 0) {
546 strcpy(loginbuf
, request
->login
);
548 if ((t
= strchr(loginbuf
, ':')))
551 strcat(loginbuf
, "@");
554 snprintf(buf
, MAX_URL
, "%s://%s%s%s%s",
555 ProtocolStr
[request
->protocol
],
559 request
->urlpath
.buf());
561 * strip arguments AFTER a question-mark
564 if (Config
.onoff
.strip_query_terms
)
565 if ((t
= strchr(buf
, '?')))
572 if (stringHasCntl(buf
))
573 xstrncpy(buf
, rfc1738_escape_unescaped(buf
), MAX_URL
);
579 * matchDomainName() compares a hostname with a domainname according
580 * to the following rules:
583 * ------------- ------------- ------
584 * foo.com foo.com YES
585 * .foo.com foo.com YES
586 * x.foo.com foo.com NO
587 * foo.com .foo.com YES
588 * .foo.com .foo.com YES
589 * x.foo.com .foo.com YES
591 * We strip leading dots on hosts (but not domains!) so that
592 * ".foo.com" is is always the same as "foo.com".
595 * 0 means the host matches the domain
596 * 1 means the host is greater than the domain
597 * -1 means the host is less than the domain
601 matchDomainName(const char *h
, const char *d
)
614 * Start at the ends of the two strings and work towards the
617 while (xtolower(h
[--hl
]) == xtolower(d
[--dl
])) {
618 if (hl
== 0 && dl
== 0) {
620 * We made it all the way to the beginning of both
621 * strings without finding any difference.
628 * The host string is shorter than the domain string.
629 * There is only one case when this can be a match.
630 * If the domain is just one character longer, and if
631 * that character is a leading '.' then we call it a
635 if (1 == dl
&& '.' == d
[0])
643 * The domain string is shorter than the host string.
644 * This is a match only if the first domain character
656 * We found different characters in the same position (from the end).
659 * If one of those character is '.' then its special. In order
660 * for splay tree sorting to work properly, "x-foo.com" must
661 * be greater than ".foo.com" even though '-' is less than '.'.
669 return (xtolower(h
[hl
]) - xtolower(d
[dl
]));
673 urlCheckRequest(const HttpRequest
* r
)
676 /* protocol "independent" methods */
678 if (r
->method
== METHOD_CONNECT
)
681 if (r
->method
== METHOD_TRACE
)
684 if (r
->method
== METHOD_PURGE
)
687 /* does method match the protocol? */
688 switch (r
->protocol
) {
700 if (r
->method
== METHOD_PUT
)
708 if (r
->method
== METHOD_GET
)
710 else if (r
->method
== METHOD_HEAD
)
724 * Squid can't originate an SSL connection, so it should
725 * never receive an "https:" URL. It should always be
740 * Quick-n-dirty host extraction from a URL. Steps:
742 * Skip any '/' after the colon
743 * Copy the next SQUID_MAXHOSTNAMELEN bytes to host[]
744 * Look for an ending '/' or ':' and terminate
745 * Look for login info preceeded by '@'
752 char * extract(char const *url
);
755 static char Host
[SQUIDHOSTNAMELEN
];
756 void init(char const *);
757 void findHostStart();
758 void trimTrailingChars();
760 char const *hostStart
;
765 urlHostname(const char *url
)
767 return URLHostName().extract(url
);
770 char URLHostName::Host
[SQUIDHOSTNAMELEN
];
773 URLHostName::init(char const *aUrl
)
780 URLHostName::findHostStart()
782 if (NULL
== (hostStart
= strchr(url
, ':')))
787 while (*hostStart
!= '\0' && *hostStart
== '/')
792 URLHostName::trimTrailingChars()
796 if ((t
= strchr(Host
, '/')))
799 if ((t
= strchr(Host
, ':')))
804 URLHostName::trimAuth()
808 if ((t
= strrchr(Host
, '@'))) {
810 xmemmove(Host
, t
, strlen(t
) + 1);
815 URLHostName::extract(char const *aUrl
)
820 if (hostStart
== NULL
)
823 xstrncpy(Host
, hostStart
, SQUIDHOSTNAMELEN
);
833 urlExtMethodAdd(const char *mstr
)
835 method_t method
= METHOD_NONE
;
837 for (++method
; method
< METHOD_ENUM_END
; ++method
) {
838 if (0 == strcmp(mstr
, RequestMethodStr
[method
])) {
839 debug(23, 2) ("Extension method '%s' already exists\n", mstr
);
843 if (0 != strncmp("%EXT", RequestMethodStr
[method
], 4))
846 /* Don't free statically allocated "%EXTnn" string */
847 RequestMethodStr
[method
] = xstrdup(mstr
);
849 debug(23, 1) ("Extension method '%s' added, enum=%d\n", mstr
, (int) method
);
854 debug(23, 1) ("WARNING: Could not add new extension method '%s' due to lack of array space\n", mstr
);
858 urlExtMethodConfigure(void)
860 wordlist
*w
= Config
.ext_methods
;
865 for (s
= w
->key
; *s
; s
++)
868 urlExtMethodAdd(w
->key
);