2 Copyright (C) 1995, 1996, 1997, 2000, 2001, 2003, 2003
3 Free Software Foundation, Inc.
5 This file is part of GNU Wget.
7 GNU Wget is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or (at
10 your option) any later version.
12 GNU Wget is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Wget; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 In addition, as a special exception, the Free Software Foundation
22 gives permission to link the code of its release of Wget with the
23 OpenSSL project's "OpenSSL" library (or with modified versions of it
24 that use the same license as the "OpenSSL" library), and distribute
25 the linked executables. You must obey the GNU General Public License
26 in all respects for all of the code used other than "OpenSSL". If you
27 modify this file, you may extend this exception to your version of the
28 file, but you are not obligated to do so. If you do not wish to do
29 so, delete this exception statement from your version. */
40 #include <sys/types.h>
64 /* Supported schemes: */
65 static struct scheme_data supported_schemes[] =
67 { "http://", DEFAULT_HTTP_PORT, 1 },
69 { "https://", DEFAULT_HTTPS_PORT, 1 },
71 { "ftp://", DEFAULT_FTP_PORT, 1 },
77 /* Forward declarations: */
79 static int path_simplify PARAMS ((char *));
81 /* Support for encoding and decoding of URL strings. We determine
82 whether a character is unsafe through static table lookup. This
83 code assumes ASCII character set and 8-bit chars. */
86 /* rfc1738 reserved chars, preserved from encoding. */
89 /* rfc1738 unsafe chars, plus some more. */
93 #define urlchr_test(c, mask) (urlchr_table[(unsigned char)(c)] & (mask))
94 #define URL_RESERVED_CHAR(c) urlchr_test(c, urlchr_reserved)
95 #define URL_UNSAFE_CHAR(c) urlchr_test(c, urlchr_unsafe)
97 /* Shorthands for the table: */
98 #define R urlchr_reserved
99 #define U urlchr_unsafe
102 const static unsigned char urlchr_table[256] =
104 U, U, U, U, U, U, U, U, /* NUL SOH STX ETX EOT ENQ ACK BEL */
105 U, U, U, U, U, U, U, U, /* BS HT LF VT FF CR SO SI */
106 U, U, U, U, U, U, U, U, /* DLE DC1 DC2 DC3 DC4 NAK SYN ETB */
107 U, U, U, U, U, U, U, U, /* CAN EM SUB ESC FS GS RS US */
108 U, 0, U, RU, 0, U, R, 0, /* SP ! " # $ % & ' */
109 0, 0, 0, R, 0, 0, 0, R, /* ( ) * + , - . / */
110 0, 0, 0, 0, 0, 0, 0, 0, /* 0 1 2 3 4 5 6 7 */
111 0, 0, RU, R, U, R, U, R, /* 8 9 : ; < = > ? */
112 RU, 0, 0, 0, 0, 0, 0, 0, /* @ A B C D E F G */
113 0, 0, 0, 0, 0, 0, 0, 0, /* H I J K L M N O */
114 0, 0, 0, 0, 0, 0, 0, 0, /* P Q R S T U V W */
115 0, 0, 0, RU, U, RU, U, 0, /* X Y Z [ \ ] ^ _ */
116 U, 0, 0, 0, 0, 0, 0, 0, /* ` a b c d e f g */
117 0, 0, 0, 0, 0, 0, 0, 0, /* h i j k l m n o */
118 0, 0, 0, 0, 0, 0, 0, 0, /* p q r s t u v w */
119 0, 0, 0, U, U, U, U, U, /* x y z { | } ~ DEL */
121 U, U, U, U, U, U, U, U, U, U, U, U, U, U, U, U,
122 U, U, U, U, U, U, U, U, U, U, U, U, U, U, U, U,
123 U, U, U, U, U, U, U, U, U, U, U, U, U, U, U, U,
124 U, U, U, U, U, U, U, U, U, U, U, U, U, U, U, U,
126 U, U, U, U, U, U, U, U, U, U, U, U, U, U, U, U,
127 U, U, U, U, U, U, U, U, U, U, U, U, U, U, U, U,
128 U, U, U, U, U, U, U, U, U, U, U, U, U, U, U, U,
129 U, U, U, U, U, U, U, U, U, U, U, U, U, U, U, U,
135 /* URL-unescape the string S.
137 This is done by transforming the sequences "%HH" to the character
138 represented by the hexadecimal digits HH. If % is not followed by
139 two hexadecimal digits, it is inserted literally.
141 The transformation is done in place. If you need the original
142 string intact, make a copy before calling this function. */
145 url_unescape (char *s)
147 char *t = s; /* t - tortoise */
148 char *h = s; /* h - hare */
159 /* Do nothing if '%' is not followed by two hex digits. */
160 if (!h[1] || !h[2] || !(ISXDIGIT (h[1]) && ISXDIGIT (h[2])))
162 *t = X2DIGITS_TO_NUM (h[1], h[2]);
169 /* The core of url_escape_* functions. Escapes the characters that
170 match the provided mask in urlchr_table.
172 If ALLOW_PASSTHROUGH is non-zero, a string with no unsafe chars
173 will be returned unchanged. If ALLOW_PASSTHROUGH is zero, a
174 freshly allocated string will be returned in all cases. */
177 url_escape_1 (const char *s, unsigned char mask, int allow_passthrough)
184 for (p1 = s; *p1; p1++)
185 if (urlchr_test (*p1, mask))
186 addition += 2; /* Two more characters (hex digits) */
189 return allow_passthrough ? (char *)s : xstrdup (s);
191 newlen = (p1 - s) + addition;
192 newstr = (char *)xmalloc (newlen + 1);
198 /* Quote the characters that match the test mask. */
199 if (urlchr_test (*p1, mask))
201 unsigned char c = *p1++;
203 *p2++ = XNUM_TO_digit (c >> 4);
204 *p2++ = XNUM_TO_digit (c & 0xf);
209 assert (p2 - newstr == newlen);
215 /* URL-escape the unsafe characters (see urlchr_table) in a given
216 string, returning a freshly allocated string. */
219 url_escape (const char *s)
221 return url_escape_1 (s, urlchr_unsafe, 0);
224 /* URL-escape the unsafe characters (see urlchr_table) in a given
225 string. If no characters are unsafe, S is returned. */
228 url_escape_allow_passthrough (const char *s)
230 return url_escape_1 (s, urlchr_unsafe, 1);
233 enum copy_method { CM_DECODE, CM_ENCODE, CM_PASSTHROUGH };
235 /* Decide whether to encode, decode, or pass through the char at P.
236 This used to be a macro, but it got a little too convoluted. */
237 static inline enum copy_method
238 decide_copy_method (const char *p)
242 if (ISXDIGIT (*(p + 1)) && ISXDIGIT (*(p + 2)))
244 /* %xx sequence: decode it, unless it would decode to an
245 unsafe or a reserved char; in that case, leave it as
247 char preempt = X2DIGITS_TO_NUM (*(p + 1), *(p + 2));
248 if (URL_UNSAFE_CHAR (preempt) || URL_RESERVED_CHAR (preempt))
249 return CM_PASSTHROUGH;
254 /* Garbled %.. sequence: encode `%'. */
257 else if (URL_UNSAFE_CHAR (*p) && !URL_RESERVED_CHAR (*p))
260 return CM_PASSTHROUGH;
263 /* Translate a %-escaped (but possibly non-conformant) input string S
264 into a %-escaped (and conformant) output string. If no characters
265 are encoded or decoded, return the same string S; otherwise, return
266 a freshly allocated string with the new contents.
268 After a URL has been run through this function, the protocols that
269 use `%' as the quote character can use the resulting string as-is,
270 while those that don't call url_unescape() to get to the intended
271 data. This function is also stable: after an input string is
272 transformed the first time, all further transformations of the
273 result yield the same result string.
275 Let's discuss why this function is needed.
277 Imagine Wget is to retrieve `http://abc.xyz/abc def'. Since a raw
278 space character would mess up the HTTP request, it needs to be
281 GET /abc%20def HTTP/1.0
283 It appears that the unsafe chars need to be quoted, for example
284 with url_escape. But what if we're requested to download
285 `abc%20def'? url_escape transforms "%" to "%25", which would leave
286 us with `abc%2520def'. This is incorrect -- since %-escapes are
287 part of URL syntax, "%20" is the correct way to denote a literal
288 space on the Wget command line. This leaves us in the conclusion
289 that in that case Wget should not call url_escape, but leave the
292 And what if the requested URI is `abc%20 def'? If we call
293 url_escape, we end up with `/abc%2520%20def', which is almost
294 certainly not intended. If we don't call url_escape, we are left
295 with the embedded space and cannot complete the request. What the
296 user meant was for Wget to request `/abc%20%20def', and this is
297 where reencode_escapes kicks in.
299 Wget used to solve this by first decoding %-quotes, and then
300 encoding all the "unsafe" characters found in the resulting string.
301 This was wrong because it didn't preserve certain URL special
302 (reserved) characters. For instance, URI containing "a%2B+b" (0x2b
303 == '+') would get translated to "a%2B%2Bb" or "a++b" depending on
304 whether we considered `+' reserved (it is). One of these results
305 is inevitable because by the second step we would lose information
306 on whether the `+' was originally encoded or not. Both results
307 were wrong because in CGI parameters + means space, while %2B means
308 literal plus. reencode_escapes correctly translates the above to
309 "a%2B+b", i.e. returns the original string.
311 This function uses an algorithm proposed by Anon Sricharoenchai:
313 1. Encode all URL_UNSAFE and the "%" that are not followed by 2
316 2. Decode all "%XX" except URL_UNSAFE, URL_RESERVED (";/?:@=&") and
319 ...except that this code conflates the two steps, and decides
320 whether to encode, decode, or pass through each character in turn.
321 The function still uses two passes, but their logic is the same --
322 the first pass exists merely for the sake of allocation. Another
323 small difference is that we include `+' to URL_RESERVED.
327 "http://abc.xyz/%20%3F%%36%31%25aa% a?a=%61+a%2Ba&b=b%26c%3Dc"
329 "http://abc.xyz/%20%3F%2561%25aa%25%20a?a=a+a%2Ba&b=b%26c%3Dc"
333 "foo bar" -> "foo%20bar"
334 "foo%20bar" -> "foo%20bar"
335 "foo %20bar" -> "foo%20%20bar"
336 "foo%%20bar" -> "foo%25%20bar" (0x25 == '%')
337 "foo%25%20bar" -> "foo%25%20bar"
338 "foo%2%20bar" -> "foo%252%20bar"
339 "foo+bar" -> "foo+bar" (plus is reserved!)
340 "foo%2b+bar" -> "foo%2b+bar" */
343 reencode_escapes (const char *s)
349 int encode_count = 0;
350 int decode_count = 0;
352 /* First, pass through the string to see if there's anything to do,
353 and to calculate the new length. */
354 for (p1 = s; *p1; p1++)
356 switch (decide_copy_method (p1))
369 if (!encode_count && !decode_count)
370 /* The string is good as it is. */
371 return (char *)s; /* C const model sucks. */
374 /* Each encoding adds two characters (hex digits), while each
375 decoding removes two characters. */
376 newlen = oldlen + 2 * (encode_count - decode_count);
377 newstr = xmalloc (newlen + 1);
384 switch (decide_copy_method (p1))
388 unsigned char c = *p1++;
390 *p2++ = XNUM_TO_DIGIT (c >> 4);
391 *p2++ = XNUM_TO_DIGIT (c & 0xf);
395 *p2++ = X2DIGITS_TO_NUM (p1[1], p1[2]);
396 p1 += 3; /* skip %xx */
403 assert (p2 - newstr == newlen);
407 /* Returns the scheme type if the scheme is supported, or
408 SCHEME_INVALID if not. */
411 url_scheme (const char *url)
415 for (i = 0; supported_schemes[i].leading_string; i++)
416 if (0 == strncasecmp (url, supported_schemes[i].leading_string,
417 strlen (supported_schemes[i].leading_string)))
419 if (supported_schemes[i].enabled)
420 return (enum url_scheme) i;
422 return SCHEME_INVALID;
425 return SCHEME_INVALID;
428 #define SCHEME_CHAR(ch) (ISALNUM (ch) || (ch) == '-' || (ch) == '+')
430 /* Return 1 if the URL begins with any "scheme", 0 otherwise. As
431 currently implemented, it returns true if URL begins with
435 url_has_scheme (const char *url)
439 /* The first char must be a scheme char. */
440 if (!*p || !SCHEME_CHAR (*p))
443 /* Followed by 0 or more scheme chars. */
444 while (*p && SCHEME_CHAR (*p))
446 /* Terminated by ':'. */
451 scheme_default_port (enum url_scheme scheme)
453 return supported_schemes[scheme].default_port;
457 scheme_disable (enum url_scheme scheme)
459 supported_schemes[scheme].enabled = 0;
462 /* Skip the username and password, if present here. The function
463 should *not* be called with the complete URL, but with the part
464 right after the scheme.
466 If no username and password are found, return 0. */
469 url_skip_credentials (const char *url)
471 /* Look for '@' that comes before terminators, such as '/', '?',
473 const char *p = (const char *)strpbrk (url, "@/?#;");
479 /* Parse credentials contained in [BEG, END). The region is expected
480 to have come from a URL and is unescaped. */
483 parse_credentials (const char *beg, const char *end, char **user, char **passwd)
489 return 0; /* empty user name */
491 colon = memchr (beg, ':', end - beg);
493 return 0; /* again empty user name */
497 *passwd = strdupdelim (colon + 1, end);
499 url_unescape (*passwd);
506 *user = strdupdelim (beg, userend);
507 url_unescape (*user);
511 /* Used by main.c: detect URLs written using the "shorthand" URL forms
512 popularized by Netscape and NcFTP. HTTP shorthands look like this:
514 www.foo.com[:port]/dir/file -> http://www.foo.com[:port]/dir/file
515 www.foo.com[:port] -> http://www.foo.com[:port]
517 FTP shorthands look like this:
519 foo.bar.com:dir/file -> ftp://foo.bar.com/dir/file
520 foo.bar.com:/absdir/file -> ftp://foo.bar.com//absdir/file
522 If the URL needs not or cannot be rewritten, return NULL. */
525 rewrite_shorthand_url (const char *url)
529 if (url_has_scheme (url))
532 /* Look for a ':' or '/'. The former signifies NcFTP syntax, the
534 for (p = url; *p && *p != ':' && *p != '/'; p++)
544 /* If the characters after the colon and before the next slash
545 or end of string are all digits, it's HTTP. */
547 for (pp = p + 1; ISDIGIT (*pp); pp++)
549 if (digits > 0 && (*pp == '/' || *pp == '\0'))
552 /* Prepend "ftp://" to the entire URL... */
553 res = xmalloc (6 + strlen (url) + 1);
554 sprintf (res, "ftp://%s", url);
555 /* ...and replace ':' with '/'. */
556 res[6 + (p - url)] = '/';
563 /* Just prepend "http://" to what we have. */
564 res = xmalloc (7 + strlen (url) + 1);
565 sprintf (res, "http://%s", url);
570 static void split_path PARAMS ((const char *, char **, char **));
572 /* Like strpbrk, with the exception that it returns the pointer to the
573 terminating zero (end-of-string aka "eos") if no matching character
576 Although I normally balk at Gcc-specific optimizations, it probably
577 makes sense here: glibc has optimizations that detect strpbrk being
578 called with literal string as ACCEPT and inline the search. That
579 optimization is defeated if strpbrk is hidden within the call to
580 another function. (And no, making strpbrk_or_eos inline doesn't
581 help because the check for literal accept is in the
586 #define strpbrk_or_eos(s, accept) ({ \
587 char *SOE_p = strpbrk (s, accept); \
589 SOE_p = (char *)s + strlen (s); \
593 #else /* not __GNUC__ */
596 strpbrk_or_eos (const char *s, const char *accept)
598 char *p = strpbrk (s, accept);
600 p = (char *)s + strlen (s);
605 /* Turn STR into lowercase; return non-zero if a character was
609 lowercase_str (char *str)
616 *str = TOLOWER (*str);
621 static char *parse_errors[] = {
622 #define PE_NO_ERROR 0
624 #define PE_UNSUPPORTED_SCHEME 1
625 "Unsupported scheme",
626 #define PE_EMPTY_HOST 2
628 #define PE_BAD_PORT_NUMBER 3
630 #define PE_INVALID_USER_NAME 4
632 #define PE_UNTERMINATED_IPV6_ADDRESS 5
633 "Unterminated IPv6 numeric address",
634 #define PE_IPV6_NOT_SUPPORTED 6
635 "IPv6 addresses not supported",
636 #define PE_INVALID_IPV6_ADDRESS 7
637 "Invalid IPv6 numeric address"
640 #define SETERR(p, v) do { \
646 /* The following two functions were adapted from glibc. */
649 is_valid_ipv4_address (const char *str, const char *end)
651 int saw_digit, octets;
661 if (ch >= '0' && ch <= '9') {
662 val = val * 10 + (ch - '0');
666 if (saw_digit == 0) {
671 } else if (ch == '.' && saw_digit == 1) {
685 static const int NS_INADDRSZ = 4;
686 static const int NS_IN6ADDRSZ = 16;
687 static const int NS_INT16SZ = 2;
690 is_valid_ipv6_address (const char *str, const char *end)
692 static const char xdigits[] = "0123456789abcdef";
705 /* Leading :: requires some special handling. */
709 if (str == end || *str != ':')
721 /* if ch is a number, add it to val. */
722 pch = strchr(xdigits, ch);
725 val |= (pch - xdigits);
732 /* if ch is a colon ... */
735 if (saw_xdigit == 0) {
740 } else if (str == end) {
743 if (tp > NS_IN6ADDRSZ - NS_INT16SZ)
751 /* if ch is a dot ... */
752 if (ch == '.' && (tp <= NS_IN6ADDRSZ - NS_INADDRSZ) &&
753 is_valid_ipv4_address(curtok, end) == 1) {
762 if (saw_xdigit == 1) {
763 if (tp > NS_IN6ADDRSZ - NS_INT16SZ)
768 if (colonp != NULL) {
769 if (tp == NS_IN6ADDRSZ)
774 if (tp != NS_IN6ADDRSZ)
783 Return a new struct url if successful, NULL on error. In case of
784 error, and if ERROR is not NULL, also set *ERROR to the appropriate
787 url_parse (const char *url, int *error)
791 int path_modified, host_modified;
793 enum url_scheme scheme;
795 const char *uname_b, *uname_e;
796 const char *host_b, *host_e;
797 const char *path_b, *path_e;
798 const char *params_b, *params_e;
799 const char *query_b, *query_e;
800 const char *fragment_b, *fragment_e;
803 char *user = NULL, *passwd = NULL;
807 scheme = url_scheme (url);
808 if (scheme == SCHEME_INVALID)
810 SETERR (error, PE_UNSUPPORTED_SCHEME);
814 url_encoded = reencode_escapes (url);
817 p += strlen (supported_schemes[scheme].leading_string);
819 p += url_skip_credentials (p);
822 /* scheme://user:pass@host[:port]... */
825 /* We attempt to break down the URL into the components path,
826 params, query, and fragment. They are ordered like this:
828 scheme://host[:port][/path][;params][?query][#fragment] */
830 params_b = params_e = NULL;
831 query_b = query_e = NULL;
832 fragment_b = fragment_e = NULL;
838 /* Handle IPv6 address inside square brackets. Ideally we'd
839 just look for the terminating ']', but rfc2732 mandates
840 rejecting invalid IPv6 addresses. */
842 /* The address begins after '['. */
844 host_e = strchr (host_b, ']');
848 SETERR (error, PE_UNTERMINATED_IPV6_ADDRESS);
853 /* Check if the IPv6 address is valid. */
854 if (!is_valid_ipv6_address(host_b, host_e))
856 SETERR (error, PE_INVALID_IPV6_ADDRESS);
860 /* Continue parsing after the closing ']'. */
863 SETERR (error, PE_IPV6_NOT_SUPPORTED);
869 p = strpbrk_or_eos (p, ":/;?#");
873 if (host_b == host_e)
875 SETERR (error, PE_EMPTY_HOST);
879 port = scheme_default_port (scheme);
882 const char *port_b, *port_e, *pp;
884 /* scheme://host:port/tralala */
888 p = strpbrk_or_eos (p, "/;?#");
891 if (port_b == port_e)
893 /* http://host:/whatever */
895 SETERR (error, PE_BAD_PORT_NUMBER);
899 for (port = 0, pp = port_b; pp < port_e; pp++)
903 /* http://host:12randomgarbage/blah */
905 SETERR (error, PE_BAD_PORT_NUMBER);
909 port = 10 * port + (*pp - '0');
917 p = strpbrk_or_eos (p, ";?#");
922 /* Path is not allowed not to exist. */
930 p = strpbrk_or_eos (p, "?#");
937 p = strpbrk_or_eos (p, "#");
940 /* Hack that allows users to use '?' (a wildcard character) in
941 FTP URLs without it being interpreted as a query string
943 if (scheme == SCHEME_FTP)
945 query_b = query_e = NULL;
958 if (uname_b != uname_e)
960 /* http://user:pass@host */
962 /* uname_b uname_e */
963 if (!parse_credentials (uname_b, uname_e - 1, &user, &passwd))
965 SETERR (error, PE_INVALID_USER_NAME);
970 u = (struct url *)xmalloc (sizeof (struct url));
971 memset (u, 0, sizeof (*u));
974 u->host = strdupdelim (host_b, host_e);
979 u->path = strdupdelim (path_b, path_e);
980 path_modified = path_simplify (u->path);
981 split_path (u->path, &u->dir, &u->file);
983 host_modified = lowercase_str (u->host);
986 u->params = strdupdelim (params_b, params_e);
988 u->query = strdupdelim (query_b, query_e);
990 u->fragment = strdupdelim (fragment_b, fragment_e);
992 if (path_modified || u->fragment || host_modified || path_b == path_e)
994 /* If we suspect that a transformation has rendered what
995 url_string might return different from URL_ENCODED, rebuild
996 u->url using url_string. */
997 u->url = url_string (u, 0);
999 if (url_encoded != url)
1000 xfree ((char *) url_encoded);
1004 if (url_encoded == url)
1005 u->url = xstrdup (url);
1007 u->url = url_encoded;
1015 url_error (int error_code)
1017 assert (error_code >= 0 && error_code < countof (parse_errors));
1018 return parse_errors[error_code];
1021 /* Split PATH into DIR and FILE. PATH comes from the URL and is
1022 expected to be URL-escaped.
1024 The path is split into directory (the part up to the last slash)
1025 and file (the part after the last slash), which are subsequently
1026 unescaped. Examples:
1029 "foo/bar/baz" "foo/bar" "baz"
1030 "foo/bar/" "foo/bar" ""
1032 "foo/bar/baz%2fqux" "foo/bar" "baz/qux" (!)
1034 DIR and FILE are freshly allocated. */
1037 split_path (const char *path, char **dir, char **file)
1039 char *last_slash = strrchr (path, '/');
1042 *dir = xstrdup ("");
1043 *file = xstrdup (path);
1047 *dir = strdupdelim (path, last_slash);
1048 *file = xstrdup (last_slash + 1);
1050 url_unescape (*dir);
1051 url_unescape (*file);
1054 /* Note: URL's "full path" is the path with the query string and
1055 params appended. The "fragment" (#foo) is intentionally ignored,
1056 but that might be changed. For example, if the original URL was
1057 "http://host:port/foo/bar/baz;bullshit?querystring#uselessfragment",
1058 the full path will be "/foo/bar/baz;bullshit?querystring". */
1060 /* Return the length of the full path, without the terminating
1064 full_path_length (const struct url *url)
1068 #define FROB(el) if (url->el) len += 1 + strlen (url->el)
1079 /* Write out the full path. */
1082 full_path_write (const struct url *url, char *where)
1084 #define FROB(el, chr) do { \
1085 char *f_el = url->el; \
1087 int l = strlen (f_el); \
1089 memcpy (where, f_el, l); \
1101 /* Public function for getting the "full path". E.g. if u->path is
1102 "foo/bar" and u->query is "param=value", full_path will be
1103 "/foo/bar?param=value". */
1106 url_full_path (const struct url *url)
1108 int length = full_path_length (url);
1109 char *full_path = (char *)xmalloc(length + 1);
1111 full_path_write (url, full_path);
1112 full_path[length] = '\0';
1117 /* Escape unsafe and reserved characters, except for the slash
1121 url_escape_dir (const char *dir)
1123 char *newdir = url_escape_1 (dir, urlchr_unsafe | urlchr_reserved, 1);
1128 /* Unescape slashes in NEWDIR. */
1130 h = newdir; /* hare */
1131 t = newdir; /* tortoise */
1133 for (; *h; h++, t++)
1135 if (*h == '%' && h[1] == '2' && h[2] == 'F')
1148 /* Sync u->path and u->url with u->dir and u->file. Called after
1149 u->file or u->dir have been changed, typically by the FTP code. */
1152 sync_path (struct url *u)
1154 char *newpath, *efile, *edir;
1158 /* u->dir and u->file are not escaped. URL-escape them before
1159 reassembling them into u->path. That way, if they contain
1160 separators like '?' or even if u->file contains slashes, the
1161 path will be correctly assembled. (u->file can contain slashes
1162 if the URL specifies it with %2f, or if an FTP server returns
1164 edir = url_escape_dir (u->dir);
1165 efile = url_escape_1 (u->file, urlchr_unsafe | urlchr_reserved, 1);
1168 newpath = xstrdup (efile);
1171 int dirlen = strlen (edir);
1172 int filelen = strlen (efile);
1174 /* Copy "DIR/FILE" to newpath. */
1175 char *p = newpath = xmalloc (dirlen + 1 + filelen + 1);
1176 memcpy (p, edir, dirlen);
1179 memcpy (p, efile, filelen);
1188 if (efile != u->file)
1191 /* Regenerate u->url as well. */
1193 u->url = url_string (u, 0);
1196 /* Mutators. Code in ftp.c insists on changing u->dir and u->file.
1197 This way we can sync u->path and u->url when they get changed. */
1200 url_set_dir (struct url *url, const char *newdir)
1203 url->dir = xstrdup (newdir);
1208 url_set_file (struct url *url, const char *newfile)
1211 url->file = xstrdup (newfile);
1216 url_free (struct url *url)
1222 FREE_MAYBE (url->params);
1223 FREE_MAYBE (url->query);
1224 FREE_MAYBE (url->fragment);
1225 FREE_MAYBE (url->user);
1226 FREE_MAYBE (url->passwd);
1234 /* Create all the necessary directories for PATH (a file). Calls
1235 mkdirhier() internally. */
1237 mkalldirs (const char *path)
1244 p = path + strlen (path);
1245 for (; *p != '/' && p != path; p--)
1248 /* Don't create if it's just a file. */
1249 if ((p == path) && (*p != '/'))
1251 t = strdupdelim (path, p);
1253 /* Check whether the directory exists. */
1254 if ((stat (t, &st) == 0))
1256 if (S_ISDIR (st.st_mode))
1263 /* If the dir exists as a file name, remove it first. This
1264 is *only* for Wget to work with buggy old CERN http
1265 servers. Here is the scenario: When Wget tries to
1266 retrieve a directory without a slash, e.g.
1267 http://foo/bar (bar being a directory), CERN server will
1268 not redirect it too http://foo/bar/ -- it will generate a
1269 directory listing containing links to bar/file1,
1270 bar/file2, etc. Wget will lose because it saves this
1271 HTML listing to a file `bar', so it cannot create the
1272 directory. To work around this, if the file of the same
1273 name exists, we just remove it and create the directory
1275 DEBUGP (("Removing %s because of directory danger!\n", t));
1279 res = make_directory (t);
1281 logprintf (LOG_NOTQUIET, "%s: %s", t, strerror (errno));
1286 /* Functions for constructing the file name out of URL components. */
1288 /* A growable string structure, used by url_file_name and friends.
1289 This should perhaps be moved to utils.c.
1291 The idea is to have a convenient and efficient way to construct a
1292 string by having various functions append data to it. Instead of
1293 passing the obligatory BASEVAR, SIZEVAR and TAILPOS to all the
1294 functions in questions, we pass the pointer to this struct. */
1302 /* Ensure that the string can accept APPEND_COUNT more characters past
1303 the current TAIL position. If necessary, this will grow the string
1304 and update its allocated size. If the string is already large
1305 enough to take TAIL+APPEND_COUNT characters, this does nothing. */
1306 #define GROW(g, append_size) do { \
1307 struct growable *G_ = g; \
1308 DO_REALLOC (G_->base, G_->size, G_->tail + append_size, char); \
1311 /* Return the tail position of the string. */
1312 #define TAIL(r) ((r)->base + (r)->tail)
1314 /* Move the tail position by APPEND_COUNT characters. */
1315 #define TAIL_INCR(r, append_count) ((r)->tail += append_count)
1317 /* Append the string STR to DEST. NOTICE: the string in DEST is not
1321 append_string (const char *str, struct growable *dest)
1323 int l = strlen (str);
1325 memcpy (TAIL (dest), str, l);
1326 TAIL_INCR (dest, l);
1329 /* Append CH to DEST. For example, append_char (0, DEST)
1330 zero-terminates DEST. */
1333 append_char (char ch, struct growable *dest)
1337 TAIL_INCR (dest, 1);
1341 filechr_not_unix = 1, /* unusable on Unix, / and \0 */
1342 filechr_not_windows = 2, /* unusable on Windows, one of \|/<>?:*" */
1343 filechr_control = 4, /* a control character, e.g. 0-31 */
1346 #define FILE_CHAR_TEST(c, mask) (filechr_table[(unsigned char)(c)] & (mask))
1348 /* Shorthands for the table: */
1349 #define U filechr_not_unix
1350 #define W filechr_not_windows
1351 #define C filechr_control
1356 /* Table of characters unsafe under various conditions (see above).
1358 Arguably we could also claim `%' to be unsafe, since we use it as
1359 the escape character. If we ever want to be able to reliably
1360 translate file name back to URL, this would become important
1361 crucial. Right now, it's better to be minimal in escaping. */
1363 const static unsigned char filechr_table[256] =
1365 UWC, C, C, C, C, C, C, C, /* NUL SOH STX ETX EOT ENQ ACK BEL */
1366 C, C, C, C, C, C, C, C, /* BS HT LF VT FF CR SO SI */
1367 C, C, C, C, C, C, C, C, /* DLE DC1 DC2 DC3 DC4 NAK SYN ETB */
1368 C, C, C, C, C, C, C, C, /* CAN EM SUB ESC FS GS RS US */
1369 0, 0, W, 0, 0, 0, 0, 0, /* SP ! " # $ % & ' */
1370 0, 0, W, 0, 0, 0, 0, UW, /* ( ) * + , - . / */
1371 0, 0, 0, 0, 0, 0, 0, 0, /* 0 1 2 3 4 5 6 7 */
1372 0, 0, W, 0, W, 0, W, W, /* 8 9 : ; < = > ? */
1373 0, 0, 0, 0, 0, 0, 0, 0, /* @ A B C D E F G */
1374 0, 0, 0, 0, 0, 0, 0, 0, /* H I J K L M N O */
1375 0, 0, 0, 0, 0, 0, 0, 0, /* P Q R S T U V W */
1376 0, 0, 0, 0, W, 0, 0, 0, /* X Y Z [ \ ] ^ _ */
1377 0, 0, 0, 0, 0, 0, 0, 0, /* ` a b c d e f g */
1378 0, 0, 0, 0, 0, 0, 0, 0, /* h i j k l m n o */
1379 0, 0, 0, 0, 0, 0, 0, 0, /* p q r s t u v w */
1380 0, 0, 0, 0, 0, 0, 0, 0, /* x y z { | } ~ DEL */
1382 C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, /* 128-143 */
1383 C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, /* 144-159 */
1384 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1385 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1387 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1388 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1389 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1390 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1398 /* FN_PORT_SEP is the separator between host and port in file names
1399 for non-standard port numbers. On Unix this is normally ':', as in
1400 "www.xemacs.org:4001/index.html". Under Windows, we set it to +
1401 because Windows can't handle ':' in file names. */
1402 #define FN_PORT_SEP (opt.restrict_files_os != restrict_windows ? ':' : '+')
1404 /* FN_QUERY_SEP is the separator between the file name and the URL
1405 query, normally '?'. Since Windows cannot handle '?' as part of
1406 file name, we use '@' instead there. */
1407 #define FN_QUERY_SEP (opt.restrict_files_os != restrict_windows ? '?' : '@')
1409 /* Quote path element, characters in [b, e), as file name, and append
1410 the quoted string to DEST. Each character is quoted as per
1411 file_unsafe_char and the corresponding table. */
1414 append_uri_pathel (const char *b, const char *e, struct growable *dest)
1423 if (opt.restrict_files_os == restrict_unix)
1424 mask = filechr_not_unix;
1426 mask = filechr_not_windows;
1427 if (opt.restrict_files_ctrl)
1428 mask |= filechr_control;
1430 /* Copy [b, e) to PATHEL and URL-unescape it. */
1431 BOUNDED_TO_ALLOCA (b, e, pathel);
1432 url_unescape (pathel);
1433 pathlen = strlen (pathel);
1435 /* Go through PATHEL and check how many characters we'll need to
1436 add for file quoting. */
1438 for (p = pathel; *p; p++)
1439 if (FILE_CHAR_TEST (*p, mask))
1442 /* p - pathel is the string length. Each quoted char means two
1443 additional characters in the string, hence 2*quoted. */
1444 outlen = (p - pathel) + (2 * quoted);
1445 GROW (dest, outlen);
1449 /* If there's nothing to quote, we don't need to go through the
1450 string the second time. */
1451 memcpy (TAIL (dest), pathel, outlen);
1455 char *q = TAIL (dest);
1456 for (p = pathel; *p; p++)
1458 if (!FILE_CHAR_TEST (*p, mask))
1462 unsigned char ch = *p;
1464 *q++ = XNUM_TO_DIGIT (ch >> 4);
1465 *q++ = XNUM_TO_DIGIT (ch & 0xf);
1468 assert (q - TAIL (dest) == outlen);
1470 TAIL_INCR (dest, outlen);
1473 /* Append to DEST the directory structure that corresponds the
1474 directory part of URL's path. For example, if the URL is
1475 http://server/dir1/dir2/file, this appends "/dir1/dir2".
1477 Each path element ("dir1" and "dir2" in the above example) is
1478 examined, url-unescaped, and re-escaped as file name element.
1480 Additionally, it cuts as many directories from the path as
1481 specified by opt.cut_dirs. For example, if opt.cut_dirs is 1, it
1482 will produce "bar" for the above example. For 2 or more, it will
1485 Each component of the path is quoted for use as file name. */
1488 append_dir_structure (const struct url *u, struct growable *dest)
1490 char *pathel, *next;
1491 int cut = opt.cut_dirs;
1493 /* Go through the path components, de-URL-quote them, and quote them
1494 (if necessary) as file names. */
1497 for (; (next = strchr (pathel, '/')) != NULL; pathel = next + 1)
1502 /* Ignore empty pathels. path_simplify should remove
1503 occurrences of "//" from the path, but it has special cases
1504 for starting / which generates an empty pathel here. */
1508 append_char ('/', dest);
1509 append_uri_pathel (pathel, next, dest);
1513 /* Return a unique file name that matches the given URL as good as
1514 possible. Does not create directories on the file system. */
1517 url_file_name (const struct url *u)
1519 struct growable fnres;
1521 char *u_file, *u_query;
1522 char *fname, *unique;
1528 /* Start with the directory prefix, if specified. */
1530 append_string (opt.dir_prefix, &fnres);
1532 /* If "dirstruct" is turned on (typically the case with -r), add
1533 the host and port (unless those have been turned off) and
1534 directory structure. */
1537 if (opt.add_hostdir)
1540 append_char ('/', &fnres);
1541 append_string (u->host, &fnres);
1542 if (u->port != scheme_default_port (u->scheme))
1545 number_to_string (portstr, u->port);
1546 append_char (FN_PORT_SEP, &fnres);
1547 append_string (portstr, &fnres);
1551 append_dir_structure (u, &fnres);
1554 /* Add the file name. */
1556 append_char ('/', &fnres);
1557 u_file = *u->file ? u->file : "index.html";
1558 append_uri_pathel (u_file, u_file + strlen (u_file), &fnres);
1560 /* Append "?query" to the file name. */
1561 u_query = u->query && *u->query ? u->query : NULL;
1564 append_char (FN_QUERY_SEP, &fnres);
1565 append_uri_pathel (u_query, u_query + strlen (u_query), &fnres);
1568 /* Zero-terminate the file name. */
1569 append_char ('\0', &fnres);
1573 /* Check the cases in which the unique extensions are not used:
1574 1) Clobbering is turned off (-nc).
1575 2) Retrieval with regetting.
1576 3) Timestamping is used.
1577 4) Hierarchy is built.
1579 The exception is the case when file does exist and is a
1580 directory (see `mkalldirs' for explanation). */
1582 if ((opt.noclobber || opt.always_rest || opt.timestamping || opt.dirstruct)
1583 && !(file_exists_p (fname) && !file_non_directory_p (fname)))
1586 unique = unique_name (fname, 1);
1587 if (unique != fname)
1592 /* Return the length of URL's path. Path is considered to be
1593 terminated by one of '?', ';', '#', or by the end of the
1596 path_length (const char *url)
1598 const char *q = strpbrk_or_eos (url, "?;#");
1602 /* Find the last occurrence of character C in the range [b, e), or
1603 NULL, if none are present. This is equivalent to strrchr(b, c),
1604 except that it accepts an END argument instead of requiring the
1605 string to be zero-terminated. Why is there no memrchr()? */
1607 find_last_char (const char *b, const char *e, char c)
1615 /* Resolve "." and ".." elements of PATH by destructively modifying
1616 PATH. "." is resolved by removing that path element, and ".." is
1617 resolved by removing the preceding path element. Leading and
1618 trailing slashes are preserved.
1620 Return non-zero if any changes have been made.
1622 For example, "a/b/c/./../d/.." will yield "a/b/". More exhaustive
1623 test examples are provided below. If you change anything in this
1624 function, run test_path_simplify to make sure you haven't broken a
1627 A previous version of this function was based on path_simplify()
1628 from GNU Bash, but it has been rewritten for Wget 1.8.1. */
1631 path_simplify (char *path)
1637 ++path; /* preserve the leading '/'. */
1640 end = p + strlen (p) + 1; /* position past the terminating zero. */
1645 /* P should point to the beginning of a path element. */
1647 if (*p == '.' && (*(p + 1) == '/' || *(p + 1) == '\0'))
1649 /* Handle "./foo" by moving "foo" two characters to the
1651 if (*(p + 1) == '/')
1654 memmove (p, p + 2, end - (p + 2));
1665 else if (*p == '.' && *(p + 1) == '.'
1666 && (*(p + 2) == '/' || *(p + 2) == '\0'))
1668 /* Handle "../foo" by moving "foo" one path element to the
1670 char *b = p; /* not p-1 because P can equal PATH */
1672 /* Backtrack by one path element, but not past the beginning
1675 /* foo/bar/../baz */
1681 /* Move backwards until B hits the beginning of the
1682 previous path element or the beginning of path. */
1683 for (--b; b > path && *(b - 1) != '/'; b--)
1688 if (*(p + 2) == '/')
1690 memmove (b, p + 3, end - (p + 3));
1704 /* Remove empty path elements. Not mandated by rfc1808 et
1705 al, but it seems like a good idea to get rid of them.
1706 Supporting them properly is hard (in which directory do
1707 you save http://x.com///y.html?) and they don't seem to
1718 memmove (p, q, end - q);
1723 /* Skip to the next path element. */
1724 while (*p && *p != '/')
1729 /* Make sure P points to the beginning of the next path element,
1730 which is location after the slash. */
1737 /* Merge BASE with LINK and return the resulting URI.
1739 Either of the URIs may be absolute or relative, complete with the
1740 host name, or path only. This tries to reasonably handle all
1741 foreseeable cases. It only employs minimal URL parsing, without
1742 knowledge of the specifics of schemes.
1744 Perhaps this function should call path_simplify so that the callers
1745 don't have to call url_parse unconditionally. */
1748 uri_merge (const char *base, const char *link)
1754 if (url_has_scheme (link))
1755 return xstrdup (link);
1757 /* We may not examine BASE past END. */
1758 end = base + path_length (base);
1759 linklength = strlen (link);
1763 /* Empty LINK points back to BASE, query string and all. */
1764 return xstrdup (base);
1766 else if (*link == '?')
1768 /* LINK points to the same location, but changes the query
1769 string. Examples: */
1770 /* uri_merge("path", "?new") -> "path?new" */
1771 /* uri_merge("path?foo", "?new") -> "path?new" */
1772 /* uri_merge("path?foo#bar", "?new") -> "path?new" */
1773 /* uri_merge("path#foo", "?new") -> "path?new" */
1774 int baselength = end - base;
1775 merge = xmalloc (baselength + linklength + 1);
1776 memcpy (merge, base, baselength);
1777 memcpy (merge + baselength, link, linklength);
1778 merge[baselength + linklength] = '\0';
1780 else if (*link == '#')
1782 /* uri_merge("path", "#new") -> "path#new" */
1783 /* uri_merge("path#foo", "#new") -> "path#new" */
1784 /* uri_merge("path?foo", "#new") -> "path?foo#new" */
1785 /* uri_merge("path?foo#bar", "#new") -> "path?foo#new" */
1787 const char *end1 = strchr (base, '#');
1789 end1 = base + strlen (base);
1790 baselength = end1 - base;
1791 merge = xmalloc (baselength + linklength + 1);
1792 memcpy (merge, base, baselength);
1793 memcpy (merge + baselength, link, linklength);
1794 merge[baselength + linklength] = '\0';
1796 else if (*link == '/' && *(link + 1) == '/')
1798 /* LINK begins with "//" and so is a net path: we need to
1799 replace everything after (and including) the double slash
1802 /* uri_merge("foo", "//new/bar") -> "//new/bar" */
1803 /* uri_merge("//old/foo", "//new/bar") -> "//new/bar" */
1804 /* uri_merge("http://old/foo", "//new/bar") -> "http://new/bar" */
1808 const char *start_insert;
1810 /* Look for first slash. */
1811 slash = memchr (base, '/', end - base);
1812 /* If found slash and it is a double slash, then replace
1813 from this point, else default to replacing from the
1815 if (slash && *(slash + 1) == '/')
1816 start_insert = slash;
1818 start_insert = base;
1820 span = start_insert - base;
1821 merge = (char *)xmalloc (span + linklength + 1);
1823 memcpy (merge, base, span);
1824 memcpy (merge + span, link, linklength);
1825 merge[span + linklength] = '\0';
1827 else if (*link == '/')
1829 /* LINK is an absolute path: we need to replace everything
1830 after (and including) the FIRST slash with LINK.
1832 So, if BASE is "http://host/whatever/foo/bar", and LINK is
1833 "/qux/xyzzy", our result should be
1834 "http://host/qux/xyzzy". */
1837 const char *start_insert = NULL; /* for gcc to shut up. */
1838 const char *pos = base;
1839 int seen_slash_slash = 0;
1840 /* We're looking for the first slash, but want to ignore
1843 slash = memchr (pos, '/', end - pos);
1844 if (slash && !seen_slash_slash)
1845 if (*(slash + 1) == '/')
1848 seen_slash_slash = 1;
1852 /* At this point, SLASH is the location of the first / after
1853 "//", or the first slash altogether. START_INSERT is the
1854 pointer to the location where LINK will be inserted. When
1855 examining the last two examples, keep in mind that LINK
1858 if (!slash && !seen_slash_slash)
1859 /* example: "foo" */
1861 start_insert = base;
1862 else if (!slash && seen_slash_slash)
1863 /* example: "http://foo" */
1866 else if (slash && !seen_slash_slash)
1867 /* example: "foo/bar" */
1869 start_insert = base;
1870 else if (slash && seen_slash_slash)
1871 /* example: "http://something/" */
1873 start_insert = slash;
1875 span = start_insert - base;
1876 merge = (char *)xmalloc (span + linklength + 1);
1878 memcpy (merge, base, span);
1879 memcpy (merge + span, link, linklength);
1880 merge[span + linklength] = '\0';
1884 /* LINK is a relative URL: we need to replace everything
1885 after last slash (possibly empty) with LINK.
1887 So, if BASE is "whatever/foo/bar", and LINK is "qux/xyzzy",
1888 our result should be "whatever/foo/qux/xyzzy". */
1889 int need_explicit_slash = 0;
1891 const char *start_insert;
1892 const char *last_slash = find_last_char (base, end, '/');
1895 /* No slash found at all. Append LINK to what we have,
1896 but we'll need a slash as a separator.
1898 Example: if base == "foo" and link == "qux/xyzzy", then
1899 we cannot just append link to base, because we'd get
1900 "fooqux/xyzzy", whereas what we want is
1903 To make sure the / gets inserted, we set
1904 need_explicit_slash to 1. We also set start_insert
1905 to end + 1, so that the length calculations work out
1906 correctly for one more (slash) character. Accessing
1907 that character is fine, since it will be the
1908 delimiter, '\0' or '?'. */
1909 /* example: "foo?..." */
1910 /* ^ ('?' gets changed to '/') */
1911 start_insert = end + 1;
1912 need_explicit_slash = 1;
1914 else if (last_slash && last_slash >= base + 2
1915 && last_slash[-2] == ':' && last_slash[-1] == '/')
1917 /* example: http://host" */
1919 start_insert = end + 1;
1920 need_explicit_slash = 1;
1924 /* example: "whatever/foo/bar" */
1926 start_insert = last_slash + 1;
1929 span = start_insert - base;
1930 merge = (char *)xmalloc (span + linklength + 1);
1932 memcpy (merge, base, span);
1933 if (need_explicit_slash)
1934 merge[span - 1] = '/';
1935 memcpy (merge + span, link, linklength);
1936 merge[span + linklength] = '\0';
1942 #define APPEND(p, s) do { \
1943 int len = strlen (s); \
1944 memcpy (p, s, len); \
1948 /* Use this instead of password when the actual password is supposed
1949 to be hidden. We intentionally use a generic string without giving
1950 away the number of characters in the password, like previous
1952 #define HIDDEN_PASSWORD "*password*"
1954 /* Recreate the URL string from the data in URL.
1956 If HIDE is non-zero (as it is when we're calling this on a URL we
1957 plan to print, but not when calling it to canonicalize a URL for
1958 use within the program), password will be hidden. Unsafe
1959 characters in the URL will be quoted. */
1962 url_string (const struct url *url, int hide_password)
1966 char *quoted_user = NULL, *quoted_passwd = NULL;
1968 int scheme_port = supported_schemes[url->scheme].default_port;
1969 char *scheme_str = supported_schemes[url->scheme].leading_string;
1970 int fplen = full_path_length (url);
1972 int brackets_around_host = 0;
1974 assert (scheme_str != NULL);
1976 /* Make sure the user name and password are quoted. */
1979 quoted_user = url_escape_allow_passthrough (url->user);
1983 quoted_passwd = HIDDEN_PASSWORD;
1985 quoted_passwd = url_escape_allow_passthrough (url->passwd);
1989 if (strchr (url->host, ':'))
1990 brackets_around_host = 1;
1992 size = (strlen (scheme_str)
1993 + strlen (url->host)
1994 + (brackets_around_host ? 2 : 0)
1997 if (url->port != scheme_port)
1998 size += 1 + numdigit (url->port);
2001 size += 1 + strlen (quoted_user);
2003 size += 1 + strlen (quoted_passwd);
2006 p = result = xmalloc (size);
2008 APPEND (p, scheme_str);
2011 APPEND (p, quoted_user);
2015 APPEND (p, quoted_passwd);
2020 if (brackets_around_host)
2022 APPEND (p, url->host);
2023 if (brackets_around_host)
2025 if (url->port != scheme_port)
2028 p = number_to_string (p, url->port);
2031 full_path_write (url, p);
2035 assert (p - result == size);
2037 if (quoted_user && quoted_user != url->user)
2038 xfree (quoted_user);
2039 if (quoted_passwd && !hide_password
2040 && quoted_passwd != url->passwd)
2041 xfree (quoted_passwd);
2046 /* Return non-zero if scheme a is similar to scheme b.
2048 Schemes are similar if they are equal. If SSL is supported, schemes
2049 are also similar if one is http (SCHEME_HTTP) and the other is https
2052 schemes_are_similar_p (enum url_scheme a, enum url_scheme b)
2057 if ((a == SCHEME_HTTP && b == SCHEME_HTTPS)
2058 || (a == SCHEME_HTTPS && b == SCHEME_HTTP))
2065 /* Debugging and testing support for path_simplify. */
2067 /* Debug: run path_simplify on PATH and return the result in a new
2068 string. Useful for calling from the debugger. */
2072 char *copy = xstrdup (path);
2073 path_simplify (copy);
2078 run_test (char *test, char *expected_result, int expected_change)
2080 char *test_copy = xstrdup (test);
2081 int modified = path_simplify (test_copy);
2083 if (0 != strcmp (test_copy, expected_result))
2085 printf ("Failed path_simplify(\"%s\"): expected \"%s\", got \"%s\".\n",
2086 test, expected_result, test_copy);
2088 if (modified != expected_change)
2090 if (expected_change == 1)
2091 printf ("Expected no modification with path_simplify(\"%s\").\n",
2094 printf ("Expected modification with path_simplify(\"%s\").\n",
2101 test_path_simplify (void)
2104 char *test, *result;
2110 { "foo", "foo", 0 },
2111 { "foo/bar", "foo/bar", 0 },
2112 { "foo///bar", "foo/bar", 1 },
2113 { "foo/.", "foo/", 1 },
2114 { "foo/./", "foo/", 1 },
2115 { "foo./", "foo./", 0 },
2116 { "foo/../bar", "bar", 1 },
2117 { "foo/../bar/", "bar/", 1 },
2118 { "foo/bar/..", "foo/", 1 },
2119 { "foo/bar/../x", "foo/x", 1 },
2120 { "foo/bar/../x/", "foo/x/", 1 },
2121 { "foo/..", "", 1 },
2122 { "foo/../..", "", 1 },
2123 { "a/b/../../c", "c", 1 },
2124 { "./a/../b", "b", 1 }
2128 for (i = 0; i < countof (tests); i++)
2130 char *test = tests[i].test;
2131 char *expected_result = tests[i].result;
2132 int expected_change = tests[i].should_modify;
2133 run_test (test, expected_result, expected_change);
2136 /* Now run all the tests with a leading slash before the test case,
2137 to prove that the slash is being preserved. */
2138 for (i = 0; i < countof (tests); i++)
2140 char *test, *expected_result;
2141 int expected_change = tests[i].should_modify;
2143 test = xmalloc (1 + strlen (tests[i].test) + 1);
2144 sprintf (test, "/%s", tests[i].test);
2146 expected_result = xmalloc (1 + strlen (tests[i].result) + 1);
2147 sprintf (expected_result, "/%s", tests[i].result);
2149 run_test (test, expected_result, expected_change);
2152 xfree (expected_result);