[email protected] | 51bcc5d | 2013-04-24 01:41:37 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 4 | |
| 5 | // Functions for canonicalizing "filesystem:file:" URLs. |
| 6 | |
[email protected] | 318076b | 2013-04-18 21:19:45 | [diff] [blame] | 7 | #include "url/url_canon.h" |
| 8 | #include "url/url_canon_internal.h" |
| 9 | #include "url/url_file.h" |
| 10 | #include "url/url_parse_internal.h" |
| 11 | #include "url/url_util.h" |
| 12 | #include "url/url_util_internal.h" |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 13 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 14 | namespace url { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 15 | |
| 16 | namespace { |
| 17 | |
| 18 | // We use the URLComponentSource for the outer URL, as it can have replacements, |
| 19 | // whereas the inner_url can't, so it uses spec. |
| 20 | template<typename CHAR, typename UCHAR> |
| 21 | bool DoCanonicalizeFileSystemURL(const CHAR* spec, |
| 22 | const URLComponentSource<CHAR>& source, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 23 | const Parsed& parsed, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 24 | CharsetConverter* charset_converter, |
| 25 | CanonOutput* output, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 26 | Parsed* new_parsed) { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 27 | // filesystem only uses {scheme, path, query, ref} -- clear the rest. |
[email protected] | e7ee68f7 | 2014-04-08 23:36:06 | [diff] [blame] | 28 | new_parsed->username.reset(); |
| 29 | new_parsed->password.reset(); |
| 30 | new_parsed->host.reset(); |
| 31 | new_parsed->port.reset(); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 32 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 33 | const Parsed* inner_parsed = parsed.inner_parsed(); |
| 34 | Parsed new_inner_parsed; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 35 | |
| 36 | // Scheme (known, so we don't bother running it through the more |
| 37 | // complicated scheme canonicalizer). |
| 38 | new_parsed->scheme.begin = output->length(); |
| 39 | output->Append("filesystem:", 11); |
| 40 | new_parsed->scheme.len = 10; |
| 41 | |
| 42 | if (!parsed.inner_parsed() || !parsed.inner_parsed()->scheme.is_valid()) |
| 43 | return false; |
| 44 | |
| 45 | bool success = true; |
Nick Carter | ff69a10 | 2018-04-04 00:15:17 | [diff] [blame] | 46 | SchemeType inner_scheme_type = SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION; |
[email protected] | cca6f39 | 2014-05-28 21:32:26 | [diff] [blame] | 47 | if (CompareSchemeComponent(spec, inner_parsed->scheme, url::kFileScheme)) { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 48 | new_inner_parsed.scheme.begin = output->length(); |
| 49 | output->Append("file://", 7); |
| 50 | new_inner_parsed.scheme.len = 4; |
| 51 | success &= CanonicalizePath(spec, inner_parsed->path, output, |
| 52 | &new_inner_parsed.path); |
Nick Carter | ff69a10 | 2018-04-04 00:15:17 | [diff] [blame] | 53 | } else if (GetStandardSchemeType(spec, inner_parsed->scheme, |
| 54 | &inner_scheme_type)) { |
| 55 | if (inner_scheme_type == SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION) { |
| 56 | // Strip out the user information from the inner URL, if any. |
| 57 | inner_scheme_type = SCHEME_WITH_HOST_AND_PORT; |
| 58 | } |
| 59 | success = CanonicalizeStandardURL( |
| 60 | spec, parsed.inner_parsed()->Length(), *parsed.inner_parsed(), |
| 61 | inner_scheme_type, charset_converter, output, &new_inner_parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 62 | } else { |
| 63 | // TODO(ericu): The URL is wrong, but should we try to output more of what |
| 64 | // we were given? Echoing back filesystem:mailto etc. doesn't seem all that |
| 65 | // useful. |
| 66 | return false; |
| 67 | } |
| 68 | // The filesystem type must be more than just a leading slash for validity. |
| 69 | success &= parsed.inner_parsed()->path.len > 1; |
| 70 | |
| 71 | success &= CanonicalizePath(source.path, parsed.path, output, |
| 72 | &new_parsed->path); |
| 73 | |
| 74 | // Ignore failures for query/ref since the URL can probably still be loaded. |
| 75 | CanonicalizeQuery(source.query, parsed.query, charset_converter, |
| 76 | output, &new_parsed->query); |
| 77 | CanonicalizeRef(source.ref, parsed.ref, output, &new_parsed->ref); |
| 78 | if (success) |
| 79 | new_parsed->set_inner_parsed(new_inner_parsed); |
| 80 | |
| 81 | return success; |
| 82 | } |
| 83 | |
| 84 | } // namespace |
| 85 | |
| 86 | bool CanonicalizeFileSystemURL(const char* spec, |
| 87 | int spec_len, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 88 | const Parsed& parsed, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 89 | CharsetConverter* charset_converter, |
| 90 | CanonOutput* output, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 91 | Parsed* new_parsed) { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 92 | return DoCanonicalizeFileSystemURL<char, unsigned char>( |
| 93 | spec, URLComponentSource<char>(spec), parsed, charset_converter, output, |
| 94 | new_parsed); |
| 95 | } |
| 96 | |
[email protected] | 3774f83 | 2013-06-11 21:21:57 | [diff] [blame] | 97 | bool CanonicalizeFileSystemURL(const base::char16* spec, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 98 | int spec_len, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 99 | const Parsed& parsed, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 100 | CharsetConverter* charset_converter, |
| 101 | CanonOutput* output, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 102 | Parsed* new_parsed) { |
[email protected] | 3774f83 | 2013-06-11 21:21:57 | [diff] [blame] | 103 | return DoCanonicalizeFileSystemURL<base::char16, base::char16>( |
| 104 | spec, URLComponentSource<base::char16>(spec), parsed, charset_converter, |
| 105 | output, new_parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | bool ReplaceFileSystemURL(const char* base, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 109 | const Parsed& base_parsed, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 110 | const Replacements<char>& replacements, |
| 111 | CharsetConverter* charset_converter, |
| 112 | CanonOutput* output, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 113 | Parsed* new_parsed) { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 114 | URLComponentSource<char> source(base); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 115 | Parsed parsed(base_parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 116 | SetupOverrideComponents(base, replacements, &source, &parsed); |
| 117 | return DoCanonicalizeFileSystemURL<char, unsigned char>( |
| 118 | base, source, parsed, charset_converter, output, new_parsed); |
| 119 | } |
| 120 | |
| 121 | bool ReplaceFileSystemURL(const char* base, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 122 | const Parsed& base_parsed, |
[email protected] | 3774f83 | 2013-06-11 21:21:57 | [diff] [blame] | 123 | const Replacements<base::char16>& replacements, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 124 | CharsetConverter* charset_converter, |
| 125 | CanonOutput* output, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 126 | Parsed* new_parsed) { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 127 | RawCanonOutput<1024> utf8; |
| 128 | URLComponentSource<char> source(base); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 129 | Parsed parsed(base_parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 130 | SetupUTF16OverrideComponents(base, replacements, &utf8, &source, &parsed); |
| 131 | return DoCanonicalizeFileSystemURL<char, unsigned char>( |
| 132 | base, source, parsed, charset_converter, output, new_parsed); |
| 133 | } |
| 134 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 135 | } // namespace url |