| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/net/url_fixer_upper.h" | 5 #include "chrome/browser/net/url_fixer_upper.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #if defined(OS_POSIX) | 9 #if defined(OS_POSIX) |
| 10 #include "base/environment.h" | 10 #include "base/environment.h" |
| (...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 // Otherwise, we need to look at things carefully. | 400 // Otherwise, we need to look at things carefully. |
| 401 std::string scheme; | 401 std::string scheme; |
| 402 if (!GetValidScheme(text, &parts->scheme, &scheme)) { | 402 if (!GetValidScheme(text, &parts->scheme, &scheme)) { |
| 403 // Couldn't determine the scheme, so just pick one. | 403 // Couldn't determine the scheme, so just pick one. |
| 404 parts->scheme.reset(); | 404 parts->scheme.reset(); |
| 405 scheme.assign(StartsWithASCII(text, "ftp.", false) ? | 405 scheme.assign(StartsWithASCII(text, "ftp.", false) ? |
| 406 chrome::kFtpScheme : chrome::kHttpScheme); | 406 chrome::kFtpScheme : chrome::kHttpScheme); |
| 407 } | 407 } |
| 408 | 408 |
| 409 // Not segmenting file schemes or nonstandard schemes. | 409 // Not segmenting file schemes or nonstandard schemes. |
| 410 if ((scheme == chrome::kFileScheme) || | 410 if ((scheme != chrome::kAboutScheme) && (scheme != chrome::kChromeUIScheme) && |
| 411 !url_util::IsStandard(scheme.c_str(), | 411 ((scheme == chrome::kFileScheme) || !url_util::IsStandard(scheme.c_str(), |
| 412 url_parse::Component(0, static_cast<int>(scheme.length())))) | 412 url_parse::Component(0, static_cast<int>(scheme.length()))))) |
| 413 return scheme; | 413 return scheme; |
| 414 | 414 |
| 415 if (parts->scheme.is_valid()) { | 415 if (parts->scheme.is_valid()) { |
| 416 // Have the GURL parser do the heavy lifting for us. | 416 // Have the GURL parser do the heavy lifting for us. |
| 417 url_parse::ParseStandardURL(text.data(), static_cast<int>(text.length()), | 417 url_parse::ParseStandardURL(text.data(), static_cast<int>(text.length()), |
| 418 parts); | 418 parts); |
| 419 return scheme; | 419 return scheme; |
| 420 } | 420 } |
| 421 | 421 |
| 422 // We need to add a scheme in order for ParseStandardURL to be happy. | 422 // We need to add a scheme in order for ParseStandardURL to be happy. |
| 423 // Find the first non-whitespace character. | 423 // Find the first non-whitespace character. |
| 424 std::string::const_iterator first_nonwhite = text.begin(); | 424 std::string::const_iterator first_nonwhite = text.begin(); |
| 425 while ((first_nonwhite != text.end()) && IsWhitespace(*first_nonwhite)) | 425 while ((first_nonwhite != text.end()) && IsWhitespace(*first_nonwhite)) |
| 426 ++first_nonwhite; | 426 ++first_nonwhite; |
| 427 | 427 |
| 428 // Construct the text to parse by inserting the scheme. | 428 // Construct the text to parse by inserting the scheme. |
| 429 std::string inserted_text(scheme); | 429 std::string inserted_text(scheme); |
| 430 inserted_text.append("://"); | 430 inserted_text.append(chrome::kStandardSchemeSeparator); |
| 431 std::string text_to_parse(text.begin(), first_nonwhite); | 431 std::string text_to_parse(text.begin(), first_nonwhite); |
| 432 text_to_parse.append(inserted_text); | 432 text_to_parse.append(inserted_text); |
| 433 text_to_parse.append(first_nonwhite, text.end()); | 433 text_to_parse.append(first_nonwhite, text.end()); |
| 434 | 434 |
| 435 // Have the GURL parser do the heavy lifting for us. | 435 // Have the GURL parser do the heavy lifting for us. |
| 436 url_parse::ParseStandardURL(text_to_parse.data(), | 436 url_parse::ParseStandardURL(text_to_parse.data(), |
| 437 static_cast<int>(text_to_parse.length()), | 437 static_cast<int>(text_to_parse.length()), |
| 438 parts); | 438 parts); |
| 439 | 439 |
| 440 // Offset the results of the parse to match the original text. | 440 // Offset the results of the parse to match the original text. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 } | 475 } |
| 476 | 476 |
| 477 // We handle the file scheme separately. | 477 // We handle the file scheme separately. |
| 478 if (scheme == chrome::kFileScheme) | 478 if (scheme == chrome::kFileScheme) |
| 479 return GURL(parts.scheme.is_valid() ? text : FixupPath(text)); | 479 return GURL(parts.scheme.is_valid() ? text : FixupPath(text)); |
| 480 | 480 |
| 481 // For some schemes whose layouts we understand, we rebuild it. | 481 // For some schemes whose layouts we understand, we rebuild it. |
| 482 if (url_util::IsStandard(scheme.c_str(), | 482 if (url_util::IsStandard(scheme.c_str(), |
| 483 url_parse::Component(0, static_cast<int>(scheme.length())))) { | 483 url_parse::Component(0, static_cast<int>(scheme.length())))) { |
| 484 std::string url(scheme); | 484 std::string url(scheme); |
| 485 url.append("://"); | 485 url.append(chrome::kStandardSchemeSeparator); |
| 486 | 486 |
| 487 // We need to check whether the |username| is valid because it is our | 487 // We need to check whether the |username| is valid because it is our |
| 488 // responsibility to append the '@' to delineate the user information from | 488 // responsibility to append the '@' to delineate the user information from |
| 489 // the host portion of the URL. | 489 // the host portion of the URL. |
| 490 if (parts.username.is_valid()) { | 490 if (parts.username.is_valid()) { |
| 491 FixupUsername(trimmed, parts.username, &url); | 491 FixupUsername(trimmed, parts.username, &url); |
| 492 FixupPassword(trimmed, parts.password, &url); | 492 FixupPassword(trimmed, parts.password, &url); |
| 493 url.append("@"); | 493 url.append("@"); |
| 494 } | 494 } |
| 495 | 495 |
| 496 FixupHost(trimmed, parts.host, parts.scheme.is_valid(), desired_tld, &url); | 496 FixupHost(trimmed, parts.host, parts.scheme.is_valid(), desired_tld, &url); |
| 497 FixupPort(trimmed, parts.port, &url); | 497 FixupPort(trimmed, parts.port, &url); |
| 498 FixupPath(trimmed, parts.path, &url); | 498 FixupPath(trimmed, parts.path, &url); |
| 499 FixupQuery(trimmed, parts.query, &url); | 499 FixupQuery(trimmed, parts.query, &url); |
| 500 FixupRef(trimmed, parts.ref, &url); | 500 FixupRef(trimmed, parts.ref, &url); |
| 501 | 501 |
| 502 return GURL(url); | 502 return GURL(url); |
| 503 } | 503 } |
| 504 | 504 |
| 505 // In the worst-case, we insert a scheme if the URL lacks one. | 505 // In the worst-case, we insert a scheme if the URL lacks one. |
| 506 if (!parts.scheme.is_valid()) { | 506 if (!parts.scheme.is_valid()) { |
| 507 std::string fixed_scheme(scheme); | 507 std::string fixed_scheme(scheme); |
| 508 fixed_scheme.append("://"); | 508 fixed_scheme.append(chrome::kStandardSchemeSeparator); |
| 509 trimmed.insert(0, fixed_scheme); | 509 trimmed.insert(0, fixed_scheme); |
| 510 } | 510 } |
| 511 | 511 |
| 512 return GURL(trimmed); | 512 return GURL(trimmed); |
| 513 } | 513 } |
| 514 | 514 |
| 515 GURL URLFixerUpper::FixupChromeURL(const GURL& url) { |
| 516 DCHECK(url.SchemeIs(chrome::kAboutScheme) || |
| 517 url.SchemeIs(chrome::kChromeUIScheme)); |
| 518 |
| 519 // Segment the URL. |
| 520 url_parse::Parsed parts; |
| 521 SegmentURL(url.spec(), &parts); |
| 522 std::string new_url(chrome::kChromeUIScheme); |
| 523 new_url.append(chrome::kStandardSchemeSeparator); |
| 524 |
| 525 // We need to check whether the |username| is valid because it is our |
| 526 // responsibility to append the '@' to delineate the user information from |
| 527 // the host portion of the URL. |
| 528 if (parts.username.is_valid()) { |
| 529 FixupUsername(url.spec(), parts.username, &new_url); |
| 530 FixupPassword(url.spec(), parts.password, &new_url); |
| 531 new_url.append("@"); |
| 532 } |
| 533 |
| 534 if (parts.host.is_valid()) |
| 535 FixupHost(url.spec(), parts.host, true, std::string(), &new_url); |
| 536 else |
| 537 new_url.append(chrome::kChromeUIVersionHost); |
| 538 FixupPort(url.spec(), parts.port, &new_url); |
| 539 FixupPath(url.spec(), parts.path, &new_url); |
| 540 FixupQuery(url.spec(), parts.query, &new_url); |
| 541 FixupRef(url.spec(), parts.ref, &new_url); |
| 542 return GURL(new_url); |
| 543 } |
| 544 |
| 515 // The rules are different here than for regular fixup, since we need to handle | 545 // The rules are different here than for regular fixup, since we need to handle |
| 516 // input like "hello.html" and know to look in the current directory. Regular | 546 // input like "hello.html" and know to look in the current directory. Regular |
| 517 // fixup will look for cues that it is actually a file path before trying to | 547 // fixup will look for cues that it is actually a file path before trying to |
| 518 // figure out what file it is. If our logic doesn't work, we will fall back on | 548 // figure out what file it is. If our logic doesn't work, we will fall back on |
| 519 // regular fixup. | 549 // regular fixup. |
| 520 GURL URLFixerUpper::FixupRelativeFile(const FilePath& base_dir, | 550 GURL URLFixerUpper::FixupRelativeFile(const FilePath& base_dir, |
| 521 const FilePath& text) { | 551 const FilePath& text) { |
| 522 FilePath old_cur_directory; | 552 FilePath old_cur_directory; |
| 523 if (!base_dir.empty()) { | 553 if (!base_dir.empty()) { |
| 524 // Save the old current directory before we move to the new one. | 554 // Save the old current directory before we move to the new one. |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 586 | 616 |
| 587 if (part->is_valid()) { | 617 if (part->is_valid()) { |
| 588 // Offset the location of this component. | 618 // Offset the location of this component. |
| 589 part->begin += offset; | 619 part->begin += offset; |
| 590 | 620 |
| 591 // This part might not have existed in the original text. | 621 // This part might not have existed in the original text. |
| 592 if (part->begin < 0) | 622 if (part->begin < 0) |
| 593 part->reset(); | 623 part->reset(); |
| 594 } | 624 } |
| 595 } | 625 } |
| OLD | NEW |