]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/url.cc
3 * $Id: url.cc,v 1.151 2006/04/23 09:02:13 serassio 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"
39 const char *RequestMethodStr
[] =
91 const char *ProtocolStr
[] =
110 static HttpRequest
*urnParse(method_t method
, char *urn
);
112 static const char *const valid_hostname_chars
=
113 #if ALLOW_HOSTNAME_UNDERSCORES
114 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
115 "abcdefghijklmnopqrstuvwxyz"
118 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
119 "abcdefghijklmnopqrstuvwxyz"
123 #endif /* CHECK_HOSTNAMES */
125 /* convert %xx in url string to a character
126 * Allocate a new string and return a pointer to converted string */
129 url_convert_hex(char *org_url
, int allocate
)
131 static char code
[] = "00";
135 url
= allocate
? (char *) xstrdup(org_url
) : org_url
;
137 if ((int) strlen(url
) < 3 || !strchr(url
, '%'))
140 for (s
= t
= url
; *s
; s
++) {
141 if (*s
== '%' && *(s
+ 1) && *(s
+ 2)) {
144 *t
++ = (char) strtol(code
, NULL
, 16);
160 debug(23, 5) ("urlInitialize: Initializing...\n");
161 assert(sizeof(ProtocolStr
) == (PROTO_MAX
+ 1) * sizeof(char *));
163 * These test that our matchDomainName() function works the
164 * way we expect it to.
166 assert(0 == matchDomainName("foo.com", "foo.com"));
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("x.foo.com", ".foo.com"));
171 assert(0 != matchDomainName("x.foo.com", "foo.com"));
172 assert(0 != matchDomainName("foo.com", "x.foo.com"));
173 assert(0 != matchDomainName("bar.com", "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("zzz.com", "foo.com"));
178 assert(0 > matchDomainName("aaa.com", "foo.com"));
179 assert(0 == matchDomainName("FOO.com", "foo.COM"));
180 assert(0 < matchDomainName("bfoo.com", "afoo.com"));
181 assert(0 > matchDomainName("afoo.com", "bfoo.com"));
182 assert(0 < matchDomainName("x-foo.com", ".foo.com"));
186 method_t
&operator++ (method_t
&aMethod
)
188 int tmp
= (int)aMethod
;
189 aMethod
= (method_t
)(++tmp
);
194 * urlParseMethod() takes begin and end pointers, but for backwards
195 * compatibility, end defaults to NULL, in which case we assume begin
196 * is NULL-terminated.
199 urlParseMethod(const char *b
, const char *e
)
201 method_t method
= METHOD_NONE
;
203 * This check for '%' makes sure that we don't
204 * match one of the extension method placeholders,
205 * which have the form %EXT[0-9][0-9]
212 * if e is NULL, b must be NULL terminated and we
213 * make e point to the first whitespace character
217 e
= b
+ strcspn(b
, w_space
);
219 for (++method
; method
< METHOD_ENUM_END
; ++method
) {
220 if (0 == strncasecmp(b
, RequestMethodStr
[method
], e
-b
))
228 * urlParseProtocol() takes begin (b) and end (e) pointers, but for
229 * backwards compatibility, e defaults to NULL, in which case we
230 * assume b is NULL-terminated.
233 urlParseProtocol(const char *b
, const char *e
)
236 * if e is NULL, b must be NULL terminated and we
237 * make e point to the first whitespace character
242 e
= b
+ strcspn(b
, ":");
246 /* test common stuff first */
248 if (strncasecmp(b
, "http", len
) == 0)
251 if (strncasecmp(b
, "ftp", len
) == 0)
254 if (strncasecmp(b
, "https", len
) == 0)
257 if (strncasecmp(b
, "file", len
) == 0)
260 if (strncasecmp(b
, "gopher", len
) == 0)
263 if (strncasecmp(b
, "wais", len
) == 0)
266 if (strncasecmp(b
, "cache_object", len
) == 0)
267 return PROTO_CACHEOBJ
;
269 if (strncasecmp(b
, "urn", len
) == 0)
272 if (strncasecmp(b
, "whois", len
) == 0)
275 if (strncasecmp(b
, "internal", len
) == 0)
276 return PROTO_INTERNAL
;
282 urlDefaultPort(protocol_t p
)
304 return CACHE_HTTP_PORT
;
317 * If the 'request' arg is non-NULL, put parsed values there instead
318 * of allocating a new HttpRequest.
321 urlParse(method_t method
, char *url
, HttpRequest
*request
)
323 LOCAL_ARRAY(char, proto
, MAX_URL
);
324 LOCAL_ARRAY(char, login
, MAX_URL
);
325 LOCAL_ARRAY(char, host
, MAX_URL
);
326 LOCAL_ARRAY(char, urlpath
, MAX_URL
);
330 protocol_t protocol
= PROTO_NONE
;
332 proto
[0] = host
[0] = urlpath
[0] = login
[0] = '\0';
334 if ((l
= strlen(url
)) + Config
.appendDomainLen
> (MAX_URL
- 1)) {
335 /* terminate so it doesn't overflow other buffers */
336 *(url
+ (MAX_URL
>> 1)) = '\0';
337 debug(23, 1) ("urlParse: URL too large (%d bytes)\n", l
);
341 if (method
== METHOD_CONNECT
) {
344 if (sscanf(url
, "%[^:]:%d", host
, &port
) < 1)
346 } else if (!strncmp(url
, "urn:", 4)) {
347 return urnParse(method
, url
);
349 if (sscanf(url
, "%[^:]://%[^/]%[^\r\n]", proto
, host
, urlpath
) < 2)
352 protocol
= urlParseProtocol(proto
);
354 port
= urlDefaultPort(protocol
);
356 /* Is there any login informaiton? */
357 if ((t
= strrchr(host
, '@'))) {
358 strcpy((char *) login
, (char *) host
);
359 t
= strrchr(login
, '@');
361 strcpy((char *) host
, t
+ 1);
364 if ((t
= strrchr(host
, ':'))) {
372 for (t
= host
; *t
; t
++)
375 if (stringHasWhitespace(host
)) {
376 if (URI_WHITESPACE_STRIP
== Config
.uri_whitespace
) {
391 if (Config
.onoff
.check_hostnames
&& strspn(host
, valid_hostname_chars
) != strlen(host
)) {
392 debug(23, 1) ("urlParse: Illegal character in hostname '%s'\n", host
);
397 #if DONT_DO_THIS_IT_BREAKS_SEMANTIC_TRANSPARENCY
398 /* remove trailing dots from hostnames */
399 while ((l
= strlen(host
)) > 0 && host
[--l
] == '.')
402 /* remove duplicate dots */
403 while ((t
= strstr(host
, "..")))
404 xmemmove(t
, t
+ 1, strlen(t
));
408 if (Config
.appendDomain
&& !strchr(host
, '.'))
409 strncat(host
, Config
.appendDomain
, SQUIDHOSTNAMELEN
- strlen(host
) - 1);
411 if (port
< 1 || port
> 65535) {
412 debug(23, 3) ("urlParse: Invalid port '%d'\n", port
);
416 #ifdef HARDCODE_DENY_PORTS
417 /* These ports are filtered in the default squid.conf, but
418 * maybe someone wants them hardcoded... */
419 if (port
== 7 || port
== 9 || port
== 19) {
420 debug(23, 0) ("urlParse: Deny access to port %d\n", port
);
425 if (stringHasWhitespace(urlpath
)) {
426 debug(23, 2) ("urlParse: URI has whitespace: {%s}\n", url
);
428 switch (Config
.uri_whitespace
) {
430 case URI_WHITESPACE_DENY
:
433 case URI_WHITESPACE_ALLOW
:
436 case URI_WHITESPACE_ENCODE
:
437 t
= rfc1738_escape_unescaped(urlpath
);
438 xstrncpy(urlpath
, t
, MAX_URL
);
441 case URI_WHITESPACE_CHOP
:
442 *(urlpath
+ strcspn(urlpath
, w_space
)) = '\0';
445 case URI_WHITESPACE_STRIP
:
462 request
= new HttpRequest(method
, protocol
, urlpath
);
464 request
->initHTTP(method
, protocol
, urlpath
);
467 xstrncpy(request
->host
, host
, SQUIDHOSTNAMELEN
);
468 xstrncpy(request
->login
, login
, MAX_LOGIN_SZ
);
469 request
->port
= (u_short
) port
;
474 urnParse(method_t method
, char *urn
)
476 debug(50, 5) ("urnParse: %s\n", urn
);
477 return new HttpRequest(method
, PROTO_URN
, urn
+ 4);
481 urlCanonical(HttpRequest
* request
)
483 LOCAL_ARRAY(char, portbuf
, 32);
484 LOCAL_ARRAY(char, urlbuf
, MAX_URL
);
486 if (request
->canonical
)
487 return request
->canonical
;
489 if (request
->protocol
== PROTO_URN
) {
490 snprintf(urlbuf
, MAX_URL
, "urn:%s", request
->urlpath
.buf());
492 switch (request
->method
) {
495 snprintf(urlbuf
, MAX_URL
, "%s:%d", request
->host
, request
->port
);
501 if (request
->port
!= urlDefaultPort(request
->protocol
))
502 snprintf(portbuf
, 32, ":%d", request
->port
);
504 snprintf(urlbuf
, MAX_URL
, "%s://%s%s%s%s%s",
505 ProtocolStr
[request
->protocol
],
507 *request
->login
? "@" : null_string
,
510 request
->urlpath
.buf());
516 return (request
->canonical
= xstrdup(urlbuf
));
520 urlCanonicalClean(const HttpRequest
* request
)
522 LOCAL_ARRAY(char, buf
, MAX_URL
);
523 LOCAL_ARRAY(char, portbuf
, 32);
524 LOCAL_ARRAY(char, loginbuf
, MAX_LOGIN_SZ
+ 1);
527 if (request
->protocol
== PROTO_URN
) {
528 snprintf(buf
, MAX_URL
, "urn:%s", request
->urlpath
.buf());
530 switch (request
->method
) {
533 snprintf(buf
, MAX_URL
, "%s:%d", request
->host
, request
->port
);
539 if (request
->port
!= urlDefaultPort(request
->protocol
))
540 snprintf(portbuf
, 32, ":%d", request
->port
);
544 if ((int) strlen(request
->login
) > 0) {
545 strcpy(loginbuf
, request
->login
);
547 if ((t
= strchr(loginbuf
, ':')))
550 strcat(loginbuf
, "@");
553 snprintf(buf
, MAX_URL
, "%s://%s%s%s%s",
554 ProtocolStr
[request
->protocol
],
558 request
->urlpath
.buf());
560 * strip arguments AFTER a question-mark
563 if (Config
.onoff
.strip_query_terms
)
564 if ((t
= strchr(buf
, '?')))
571 if (stringHasCntl(buf
))
572 xstrncpy(buf
, rfc1738_escape_unescaped(buf
), MAX_URL
);
578 * matchDomainName() compares a hostname with a domainname according
579 * to the following rules:
582 * ------------- ------------- ------
583 * foo.com foo.com YES
584 * .foo.com foo.com YES
585 * x.foo.com foo.com NO
586 * foo.com .foo.com YES
587 * .foo.com .foo.com YES
588 * x.foo.com .foo.com YES
590 * We strip leading dots on hosts (but not domains!) so that
591 * ".foo.com" is is always the same as "foo.com".
594 * 0 means the host matches the domain
595 * 1 means the host is greater than the domain
596 * -1 means the host is less than the domain
600 matchDomainName(const char *h
, const char *d
)
613 * Start at the ends of the two strings and work towards the
616 while (xtolower(h
[--hl
]) == xtolower(d
[--dl
])) {
617 if (hl
== 0 && dl
== 0) {
619 * We made it all the way to the beginning of both
620 * strings without finding any difference.
627 * The host string is shorter than the domain string.
628 * There is only one case when this can be a match.
629 * If the domain is just one character longer, and if
630 * that character is a leading '.' then we call it a
634 if (1 == dl
&& '.' == d
[0])
642 * The domain string is shorter than the host string.
643 * This is a match only if the first domain character
655 * We found different characters in the same position (from the end).
658 * If one of those character is '.' then its special. In order
659 * for splay tree sorting to work properly, "x-foo.com" must
660 * be greater than ".foo.com" even though '-' is less than '.'.
668 return (xtolower(h
[hl
]) - xtolower(d
[dl
]));
672 urlCheckRequest(const HttpRequest
* r
)
675 /* protocol "independent" methods */
677 if (r
->method
== METHOD_CONNECT
)
680 if (r
->method
== METHOD_TRACE
)
683 if (r
->method
== METHOD_PURGE
)
686 /* does method match the protocol? */
687 switch (r
->protocol
) {
699 if (r
->method
== METHOD_PUT
)
707 if (r
->method
== METHOD_GET
)
709 else if (r
->method
== METHOD_HEAD
)
723 * Squid can't originate an SSL connection, so it should
724 * never receive an "https:" URL. It should always be
739 * Quick-n-dirty host extraction from a URL. Steps:
741 * Skip any '/' after the colon
742 * Copy the next SQUID_MAXHOSTNAMELEN bytes to host[]
743 * Look for an ending '/' or ':' and terminate
744 * Look for login info preceeded by '@'
751 char * extract(char const *url
);
754 static char Host
[SQUIDHOSTNAMELEN
];
755 void init(char const *);
756 void findHostStart();
757 void trimTrailingChars();
759 char const *hostStart
;
764 urlHostname(const char *url
)
766 return URLHostName().extract(url
);
769 char URLHostName::Host
[SQUIDHOSTNAMELEN
];
772 URLHostName::init(char const *aUrl
)
779 URLHostName::findHostStart()
781 if (NULL
== (hostStart
= strchr(url
, ':')))
786 while (*hostStart
!= '\0' && *hostStart
== '/')
791 URLHostName::trimTrailingChars()
795 if ((t
= strchr(Host
, '/')))
798 if ((t
= strchr(Host
, ':')))
803 URLHostName::trimAuth()
807 if ((t
= strrchr(Host
, '@'))) {
809 xmemmove(Host
, t
, strlen(t
) + 1);
814 URLHostName::extract(char const *aUrl
)
819 if (hostStart
== NULL
)
822 xstrncpy(Host
, hostStart
, SQUIDHOSTNAMELEN
);
832 urlExtMethodAdd(const char *mstr
)
834 method_t method
= METHOD_NONE
;
836 for (++method
; method
< METHOD_ENUM_END
; ++method
) {
837 if (0 == strcmp(mstr
, RequestMethodStr
[method
])) {
838 debug(23, 2) ("Extension method '%s' already exists\n", mstr
);
842 if (0 != strncmp("%EXT", RequestMethodStr
[method
], 4))
845 /* Don't free statically allocated "%EXTnn" string */
846 RequestMethodStr
[method
] = xstrdup(mstr
);
848 debug(23, 1) ("Extension method '%s' added, enum=%d\n", mstr
, (int) method
);
853 debug(23, 1) ("WARNING: Could not add new extension method '%s' due to lack of array space\n", mstr
);
857 urlExtMethodConfigure(void)
859 wordlist
*w
= Config
.ext_methods
;
864 for (s
= w
->key
; *s
; s
++)
867 urlExtMethodAdd(w
->key
);