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 661 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
672 new_parsed = &parsed_temp; | 672 new_parsed = &parsed_temp; |
673 else | 673 else |
674 *new_parsed = url::Parsed(); | 674 *new_parsed = url::Parsed(); |
675 | 675 |
676 // Special handling for view-source:. Don't use content::kViewSourceScheme | 676 // Special handling for view-source:. Don't use content::kViewSourceScheme |
677 // because this library shouldn't depend on chrome. | 677 // because this library shouldn't depend on chrome. |
678 const char kViewSource[] = "view-source"; | 678 const char kViewSource[] = "view-source"; |
679 // Reject "view-source:view-source:..." to avoid deep recursion. | 679 // Reject "view-source:view-source:..." to avoid deep recursion. |
680 const char kViewSourceTwice[] = "view-source:view-source:"; | 680 const char kViewSourceTwice[] = "view-source:view-source:"; |
681 if (url.SchemeIs(kViewSource) && | 681 if (url.SchemeIs(kViewSource) && |
682 !StartsWithASCII(url.possibly_invalid_spec(), kViewSourceTwice, false)) { | 682 !base::StartsWithASCII(url.possibly_invalid_spec(), kViewSourceTwice, |
| 683 false)) { |
683 return FormatViewSourceUrl(url, languages, format_types, | 684 return FormatViewSourceUrl(url, languages, format_types, |
684 unescape_rules, new_parsed, prefix_end, | 685 unescape_rules, new_parsed, prefix_end, |
685 adjustments); | 686 adjustments); |
686 } | 687 } |
687 | 688 |
688 // We handle both valid and invalid URLs (this will give us the spec | 689 // We handle both valid and invalid URLs (this will give us the spec |
689 // regardless of validity). | 690 // regardless of validity). |
690 const std::string& spec = url.possibly_invalid_spec(); | 691 const std::string& spec = url.possibly_invalid_spec(); |
691 const url::Parsed& parsed = url.parsed_for_possibly_invalid_spec(); | 692 const url::Parsed& parsed = url.parsed_for_possibly_invalid_spec(); |
692 | 693 |
693 // Scheme & separators. These are ASCII. | 694 // Scheme & separators. These are ASCII. |
694 base::string16 url_string; | 695 base::string16 url_string; |
695 url_string.insert( | 696 url_string.insert( |
696 url_string.end(), spec.begin(), | 697 url_string.end(), spec.begin(), |
697 spec.begin() + parsed.CountCharactersBefore(url::Parsed::USERNAME, true)); | 698 spec.begin() + parsed.CountCharactersBefore(url::Parsed::USERNAME, true)); |
698 const char kHTTP[] = "http://"; | 699 const char kHTTP[] = "http://"; |
699 const char kFTP[] = "ftp."; | 700 const char kFTP[] = "ftp."; |
700 // url_fixer::FixupURL() treats "ftp.foo.com" as ftp://ftp.foo.com. This | 701 // url_fixer::FixupURL() treats "ftp.foo.com" as ftp://ftp.foo.com. This |
701 // means that if we trim "http://" off a URL whose host starts with "ftp." and | 702 // means that if we trim "http://" off a URL whose host starts with "ftp." and |
702 // the user inputs this into any field subject to fixup (which is basically | 703 // the user inputs this into any field subject to fixup (which is basically |
703 // all input fields), the meaning would be changed. (In fact, often the | 704 // all input fields), the meaning would be changed. (In fact, often the |
704 // formatted URL is directly pre-filled into an input field.) For this reason | 705 // formatted URL is directly pre-filled into an input field.) For this reason |
705 // we avoid stripping "http://" in this case. | 706 // we avoid stripping "http://" in this case. |
706 bool omit_http = (format_types & kFormatUrlOmitHTTP) && | 707 bool omit_http = (format_types & kFormatUrlOmitHTTP) && |
707 base::EqualsASCII(url_string, kHTTP) && | 708 base::EqualsASCII(url_string, kHTTP) && |
708 !StartsWithASCII(url.host(), kFTP, true); | 709 !base::StartsWithASCII(url.host(), kFTP, true); |
709 new_parsed->scheme = parsed.scheme; | 710 new_parsed->scheme = parsed.scheme; |
710 | 711 |
711 // Username & password. | 712 // Username & password. |
712 if ((format_types & kFormatUrlOmitUsernamePassword) != 0) { | 713 if ((format_types & kFormatUrlOmitUsernamePassword) != 0) { |
713 // Remove the username and password fields. We don't want to display those | 714 // Remove the username and password fields. We don't want to display those |
714 // to the user since they can be used for attacks, | 715 // to the user since they can be used for attacks, |
715 // e.g. "http://google.com:search@evil.ru/" | 716 // e.g. "http://google.com:search@evil.ru/" |
716 new_parsed->username.reset(); | 717 new_parsed->username.reset(); |
717 new_parsed->password.reset(); | 718 new_parsed->password.reset(); |
718 // Update the adjustments based on removed username and/or password. | 719 // Update the adjustments based on removed username and/or password. |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
785 &url_string, &new_parsed->query, adjustments); | 786 &url_string, &new_parsed->query, adjustments); |
786 | 787 |
787 // Ref. This is valid, unescaped UTF-8, so we can just convert. | 788 // Ref. This is valid, unescaped UTF-8, so we can just convert. |
788 if (parsed.ref.is_valid()) | 789 if (parsed.ref.is_valid()) |
789 url_string.push_back('#'); | 790 url_string.push_back('#'); |
790 AppendFormattedComponent(spec, parsed.ref, | 791 AppendFormattedComponent(spec, parsed.ref, |
791 NonHostComponentTransform(UnescapeRule::NONE), | 792 NonHostComponentTransform(UnescapeRule::NONE), |
792 &url_string, &new_parsed->ref, adjustments); | 793 &url_string, &new_parsed->ref, adjustments); |
793 | 794 |
794 // If we need to strip out http do it after the fact. | 795 // If we need to strip out http do it after the fact. |
795 if (omit_http && StartsWith(url_string, base::ASCIIToUTF16(kHTTP), true)) { | 796 if (omit_http && |
| 797 base::StartsWith(url_string, base::ASCIIToUTF16(kHTTP), true)) { |
796 const size_t kHTTPSize = arraysize(kHTTP) - 1; | 798 const size_t kHTTPSize = arraysize(kHTTP) - 1; |
797 url_string = url_string.substr(kHTTPSize); | 799 url_string = url_string.substr(kHTTPSize); |
798 // Because offsets in the |adjustments| are already calculated with respect | 800 // Because offsets in the |adjustments| are already calculated with respect |
799 // to the string with the http:// prefix in it, those offsets remain correct | 801 // to the string with the http:// prefix in it, those offsets remain correct |
800 // after stripping the prefix. The only thing necessary is to add an | 802 // after stripping the prefix. The only thing necessary is to add an |
801 // adjustment to reflect the stripped prefix. | 803 // adjustment to reflect the stripped prefix. |
802 adjustments->insert(adjustments->begin(), | 804 adjustments->insert(adjustments->begin(), |
803 base::OffsetAdjuster::Adjustment(0, kHTTPSize, 0)); | 805 base::OffsetAdjuster::Adjustment(0, kHTTPSize, 0)); |
804 | 806 |
805 if (prefix_end) | 807 if (prefix_end) |
(...skipping 20 matching lines...) Expand all Loading... |
826 if (offset_for_adjustment) | 828 if (offset_for_adjustment) |
827 offsets.push_back(*offset_for_adjustment); | 829 offsets.push_back(*offset_for_adjustment); |
828 base::string16 result = FormatUrlWithOffsets(url, languages, format_types, | 830 base::string16 result = FormatUrlWithOffsets(url, languages, format_types, |
829 unescape_rules, new_parsed, prefix_end, &offsets); | 831 unescape_rules, new_parsed, prefix_end, &offsets); |
830 if (offset_for_adjustment) | 832 if (offset_for_adjustment) |
831 *offset_for_adjustment = offsets[0]; | 833 *offset_for_adjustment = offsets[0]; |
832 return result; | 834 return result; |
833 } | 835 } |
834 | 836 |
835 } // namespace net | 837 } // namespace net |
OLD | NEW |