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 556 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
567 output_component->reset(); | 567 output_component->reset(); |
568 } | 568 } |
569 } | 569 } |
570 | 570 |
571 } // namespace | 571 } // namespace |
572 | 572 |
573 const FormatUrlType kFormatUrlOmitNothing = 0; | 573 const FormatUrlType kFormatUrlOmitNothing = 0; |
574 const FormatUrlType kFormatUrlOmitUsernamePassword = 1 << 0; | 574 const FormatUrlType kFormatUrlOmitUsernamePassword = 1 << 0; |
575 const FormatUrlType kFormatUrlOmitHTTP = 1 << 1; | 575 const FormatUrlType kFormatUrlOmitHTTP = 1 << 1; |
576 const FormatUrlType kFormatUrlOmitTrailingSlashOnBareHostname = 1 << 2; | 576 const FormatUrlType kFormatUrlOmitTrailingSlashOnBareHostname = 1 << 2; |
577 const FormatUrlType kFormatUrlOmitScheme = 1 << 3; | |
578 const FormatUrlType kFormatUrlOmitPort = 1 << 4; | |
577 const FormatUrlType kFormatUrlOmitAll = kFormatUrlOmitUsernamePassword | | 579 const FormatUrlType kFormatUrlOmitAll = kFormatUrlOmitUsernamePassword | |
578 kFormatUrlOmitHTTP | kFormatUrlOmitTrailingSlashOnBareHostname; | 580 kFormatUrlOmitHTTP | kFormatUrlOmitTrailingSlashOnBareHostname; |
579 | 581 |
580 base::string16 IDNToUnicode(const std::string& host, | 582 base::string16 IDNToUnicode(const std::string& host, |
581 const std::string& languages) { | 583 const std::string& languages) { |
582 return IDNToUnicodeWithAdjustments(host, languages, NULL); | 584 return IDNToUnicodeWithAdjustments(host, languages, NULL); |
583 } | 585 } |
584 | 586 |
585 std::string GetDirectoryListingEntry(const base::string16& name, | 587 std::string GetDirectoryListingEntry(const base::string16& name, |
586 const std::string& raw_bytes, | 588 const std::string& raw_bytes, |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
679 unescape_rules, new_parsed, prefix_end, | 681 unescape_rules, new_parsed, prefix_end, |
680 adjustments); | 682 adjustments); |
681 } | 683 } |
682 | 684 |
683 // We handle both valid and invalid URLs (this will give us the spec | 685 // We handle both valid and invalid URLs (this will give us the spec |
684 // regardless of validity). | 686 // regardless of validity). |
685 const std::string& spec = url.possibly_invalid_spec(); | 687 const std::string& spec = url.possibly_invalid_spec(); |
686 const url::Parsed& parsed = url.parsed_for_possibly_invalid_spec(); | 688 const url::Parsed& parsed = url.parsed_for_possibly_invalid_spec(); |
687 | 689 |
688 // Scheme & separators. These are ASCII. | 690 // Scheme & separators. These are ASCII. |
691 // Scheme removal occurs at the end. | |
689 base::string16 url_string; | 692 base::string16 url_string; |
690 url_string.insert( | 693 url_string.insert( |
691 url_string.end(), spec.begin(), | 694 url_string.end(), spec.begin(), |
692 spec.begin() + parsed.CountCharactersBefore(url::Parsed::USERNAME, true)); | 695 spec.begin() + parsed.CountCharactersBefore(url::Parsed::USERNAME, true)); |
693 const char kHTTP[] = "http://"; | |
694 const char kFTP[] = "ftp."; | |
695 // url_fixer::FixupURL() treats "ftp.foo.com" as ftp://ftp.foo.com. This | |
696 // means that if we trim "http://" off a URL whose host starts with "ftp." and | |
697 // the user inputs this into any field subject to fixup (which is basically | |
698 // all input fields), the meaning would be changed. (In fact, often the | |
699 // formatted URL is directly pre-filled into an input field.) For this reason | |
700 // we avoid stripping "http://" in this case. | |
701 bool omit_http = (format_types & kFormatUrlOmitHTTP) && | |
702 EqualsASCII(url_string, kHTTP) && | |
703 !StartsWithASCII(url.host(), kFTP, true); | |
704 new_parsed->scheme = parsed.scheme; | 696 new_parsed->scheme = parsed.scheme; |
705 | 697 |
706 // Username & password. | 698 // Username & password. |
707 if ((format_types & kFormatUrlOmitUsernamePassword) != 0) { | 699 if ((format_types & kFormatUrlOmitUsernamePassword) != 0) { |
708 // Remove the username and password fields. We don't want to display those | 700 // Remove the username and password fields. We don't want to display those |
709 // to the user since they can be used for attacks, | 701 // to the user since they can be used for attacks, |
710 // e.g. "http://google.com:search@evil.ru/" | 702 // e.g. "http://google.com:search@evil.ru/" |
711 new_parsed->username.reset(); | 703 new_parsed->username.reset(); |
712 new_parsed->password.reset(); | 704 new_parsed->password.reset(); |
713 // Update the adjustments based on removed username and/or password. | 705 // Update the adjustments based on removed username and/or password. |
(...skipping 29 matching lines...) Expand all Loading... | |
743 url_string.push_back('@'); | 735 url_string.push_back('@'); |
744 } | 736 } |
745 if (prefix_end) | 737 if (prefix_end) |
746 *prefix_end = static_cast<size_t>(url_string.length()); | 738 *prefix_end = static_cast<size_t>(url_string.length()); |
747 | 739 |
748 // Host. | 740 // Host. |
749 AppendFormattedComponent(spec, parsed.host, HostComponentTransform(languages), | 741 AppendFormattedComponent(spec, parsed.host, HostComponentTransform(languages), |
750 &url_string, &new_parsed->host, adjustments); | 742 &url_string, &new_parsed->host, adjustments); |
751 | 743 |
752 // Port. | 744 // Port. |
753 if (parsed.port.is_nonempty()) { | 745 if (parsed.port.is_nonempty() && !(format_types & kFormatUrlOmitPort)) { |
754 url_string.push_back(':'); | 746 url_string.push_back(':'); |
755 new_parsed->port.begin = url_string.length(); | 747 new_parsed->port.begin = url_string.length(); |
756 url_string.insert(url_string.end(), | 748 url_string.insert(url_string.end(), |
757 spec.begin() + parsed.port.begin, | 749 spec.begin() + parsed.port.begin, |
758 spec.begin() + parsed.port.end()); | 750 spec.begin() + parsed.port.end()); |
759 new_parsed->port.len = url_string.length() - new_parsed->port.begin; | 751 new_parsed->port.len = url_string.length() - new_parsed->port.begin; |
760 } else { | 752 } else { |
761 new_parsed->port.reset(); | 753 new_parsed->port.reset(); |
762 } | 754 } |
763 | 755 |
(...skipping 15 matching lines...) Expand all Loading... | |
779 NonHostComponentTransform(unescape_rules), | 771 NonHostComponentTransform(unescape_rules), |
780 &url_string, &new_parsed->query, adjustments); | 772 &url_string, &new_parsed->query, adjustments); |
781 | 773 |
782 // Ref. This is valid, unescaped UTF-8, so we can just convert. | 774 // Ref. This is valid, unescaped UTF-8, so we can just convert. |
783 if (parsed.ref.is_valid()) | 775 if (parsed.ref.is_valid()) |
784 url_string.push_back('#'); | 776 url_string.push_back('#'); |
785 AppendFormattedComponent(spec, parsed.ref, | 777 AppendFormattedComponent(spec, parsed.ref, |
786 NonHostComponentTransform(UnescapeRule::NONE), | 778 NonHostComponentTransform(UnescapeRule::NONE), |
787 &url_string, &new_parsed->ref, adjustments); | 779 &url_string, &new_parsed->ref, adjustments); |
788 | 780 |
789 // If we need to strip out http do it after the fact. | 781 // Strip out the scheme, after the fact. |
790 if (omit_http && StartsWith(url_string, base::ASCIIToUTF16(kHTTP), true)) { | 782 bool omit_all_schemes = format_types & kFormatUrlOmitScheme; |
791 const size_t kHTTPSize = arraysize(kHTTP) - 1; | 783 // url_fixer::FixupURL() treats "ftp.foo.com" as ftp://ftp.foo.com. This |
792 url_string = url_string.substr(kHTTPSize); | 784 // means that if we trim "http://" off a URL whose host starts with "ftp." and |
785 // the user inputs this into any field subject to fixup (which is basically | |
786 // all input fields), the meaning would be changed. (In fact, often the | |
787 // formatted URL is directly pre-filled into an input field.) For this reason | |
788 // we avoid stripping "http://" in this case. | |
789 bool omit_http = (format_types & kFormatUrlOmitHTTP) && | |
790 url.SchemeIs("http") && !StartsWithASCII(url.host(), "ftp.", true); | |
791 if (omit_all_schemes || omit_http) { | |
792 const int scheme_size = | |
793 parsed.CountCharactersBefore(url::Parsed::USERNAME, true); | |
794 url_string = url_string.substr(scheme_size); | |
793 // Because offsets in the |adjustments| are already calculated with respect | 795 // Because offsets in the |adjustments| are already calculated with respect |
794 // to the string with the http:// prefix in it, those offsets remain correct | 796 // to the string with the scheme prefix in it, those offsets remain correct |
795 // after stripping the prefix. The only thing necessary is to add an | 797 // after stripping the prefix. The only thing necessary is to add an |
796 // adjustment to reflect the stripped prefix. | 798 // adjustment to reflect the stripped prefix. |
797 adjustments->insert(adjustments->begin(), | 799 adjustments->insert(adjustments->begin(), |
798 base::OffsetAdjuster::Adjustment(0, kHTTPSize, 0)); | 800 base::OffsetAdjuster::Adjustment(0, scheme_size, 0)); |
799 | 801 |
800 if (prefix_end) | 802 if (prefix_end) |
801 *prefix_end -= kHTTPSize; | 803 *prefix_end -= scheme_size; |
802 | 804 |
803 // Adjust new_parsed. | |
804 DCHECK(new_parsed->scheme.is_valid()); | 805 DCHECK(new_parsed->scheme.is_valid()); |
805 int delta = -(new_parsed->scheme.len + 3); // +3 for ://. | 806 DCHECK_EQ(scheme_size, new_parsed->scheme.len + 3); |
806 new_parsed->scheme.reset(); | 807 new_parsed->scheme.reset(); |
807 AdjustAllComponentsButScheme(delta, new_parsed); | 808 AdjustAllComponentsButScheme(-scheme_size, new_parsed); |
808 } | 809 } |
809 | 810 |
810 return url_string; | 811 return url_string; |
811 } | 812 } |
812 | 813 |
813 base::string16 FormatUrl(const GURL& url, | 814 base::string16 FormatUrl(const GURL& url, |
814 const std::string& languages, | 815 const std::string& languages, |
815 FormatUrlTypes format_types, | 816 FormatUrlTypes format_types, |
816 UnescapeRule::Type unescape_rules, | 817 UnescapeRule::Type unescape_rules, |
817 url::Parsed* new_parsed, | 818 url::Parsed* new_parsed, |
818 size_t* prefix_end, | 819 size_t* prefix_end, |
819 size_t* offset_for_adjustment) { | 820 size_t* offset_for_adjustment) { |
820 Offsets offsets; | 821 Offsets offsets; |
821 if (offset_for_adjustment) | 822 if (offset_for_adjustment) |
822 offsets.push_back(*offset_for_adjustment); | 823 offsets.push_back(*offset_for_adjustment); |
823 base::string16 result = FormatUrlWithOffsets(url, languages, format_types, | 824 base::string16 result = FormatUrlWithOffsets(url, languages, format_types, |
824 unescape_rules, new_parsed, prefix_end, &offsets); | 825 unescape_rules, new_parsed, prefix_end, &offsets); |
825 if (offset_for_adjustment) | 826 if (offset_for_adjustment) |
826 *offset_for_adjustment = offsets[0]; | 827 *offset_for_adjustment = offsets[0]; |
827 return result; | 828 return result; |
828 } | 829 } |
829 | 830 |
831 base::string16 FormatOriginForDisplay(const GURL& url, | |
832 const std::string& languages, | |
833 bool always_show_scheme) { | |
834 if (url.SchemeIsFile() || url.SchemeIsFileSystem() || !url.IsStandard()) | |
835 return FormatUrl(url, languages); | |
836 GURL display_origin = url.GetOrigin(); | |
837 | |
838 FormatUrlTypes format_types = kFormatUrlOmitUsernamePassword | | |
839 kFormatUrlOmitTrailingSlashOnBareHostname; | |
840 if (!always_show_scheme && display_origin.SchemeIsSecure()) | |
841 format_types |= kFormatUrlOmitScheme; | |
842 | |
843 if ((display_origin.SchemeIs("http") && display_origin.port() == "80") || | |
844 (display_origin.SchemeIs("https") && display_origin.port() == "443")) | |
Ryan Sleevi
2014/09/02 19:26:17
We already have a //net concept of "default port f
| |
845 format_types |= kFormatUrlOmitPort; | |
846 | |
847 return FormatUrl(display_origin, | |
848 languages, | |
849 format_types, | |
850 UnescapeRule::SPACES, | |
851 NULL, NULL, NULL); | |
852 } | |
853 | |
830 } // namespace net | 854 } // namespace net |
OLD | NEW |