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_fixer/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 "net/base/escape.h" |
| 18 #include "net/base/filename_util.h" |
| 19 #include "net/base/net_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 const char* url_fixer::home_directory_override = NULL; |
| 26 |
| 27 namespace { |
| 28 |
| 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 |
| 34 // TODO(estade): Remove these ugly, ugly functions. They are only used in |
| 35 // SegmentURL. A url::Parsed object keeps track of a bunch of indices into |
| 36 // a url string, and these need to be updated when the URL is converted from |
| 37 // UTF8 to UTF16. Instead of this after-the-fact adjustment, we should parse it |
| 38 // in the correct string format to begin with. |
| 39 url::Component UTF8ComponentToUTF16Component( |
| 40 const std::string& text_utf8, |
| 41 const url::Component& component_utf8) { |
| 42 if (component_utf8.len == -1) |
| 43 return url::Component(); |
| 44 |
| 45 std::string before_component_string = |
| 46 text_utf8.substr(0, component_utf8.begin); |
| 47 std::string component_string = |
| 48 text_utf8.substr(component_utf8.begin, component_utf8.len); |
| 49 base::string16 before_component_string_16 = |
| 50 base::UTF8ToUTF16(before_component_string); |
| 51 base::string16 component_string_16 = base::UTF8ToUTF16(component_string); |
| 52 url::Component component_16(before_component_string_16.length(), |
| 53 component_string_16.length()); |
| 54 return component_16; |
| 55 } |
| 56 |
| 57 void UTF8PartsToUTF16Parts(const std::string& text_utf8, |
| 58 const url::Parsed& parts_utf8, |
| 59 url::Parsed* parts) { |
| 60 if (base::IsStringASCII(text_utf8)) { |
| 61 *parts = parts_utf8; |
| 62 return; |
| 63 } |
| 64 |
| 65 parts->scheme = UTF8ComponentToUTF16Component(text_utf8, parts_utf8.scheme); |
| 66 parts->username = |
| 67 UTF8ComponentToUTF16Component(text_utf8, parts_utf8.username); |
| 68 parts->password = |
| 69 UTF8ComponentToUTF16Component(text_utf8, parts_utf8.password); |
| 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); |
| 75 } |
| 76 |
| 77 base::TrimPositions TrimWhitespaceUTF8(const std::string& input, |
| 78 base::TrimPositions positions, |
| 79 std::string* output) { |
| 80 // This implementation is not so fast since it converts the text encoding |
| 81 // twice. Please feel free to file a bug if this function hurts the |
| 82 // performance of Chrome. |
| 83 DCHECK(base::IsStringUTF8(input)); |
| 84 base::string16 input16 = base::UTF8ToUTF16(input); |
| 85 base::string16 output16; |
| 86 base::TrimPositions result = |
| 87 base::TrimWhitespace(input16, positions, &output16); |
| 88 *output = base::UTF16ToUTF8(output16); |
| 89 return result; |
| 90 } |
| 91 |
| 92 // does some basic fixes for input that we want to test for file-ness |
| 93 void PrepareStringForFileOps(const base::FilePath& text, |
| 94 base::FilePath::StringType* output) { |
| 95 #if defined(OS_WIN) |
| 96 base::TrimWhitespace(text.value(), base::TRIM_ALL, output); |
| 97 replace(output->begin(), output->end(), '/', '\\'); |
| 98 #else |
| 99 TrimWhitespaceUTF8(text.value(), base::TRIM_ALL, output); |
| 100 #endif |
| 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. |
| 106 bool ValidPathForFile(const base::FilePath::StringType& text, |
| 107 base::FilePath* full_path) { |
| 108 base::FilePath file_path = base::MakeAbsoluteFilePath(base::FilePath(text)); |
| 109 if (file_path.empty()) |
| 110 return false; |
| 111 |
| 112 if (!base::PathExists(file_path)) |
| 113 return false; |
| 114 |
| 115 *full_path = file_path; |
| 116 return true; |
| 117 } |
| 118 |
| 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. |
| 122 std::string FixupHomedir(const std::string& text) { |
| 123 DCHECK(text.length() > 0 && text[0] == '~'); |
| 124 |
| 125 if (text.length() == 1 || text[1] == '/') { |
| 126 base::FilePath file_path; |
| 127 if (url_fixer::home_directory_override) |
| 128 file_path = base::FilePath(url_fixer::home_directory_override); |
| 129 else |
| 130 PathService::Get(base::DIR_HOME, &file_path); |
| 131 |
| 132 // We'll probably break elsewhere if $HOME is undefined, but check here |
| 133 // just in case. |
| 134 if (file_path.value().empty()) |
| 135 return text; |
| 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(); |
| 142 } |
| 143 |
| 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. |
| 147 |
| 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 |
| 157 // Tries to create a file: URL from |text| if it looks like a filename, even if |
| 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). |
| 162 std::string FixupPath(const std::string& text) { |
| 163 DCHECK(!text.empty()); |
| 164 |
| 165 base::FilePath::StringType filename; |
| 166 #if defined(OS_WIN) |
| 167 base::FilePath input_path(base::UTF8ToWide(text)); |
| 168 PrepareStringForFileOps(input_path, &filename); |
| 169 |
| 170 // Fixup Windows-style drive letters, where "C:" gets rewritten to "C|". |
| 171 if (filename.length() > 1 && filename[1] == '|') |
| 172 filename[1] = ':'; |
| 173 #elif defined(OS_POSIX) |
| 174 base::FilePath input_path(text); |
| 175 PrepareStringForFileOps(input_path, &filename); |
| 176 if (filename.length() > 0 && filename[0] == '~') |
| 177 filename = FixupHomedir(filename); |
| 178 #endif |
| 179 |
| 180 // Here, we know the input looks like a file. |
| 181 GURL file_url = net::FilePathToFileURL(base::FilePath(filename)); |
| 182 if (file_url.is_valid()) { |
| 183 return base::UTF16ToUTF8(net::FormatUrl(file_url, |
| 184 std::string(), |
| 185 net::kFormatUrlOmitUsernamePassword, |
| 186 net::UnescapeRule::NORMAL, |
| 187 NULL, |
| 188 NULL, |
| 189 NULL)); |
| 190 } |
| 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 |
| 197 // |desired_tld| to the domain, and prepends "www." unless it's already present. |
| 198 void AddDesiredTLD(const std::string& desired_tld, std::string* domain) { |
| 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 |
| 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". |
| 211 const size_t registry_length = |
| 212 net::registry_controlled_domains::GetRegistryLength( |
| 213 *domain, |
| 214 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, |
| 215 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); |
| 216 if ((registry_length != 0) && (registry_length != std::string::npos)) |
| 217 return; |
| 218 |
| 219 // Add the suffix at the end of the domain. |
| 220 const size_t domain_length(domain->length()); |
| 221 DCHECK_GT(domain_length, 0U); |
| 222 DCHECK_NE(desired_tld[0], '.'); |
| 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. |
| 228 const std::string prefix("www."); |
| 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 |
| 235 inline void FixupUsername(const std::string& text, |
| 236 const url::Component& part, |
| 237 std::string* url) { |
| 238 if (!part.is_valid()) |
| 239 return; |
| 240 |
| 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 |
| 247 inline void FixupPassword(const std::string& text, |
| 248 const url::Component& part, |
| 249 std::string* url) { |
| 250 if (!part.is_valid()) |
| 251 return; |
| 252 |
| 253 // We don't fix up the password at the moment. |
| 254 url->append(":"); |
| 255 url->append(text, part.begin, part.len); |
| 256 } |
| 257 |
| 258 void FixupHost(const std::string& text, |
| 259 const url::Component& part, |
| 260 bool has_scheme, |
| 261 const std::string& desired_tld, |
| 262 std::string* url) { |
| 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. |
| 270 std::string domain(text, part.begin, part.len); |
| 271 const size_t first_nondot(domain.find_first_not_of('.')); |
| 272 if (first_nondot != std::string::npos) { |
| 273 domain.erase(0, first_nondot); |
| 274 size_t last_nondot(domain.find_last_not_of('.')); |
| 275 DCHECK(last_nondot != std::string::npos); |
| 276 last_nondot += 2; // Point at second period in ending string |
| 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); |
| 283 |
| 284 url->append(domain); |
| 285 } |
| 286 |
| 287 void FixupPort(const std::string& text, |
| 288 const url::Component& part, |
| 289 std::string* url) { |
| 290 if (!part.is_valid()) |
| 291 return; |
| 292 |
| 293 // We don't fix up the port at the moment. |
| 294 url->append(":"); |
| 295 url->append(text, part.begin, part.len); |
| 296 } |
| 297 |
| 298 inline void FixupPath(const std::string& text, |
| 299 const url::Component& part, |
| 300 std::string* url) { |
| 301 if (!part.is_valid() || part.len == 0) { |
| 302 // We should always have a path. |
| 303 url->append("/"); |
| 304 return; |
| 305 } |
| 306 |
| 307 // Append the path as is. |
| 308 url->append(text, part.begin, part.len); |
| 309 } |
| 310 |
| 311 inline void FixupQuery(const std::string& text, |
| 312 const url::Component& part, |
| 313 std::string* url) { |
| 314 if (!part.is_valid()) |
| 315 return; |
| 316 |
| 317 // We don't fix up the query at the moment. |
| 318 url->append("?"); |
| 319 url->append(text, part.begin, part.len); |
| 320 } |
| 321 |
| 322 inline void FixupRef(const std::string& text, |
| 323 const url::Component& part, |
| 324 std::string* url) { |
| 325 if (!part.is_valid()) |
| 326 return; |
| 327 |
| 328 // We don't fix up the ref at the moment. |
| 329 url->append("#"); |
| 330 url->append(text, part.begin, part.len); |
| 331 } |
| 332 |
| 333 bool HasPort(const std::string& original_text, |
| 334 const url::Component& scheme_component) { |
| 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()) && |
| 339 !url::IsAuthorityTerminator(original_text[port_end])) |
| 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) { |
| 346 if (!base::IsAsciiDigit(original_text[i])) |
| 347 return false; |
| 348 } |
| 349 |
| 350 return true; |
| 351 } |
| 352 |
| 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. |
| 357 bool GetValidScheme(const std::string& text, |
| 358 url::Component* scheme_component, |
| 359 std::string* canon_scheme) { |
| 360 canon_scheme->clear(); |
| 361 |
| 362 // Locate everything up to (but not including) the first ':' |
| 363 if (!url::ExtractScheme( |
| 364 text.data(), static_cast<int>(text.length()), scheme_component)) { |
| 365 return false; |
| 366 } |
| 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. |
| 371 url::StdStringCanonOutput canon_scheme_output(canon_scheme); |
| 372 url::Component canon_scheme_component; |
| 373 if (!url::CanonicalizeScheme(text.data(), |
| 374 *scheme_component, |
| 375 &canon_scheme_output, |
| 376 &canon_scheme_component)) { |
| 377 return false; |
| 378 } |
| 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. |
| 386 if (canon_scheme->find('.') != std::string::npos) |
| 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 |
| 399 // Performs the work for url_fixer::SegmentURL. |text| may be modified on |
| 400 // output on success: a semicolon following a valid scheme is replaced with a |
| 401 // colon. |
| 402 std::string SegmentURLInternal(std::string* text, url::Parsed* parts) { |
| 403 // Initialize the result. |
| 404 *parts = url::Parsed(); |
| 405 |
| 406 std::string trimmed; |
| 407 TrimWhitespaceUTF8(*text, base::TRIM_ALL, &trimmed); |
| 408 if (trimmed.empty()) |
| 409 return std::string(); // Nothing to segment. |
| 410 |
| 411 #if defined(OS_WIN) |
| 412 int trimmed_length = static_cast<int>(trimmed.length()); |
| 413 if (url::DoesBeginWindowsDriveSpec(trimmed.data(), 0, trimmed_length) || |
| 414 url::DoesBeginUNCPath(trimmed.data(), 0, trimmed_length, true)) |
| 415 return "file"; |
| 416 #elif defined(OS_POSIX) |
| 417 if (base::FilePath::IsSeparator(trimmed.data()[0]) || |
| 418 trimmed.data()[0] == '~') |
| 419 return "file"; |
| 420 #endif |
| 421 |
| 422 // Otherwise, we need to look at things carefully. |
| 423 std::string scheme; |
| 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(); |
| 439 scheme = base::StartsWith(*text, "ftp.", |
| 440 base::CompareCase::INSENSITIVE_ASCII) ? |
| 441 url::kFtpScheme : url::kHttpScheme; |
| 442 } |
| 443 } |
| 444 |
| 445 // Proceed with about and chrome schemes, but not file or nonstandard schemes. |
| 446 if ((scheme != url::kAboutScheme) && (scheme != kChromeUIScheme) && |
| 447 ((scheme == url::kFileScheme) || |
| 448 !url::IsStandard( |
| 449 scheme.c_str(), |
| 450 url::Component(0, static_cast<int>(scheme.length()))))) { |
| 451 return scheme; |
| 452 } |
| 453 |
| 454 if (scheme == url::kFileSystemScheme) { |
| 455 // Have the GURL parser do the heavy lifting for us. |
| 456 url::ParseFileSystemURL( |
| 457 text->data(), static_cast<int>(text->length()), parts); |
| 458 return scheme; |
| 459 } |
| 460 |
| 461 if (parts->scheme.is_valid()) { |
| 462 // Have the GURL parser do the heavy lifting for us. |
| 463 url::ParseStandardURL( |
| 464 text->data(), static_cast<int>(text->length()), parts); |
| 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. |
| 470 std::string::iterator first_nonwhite = text->begin(); |
| 471 while ((first_nonwhite != text->end()) && |
| 472 base::IsUnicodeWhitespace(*first_nonwhite)) |
| 473 ++first_nonwhite; |
| 474 |
| 475 // Construct the text to parse by inserting the scheme. |
| 476 std::string inserted_text(scheme); |
| 477 inserted_text.append(url::kStandardSchemeSeparator); |
| 478 std::string text_to_parse(text->begin(), first_nonwhite); |
| 479 text_to_parse.append(inserted_text); |
| 480 text_to_parse.append(first_nonwhite, text->end()); |
| 481 |
| 482 // Have the GURL parser do the heavy lifting for us. |
| 483 url::ParseStandardURL( |
| 484 text_to_parse.data(), static_cast<int>(text_to_parse.length()), parts); |
| 485 |
| 486 // Offset the results of the parse to match the original text. |
| 487 const int offset = -static_cast<int>(inserted_text.length()); |
| 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); |
| 496 |
| 497 return scheme; |
| 498 } |
| 499 |
| 500 } // namespace |
| 501 |
| 502 std::string url_fixer::SegmentURL(const std::string& text, url::Parsed* parts) { |
| 503 std::string mutable_text(text); |
| 504 return SegmentURLInternal(&mutable_text, parts); |
| 505 } |
| 506 |
| 507 base::string16 url_fixer::SegmentURL(const base::string16& text, |
| 508 url::Parsed* parts) { |
| 509 std::string text_utf8 = base::UTF16ToUTF8(text); |
| 510 url::Parsed parts_utf8; |
| 511 std::string scheme_utf8 = SegmentURL(text_utf8, &parts_utf8); |
| 512 UTF8PartsToUTF16Parts(text_utf8, parts_utf8, parts); |
| 513 return base::UTF8ToUTF16(scheme_utf8); |
| 514 } |
| 515 |
| 516 GURL url_fixer::FixupURL(const std::string& text, |
| 517 const std::string& desired_tld) { |
| 518 std::string trimmed; |
| 519 TrimWhitespaceUTF8(text, base::TRIM_ALL, &trimmed); |
| 520 if (trimmed.empty()) |
| 521 return GURL(); // Nothing here. |
| 522 |
| 523 // Segment the URL. |
| 524 url::Parsed parts; |
| 525 std::string scheme(SegmentURLInternal(&trimmed, &parts)); |
| 526 |
| 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". |
| 529 if (scheme == kViewSourceScheme) { |
| 530 // Reject "view-source:view-source:..." to avoid deep recursion. |
| 531 std::string view_source(kViewSourceScheme + std::string(":")); |
| 532 if (!base::StartsWith(text, view_source + view_source, |
| 533 base::CompareCase::INSENSITIVE_ASCII)) { |
| 534 return GURL(kViewSourceScheme + std::string(":") + |
| 535 FixupURL(trimmed.substr(scheme.length() + 1), desired_tld) |
| 536 .possibly_invalid_spec()); |
| 537 } |
| 538 } |
| 539 |
| 540 // We handle the file scheme separately. |
| 541 if (scheme == url::kFileScheme) |
| 542 return GURL(parts.scheme.is_valid() ? text : FixupPath(text)); |
| 543 |
| 544 // We handle the filesystem scheme separately. |
| 545 if (scheme == url::kFileSystemScheme) { |
| 546 if (parts.inner_parsed() && parts.inner_parsed()->scheme.is_valid()) |
| 547 return GURL(text); |
| 548 return GURL(); |
| 549 } |
| 550 |
| 551 // Parse and rebuild about: and chrome: URLs, except about:blank. |
| 552 bool chrome_url = |
| 553 !base::LowerCaseEqualsASCII(trimmed, url::kAboutBlankURL) && |
| 554 ((scheme == url::kAboutScheme) || (scheme == kChromeUIScheme)); |
| 555 |
| 556 // For some schemes whose layouts we understand, we rebuild it. |
| 557 if (chrome_url || |
| 558 url::IsStandard(scheme.c_str(), |
| 559 url::Component(0, static_cast<int>(scheme.length())))) { |
| 560 // Replace the about: scheme with the chrome: scheme. |
| 561 std::string url(chrome_url ? kChromeUIScheme : scheme); |
| 562 url.append(url::kStandardSchemeSeparator); |
| 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); |
| 570 url.append("@"); |
| 571 } |
| 572 |
| 573 FixupHost(trimmed, parts.host, parts.scheme.is_valid(), desired_tld, &url); |
| 574 if (chrome_url && !parts.host.is_valid()) |
| 575 url.append(kChromeUIDefaultHost); |
| 576 FixupPort(trimmed, parts.port, &url); |
| 577 FixupPath(trimmed, parts.path, &url); |
| 578 FixupQuery(trimmed, parts.query, &url); |
| 579 FixupRef(trimmed, parts.ref, &url); |
| 580 |
| 581 return GURL(url); |
| 582 } |
| 583 |
| 584 // In the worst-case, we insert a scheme if the URL lacks one. |
| 585 if (!parts.scheme.is_valid()) { |
| 586 std::string fixed_scheme(scheme); |
| 587 fixed_scheme.append(url::kStandardSchemeSeparator); |
| 588 trimmed.insert(0, fixed_scheme); |
| 589 } |
| 590 |
| 591 return GURL(trimmed); |
| 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. |
| 599 GURL url_fixer::FixupRelativeFile(const base::FilePath& base_dir, |
| 600 const base::FilePath& text) { |
| 601 base::FilePath old_cur_directory; |
| 602 if (!base_dir.empty()) { |
| 603 // Save the old current directory before we move to the new one. |
| 604 base::GetCurrentDirectory(&old_cur_directory); |
| 605 base::SetCurrentDirectory(base_dir); |
| 606 } |
| 607 |
| 608 // Allow funny input with extra whitespace and the wrong kind of slashes. |
| 609 base::FilePath::StringType trimmed; |
| 610 PrepareStringForFileOps(text, &trimmed); |
| 611 |
| 612 bool is_file = true; |
| 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; |
| 617 base::FilePath full_path; |
| 618 if (is_file && !ValidPathForFile(trimmed, &full_path)) { |
| 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. |
| 622 #if defined(OS_WIN) |
| 623 std::wstring unescaped = base::UTF8ToWide(net::UnescapeURLComponent( |
| 624 base::WideToUTF8(trimmed), |
| 625 net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS)); |
| 626 #elif defined(OS_POSIX) |
| 627 std::string unescaped = net::UnescapeURLComponent( |
| 628 trimmed, |
| 629 net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS); |
| 630 #endif |
| 631 |
| 632 if (!ValidPathForFile(unescaped, &full_path)) |
| 633 is_file = false; |
| 634 } |
| 635 |
| 636 // Put back the current directory if we saved it. |
| 637 if (!base_dir.empty()) |
| 638 base::SetCurrentDirectory(old_cur_directory); |
| 639 |
| 640 if (is_file) { |
| 641 GURL file_url = net::FilePathToFileURL(full_path); |
| 642 if (file_url.is_valid()) |
| 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))); |
| 651 // Invalid files fall through to regular processing. |
| 652 } |
| 653 |
| 654 // Fall back on regular fixup for this input. |
| 655 #if defined(OS_WIN) |
| 656 std::string text_utf8 = base::WideToUTF8(text.value()); |
| 657 #elif defined(OS_POSIX) |
| 658 std::string text_utf8 = text.value(); |
| 659 #endif |
| 660 return FixupURL(text_utf8, std::string()); |
| 661 } |
| 662 |
| 663 void url_fixer::OffsetComponent(int offset, url::Component* part) { |
| 664 DCHECK(part); |
| 665 |
| 666 if (part->is_valid()) { |
| 667 // Offset the location of this component. |
| 668 part->begin += offset; |
| 669 |
| 670 // This part might not have existed in the original text. |
| 671 if (part->begin < 0) |
| 672 part->reset(); |
| 673 } |
| 674 } |
| 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 } |
OLD | NEW |