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