]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/url.cc
3 * $Id: url.cc,v 1.135 2002/08/19 22:47:54 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.
38 const char *RequestMethodStr
[] =
89 const char *ProtocolStr
[] =
108 static request_t
*urnParse(method_t method
, char *urn
);
109 static const char *const valid_hostname_chars
=
110 #if ALLOW_HOSTNAME_UNDERSCORES
111 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
112 "abcdefghijklmnopqrstuvwxyz"
115 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
116 "abcdefghijklmnopqrstuvwxyz"
120 /* convert %xx in url string to a character
121 * Allocate a new string and return a pointer to converted string */
124 url_convert_hex(char *org_url
, int allocate
)
126 static char code
[] = "00";
130 url
= allocate
? (char *) xstrdup(org_url
) : org_url
;
131 if ((int) strlen(url
) < 3 || !strchr(url
, '%'))
133 for (s
= t
= url
; *s
; s
++) {
134 if (*s
== '%' && *(s
+ 1) && *(s
+ 2)) {
137 *t
++ = (char) strtol(code
, NULL
, 16);
151 debug(23, 5) ("urlInitialize: Initializing...\n");
152 assert(sizeof(ProtocolStr
) == (PROTO_MAX
+ 1) * sizeof(char *));
153 memset(&null_request_flags
, '\0', sizeof(null_request_flags
));
155 * These test that our matchDomainName() function works the
156 * way we expect it to.
158 assert(0 == matchDomainName("foo.com", "foo.com"));
159 assert(0 == matchDomainName(".foo.com", "foo.com"));
160 assert(0 == matchDomainName("foo.com", ".foo.com"));
161 assert(0 == matchDomainName(".foo.com", ".foo.com"));
162 assert(0 == matchDomainName("x.foo.com", ".foo.com"));
163 assert(0 != matchDomainName("x.foo.com", "foo.com"));
164 assert(0 != matchDomainName("foo.com", "x.foo.com"));
165 assert(0 != matchDomainName("bar.com", "foo.com"));
166 assert(0 != matchDomainName(".bar.com", "foo.com"));
167 assert(0 != matchDomainName(".bar.com", ".foo.com"));
168 assert(0 != matchDomainName("bar.com", ".foo.com"));
169 assert(0 < matchDomainName("zzz.com", "foo.com"));
170 assert(0 > matchDomainName("aaa.com", "foo.com"));
171 assert(0 == matchDomainName("FOO.com", "foo.COM"));
172 assert(0 < matchDomainName("bfoo.com", "afoo.com"));
173 assert(0 > matchDomainName("afoo.com", "bfoo.com"));
174 assert(0 < matchDomainName("x-foo.com", ".foo.com"));
179 urlParseMethod(const char *s
)
181 method_t method
= METHOD_NONE
;
183 * This check for '%' makes sure that we don't
184 * match one of the extension method placeholders,
185 * which have the form %EXT[0-9][0-9]
189 for (method
++; method
< METHOD_ENUM_END
; method
++) {
190 if (0 == strcasecmp(s
, RequestMethodStr
[method
]))
198 urlParseProtocol(const char *s
)
200 /* test common stuff first */
201 if (strcasecmp(s
, "http") == 0)
203 if (strcasecmp(s
, "ftp") == 0)
205 if (strcasecmp(s
, "https") == 0)
207 if (strcasecmp(s
, "file") == 0)
209 if (strcasecmp(s
, "gopher") == 0)
211 if (strcasecmp(s
, "wais") == 0)
213 if (strcasecmp(s
, "cache_object") == 0)
214 return PROTO_CACHEOBJ
;
215 if (strcasecmp(s
, "urn") == 0)
217 if (strcasecmp(s
, "whois") == 0)
219 if (strcasecmp(s
, "internal") == 0)
220 return PROTO_INTERNAL
;
226 urlDefaultPort(protocol_t p
)
241 return CACHE_HTTP_PORT
;
250 urlParse(method_t method
, char *url
)
252 LOCAL_ARRAY(char, proto
, MAX_URL
);
253 LOCAL_ARRAY(char, login
, MAX_URL
);
254 LOCAL_ARRAY(char, host
, MAX_URL
);
255 LOCAL_ARRAY(char, urlpath
, MAX_URL
);
256 request_t
*request
= NULL
;
260 protocol_t protocol
= PROTO_NONE
;
262 proto
[0] = host
[0] = urlpath
[0] = login
[0] = '\0';
264 if ((l
= strlen(url
)) + Config
.appendDomainLen
> (MAX_URL
- 1)) {
265 /* terminate so it doesn't overflow other buffers */
266 *(url
+ (MAX_URL
>> 1)) = '\0';
267 debug(23, 1) ("urlParse: URL too large (%d bytes)\n", l
);
270 if (method
== METHOD_CONNECT
) {
272 if (sscanf(url
, "%[^:]:%d", host
, &port
) < 1)
274 } else if (!strncmp(url
, "urn:", 4)) {
275 return urnParse(method
, url
);
277 if (sscanf(url
, "%[^:]://%[^/]%[^\r\n]", proto
, host
, urlpath
) < 2)
279 protocol
= urlParseProtocol(proto
);
280 port
= urlDefaultPort(protocol
);
281 /* Is there any login informaiton? */
282 if ((t
= strrchr(host
, '@'))) {
283 strcpy((char *) login
, (char *) host
);
284 t
= strrchr(login
, '@');
286 strcpy((char *) host
, t
+ 1);
288 if ((t
= strrchr(host
, ':'))) {
294 for (t
= host
; *t
; t
++)
296 if (stringHasWhitespace(host
)) {
297 if (URI_WHITESPACE_STRIP
== Config
.uri_whitespace
) {
307 if (strspn(host
, valid_hostname_chars
) != strlen(host
)) {
308 debug(23, 1) ("urlParse: Illegal character in hostname '%s'\n", host
);
311 /* remove trailing dots from hostnames */
312 while ((l
= strlen(host
)) > 0 && host
[--l
] == '.')
314 /* remove duplicate dots */
315 while ((t
= strstr(host
, "..")))
316 xmemmove(t
, t
+ 1, strlen(t
));
317 if (Config
.appendDomain
&& !strchr(host
, '.'))
318 strncat(host
, Config
.appendDomain
, SQUIDHOSTNAMELEN
);
320 debug(23, 3) ("urlParse: Invalid port == 0\n");
323 #ifdef HARDCODE_DENY_PORTS
324 /* These ports are filtered in the default squid.conf, but
325 * maybe someone wants them hardcoded... */
326 if (port
== 7 || port
== 9 || port
== 19) {
327 debug(23, 0) ("urlParse: Deny access to port %d\n", port
);
331 if (stringHasWhitespace(urlpath
)) {
332 debug(23, 2) ("urlParse: URI has whitespace: {%s}\n", url
);
333 switch (Config
.uri_whitespace
) {
334 case URI_WHITESPACE_DENY
:
336 case URI_WHITESPACE_ALLOW
:
338 case URI_WHITESPACE_ENCODE
:
339 t
= rfc1738_escape_unescaped(urlpath
);
340 xstrncpy(urlpath
, t
, MAX_URL
);
342 case URI_WHITESPACE_CHOP
:
343 *(urlpath
+ strcspn(urlpath
, w_space
)) = '\0';
345 case URI_WHITESPACE_STRIP
:
356 request
= requestCreate(method
, protocol
, urlpath
);
357 xstrncpy(request
->host
, host
, SQUIDHOSTNAMELEN
);
358 xstrncpy(request
->login
, login
, MAX_LOGIN_SZ
);
359 request
->port
= (u_short
) port
;
364 urnParse(method_t method
, char *urn
)
366 debug(50, 5) ("urnParse: %s\n", urn
);
367 return requestCreate(method
, PROTO_URN
, urn
+ 4);
371 urlCanonical(request_t
* request
)
373 LOCAL_ARRAY(char, portbuf
, 32);
374 LOCAL_ARRAY(char, urlbuf
, MAX_URL
);
375 if (request
->canonical
)
376 return request
->canonical
;
377 if (request
->protocol
== PROTO_URN
) {
378 snprintf(urlbuf
, MAX_URL
, "urn:%s", strBuf(request
->urlpath
));
380 switch (request
->method
) {
382 snprintf(urlbuf
, MAX_URL
, "%s:%d", request
->host
, request
->port
);
386 if (request
->port
!= urlDefaultPort(request
->protocol
))
387 snprintf(portbuf
, 32, ":%d", request
->port
);
388 snprintf(urlbuf
, MAX_URL
, "%s://%s%s%s%s%s",
389 ProtocolStr
[request
->protocol
],
391 *request
->login
? "@" : null_string
,
394 strBuf(request
->urlpath
));
398 return (request
->canonical
= xstrdup(urlbuf
));
402 urlCanonicalClean(const request_t
* request
)
404 LOCAL_ARRAY(char, buf
, MAX_URL
);
405 LOCAL_ARRAY(char, portbuf
, 32);
406 LOCAL_ARRAY(char, loginbuf
, MAX_LOGIN_SZ
+ 1);
408 if (request
->protocol
== PROTO_URN
) {
409 snprintf(buf
, MAX_URL
, "urn:%s", strBuf(request
->urlpath
));
411 switch (request
->method
) {
413 snprintf(buf
, MAX_URL
, "%s:%d", request
->host
, request
->port
);
417 if (request
->port
!= urlDefaultPort(request
->protocol
))
418 snprintf(portbuf
, 32, ":%d", request
->port
);
420 if ((int) strlen(request
->login
) > 0) {
421 strcpy(loginbuf
, request
->login
);
422 if ((t
= strchr(loginbuf
, ':')))
424 strcat(loginbuf
, "@");
426 snprintf(buf
, MAX_URL
, "%s://%s%s%s%s",
427 ProtocolStr
[request
->protocol
],
431 strBuf(request
->urlpath
));
433 * strip arguments AFTER a question-mark
435 if (Config
.onoff
.strip_query_terms
)
436 if ((t
= strchr(buf
, '?')))
441 if (stringHasCntl(buf
))
442 xstrncpy(buf
, rfc1738_escape_unescaped(buf
), MAX_URL
);
447 * matchDomainName() compares a hostname with a domainname according
448 * to the following rules:
451 * ------------- ------------- ------
452 * foo.com foo.com YES
453 * .foo.com foo.com YES
454 * x.foo.com foo.com NO
455 * foo.com .foo.com YES
456 * .foo.com .foo.com YES
457 * x.foo.com .foo.com YES
459 * We strip leading dots on hosts (but not domains!) so that
460 * ".foo.com" is is always the same as "foo.com".
463 * 0 means the host matches the domain
464 * 1 means the host is greater than the domain
465 * -1 means the host is less than the domain
469 matchDomainName(const char *h
, const char *d
)
478 * Start at the ends of the two strings and work towards the
481 while (xtolower(h
[--hl
]) == xtolower(d
[--dl
])) {
482 if (hl
== 0 && dl
== 0) {
484 * We made it all the way to the beginning of both
485 * strings without finding any difference.
491 * The host string is shorter than the domain string.
492 * There is only one case when this can be a match.
493 * If the domain is just one character longer, and if
494 * that character is a leading '.' then we call it a
497 if (1 == dl
&& '.' == d
[0])
504 * The domain string is shorter than the host string.
505 * This is a match only if the first domain character
515 * We found different characters in the same position (from the end).
518 * If one of those character is '.' then its special. In order
519 * for splay tree sorting to work properly, "x-foo.com" must
520 * be greater than ".foo.com" even though '-' is less than '.'.
526 return (xtolower(h
[hl
]) - xtolower(d
[dl
]));
530 urlCheckRequest(const request_t
* r
)
533 /* protocol "independent" methods */
534 if (r
->method
== METHOD_CONNECT
)
536 if (r
->method
== METHOD_TRACE
)
538 if (r
->method
== METHOD_PURGE
)
540 /* does method match the protocol? */
541 switch (r
->protocol
) {
548 if (r
->method
== METHOD_PUT
)
553 if (r
->method
== METHOD_GET
)
555 else if (r
->method
== METHOD_HEAD
)
564 * Squid can't originate an SSL connection, so it should
565 * never receive an "https:" URL. It should always be
577 * Quick-n-dirty host extraction from a URL. Steps:
579 * Skip any '/' after the colon
580 * Copy the next SQUID_MAXHOSTNAMELEN bytes to host[]
581 * Look for an ending '/' or ':' and terminate
582 * Look for login info preceeded by '@'
585 urlHostname(const char *url
)
587 LOCAL_ARRAY(char, host
, SQUIDHOSTNAMELEN
);
590 if (NULL
== (t
= strchr(url
, ':')))
593 while (*t
!= '\0' && *t
== '/')
595 xstrncpy(host
, t
, SQUIDHOSTNAMELEN
);
596 if ((t
= strchr(host
, '/')))
598 if ((t
= strchr(host
, ':')))
600 if ((t
= strrchr(host
, '@'))) {
602 xmemmove(host
, t
, strlen(t
) + 1);
608 urlExtMethodAdd(const char *mstr
)
611 for (method
++; method
< METHOD_ENUM_END
; method
++) {
612 if (0 == strcmp(mstr
, RequestMethodStr
[method
])) {
613 debug(23, 2) ("Extension method '%s' already exists\n", mstr
);
616 if (0 != strncmp("%EXT", RequestMethodStr
[method
], 4))
618 /* Don't free statically allocated "%EXTnn" string */
619 RequestMethodStr
[method
] = xstrdup(mstr
);
620 debug(23, 1) ("Extension method '%s' added, enum=%d\n", mstr
, (int) method
);
623 debug(23, 1) ("WARNING: Could not add new extension method '%s' due to lack of array space\n", mstr
);
627 urlExtMethodConfigure(void)
629 wordlist
*w
= Config
.ext_methods
;
632 for (s
= w
->key
; *s
; s
++)
634 urlExtMethodAdd(w
->key
);