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