]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/url.cc
3 * $Id: url.cc,v 1.146 2003/08/10 11:00:45 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"
39 const char *RequestMethodStr
[] =
90 const char *ProtocolStr
[] =
109 static HttpRequest
*urnParse(method_t method
, char *urn
);
111 static const char *const valid_hostname_chars
=
112 #if ALLOW_HOSTNAME_UNDERSCORES
113 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
114 "abcdefghijklmnopqrstuvwxyz"
117 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
118 "abcdefghijklmnopqrstuvwxyz"
122 #endif /* CHECK_HOSTNAMES */
124 /* convert %xx in url string to a character
125 * Allocate a new string and return a pointer to converted string */
128 url_convert_hex(char *org_url
, int allocate
)
130 static char code
[] = "00";
134 url
= allocate
? (char *) xstrdup(org_url
) : org_url
;
136 if ((int) strlen(url
) < 3 || !strchr(url
, '%'))
139 for (s
= t
= url
; *s
; s
++) {
140 if (*s
== '%' && *(s
+ 1) && *(s
+ 2)) {
143 *t
++ = (char) strtol(code
, NULL
, 16);
159 debug(23, 5) ("urlInitialize: Initializing...\n");
160 assert(sizeof(ProtocolStr
) == (PROTO_MAX
+ 1) * sizeof(char *));
162 * These test that our matchDomainName() function works the
163 * way we expect it to.
165 assert(0 == matchDomainName("foo.com", "foo.com"));
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("x.foo.com", ".foo.com"));
170 assert(0 != matchDomainName("x.foo.com", "foo.com"));
171 assert(0 != matchDomainName("foo.com", "x.foo.com"));
172 assert(0 != matchDomainName("bar.com", "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("zzz.com", "foo.com"));
177 assert(0 > matchDomainName("aaa.com", "foo.com"));
178 assert(0 == matchDomainName("FOO.com", "foo.COM"));
179 assert(0 < matchDomainName("bfoo.com", "afoo.com"));
180 assert(0 > matchDomainName("afoo.com", "bfoo.com"));
181 assert(0 < matchDomainName("x-foo.com", ".foo.com"));
185 method_t
&operator++ (method_t
&aMethod
)
187 int tmp
= (int)aMethod
;
188 aMethod
= (method_t
)(++tmp
);
194 urlParseMethod(const char *s
)
196 method_t method
= METHOD_NONE
;
198 * This check for '%' makes sure that we don't
199 * match one of the extension method placeholders,
200 * which have the form %EXT[0-9][0-9]
206 for (++method
; method
< METHOD_ENUM_END
; ++method
) {
207 if (0 == strcasecmp(s
, RequestMethodStr
[method
]))
216 urlParseProtocol(const char *s
)
218 /* test common stuff first */
220 if (strcasecmp(s
, "http") == 0)
223 if (strcasecmp(s
, "ftp") == 0)
226 if (strcasecmp(s
, "https") == 0)
229 if (strcasecmp(s
, "file") == 0)
232 if (strcasecmp(s
, "gopher") == 0)
235 if (strcasecmp(s
, "wais") == 0)
238 if (strcasecmp(s
, "cache_object") == 0)
239 return PROTO_CACHEOBJ
;
241 if (strcasecmp(s
, "urn") == 0)
244 if (strcasecmp(s
, "whois") == 0)
247 if (strcasecmp(s
, "internal") == 0)
248 return PROTO_INTERNAL
;
255 urlDefaultPort(protocol_t p
)
277 return CACHE_HTTP_PORT
;
288 urlParse(method_t method
, char *url
)
290 LOCAL_ARRAY(char, proto
, MAX_URL
);
291 LOCAL_ARRAY(char, login
, MAX_URL
);
292 LOCAL_ARRAY(char, host
, MAX_URL
);
293 LOCAL_ARRAY(char, urlpath
, MAX_URL
);
294 HttpRequest
*request
= NULL
;
298 protocol_t protocol
= PROTO_NONE
;
300 proto
[0] = host
[0] = urlpath
[0] = login
[0] = '\0';
302 if ((l
= strlen(url
)) + Config
.appendDomainLen
> (MAX_URL
- 1)) {
303 /* terminate so it doesn't overflow other buffers */
304 *(url
+ (MAX_URL
>> 1)) = '\0';
305 debug(23, 1) ("urlParse: URL too large (%d bytes)\n", l
);
309 if (method
== METHOD_CONNECT
) {
312 if (sscanf(url
, "%[^:]:%d", host
, &port
) < 1)
314 } else if (!strncmp(url
, "urn:", 4)) {
315 return urnParse(method
, url
);
317 if (sscanf(url
, "%[^:]://%[^/]%[^\r\n]", proto
, host
, urlpath
) < 2)
320 protocol
= urlParseProtocol(proto
);
322 port
= urlDefaultPort(protocol
);
324 /* Is there any login informaiton? */
325 if ((t
= strrchr(host
, '@'))) {
326 strcpy((char *) login
, (char *) host
);
327 t
= strrchr(login
, '@');
329 strcpy((char *) host
, t
+ 1);
332 if ((t
= strrchr(host
, ':'))) {
340 for (t
= host
; *t
; t
++)
343 if (stringHasWhitespace(host
)) {
344 if (URI_WHITESPACE_STRIP
== Config
.uri_whitespace
) {
359 if (Config
.onoff
.check_hostnames
&& strspn(host
, valid_hostname_chars
) != strlen(host
)) {
360 debug(23, 1) ("urlParse: Illegal character in hostname '%s'\n", host
);
365 #if DONT_DO_THIS_IT_BREAKS_SEMANTIC_TRANSPARENCY
366 /* remove trailing dots from hostnames */
367 while ((l
= strlen(host
)) > 0 && host
[--l
] == '.')
370 /* remove duplicate dots */
371 while ((t
= strstr(host
, "..")))
372 xmemmove(t
, t
+ 1, strlen(t
));
376 if (Config
.appendDomain
&& !strchr(host
, '.'))
377 strncat(host
, Config
.appendDomain
, SQUIDHOSTNAMELEN
);
379 if (port
< 1 || port
> 65535) {
380 debug(23, 3) ("urlParse: Invalid port '%d'\n", port
);
384 #ifdef HARDCODE_DENY_PORTS
385 /* These ports are filtered in the default squid.conf, but
386 * maybe someone wants them hardcoded... */
387 if (port
== 7 || port
== 9 || port
== 19) {
388 debug(23, 0) ("urlParse: Deny access to port %d\n", port
);
393 if (stringHasWhitespace(urlpath
)) {
394 debug(23, 2) ("urlParse: URI has whitespace: {%s}\n", url
);
396 switch (Config
.uri_whitespace
) {
398 case URI_WHITESPACE_DENY
:
401 case URI_WHITESPACE_ALLOW
:
404 case URI_WHITESPACE_ENCODE
:
405 t
= rfc1738_escape_unescaped(urlpath
);
406 xstrncpy(urlpath
, t
, MAX_URL
);
409 case URI_WHITESPACE_CHOP
:
410 *(urlpath
+ strcspn(urlpath
, w_space
)) = '\0';
413 case URI_WHITESPACE_STRIP
:
429 request
= requestCreate(method
, protocol
, urlpath
);
430 xstrncpy(request
->host
, host
, SQUIDHOSTNAMELEN
);
431 xstrncpy(request
->login
, login
, MAX_LOGIN_SZ
);
432 request
->port
= (u_short
) port
;
437 urnParse(method_t method
, char *urn
)
439 debug(50, 5) ("urnParse: %s\n", urn
);
440 return requestCreate(method
, PROTO_URN
, urn
+ 4);
444 urlCanonical(HttpRequest
* request
)
446 LOCAL_ARRAY(char, portbuf
, 32);
447 LOCAL_ARRAY(char, urlbuf
, MAX_URL
);
449 if (request
->canonical
)
450 return request
->canonical
;
452 if (request
->protocol
== PROTO_URN
) {
453 snprintf(urlbuf
, MAX_URL
, "urn:%s", request
->urlpath
.buf());
455 switch (request
->method
) {
458 snprintf(urlbuf
, MAX_URL
, "%s:%d", request
->host
, request
->port
);
464 if (request
->port
!= urlDefaultPort(request
->protocol
))
465 snprintf(portbuf
, 32, ":%d", request
->port
);
467 snprintf(urlbuf
, MAX_URL
, "%s://%s%s%s%s%s",
468 ProtocolStr
[request
->protocol
],
470 *request
->login
? "@" : null_string
,
473 request
->urlpath
.buf());
479 return (request
->canonical
= xstrdup(urlbuf
));
483 urlCanonicalClean(const HttpRequest
* request
)
485 LOCAL_ARRAY(char, buf
, MAX_URL
);
486 LOCAL_ARRAY(char, portbuf
, 32);
487 LOCAL_ARRAY(char, loginbuf
, MAX_LOGIN_SZ
+ 1);
490 if (request
->protocol
== PROTO_URN
) {
491 snprintf(buf
, MAX_URL
, "urn:%s", request
->urlpath
.buf());
493 switch (request
->method
) {
496 snprintf(buf
, MAX_URL
, "%s:%d", request
->host
, request
->port
);
502 if (request
->port
!= urlDefaultPort(request
->protocol
))
503 snprintf(portbuf
, 32, ":%d", request
->port
);
507 if ((int) strlen(request
->login
) > 0) {
508 strcpy(loginbuf
, request
->login
);
510 if ((t
= strchr(loginbuf
, ':')))
513 strcat(loginbuf
, "@");
516 snprintf(buf
, MAX_URL
, "%s://%s%s%s%s",
517 ProtocolStr
[request
->protocol
],
521 request
->urlpath
.buf());
523 * strip arguments AFTER a question-mark
526 if (Config
.onoff
.strip_query_terms
)
527 if ((t
= strchr(buf
, '?')))
534 if (stringHasCntl(buf
))
535 xstrncpy(buf
, rfc1738_escape_unescaped(buf
), MAX_URL
);
541 * matchDomainName() compares a hostname with a domainname according
542 * to the following rules:
545 * ------------- ------------- ------
546 * foo.com foo.com YES
547 * .foo.com foo.com YES
548 * x.foo.com foo.com NO
549 * foo.com .foo.com YES
550 * .foo.com .foo.com YES
551 * x.foo.com .foo.com YES
553 * We strip leading dots on hosts (but not domains!) so that
554 * ".foo.com" is is always the same as "foo.com".
557 * 0 means the host matches the domain
558 * 1 means the host is greater than the domain
559 * -1 means the host is less than the domain
563 matchDomainName(const char *h
, const char *d
)
576 * Start at the ends of the two strings and work towards the
579 while (xtolower(h
[--hl
]) == xtolower(d
[--dl
])) {
580 if (hl
== 0 && dl
== 0) {
582 * We made it all the way to the beginning of both
583 * strings without finding any difference.
590 * The host string is shorter than the domain string.
591 * There is only one case when this can be a match.
592 * If the domain is just one character longer, and if
593 * that character is a leading '.' then we call it a
597 if (1 == dl
&& '.' == d
[0])
605 * The domain string is shorter than the host string.
606 * This is a match only if the first domain character
618 * We found different characters in the same position (from the end).
621 * If one of those character is '.' then its special. In order
622 * for splay tree sorting to work properly, "x-foo.com" must
623 * be greater than ".foo.com" even though '-' is less than '.'.
631 return (xtolower(h
[hl
]) - xtolower(d
[dl
]));
635 urlCheckRequest(const HttpRequest
* r
)
638 /* protocol "independent" methods */
640 if (r
->method
== METHOD_CONNECT
)
643 if (r
->method
== METHOD_TRACE
)
646 if (r
->method
== METHOD_PURGE
)
649 /* does method match the protocol? */
650 switch (r
->protocol
) {
662 if (r
->method
== METHOD_PUT
)
670 if (r
->method
== METHOD_GET
)
672 else if (r
->method
== METHOD_HEAD
)
686 * Squid can't originate an SSL connection, so it should
687 * never receive an "https:" URL. It should always be
702 * Quick-n-dirty host extraction from a URL. Steps:
704 * Skip any '/' after the colon
705 * Copy the next SQUID_MAXHOSTNAMELEN bytes to host[]
706 * Look for an ending '/' or ':' and terminate
707 * Look for login info preceeded by '@'
714 char * extract(char const *url
);
717 static char Host
[SQUIDHOSTNAMELEN
];
718 void init(char const *);
719 void findHostStart();
720 void trimTrailingChars();
722 char const *hostStart
;
727 urlHostname(const char *url
)
729 return URLHostName().extract(url
);
732 char URLHostName::Host
[SQUIDHOSTNAMELEN
];
735 URLHostName::init(char const *aUrl
)
742 URLHostName::findHostStart()
744 if (NULL
== (hostStart
= strchr(url
, ':')))
749 while (*hostStart
!= '\0' && *hostStart
== '/')
754 URLHostName::trimTrailingChars()
758 if ((t
= strchr(Host
, '/')))
761 if ((t
= strchr(Host
, ':')))
766 URLHostName::trimAuth()
770 if ((t
= strrchr(Host
, '@'))) {
772 xmemmove(Host
, t
, strlen(t
) + 1);
777 URLHostName::extract(char const *aUrl
)
782 if (hostStart
== NULL
)
785 xstrncpy(Host
, hostStart
, SQUIDHOSTNAMELEN
);
795 urlExtMethodAdd(const char *mstr
)
797 method_t method
= METHOD_NONE
;
799 for (++method
; method
< METHOD_ENUM_END
; ++method
) {
800 if (0 == strcmp(mstr
, RequestMethodStr
[method
])) {
801 debug(23, 2) ("Extension method '%s' already exists\n", mstr
);
805 if (0 != strncmp("%EXT", RequestMethodStr
[method
], 4))
808 /* Don't free statically allocated "%EXTnn" string */
809 RequestMethodStr
[method
] = xstrdup(mstr
);
811 debug(23, 1) ("Extension method '%s' added, enum=%d\n", mstr
, (int) method
);
816 debug(23, 1) ("WARNING: Could not add new extension method '%s' due to lack of array space\n", mstr
);
820 urlExtMethodConfigure(void)
822 wordlist
*w
= Config
.ext_methods
;
827 for (s
= w
->key
; *s
; s
++)
830 urlExtMethodAdd(w
->key
);