OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "net/base/net_util.h" | 5 #include "net/base/net_util.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/i18n/time_formatting.h" | 10 #include "base/i18n/time_formatting.h" |
(...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 | 402 |
403 // Need to add the dot we just found (if we found one). | 403 // Need to add the dot we just found (if we found one). |
404 if (component_end < input16.length()) | 404 if (component_end < input16.length()) |
405 out16.push_back('.'); | 405 out16.push_back('.'); |
406 } | 406 } |
407 } | 407 } |
408 return out16; | 408 return out16; |
409 } | 409 } |
410 | 410 |
411 // If |component| is valid, its begin is incremented by |delta|. | 411 // If |component| is valid, its begin is incremented by |delta|. |
412 void AdjustComponent(int delta, url_parse::Component* component) { | 412 void AdjustComponent(int delta, url::Component* component) { |
413 if (!component->is_valid()) | 413 if (!component->is_valid()) |
414 return; | 414 return; |
415 | 415 |
416 DCHECK(delta >= 0 || component->begin >= -delta); | 416 DCHECK(delta >= 0 || component->begin >= -delta); |
417 component->begin += delta; | 417 component->begin += delta; |
418 } | 418 } |
419 | 419 |
420 // Adjusts all the components of |parsed| by |delta|, except for the scheme. | 420 // Adjusts all the components of |parsed| by |delta|, except for the scheme. |
421 void AdjustAllComponentsButScheme(int delta, url_parse::Parsed* parsed) { | 421 void AdjustAllComponentsButScheme(int delta, url::Parsed* parsed) { |
422 AdjustComponent(delta, &(parsed->username)); | 422 AdjustComponent(delta, &(parsed->username)); |
423 AdjustComponent(delta, &(parsed->password)); | 423 AdjustComponent(delta, &(parsed->password)); |
424 AdjustComponent(delta, &(parsed->host)); | 424 AdjustComponent(delta, &(parsed->host)); |
425 AdjustComponent(delta, &(parsed->port)); | 425 AdjustComponent(delta, &(parsed->port)); |
426 AdjustComponent(delta, &(parsed->path)); | 426 AdjustComponent(delta, &(parsed->path)); |
427 AdjustComponent(delta, &(parsed->query)); | 427 AdjustComponent(delta, &(parsed->query)); |
428 AdjustComponent(delta, &(parsed->ref)); | 428 AdjustComponent(delta, &(parsed->ref)); |
429 } | 429 } |
430 | 430 |
431 // Helper for FormatUrlWithOffsets(). | 431 // Helper for FormatUrlWithOffsets(). |
432 base::string16 FormatViewSourceUrl( | 432 base::string16 FormatViewSourceUrl( |
433 const GURL& url, | 433 const GURL& url, |
434 const std::string& languages, | 434 const std::string& languages, |
435 FormatUrlTypes format_types, | 435 FormatUrlTypes format_types, |
436 UnescapeRule::Type unescape_rules, | 436 UnescapeRule::Type unescape_rules, |
437 url_parse::Parsed* new_parsed, | 437 url::Parsed* new_parsed, |
438 size_t* prefix_end, | 438 size_t* prefix_end, |
439 base::OffsetAdjuster::Adjustments* adjustments) { | 439 base::OffsetAdjuster::Adjustments* adjustments) { |
440 DCHECK(new_parsed); | 440 DCHECK(new_parsed); |
441 const char kViewSource[] = "view-source:"; | 441 const char kViewSource[] = "view-source:"; |
442 const size_t kViewSourceLength = arraysize(kViewSource) - 1; | 442 const size_t kViewSourceLength = arraysize(kViewSource) - 1; |
443 | 443 |
444 // Format the underlying URL and record adjustments. | 444 // Format the underlying URL and record adjustments. |
445 const std::string& url_str(url.possibly_invalid_spec()); | 445 const std::string& url_str(url.possibly_invalid_spec()); |
446 adjustments->clear(); | 446 adjustments->clear(); |
447 base::string16 result(base::ASCIIToUTF16(kViewSource) + | 447 base::string16 result(base::ASCIIToUTF16(kViewSource) + |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
520 const UnescapeRule::Type unescape_rules_; | 520 const UnescapeRule::Type unescape_rules_; |
521 }; | 521 }; |
522 | 522 |
523 // Transforms the portion of |spec| covered by |original_component| according to | 523 // Transforms the portion of |spec| covered by |original_component| according to |
524 // |transform|. Appends the result to |output|. If |output_component| is | 524 // |transform|. Appends the result to |output|. If |output_component| is |
525 // non-NULL, its start and length are set to the transformed component's new | 525 // non-NULL, its start and length are set to the transformed component's new |
526 // start and length. If |adjustments| is non-NULL, appends adjustments (if | 526 // start and length. If |adjustments| is non-NULL, appends adjustments (if |
527 // any) that reflect the transformation the original component underwent to | 527 // any) that reflect the transformation the original component underwent to |
528 // become the transformed value appended to |output|. | 528 // become the transformed value appended to |output|. |
529 void AppendFormattedComponent(const std::string& spec, | 529 void AppendFormattedComponent(const std::string& spec, |
530 const url_parse::Component& original_component, | 530 const url::Component& original_component, |
531 const AppendComponentTransform& transform, | 531 const AppendComponentTransform& transform, |
532 base::string16* output, | 532 base::string16* output, |
533 url_parse::Component* output_component, | 533 url::Component* output_component, |
534 base::OffsetAdjuster::Adjustments* adjustments) { | 534 base::OffsetAdjuster::Adjustments* adjustments) { |
535 DCHECK(output); | 535 DCHECK(output); |
536 if (original_component.is_nonempty()) { | 536 if (original_component.is_nonempty()) { |
537 size_t original_component_begin = | 537 size_t original_component_begin = |
538 static_cast<size_t>(original_component.begin); | 538 static_cast<size_t>(original_component.begin); |
539 size_t output_component_begin = output->length(); | 539 size_t output_component_begin = output->length(); |
540 std::string component_str(spec, original_component_begin, | 540 std::string component_str(spec, original_component_begin, |
541 static_cast<size_t>(original_component.len)); | 541 static_cast<size_t>(original_component.len)); |
542 | 542 |
543 // Transform |component_str| and modify |adjustments| appropriately. | 543 // Transform |component_str| and modify |adjustments| appropriately. |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
628 AppendFormattedComponent(url.possibly_invalid_spec(), | 628 AppendFormattedComponent(url.possibly_invalid_spec(), |
629 url.parsed_for_possibly_invalid_spec().host, | 629 url.parsed_for_possibly_invalid_spec().host, |
630 HostComponentTransform(languages), output, NULL, NULL); | 630 HostComponentTransform(languages), output, NULL, NULL); |
631 } | 631 } |
632 | 632 |
633 base::string16 FormatUrlWithOffsets( | 633 base::string16 FormatUrlWithOffsets( |
634 const GURL& url, | 634 const GURL& url, |
635 const std::string& languages, | 635 const std::string& languages, |
636 FormatUrlTypes format_types, | 636 FormatUrlTypes format_types, |
637 UnescapeRule::Type unescape_rules, | 637 UnescapeRule::Type unescape_rules, |
638 url_parse::Parsed* new_parsed, | 638 url::Parsed* new_parsed, |
639 size_t* prefix_end, | 639 size_t* prefix_end, |
640 std::vector<size_t>* offsets_for_adjustment) { | 640 std::vector<size_t>* offsets_for_adjustment) { |
641 base::OffsetAdjuster::Adjustments adjustments; | 641 base::OffsetAdjuster::Adjustments adjustments; |
642 const base::string16& format_url_return_value = | 642 const base::string16& format_url_return_value = |
643 FormatUrlWithAdjustments(url, languages, format_types, unescape_rules, | 643 FormatUrlWithAdjustments(url, languages, format_types, unescape_rules, |
644 new_parsed, prefix_end, &adjustments); | 644 new_parsed, prefix_end, &adjustments); |
645 base::OffsetAdjuster::AdjustOffsets(adjustments, offsets_for_adjustment); | 645 base::OffsetAdjuster::AdjustOffsets(adjustments, offsets_for_adjustment); |
646 if (offsets_for_adjustment) { | 646 if (offsets_for_adjustment) { |
647 std::for_each( | 647 std::for_each( |
648 offsets_for_adjustment->begin(), | 648 offsets_for_adjustment->begin(), |
649 offsets_for_adjustment->end(), | 649 offsets_for_adjustment->end(), |
650 base::LimitOffset<std::string>(format_url_return_value.length())); | 650 base::LimitOffset<std::string>(format_url_return_value.length())); |
651 } | 651 } |
652 return format_url_return_value; | 652 return format_url_return_value; |
653 } | 653 } |
654 | 654 |
655 base::string16 FormatUrlWithAdjustments( | 655 base::string16 FormatUrlWithAdjustments( |
656 const GURL& url, | 656 const GURL& url, |
657 const std::string& languages, | 657 const std::string& languages, |
658 FormatUrlTypes format_types, | 658 FormatUrlTypes format_types, |
659 UnescapeRule::Type unescape_rules, | 659 UnescapeRule::Type unescape_rules, |
660 url_parse::Parsed* new_parsed, | 660 url::Parsed* new_parsed, |
661 size_t* prefix_end, | 661 size_t* prefix_end, |
662 base::OffsetAdjuster::Adjustments* adjustments) { | 662 base::OffsetAdjuster::Adjustments* adjustments) { |
663 DCHECK(adjustments != NULL); | 663 DCHECK(adjustments != NULL); |
664 adjustments->clear(); | 664 adjustments->clear(); |
665 url_parse::Parsed parsed_temp; | 665 url::Parsed parsed_temp; |
666 if (!new_parsed) | 666 if (!new_parsed) |
667 new_parsed = &parsed_temp; | 667 new_parsed = &parsed_temp; |
668 else | 668 else |
669 *new_parsed = url_parse::Parsed(); | 669 *new_parsed = url::Parsed(); |
670 | 670 |
671 // Special handling for view-source:. Don't use content::kViewSourceScheme | 671 // Special handling for view-source:. Don't use content::kViewSourceScheme |
672 // because this library shouldn't depend on chrome. | 672 // because this library shouldn't depend on chrome. |
673 const char* const kViewSource = "view-source"; | 673 const char* const kViewSource = "view-source"; |
674 // Reject "view-source:view-source:..." to avoid deep recursion. | 674 // Reject "view-source:view-source:..." to avoid deep recursion. |
675 const char* const kViewSourceTwice = "view-source:view-source:"; | 675 const char* const kViewSourceTwice = "view-source:view-source:"; |
676 if (url.SchemeIs(kViewSource) && | 676 if (url.SchemeIs(kViewSource) && |
677 !StartsWithASCII(url.possibly_invalid_spec(), kViewSourceTwice, false)) { | 677 !StartsWithASCII(url.possibly_invalid_spec(), kViewSourceTwice, false)) { |
678 return FormatViewSourceUrl(url, languages, format_types, | 678 return FormatViewSourceUrl(url, languages, format_types, |
679 unescape_rules, new_parsed, prefix_end, | 679 unescape_rules, new_parsed, prefix_end, |
680 adjustments); | 680 adjustments); |
681 } | 681 } |
682 | 682 |
683 // We handle both valid and invalid URLs (this will give us the spec | 683 // We handle both valid and invalid URLs (this will give us the spec |
684 // regardless of validity). | 684 // regardless of validity). |
685 const std::string& spec = url.possibly_invalid_spec(); | 685 const std::string& spec = url.possibly_invalid_spec(); |
686 const url_parse::Parsed& parsed = url.parsed_for_possibly_invalid_spec(); | 686 const url::Parsed& parsed = url.parsed_for_possibly_invalid_spec(); |
687 | 687 |
688 // Scheme & separators. These are ASCII. | 688 // Scheme & separators. These are ASCII. |
689 base::string16 url_string; | 689 base::string16 url_string; |
690 url_string.insert( | 690 url_string.insert( |
691 url_string.end(), spec.begin(), | 691 url_string.end(), spec.begin(), |
692 spec.begin() + parsed.CountCharactersBefore(url_parse::Parsed::USERNAME, | 692 spec.begin() + parsed.CountCharactersBefore(url::Parsed::USERNAME, true)); |
693 true)); | |
694 const char kHTTP[] = "http://"; | 693 const char kHTTP[] = "http://"; |
695 const char kFTP[] = "ftp."; | 694 const char kFTP[] = "ftp."; |
696 // URLFixerUpper::FixupURL() treats "ftp.foo.com" as ftp://ftp.foo.com. This | 695 // URLFixerUpper::FixupURL() treats "ftp.foo.com" as ftp://ftp.foo.com. This |
697 // means that if we trim "http://" off a URL whose host starts with "ftp." and | 696 // means that if we trim "http://" off a URL whose host starts with "ftp." and |
698 // the user inputs this into any field subject to fixup (which is basically | 697 // the user inputs this into any field subject to fixup (which is basically |
699 // all input fields), the meaning would be changed. (In fact, often the | 698 // all input fields), the meaning would be changed. (In fact, often the |
700 // formatted URL is directly pre-filled into an input field.) For this reason | 699 // formatted URL is directly pre-filled into an input field.) For this reason |
701 // we avoid stripping "http://" in this case. | 700 // we avoid stripping "http://" in this case. |
702 bool omit_http = (format_types & kFormatUrlOmitHTTP) && | 701 bool omit_http = (format_types & kFormatUrlOmitHTTP) && |
703 EqualsASCII(url_string, kHTTP) && | 702 EqualsASCII(url_string, kHTTP) && |
(...skipping 10 matching lines...) Expand all Loading... |
714 // Update the adjustments based on removed username and/or password. | 713 // Update the adjustments based on removed username and/or password. |
715 if (parsed.username.is_nonempty() || parsed.password.is_nonempty()) { | 714 if (parsed.username.is_nonempty() || parsed.password.is_nonempty()) { |
716 if (parsed.username.is_nonempty() && parsed.password.is_nonempty()) { | 715 if (parsed.username.is_nonempty() && parsed.password.is_nonempty()) { |
717 // The seeming off-by-two is to account for the ':' after the username | 716 // The seeming off-by-two is to account for the ':' after the username |
718 // and '@' after the password. | 717 // and '@' after the password. |
719 adjustments->push_back(base::OffsetAdjuster::Adjustment( | 718 adjustments->push_back(base::OffsetAdjuster::Adjustment( |
720 static_cast<size_t>(parsed.username.begin), | 719 static_cast<size_t>(parsed.username.begin), |
721 static_cast<size_t>(parsed.username.len + parsed.password.len + 2), | 720 static_cast<size_t>(parsed.username.len + parsed.password.len + 2), |
722 0)); | 721 0)); |
723 } else { | 722 } else { |
724 const url_parse::Component* nonempty_component = | 723 const url::Component* nonempty_component = |
725 parsed.username.is_nonempty() ? &parsed.username : &parsed.password; | 724 parsed.username.is_nonempty() ? &parsed.username : &parsed.password; |
726 // The seeming off-by-one is to account for the '@' after the | 725 // The seeming off-by-one is to account for the '@' after the |
727 // username/password. | 726 // username/password. |
728 adjustments->push_back(base::OffsetAdjuster::Adjustment( | 727 adjustments->push_back(base::OffsetAdjuster::Adjustment( |
729 static_cast<size_t>(nonempty_component->begin), | 728 static_cast<size_t>(nonempty_component->begin), |
730 static_cast<size_t>(nonempty_component->len + 1), | 729 static_cast<size_t>(nonempty_component->len + 1), |
731 0)); | 730 0)); |
732 } | 731 } |
733 } | 732 } |
734 } else { | 733 } else { |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
808 AdjustAllComponentsButScheme(delta, new_parsed); | 807 AdjustAllComponentsButScheme(delta, new_parsed); |
809 } | 808 } |
810 | 809 |
811 return url_string; | 810 return url_string; |
812 } | 811 } |
813 | 812 |
814 base::string16 FormatUrl(const GURL& url, | 813 base::string16 FormatUrl(const GURL& url, |
815 const std::string& languages, | 814 const std::string& languages, |
816 FormatUrlTypes format_types, | 815 FormatUrlTypes format_types, |
817 UnescapeRule::Type unescape_rules, | 816 UnescapeRule::Type unescape_rules, |
818 url_parse::Parsed* new_parsed, | 817 url::Parsed* new_parsed, |
819 size_t* prefix_end, | 818 size_t* prefix_end, |
820 size_t* offset_for_adjustment) { | 819 size_t* offset_for_adjustment) { |
821 Offsets offsets; | 820 Offsets offsets; |
822 if (offset_for_adjustment) | 821 if (offset_for_adjustment) |
823 offsets.push_back(*offset_for_adjustment); | 822 offsets.push_back(*offset_for_adjustment); |
824 base::string16 result = FormatUrlWithOffsets(url, languages, format_types, | 823 base::string16 result = FormatUrlWithOffsets(url, languages, format_types, |
825 unescape_rules, new_parsed, prefix_end, &offsets); | 824 unescape_rules, new_parsed, prefix_end, &offsets); |
826 if (offset_for_adjustment) | 825 if (offset_for_adjustment) |
827 *offset_for_adjustment = offsets[0]; | 826 *offset_for_adjustment = offsets[0]; |
828 return result; | 827 return result; |
829 } | 828 } |
830 | 829 |
831 } // namespace net | 830 } // namespace net |
OLD | NEW |