[email protected] | f1f8639 | 2012-04-03 13:51:58 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 9b5b1d60 | 2014-06-12 14:29:02 | [diff] [blame] | 5 | #include "components/url_fixer/url_fixer.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
[email protected] | 91e81ae | 2009-05-08 22:14:38 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
skuhne | 66e0255 | 2014-08-29 00:13:56 | [diff] [blame] | 9 | #include "base/files/file_path.h" |
thestig | 819adcc8 | 2014-09-10 22:24:53 | [diff] [blame] | 10 | #include "base/files/file_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 11 | #include "base/logging.h" |
skuhne | 66e0255 | 2014-08-29 00:13:56 | [diff] [blame] | 12 | #if defined(OS_POSIX) |
| 13 | #include "base/path_service.h" |
| 14 | #endif |
[email protected] | f9b29436 | 2013-06-10 20:22:31 | [diff] [blame] | 15 | #include "base/strings/string_util.h" |
[email protected] | 112158af | 2013-06-07 23:46:18 | [diff] [blame] | 16 | #include "base/strings/utf_string_conversions.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 17 | #include "net/base/escape.h" |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 18 | #include "net/base/filename_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 19 | #include "net/base/net_util.h" |
[email protected] | be28b5f4 | 2012-07-20 11:31:25 | [diff] [blame] | 20 | #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
tfarina | 018de6e | 2015-05-26 17:41:20 | [diff] [blame] | 21 | #include "url/third_party/mozilla/url_parse.h" |
[email protected] | 761fa470 | 2013-07-02 15:25:15 | [diff] [blame] | 22 | #include "url/url_file.h" |
[email protected] | 761fa470 | 2013-07-02 15:25:15 | [diff] [blame] | 23 | #include "url/url_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 24 | |
[email protected] | 9b5b1d60 | 2014-06-12 14:29:02 | [diff] [blame] | 25 | const char* url_fixer::home_directory_override = NULL; |
[email protected] | 762c554 | 2009-10-21 16:45:38 | [diff] [blame] | 26 | |
[email protected] | a6380108 | 2009-04-08 04:28:25 | [diff] [blame] | 27 | namespace { |
| 28 | |
[email protected] | 9b5b1d60 | 2014-06-12 14:29:02 | [diff] [blame] | 29 | // Hardcode these constants to avoid dependences on //chrome and //content. |
| 30 | const char kChromeUIScheme[] = "chrome"; |
| 31 | const char kChromeUIDefaultHost[] = "version"; |
| 32 | const char kViewSourceScheme[] = "view-source"; |
| 33 | |
[email protected] | f20dead | 2013-03-02 03:01:48 | [diff] [blame] | 34 | // TODO(estade): Remove these ugly, ugly functions. They are only used in |
[email protected] | b4533450 | 2014-04-30 19:44:05 | [diff] [blame] | 35 | // SegmentURL. A url::Parsed object keeps track of a bunch of indices into |
[email protected] | f20dead | 2013-03-02 03:01:48 | [diff] [blame] | 36 | // a url string, and these need to be updated when the URL is converted from |
[email protected] | a6380108 | 2009-04-08 04:28:25 | [diff] [blame] | 37 | // UTF8 to UTF16. Instead of this after-the-fact adjustment, we should parse it |
| 38 | // in the correct string format to begin with. |
[email protected] | b4533450 | 2014-04-30 19:44:05 | [diff] [blame] | 39 | url::Component UTF8ComponentToUTF16Component( |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 40 | const std::string& text_utf8, |
[email protected] | b4533450 | 2014-04-30 19:44:05 | [diff] [blame] | 41 | const url::Component& component_utf8) { |
[email protected] | f20dead | 2013-03-02 03:01:48 | [diff] [blame] | 42 | if (component_utf8.len == -1) |
[email protected] | b4533450 | 2014-04-30 19:44:05 | [diff] [blame] | 43 | return url::Component(); |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 44 | |
| 45 | std::string before_component_string = |
| 46 | text_utf8.substr(0, component_utf8.begin); |
[email protected] | 9b5b1d60 | 2014-06-12 14:29:02 | [diff] [blame] | 47 | std::string component_string = |
| 48 | text_utf8.substr(component_utf8.begin, component_utf8.len); |
[email protected] | 428fac1 | 2013-12-05 21:38:49 | [diff] [blame] | 49 | base::string16 before_component_string_16 = |
[email protected] | 036a5f3 | 2013-12-25 00:26:11 | [diff] [blame] | 50 | base::UTF8ToUTF16(before_component_string); |
| 51 | base::string16 component_string_16 = base::UTF8ToUTF16(component_string); |
[email protected] | b4533450 | 2014-04-30 19:44:05 | [diff] [blame] | 52 | url::Component component_16(before_component_string_16.length(), |
| 53 | component_string_16.length()); |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 54 | return component_16; |
| 55 | } |
| 56 | |
| 57 | void UTF8PartsToUTF16Parts(const std::string& text_utf8, |
[email protected] | b4533450 | 2014-04-30 19:44:05 | [diff] [blame] | 58 | const url::Parsed& parts_utf8, |
| 59 | url::Parsed* parts) { |
[email protected] | 52796541 | 2014-05-07 14:38:26 | [diff] [blame] | 60 | if (base::IsStringASCII(text_utf8)) { |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 61 | *parts = parts_utf8; |
| 62 | return; |
| 63 | } |
| 64 | |
[email protected] | 9b5b1d60 | 2014-06-12 14:29:02 | [diff] [blame] | 65 | parts->scheme = UTF8ComponentToUTF16Component(text_utf8, parts_utf8.scheme); |
| 66 | parts->username = |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 67 | UTF8ComponentToUTF16Component(text_utf8, parts_utf8.username); |
| 68 | parts->password = |
| 69 | UTF8ComponentToUTF16Component(text_utf8, parts_utf8.password); |
[email protected] | 9b5b1d60 | 2014-06-12 14:29:02 | [diff] [blame] | 70 | parts->host = UTF8ComponentToUTF16Component(text_utf8, parts_utf8.host); |
| 71 | parts->port = UTF8ComponentToUTF16Component(text_utf8, parts_utf8.port); |
| 72 | parts->path = UTF8ComponentToUTF16Component(text_utf8, parts_utf8.path); |
| 73 | parts->query = UTF8ComponentToUTF16Component(text_utf8, parts_utf8.query); |
| 74 | parts->ref = UTF8ComponentToUTF16Component(text_utf8, parts_utf8.ref); |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 75 | } |
[email protected] | a6380108 | 2009-04-08 04:28:25 | [diff] [blame] | 76 | |
[email protected] | 8af69c6c | 2014-03-03 19:05:31 | [diff] [blame] | 77 | base::TrimPositions TrimWhitespaceUTF8(const std::string& input, |
| 78 | base::TrimPositions positions, |
| 79 | std::string* output) { |
[email protected] | f20dead | 2013-03-02 03:01:48 | [diff] [blame] | 80 | // This implementation is not so fast since it converts the text encoding |
[email protected] | d0767cb54 | 2009-10-08 17:38:30 | [diff] [blame] | 81 | // twice. Please feel free to file a bug if this function hurts the |
| 82 | // performance of Chrome. |
[email protected] | 52796541 | 2014-05-07 14:38:26 | [diff] [blame] | 83 | DCHECK(base::IsStringUTF8(input)); |
[email protected] | 036a5f3 | 2013-12-25 00:26:11 | [diff] [blame] | 84 | base::string16 input16 = base::UTF8ToUTF16(input); |
[email protected] | 428fac1 | 2013-12-05 21:38:49 | [diff] [blame] | 85 | base::string16 output16; |
[email protected] | 8af69c6c | 2014-03-03 19:05:31 | [diff] [blame] | 86 | base::TrimPositions result = |
| 87 | base::TrimWhitespace(input16, positions, &output16); |
[email protected] | 036a5f3 | 2013-12-25 00:26:11 | [diff] [blame] | 88 | *output = base::UTF16ToUTF8(output16); |
[email protected] | d0767cb54 | 2009-10-08 17:38:30 | [diff] [blame] | 89 | return result; |
| 90 | } |
| 91 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 92 | // does some basic fixes for input that we want to test for file-ness |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 93 | void PrepareStringForFileOps(const base::FilePath& text, |
| 94 | base::FilePath::StringType* output) { |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 95 | #if defined(OS_WIN) |
[email protected] | 8af69c6c | 2014-03-03 19:05:31 | [diff] [blame] | 96 | base::TrimWhitespace(text.value(), base::TRIM_ALL, output); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 97 | replace(output->begin(), output->end(), '/', '\\'); |
[email protected] | 94161ccf | 2009-08-19 09:22:56 | [diff] [blame] | 98 | #else |
[email protected] | 8af69c6c | 2014-03-03 19:05:31 | [diff] [blame] | 99 | TrimWhitespaceUTF8(text.value(), base::TRIM_ALL, output); |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 100 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | // Tries to create a full path from |text|. If the result is valid and the |
| 104 | // file exists, returns true and sets |full_path| to the result. Otherwise, |
| 105 | // returns false and leaves |full_path| unchanged. |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 106 | bool ValidPathForFile(const base::FilePath::StringType& text, |
| 107 | base::FilePath* full_path) { |
[email protected] | 1547693 | 2013-04-12 05:17:15 | [diff] [blame] | 108 | base::FilePath file_path = base::MakeAbsoluteFilePath(base::FilePath(text)); |
| 109 | if (file_path.empty()) |
[email protected] | 6c56c99 | 2009-03-19 04:06:37 | [diff] [blame] | 110 | return false; |
[email protected] | f20dead | 2013-03-02 03:01:48 | [diff] [blame] | 111 | |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 112 | if (!base::PathExists(file_path)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 113 | return false; |
[email protected] | f20dead | 2013-03-02 03:01:48 | [diff] [blame] | 114 | |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 115 | *full_path = file_path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 116 | return true; |
| 117 | } |
| 118 | |
[email protected] | 762c554 | 2009-10-21 16:45:38 | [diff] [blame] | 119 | #if defined(OS_POSIX) |
| 120 | // Given a path that starts with ~, return a path that starts with an |
| 121 | // expanded-out /user/foobar directory. |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 122 | std::string FixupHomedir(const std::string& text) { |
[email protected] | 762c554 | 2009-10-21 16:45:38 | [diff] [blame] | 123 | DCHECK(text.length() > 0 && text[0] == '~'); |
| 124 | |
| 125 | if (text.length() == 1 || text[1] == '/') { |
skuhne | 66e0255 | 2014-08-29 00:13:56 | [diff] [blame] | 126 | base::FilePath file_path; |
[email protected] | 9b5b1d60 | 2014-06-12 14:29:02 | [diff] [blame] | 127 | if (url_fixer::home_directory_override) |
skuhne | 66e0255 | 2014-08-29 00:13:56 | [diff] [blame] | 128 | file_path = base::FilePath(url_fixer::home_directory_override); |
| 129 | else |
| 130 | PathService::Get(base::DIR_HOME, &file_path); |
| 131 | |
[email protected] | 762c554 | 2009-10-21 16:45:38 | [diff] [blame] | 132 | // We'll probably break elsewhere if $HOME is undefined, but check here |
| 133 | // just in case. |
skuhne | 66e0255 | 2014-08-29 00:13:56 | [diff] [blame] | 134 | if (file_path.value().empty()) |
[email protected] | 762c554 | 2009-10-21 16:45:38 | [diff] [blame] | 135 | return text; |
skuhne | 66e0255 | 2014-08-29 00:13:56 | [diff] [blame] | 136 | // Append requires to be a relative path, so we have to cut all preceeding |
| 137 | // '/' characters. |
| 138 | size_t i = 1; |
| 139 | while (i < text.length() && text[i] == '/') |
| 140 | ++i; |
| 141 | return file_path.Append(text.substr(i)).value(); |
[email protected] | 762c554 | 2009-10-21 16:45:38 | [diff] [blame] | 142 | } |
| 143 | |
[email protected] | 9b5b1d60 | 2014-06-12 14:29:02 | [diff] [blame] | 144 | // Otherwise, this is a path like ~foobar/baz, where we must expand to |
| 145 | // user foobar's home directory. Officially, we should use getpwent(), |
| 146 | // but that is a nasty blocking call. |
[email protected] | f20dead | 2013-03-02 03:01:48 | [diff] [blame] | 147 | |
[email protected] | 762c554 | 2009-10-21 16:45:38 | [diff] [blame] | 148 | #if defined(OS_MACOSX) |
| 149 | static const char kHome[] = "/Users/"; |
| 150 | #else |
| 151 | static const char kHome[] = "/home/"; |
| 152 | #endif |
| 153 | return kHome + text.substr(1); |
| 154 | } |
| 155 | #endif |
| 156 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 157 | // Tries to create a file: URL from |text| if it looks like a filename, even if |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 158 | // it doesn't resolve as a valid path or to an existing file. Returns a |
| 159 | // (possibly invalid) file: URL in |fixed_up_url| for input beginning |
| 160 | // with a drive specifier or "\\". Returns the unchanged input in other cases |
| 161 | // (including file: URLs: these don't look like filenames). |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 162 | std::string FixupPath(const std::string& text) { |
[email protected] | ba1321d1 | 2009-04-21 22:42:29 | [diff] [blame] | 163 | DCHECK(!text.empty()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 164 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 165 | base::FilePath::StringType filename; |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 166 | #if defined(OS_WIN) |
[email protected] | 036a5f3 | 2013-12-25 00:26:11 | [diff] [blame] | 167 | base::FilePath input_path(base::UTF8ToWide(text)); |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 168 | PrepareStringForFileOps(input_path, &filename); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 169 | |
[email protected] | ba1321d1 | 2009-04-21 22:42:29 | [diff] [blame] | 170 | // Fixup Windows-style drive letters, where "C:" gets rewritten to "C|". |
| 171 | if (filename.length() > 1 && filename[1] == '|') |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 172 | filename[1] = ':'; |
[email protected] | ba1321d1 | 2009-04-21 22:42:29 | [diff] [blame] | 173 | #elif defined(OS_POSIX) |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 174 | base::FilePath input_path(text); |
[email protected] | ba1321d1 | 2009-04-21 22:42:29 | [diff] [blame] | 175 | PrepareStringForFileOps(input_path, &filename); |
[email protected] | 762c554 | 2009-10-21 16:45:38 | [diff] [blame] | 176 | if (filename.length() > 0 && filename[0] == '~') |
| 177 | filename = FixupHomedir(filename); |
[email protected] | ba1321d1 | 2009-04-21 22:42:29 | [diff] [blame] | 178 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 179 | |
| 180 | // Here, we know the input looks like a file. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 181 | GURL file_url = net::FilePathToFileURL(base::FilePath(filename)); |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 182 | if (file_url.is_valid()) { |
[email protected] | 9b5b1d60 | 2014-06-12 14:29:02 | [diff] [blame] | 183 | return base::UTF16ToUTF8(net::FormatUrl(file_url, |
| 184 | std::string(), |
| 185 | net::kFormatUrlOmitUsernamePassword, |
| 186 | net::UnescapeRule::NORMAL, |
| 187 | NULL, |
| 188 | NULL, |
| 189 | NULL)); |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 190 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 191 | |
| 192 | // Invalid file URL, just return the input. |
| 193 | return text; |
| 194 | } |
| 195 | |
| 196 | // Checks |domain| to see if a valid TLD is already present. If not, appends |
[email protected] | f20dead | 2013-03-02 03:01:48 | [diff] [blame] | 197 | // |desired_tld| to the domain, and prepends "www." unless it's already present. |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 198 | void AddDesiredTLD(const std::string& desired_tld, std::string* domain) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 199 | if (desired_tld.empty() || domain->empty()) |
| 200 | return; |
| 201 | |
| 202 | // Check the TLD. If the return value is positive, we already have a TLD, so |
[email protected] | 7e56381 | 2010-03-22 20:05:59 | [diff] [blame] | 203 | // abort. If the return value is std::string::npos, there's no valid host, |
| 204 | // but we can try to append a TLD anyway, since the host may become valid once |
| 205 | // the TLD is attached -- for example, "999999999999" is detected as a broken |
| 206 | // IP address and marked invalid, but attaching ".com" makes it legal. When |
| 207 | // the return value is 0, there's a valid host with no known TLD, so we can |
| 208 | // definitely append the user's TLD. We disallow unknown registries here so |
| 209 | // users can input "mail.yahoo" and hit ctrl-enter to get |
| 210 | // "www.mail.yahoo.com". |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 211 | const size_t registry_length = |
[email protected] | ed32c21 | 2013-05-14 20:49:29 | [diff] [blame] | 212 | net::registry_controlled_domains::GetRegistryLength( |
| 213 | *domain, |
| 214 | net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, |
| 215 | net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); |
[email protected] | 7e56381 | 2010-03-22 20:05:59 | [diff] [blame] | 216 | if ((registry_length != 0) && (registry_length != std::string::npos)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 217 | return; |
| 218 | |
| 219 | // Add the suffix at the end of the domain. |
| 220 | const size_t domain_length(domain->length()); |
[email protected] | 1cb92b8 | 2010-03-08 23:12:15 | [diff] [blame] | 221 | DCHECK_GT(domain_length, 0U); |
| 222 | DCHECK_NE(desired_tld[0], '.'); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 223 | if ((*domain)[domain_length - 1] != '.') |
| 224 | domain->push_back('.'); |
| 225 | domain->append(desired_tld); |
| 226 | |
| 227 | // Now, if the domain begins with "www.", stop. |
[email protected] | 7e56381 | 2010-03-22 20:05:59 | [diff] [blame] | 228 | const std::string prefix("www."); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 229 | if (domain->compare(0, prefix.length(), prefix) != 0) { |
| 230 | // Otherwise, add www. to the beginning of the URL. |
| 231 | domain->insert(0, prefix); |
| 232 | } |
| 233 | } |
| 234 | |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 235 | inline void FixupUsername(const std::string& text, |
[email protected] | b4533450 | 2014-04-30 19:44:05 | [diff] [blame] | 236 | const url::Component& part, |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 237 | std::string* url) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 238 | if (!part.is_valid()) |
| 239 | return; |
[email protected] | f20dead | 2013-03-02 03:01:48 | [diff] [blame] | 240 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 241 | // We don't fix up the username at the moment. |
| 242 | url->append(text, part.begin, part.len); |
| 243 | // Do not append the trailing '@' because we might need to include the user's |
| 244 | // password. FixupURL itself will append the '@' for us. |
| 245 | } |
| 246 | |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 247 | inline void FixupPassword(const std::string& text, |
[email protected] | b4533450 | 2014-04-30 19:44:05 | [diff] [blame] | 248 | const url::Component& part, |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 249 | std::string* url) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 250 | if (!part.is_valid()) |
| 251 | return; |
[email protected] | f20dead | 2013-03-02 03:01:48 | [diff] [blame] | 252 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 253 | // We don't fix up the password at the moment. |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 254 | url->append(":"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 255 | url->append(text, part.begin, part.len); |
| 256 | } |
| 257 | |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 258 | void FixupHost(const std::string& text, |
[email protected] | b4533450 | 2014-04-30 19:44:05 | [diff] [blame] | 259 | const url::Component& part, |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 260 | bool has_scheme, |
| 261 | const std::string& desired_tld, |
| 262 | std::string* url) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 263 | if (!part.is_valid()) |
| 264 | return; |
| 265 | |
| 266 | // Make domain valid. |
| 267 | // Strip all leading dots and all but one trailing dot, unless the user only |
| 268 | // typed dots, in which case their input is totally invalid and we should just |
| 269 | // leave it unchanged. |
[email protected] | 7e56381 | 2010-03-22 20:05:59 | [diff] [blame] | 270 | std::string domain(text, part.begin, part.len); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 271 | const size_t first_nondot(domain.find_first_not_of('.')); |
[email protected] | 7e56381 | 2010-03-22 20:05:59 | [diff] [blame] | 272 | if (first_nondot != std::string::npos) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 273 | domain.erase(0, first_nondot); |
| 274 | size_t last_nondot(domain.find_last_not_of('.')); |
[email protected] | 7e56381 | 2010-03-22 20:05:59 | [diff] [blame] | 275 | DCHECK(last_nondot != std::string::npos); |
[email protected] | 1cb92b8 | 2010-03-08 23:12:15 | [diff] [blame] | 276 | last_nondot += 2; // Point at second period in ending string |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 277 | if (last_nondot < domain.length()) |
| 278 | domain.erase(last_nondot); |
| 279 | } |
| 280 | |
| 281 | // Add any user-specified TLD, if applicable. |
| 282 | AddDesiredTLD(desired_tld, &domain); |
[email protected] | f20dead | 2013-03-02 03:01:48 | [diff] [blame] | 283 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 284 | url->append(domain); |
| 285 | } |
| 286 | |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 287 | void FixupPort(const std::string& text, |
[email protected] | b4533450 | 2014-04-30 19:44:05 | [diff] [blame] | 288 | const url::Component& part, |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 289 | std::string* url) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 290 | if (!part.is_valid()) |
| 291 | return; |
[email protected] | f20dead | 2013-03-02 03:01:48 | [diff] [blame] | 292 | |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 293 | // We don't fix up the port at the moment. |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 294 | url->append(":"); |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 295 | url->append(text, part.begin, part.len); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 296 | } |
| 297 | |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 298 | inline void FixupPath(const std::string& text, |
[email protected] | b4533450 | 2014-04-30 19:44:05 | [diff] [blame] | 299 | const url::Component& part, |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 300 | std::string* url) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 301 | if (!part.is_valid() || part.len == 0) { |
| 302 | // We should always have a path. |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 303 | url->append("/"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 304 | return; |
| 305 | } |
| 306 | |
| 307 | // Append the path as is. |
| 308 | url->append(text, part.begin, part.len); |
| 309 | } |
| 310 | |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 311 | inline void FixupQuery(const std::string& text, |
[email protected] | b4533450 | 2014-04-30 19:44:05 | [diff] [blame] | 312 | const url::Component& part, |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 313 | std::string* url) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 314 | if (!part.is_valid()) |
| 315 | return; |
[email protected] | f20dead | 2013-03-02 03:01:48 | [diff] [blame] | 316 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 317 | // We don't fix up the query at the moment. |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 318 | url->append("?"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 319 | url->append(text, part.begin, part.len); |
| 320 | } |
| 321 | |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 322 | inline void FixupRef(const std::string& text, |
[email protected] | b4533450 | 2014-04-30 19:44:05 | [diff] [blame] | 323 | const url::Component& part, |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 324 | std::string* url) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 325 | if (!part.is_valid()) |
| 326 | return; |
[email protected] | f20dead | 2013-03-02 03:01:48 | [diff] [blame] | 327 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 328 | // We don't fix up the ref at the moment. |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 329 | url->append("#"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 330 | url->append(text, part.begin, part.len); |
| 331 | } |
| 332 | |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 333 | bool HasPort(const std::string& original_text, |
[email protected] | b4533450 | 2014-04-30 19:44:05 | [diff] [blame] | 334 | const url::Component& scheme_component) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 335 | // Find the range between the ":" and the "/". |
| 336 | size_t port_start = scheme_component.end() + 1; |
| 337 | size_t port_end = port_start; |
| 338 | while ((port_end < original_text.length()) && |
[email protected] | b4533450 | 2014-04-30 19:44:05 | [diff] [blame] | 339 | !url::IsAuthorityTerminator(original_text[port_end])) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 340 | ++port_end; |
| 341 | if (port_end == port_start) |
| 342 | return false; |
| 343 | |
| 344 | // Scan the range to see if it is entirely digits. |
| 345 | for (size_t i = port_start; i < port_end; ++i) { |
brettw | b341306 | 2015-06-24 00:39:02 | [diff] [blame] | 346 | if (!base::IsAsciiDigit(original_text[i])) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 347 | return false; |
| 348 | } |
| 349 | |
| 350 | return true; |
| 351 | } |
| 352 | |
[email protected] | 818071ce | 2009-05-18 01:25:25 | [diff] [blame] | 353 | // Try to extract a valid scheme from the beginning of |text|. |
| 354 | // If successful, set |scheme_component| to the text range where the scheme |
| 355 | // was located, and fill |canon_scheme| with its canonicalized form. |
| 356 | // Otherwise, return false and leave the outputs in an indeterminate state. |
[email protected] | 135c4512 | 2014-02-12 02:44:32 | [diff] [blame] | 357 | bool GetValidScheme(const std::string& text, |
[email protected] | b4533450 | 2014-04-30 19:44:05 | [diff] [blame] | 358 | url::Component* scheme_component, |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 359 | std::string* canon_scheme) { |
[email protected] | 135c4512 | 2014-02-12 02:44:32 | [diff] [blame] | 360 | canon_scheme->clear(); |
| 361 | |
[email protected] | 818071ce | 2009-05-18 01:25:25 | [diff] [blame] | 362 | // Locate everything up to (but not including) the first ':' |
[email protected] | 9b5b1d60 | 2014-06-12 14:29:02 | [diff] [blame] | 363 | if (!url::ExtractScheme( |
| 364 | text.data(), static_cast<int>(text.length()), scheme_component)) { |
[email protected] | 818071ce | 2009-05-18 01:25:25 | [diff] [blame] | 365 | return false; |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 366 | } |
[email protected] | 818071ce | 2009-05-18 01:25:25 | [diff] [blame] | 367 | |
| 368 | // Make sure the scheme contains only valid characters, and convert |
| 369 | // to lowercase. This also catches IPv6 literals like [::1], because |
| 370 | // brackets are not in the whitelist. |
[email protected] | b4533450 | 2014-04-30 19:44:05 | [diff] [blame] | 371 | url::StdStringCanonOutput canon_scheme_output(canon_scheme); |
| 372 | url::Component canon_scheme_component; |
[email protected] | 9b5b1d60 | 2014-06-12 14:29:02 | [diff] [blame] | 373 | if (!url::CanonicalizeScheme(text.data(), |
| 374 | *scheme_component, |
| 375 | &canon_scheme_output, |
| 376 | &canon_scheme_component)) { |
[email protected] | 818071ce | 2009-05-18 01:25:25 | [diff] [blame] | 377 | return false; |
[email protected] | b4533450 | 2014-04-30 19:44:05 | [diff] [blame] | 378 | } |
[email protected] | 818071ce | 2009-05-18 01:25:25 | [diff] [blame] | 379 | |
| 380 | // Strip the ':', and any trailing buffer space. |
| 381 | DCHECK_EQ(0, canon_scheme_component.begin); |
| 382 | canon_scheme->erase(canon_scheme_component.len); |
| 383 | |
| 384 | // We need to fix up the segmentation for "www.example.com:/". For this |
| 385 | // case, we guess that schemes with a "." are not actually schemes. |
[email protected] | 7e56381 | 2010-03-22 20:05:59 | [diff] [blame] | 386 | if (canon_scheme->find('.') != std::string::npos) |
[email protected] | 818071ce | 2009-05-18 01:25:25 | [diff] [blame] | 387 | return false; |
| 388 | |
| 389 | // We need to fix up the segmentation for "www:123/". For this case, we |
| 390 | // will add an HTTP scheme later and make the URL parser happy. |
| 391 | // TODO(pkasting): Maybe we should try to use GURL's parser for this? |
| 392 | if (HasPort(text, *scheme_component)) |
| 393 | return false; |
| 394 | |
| 395 | // Everything checks out. |
| 396 | return true; |
| 397 | } |
| 398 | |
[email protected] | 9b5b1d60 | 2014-06-12 14:29:02 | [diff] [blame] | 399 | // Performs the work for url_fixer::SegmentURL. |text| may be modified on |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 400 | // output on success: a semicolon following a valid scheme is replaced with a |
| 401 | // colon. |
[email protected] | b4533450 | 2014-04-30 19:44:05 | [diff] [blame] | 402 | std::string SegmentURLInternal(std::string* text, url::Parsed* parts) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 403 | // Initialize the result. |
[email protected] | b4533450 | 2014-04-30 19:44:05 | [diff] [blame] | 404 | *parts = url::Parsed(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 405 | |
[email protected] | 7e56381 | 2010-03-22 20:05:59 | [diff] [blame] | 406 | std::string trimmed; |
[email protected] | 8af69c6c | 2014-03-03 19:05:31 | [diff] [blame] | 407 | TrimWhitespaceUTF8(*text, base::TRIM_ALL, &trimmed); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 408 | if (trimmed.empty()) |
[email protected] | 7e56381 | 2010-03-22 20:05:59 | [diff] [blame] | 409 | return std::string(); // Nothing to segment. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 410 | |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 411 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 412 | int trimmed_length = static_cast<int>(trimmed.length()); |
[email protected] | b4533450 | 2014-04-30 19:44:05 | [diff] [blame] | 413 | if (url::DoesBeginWindowsDriveSpec(trimmed.data(), 0, trimmed_length) || |
| 414 | url::DoesBeginUNCPath(trimmed.data(), 0, trimmed_length, true)) |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 415 | return "file"; |
| 416 | #elif defined(OS_POSIX) |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 417 | if (base::FilePath::IsSeparator(trimmed.data()[0]) || |
| 418 | trimmed.data()[0] == '~') |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 419 | return "file"; |
| 420 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 421 | |
| 422 | // Otherwise, we need to look at things carefully. |
[email protected] | 7e56381 | 2010-03-22 20:05:59 | [diff] [blame] | 423 | std::string scheme; |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 424 | if (!GetValidScheme(*text, &parts->scheme, &scheme)) { |
| 425 | // Try again if there is a ';' in the text. If changing it to a ':' results |
| 426 | // in a scheme being found, continue processing with the modified text. |
| 427 | bool found_scheme = false; |
| 428 | size_t semicolon = text->find(';'); |
| 429 | if (semicolon != 0 && semicolon != std::string::npos) { |
| 430 | (*text)[semicolon] = ':'; |
| 431 | if (GetValidScheme(*text, &parts->scheme, &scheme)) |
| 432 | found_scheme = true; |
| 433 | else |
| 434 | (*text)[semicolon] = ';'; |
| 435 | } |
| 436 | if (!found_scheme) { |
| 437 | // Couldn't determine the scheme, so just pick one. |
| 438 | parts->scheme.reset(); |
brettw | 9550931 | 2015-07-16 23:57:33 | [diff] [blame^] | 439 | scheme = base::StartsWith(*text, "ftp.", |
| 440 | base::CompareCase::INSENSITIVE_ASCII) ? |
| 441 | url::kFtpScheme : url::kHttpScheme; |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 442 | } |
[email protected] | dcf7d35 | 2009-02-26 01:56:02 | [diff] [blame] | 443 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 444 | |
[email protected] | 89f550b | 2011-06-08 18:34:03 | [diff] [blame] | 445 | // Proceed with about and chrome schemes, but not file or nonstandard schemes. |
[email protected] | 9b5b1d60 | 2014-06-12 14:29:02 | [diff] [blame] | 446 | if ((scheme != url::kAboutScheme) && (scheme != kChromeUIScheme) && |
[email protected] | cca6f39 | 2014-05-28 21:32:26 | [diff] [blame] | 447 | ((scheme == url::kFileScheme) || |
[email protected] | 8e09c7af | 2014-06-10 11:46:17 | [diff] [blame] | 448 | !url::IsStandard( |
| 449 | scheme.c_str(), |
| 450 | url::Component(0, static_cast<int>(scheme.length()))))) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 451 | return scheme; |
[email protected] | b4533450 | 2014-04-30 19:44:05 | [diff] [blame] | 452 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 453 | |
[email protected] | cca6f39 | 2014-05-28 21:32:26 | [diff] [blame] | 454 | if (scheme == url::kFileSystemScheme) { |
[email protected] | f1f8639 | 2012-04-03 13:51:58 | [diff] [blame] | 455 | // Have the GURL parser do the heavy lifting for us. |
[email protected] | 9b5b1d60 | 2014-06-12 14:29:02 | [diff] [blame] | 456 | url::ParseFileSystemURL( |
| 457 | text->data(), static_cast<int>(text->length()), parts); |
[email protected] | f1f8639 | 2012-04-03 13:51:58 | [diff] [blame] | 458 | return scheme; |
| 459 | } |
| 460 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 461 | if (parts->scheme.is_valid()) { |
| 462 | // Have the GURL parser do the heavy lifting for us. |
[email protected] | 9b5b1d60 | 2014-06-12 14:29:02 | [diff] [blame] | 463 | url::ParseStandardURL( |
| 464 | text->data(), static_cast<int>(text->length()), parts); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 465 | return scheme; |
| 466 | } |
| 467 | |
| 468 | // We need to add a scheme in order for ParseStandardURL to be happy. |
| 469 | // Find the first non-whitespace character. |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 470 | std::string::iterator first_nonwhite = text->begin(); |
brettw | b341306 | 2015-06-24 00:39:02 | [diff] [blame] | 471 | while ((first_nonwhite != text->end()) && |
| 472 | base::IsUnicodeWhitespace(*first_nonwhite)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 473 | ++first_nonwhite; |
| 474 | |
| 475 | // Construct the text to parse by inserting the scheme. |
[email protected] | 7e56381 | 2010-03-22 20:05:59 | [diff] [blame] | 476 | std::string inserted_text(scheme); |
[email protected] | fb4fe095 | 2014-06-05 09:44:24 | [diff] [blame] | 477 | inserted_text.append(url::kStandardSchemeSeparator); |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 478 | std::string text_to_parse(text->begin(), first_nonwhite); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 479 | text_to_parse.append(inserted_text); |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 480 | text_to_parse.append(first_nonwhite, text->end()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 481 | |
| 482 | // Have the GURL parser do the heavy lifting for us. |
[email protected] | 9b5b1d60 | 2014-06-12 14:29:02 | [diff] [blame] | 483 | url::ParseStandardURL( |
| 484 | text_to_parse.data(), static_cast<int>(text_to_parse.length()), parts); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 485 | |
| 486 | // Offset the results of the parse to match the original text. |
| 487 | const int offset = -static_cast<int>(inserted_text.length()); |
[email protected] | 9b5b1d60 | 2014-06-12 14:29:02 | [diff] [blame] | 488 | url_fixer::OffsetComponent(offset, &parts->scheme); |
| 489 | url_fixer::OffsetComponent(offset, &parts->username); |
| 490 | url_fixer::OffsetComponent(offset, &parts->password); |
| 491 | url_fixer::OffsetComponent(offset, &parts->host); |
| 492 | url_fixer::OffsetComponent(offset, &parts->port); |
| 493 | url_fixer::OffsetComponent(offset, &parts->path); |
| 494 | url_fixer::OffsetComponent(offset, &parts->query); |
| 495 | url_fixer::OffsetComponent(offset, &parts->ref); |
[email protected] | f20dead | 2013-03-02 03:01:48 | [diff] [blame] | 496 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 497 | return scheme; |
| 498 | } |
| 499 | |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 500 | } // namespace |
| 501 | |
[email protected] | 9b5b1d60 | 2014-06-12 14:29:02 | [diff] [blame] | 502 | std::string url_fixer::SegmentURL(const std::string& text, url::Parsed* parts) { |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 503 | std::string mutable_text(text); |
| 504 | return SegmentURLInternal(&mutable_text, parts); |
| 505 | } |
| 506 | |
[email protected] | 9b5b1d60 | 2014-06-12 14:29:02 | [diff] [blame] | 507 | base::string16 url_fixer::SegmentURL(const base::string16& text, |
| 508 | url::Parsed* parts) { |
[email protected] | 036a5f3 | 2013-12-25 00:26:11 | [diff] [blame] | 509 | std::string text_utf8 = base::UTF16ToUTF8(text); |
[email protected] | b4533450 | 2014-04-30 19:44:05 | [diff] [blame] | 510 | url::Parsed parts_utf8; |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 511 | std::string scheme_utf8 = SegmentURL(text_utf8, &parts_utf8); |
| 512 | UTF8PartsToUTF16Parts(text_utf8, parts_utf8, parts); |
[email protected] | 036a5f3 | 2013-12-25 00:26:11 | [diff] [blame] | 513 | return base::UTF8ToUTF16(scheme_utf8); |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 514 | } |
| 515 | |
[email protected] | 9b5b1d60 | 2014-06-12 14:29:02 | [diff] [blame] | 516 | GURL url_fixer::FixupURL(const std::string& text, |
| 517 | const std::string& desired_tld) { |
[email protected] | 7e56381 | 2010-03-22 20:05:59 | [diff] [blame] | 518 | std::string trimmed; |
[email protected] | 8af69c6c | 2014-03-03 19:05:31 | [diff] [blame] | 519 | TrimWhitespaceUTF8(text, base::TRIM_ALL, &trimmed); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 520 | if (trimmed.empty()) |
[email protected] | 76e7da2 | 2010-06-18 22:44:49 | [diff] [blame] | 521 | return GURL(); // Nothing here. |
[email protected] | f20dead | 2013-03-02 03:01:48 | [diff] [blame] | 522 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 523 | // Segment the URL. |
[email protected] | b4533450 | 2014-04-30 19:44:05 | [diff] [blame] | 524 | url::Parsed parts; |
[email protected] | ae8e367 | 2013-03-20 09:00:08 | [diff] [blame] | 525 | std::string scheme(SegmentURLInternal(&trimmed, &parts)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 526 | |
[email protected] | 76e7da2 | 2010-06-18 22:44:49 | [diff] [blame] | 527 | // For view-source: URLs, we strip "view-source:", do fixup, and stick it back |
| 528 | // on. This allows us to handle things like "view-source:google.com". |
[email protected] | 9b5b1d60 | 2014-06-12 14:29:02 | [diff] [blame] | 529 | if (scheme == kViewSourceScheme) { |
[email protected] | 76e7da2 | 2010-06-18 22:44:49 | [diff] [blame] | 530 | // Reject "view-source:view-source:..." to avoid deep recursion. |
[email protected] | 9b5b1d60 | 2014-06-12 14:29:02 | [diff] [blame] | 531 | std::string view_source(kViewSourceScheme + std::string(":")); |
brettw | 9550931 | 2015-07-16 23:57:33 | [diff] [blame^] | 532 | if (!base::StartsWith(text, view_source + view_source, |
| 533 | base::CompareCase::INSENSITIVE_ASCII)) { |
[email protected] | 9b5b1d60 | 2014-06-12 14:29:02 | [diff] [blame] | 534 | return GURL(kViewSourceScheme + std::string(":") + |
| 535 | FixupURL(trimmed.substr(scheme.length() + 1), desired_tld) |
| 536 | .possibly_invalid_spec()); |
[email protected] | 76e7da2 | 2010-06-18 22:44:49 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 540 | // We handle the file scheme separately. |
[email protected] | cca6f39 | 2014-05-28 21:32:26 | [diff] [blame] | 541 | if (scheme == url::kFileScheme) |
[email protected] | 76e7da2 | 2010-06-18 22:44:49 | [diff] [blame] | 542 | return GURL(parts.scheme.is_valid() ? text : FixupPath(text)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 543 | |
[email protected] | f1f8639 | 2012-04-03 13:51:58 | [diff] [blame] | 544 | // We handle the filesystem scheme separately. |
[email protected] | cca6f39 | 2014-05-28 21:32:26 | [diff] [blame] | 545 | if (scheme == url::kFileSystemScheme) { |
[email protected] | f1f8639 | 2012-04-03 13:51:58 | [diff] [blame] | 546 | if (parts.inner_parsed() && parts.inner_parsed()->scheme.is_valid()) |
| 547 | return GURL(text); |
| 548 | return GURL(); |
| 549 | } |
| 550 | |
[email protected] | 89f550b | 2011-06-08 18:34:03 | [diff] [blame] | 551 | // Parse and rebuild about: and chrome: URLs, except about:blank. |
[email protected] | 8e09c7af | 2014-06-10 11:46:17 | [diff] [blame] | 552 | bool chrome_url = |
brettw | bc17d2c8 | 2015-06-09 22:39:08 | [diff] [blame] | 553 | !base::LowerCaseEqualsASCII(trimmed, url::kAboutBlankURL) && |
[email protected] | 9b5b1d60 | 2014-06-12 14:29:02 | [diff] [blame] | 554 | ((scheme == url::kAboutScheme) || (scheme == kChromeUIScheme)); |
[email protected] | 89f550b | 2011-06-08 18:34:03 | [diff] [blame] | 555 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 556 | // For some schemes whose layouts we understand, we rebuild it. |
[email protected] | b4533450 | 2014-04-30 19:44:05 | [diff] [blame] | 557 | if (chrome_url || |
| 558 | url::IsStandard(scheme.c_str(), |
| 559 | url::Component(0, static_cast<int>(scheme.length())))) { |
[email protected] | 89f550b | 2011-06-08 18:34:03 | [diff] [blame] | 560 | // Replace the about: scheme with the chrome: scheme. |
[email protected] | 9b5b1d60 | 2014-06-12 14:29:02 | [diff] [blame] | 561 | std::string url(chrome_url ? kChromeUIScheme : scheme); |
[email protected] | fb4fe095 | 2014-06-05 09:44:24 | [diff] [blame] | 562 | url.append(url::kStandardSchemeSeparator); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 563 | |
| 564 | // We need to check whether the |username| is valid because it is our |
| 565 | // responsibility to append the '@' to delineate the user information from |
| 566 | // the host portion of the URL. |
| 567 | if (parts.username.is_valid()) { |
| 568 | FixupUsername(trimmed, parts.username, &url); |
| 569 | FixupPassword(trimmed, parts.password, &url); |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 570 | url.append("@"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | FixupHost(trimmed, parts.host, parts.scheme.is_valid(), desired_tld, &url); |
[email protected] | 89f550b | 2011-06-08 18:34:03 | [diff] [blame] | 574 | if (chrome_url && !parts.host.is_valid()) |
[email protected] | 9b5b1d60 | 2014-06-12 14:29:02 | [diff] [blame] | 575 | url.append(kChromeUIDefaultHost); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 576 | FixupPort(trimmed, parts.port, &url); |
| 577 | FixupPath(trimmed, parts.path, &url); |
| 578 | FixupQuery(trimmed, parts.query, &url); |
| 579 | FixupRef(trimmed, parts.ref, &url); |
[email protected] | f20dead | 2013-03-02 03:01:48 | [diff] [blame] | 580 | |
[email protected] | 76e7da2 | 2010-06-18 22:44:49 | [diff] [blame] | 581 | return GURL(url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | // In the worst-case, we insert a scheme if the URL lacks one. |
| 585 | if (!parts.scheme.is_valid()) { |
[email protected] | 7e56381 | 2010-03-22 20:05:59 | [diff] [blame] | 586 | std::string fixed_scheme(scheme); |
[email protected] | fb4fe095 | 2014-06-05 09:44:24 | [diff] [blame] | 587 | fixed_scheme.append(url::kStandardSchemeSeparator); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 588 | trimmed.insert(0, fixed_scheme); |
| 589 | } |
[email protected] | f20dead | 2013-03-02 03:01:48 | [diff] [blame] | 590 | |
[email protected] | 76e7da2 | 2010-06-18 22:44:49 | [diff] [blame] | 591 | return GURL(trimmed); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | // The rules are different here than for regular fixup, since we need to handle |
| 595 | // input like "hello.html" and know to look in the current directory. Regular |
| 596 | // fixup will look for cues that it is actually a file path before trying to |
| 597 | // figure out what file it is. If our logic doesn't work, we will fall back on |
| 598 | // regular fixup. |
[email protected] | 9b5b1d60 | 2014-06-12 14:29:02 | [diff] [blame] | 599 | GURL url_fixer::FixupRelativeFile(const base::FilePath& base_dir, |
| 600 | const base::FilePath& text) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 601 | base::FilePath old_cur_directory; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 602 | if (!base_dir.empty()) { |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 603 | // Save the old current directory before we move to the new one. |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 604 | base::GetCurrentDirectory(&old_cur_directory); |
| 605 | base::SetCurrentDirectory(base_dir); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 606 | } |
| 607 | |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 608 | // Allow funny input with extra whitespace and the wrong kind of slashes. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 609 | base::FilePath::StringType trimmed; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 610 | PrepareStringForFileOps(text, &trimmed); |
| 611 | |
| 612 | bool is_file = true; |
[email protected] | a64c3cf | 2011-08-06 05:25:55 | [diff] [blame] | 613 | // Avoid recognizing definite non-file URLs as file paths. |
| 614 | GURL gurl(trimmed); |
| 615 | if (gurl.is_valid() && gurl.IsStandard()) |
| 616 | is_file = false; |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 617 | base::FilePath full_path; |
[email protected] | a64c3cf | 2011-08-06 05:25:55 | [diff] [blame] | 618 | if (is_file && !ValidPathForFile(trimmed, &full_path)) { |
[email protected] | 9b5b1d60 | 2014-06-12 14:29:02 | [diff] [blame] | 619 | // Not a path as entered, try unescaping it in case the user has |
| 620 | // escaped things. We need to go through 8-bit since the escaped values |
| 621 | // only represent 8-bit values. |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 622 | #if defined(OS_WIN) |
[email protected] | 036a5f3 | 2013-12-25 00:26:11 | [diff] [blame] | 623 | std::wstring unescaped = base::UTF8ToWide(net::UnescapeURLComponent( |
| 624 | base::WideToUTF8(trimmed), |
[email protected] | b60ae4b0 | 2011-11-15 14:58:21 | [diff] [blame] | 625 | net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS)); |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 626 | #elif defined(OS_POSIX) |
[email protected] | 4879790 | 2011-10-02 23:05:08 | [diff] [blame] | 627 | std::string unescaped = net::UnescapeURLComponent( |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 628 | trimmed, |
[email protected] | b60ae4b0 | 2011-11-15 14:58:21 | [diff] [blame] | 629 | net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS); |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 630 | #endif |
| 631 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 632 | if (!ValidPathForFile(unescaped, &full_path)) |
| 633 | is_file = false; |
| 634 | } |
| 635 | |
| 636 | // Put back the current directory if we saved it. |
[email protected] | 76e7da2 | 2010-06-18 22:44:49 | [diff] [blame] | 637 | if (!base_dir.empty()) |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 638 | base::SetCurrentDirectory(old_cur_directory); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 639 | |
| 640 | if (is_file) { |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 641 | GURL file_url = net::FilePathToFileURL(full_path); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 642 | if (file_url.is_valid()) |
[email protected] | 9b5b1d60 | 2014-06-12 14:29:02 | [diff] [blame] | 643 | return GURL( |
| 644 | base::UTF16ToUTF8(net::FormatUrl(file_url, |
| 645 | std::string(), |
| 646 | net::kFormatUrlOmitUsernamePassword, |
| 647 | net::UnescapeRule::NORMAL, |
| 648 | NULL, |
| 649 | NULL, |
| 650 | NULL))); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 651 | // Invalid files fall through to regular processing. |
| 652 | } |
| 653 | |
[email protected] | 9b5b1d60 | 2014-06-12 14:29:02 | [diff] [blame] | 654 | // Fall back on regular fixup for this input. |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 655 | #if defined(OS_WIN) |
[email protected] | 036a5f3 | 2013-12-25 00:26:11 | [diff] [blame] | 656 | std::string text_utf8 = base::WideToUTF8(text.value()); |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 657 | #elif defined(OS_POSIX) |
[email protected] | 7e56381 | 2010-03-22 20:05:59 | [diff] [blame] | 658 | std::string text_utf8 = text.value(); |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 659 | #endif |
[email protected] | 76e7da2 | 2010-06-18 22:44:49 | [diff] [blame] | 660 | return FixupURL(text_utf8, std::string()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 661 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 662 | |
[email protected] | 9b5b1d60 | 2014-06-12 14:29:02 | [diff] [blame] | 663 | void url_fixer::OffsetComponent(int offset, url::Component* part) { |
[email protected] | f20dead | 2013-03-02 03:01:48 | [diff] [blame] | 664 | DCHECK(part); |
| 665 | |
| 666 | if (part->is_valid()) { |
[email protected] | d1e83b3 | 2010-12-22 00:34:35 | [diff] [blame] | 667 | // Offset the location of this component. |
| 668 | part->begin += offset; |
[email protected] | f20dead | 2013-03-02 03:01:48 | [diff] [blame] | 669 | |
[email protected] | d1e83b3 | 2010-12-22 00:34:35 | [diff] [blame] | 670 | // This part might not have existed in the original text. |
| 671 | if (part->begin < 0) |
| 672 | part->reset(); |
| 673 | } |
| 674 | } |
[email protected] | 8f6e532 | 2014-08-11 08:06:08 | [diff] [blame] | 675 | |
| 676 | bool url_fixer::IsEquivalentScheme(const std::string& scheme1, |
| 677 | const std::string& scheme2) { |
| 678 | return scheme1 == scheme2 || |
| 679 | (scheme1 == url::kAboutScheme && scheme2 == kChromeUIScheme) || |
| 680 | (scheme1 == kChromeUIScheme && scheme2 == url::kAboutScheme); |
| 681 | } |