Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2004, 2007, 2008, 2011, 2012 Apple Inc. All rights reserved. | 2 * Copyright (C) 2004, 2007, 2008, 2011, 2012 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2012 Research In Motion Limited. All rights reserved. | 3 * Copyright (C) 2012 Research In Motion Limited. All rights reserved. |
| 4 * Copyright (C) 2008, 2009, 2011 Google Inc. All rights reserved. | 4 * Copyright (C) 2008, 2009, 2011 Google Inc. All rights reserved. |
| 5 * | 5 * |
| 6 * Redistribution and use in source and binary forms, with or without | 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions | 7 * modification, are permitted provided that the following conditions |
| 8 * are met: | 8 * are met: |
| 9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 */ | 26 */ |
| 27 | 27 |
| 28 #include "platform/weborigin/KURL.h" | 28 #include "platform/weborigin/KURL.h" |
| 29 | 29 |
| 30 #include "platform/weborigin/KnownPorts.h" | 30 #include "platform/weborigin/KnownPorts.h" |
| 31 #include "url/url_util.h" | 31 #include "url/url_util.h" |
| 32 #include "wtf/CheckedNumeric.h" | |
| 32 #include "wtf/PtrUtil.h" | 33 #include "wtf/PtrUtil.h" |
| 33 #include "wtf/StdLibExtras.h" | 34 #include "wtf/StdLibExtras.h" |
| 34 #include "wtf/text/CString.h" | 35 #include "wtf/text/CString.h" |
| 35 #include "wtf/text/StringHash.h" | 36 #include "wtf/text/StringHash.h" |
| 36 #include "wtf/text/StringUTF8Adaptor.h" | 37 #include "wtf/text/StringUTF8Adaptor.h" |
| 37 #include "wtf/text/TextEncoding.h" | 38 #include "wtf/text/TextEncoding.h" |
| 38 #include <algorithm> | 39 #include <algorithm> |
| 40 #include <limits> | |
| 39 #ifndef NDEBUG | 41 #ifndef NDEBUG |
| 40 #include <stdio.h> | 42 #include <stdio.h> |
| 41 #endif | 43 #endif |
| 42 | 44 |
| 43 namespace blink { | 45 namespace blink { |
| 44 | 46 |
| 45 static const int maximumValidPortNumber = 0xFFFE; | 47 static const int maximumValidPortNumber = 0xFFFE; |
| 46 static const int invalidPortNumber = 0xFFFF; | 48 static const int invalidPortNumber = 0xFFFF; |
| 47 | 49 |
| 48 static void assertProtocolIsGood(const char* protocol) { | 50 static void assertProtocolIsGood(const char* protocol) { |
| (...skipping 661 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 710 if (url.is8Bit()) | 712 if (url.is8Bit()) |
| 711 return url::FindAndCompareScheme(asURLChar8Subtle(url), url.length(), | 713 return url::FindAndCompareScheme(asURLChar8Subtle(url), url.length(), |
| 712 protocol, 0); | 714 protocol, 0); |
| 713 return url::FindAndCompareScheme(url.characters16(), url.length(), protocol, | 715 return url::FindAndCompareScheme(url.characters16(), url.length(), protocol, |
| 714 0); | 716 0); |
| 715 } | 717 } |
| 716 | 718 |
| 717 void KURL::init(const KURL& base, | 719 void KURL::init(const KURL& base, |
| 718 const String& relative, | 720 const String& relative, |
| 719 const WTF::TextEncoding* queryEncoding) { | 721 const WTF::TextEncoding* queryEncoding) { |
| 720 if (!relative.isNull() && relative.is8Bit()) { | |
| 721 StringUTF8Adaptor relativeUTF8(relative); | |
| 722 init(base, relativeUTF8.data(), relativeUTF8.length(), queryEncoding); | |
| 723 } else | |
| 724 init(base, relative.characters16(), relative.length(), queryEncoding); | |
| 725 initProtocolIsInHTTPFamily(); | |
| 726 initInnerURL(); | |
| 727 } | |
| 728 | |
| 729 template <typename CHAR> | |
| 730 void KURL::init(const KURL& base, | |
| 731 const CHAR* relative, | |
| 732 int relativeLength, | |
| 733 const WTF::TextEncoding* queryEncoding) { | |
| 734 // As a performance optimization, we do not use the charset converter | 722 // As a performance optimization, we do not use the charset converter |
| 735 // if encoding is UTF-8 or other Unicode encodings. Note that this is | 723 // if encoding is UTF-8 or other Unicode encodings. Note that this is |
| 736 // per HTML5 2.5.3 (resolving URL). The URL canonicalizer will be more | 724 // per HTML5 2.5.3 (resolving URL). The URL canonicalizer will be more |
| 737 // efficient with no charset converter object because it can do UTF-8 | 725 // efficient with no charset converter object because it can do UTF-8 |
| 738 // internally with no extra copies. | 726 // internally with no extra copies. |
| 739 | 727 |
| 728 StringUTF8Adaptor baseUTF8(base.getString()); | |
| 729 | |
| 740 // We feel free to make the charset converter object every time since it's | 730 // We feel free to make the charset converter object every time since it's |
| 741 // just a wrapper around a reference. | 731 // just a wrapper around a reference. |
| 742 KURLCharsetConverter charsetConverterObject(queryEncoding); | 732 KURLCharsetConverter charsetConverterObject(queryEncoding); |
| 743 KURLCharsetConverter* charsetConverter = | 733 KURLCharsetConverter* charsetConverter = |
| 744 (!queryEncoding || isUnicodeEncoding(queryEncoding)) | 734 (!queryEncoding || isUnicodeEncoding(queryEncoding)) |
| 745 ? 0 | 735 ? 0 |
| 746 : &charsetConverterObject; | 736 : &charsetConverterObject; |
| 747 | 737 |
| 748 StringUTF8Adaptor baseUTF8(base.getString()); | 738 // Clamp to int max to avoid overflow. |
| 739 url::RawCanonOutputT<char> output; | |
| 740 if (!relative.isNull() && relative.is8Bit()) { | |
| 741 StringUTF8Adaptor relativeUTF8(relative); | |
| 742 CheckedNumeric<int> relativeLength( | |
|
esprehn
2016/11/04 22:55:59
I think you want to use clampTo() in here instead
Charlie Harrison
2016/11/05 02:23:13
Aha, thanks. Done.
| |
| 743 CheckedNumeric<size_t>(relativeUTF8.length())); | |
| 744 m_isValid = url::ResolveRelative( | |
| 745 baseUTF8.data(), baseUTF8.length(), base.m_parsed, relativeUTF8.data(), | |
| 746 relativeLength.ValueOrDefault(std::numeric_limits<int>::max()), | |
| 747 charsetConverter, &output, &m_parsed); | |
| 748 } else { | |
| 749 CheckedNumeric<int> relativeLength( | |
|
esprehn
2016/11/04 22:55:59
ditto
Charlie Harrison
2016/11/05 02:23:13
Done.
| |
| 750 CheckedNumeric<unsigned>(relative.length())); | |
| 751 m_isValid = url::ResolveRelative( | |
| 752 baseUTF8.data(), baseUTF8.length(), base.m_parsed, | |
| 753 relative.characters16(), | |
| 754 relativeLength.ValueOrDefault(std::numeric_limits<int>::max()), | |
| 755 charsetConverter, &output, &m_parsed); | |
| 756 } | |
| 749 | 757 |
| 750 url::RawCanonOutputT<char> output; | 758 // AtomicString::fromUTF8 will re-hash the raw output and check the |
| 751 m_isValid = url::ResolveRelative(baseUTF8.data(), baseUTF8.length(), | 759 // AtomicStringTable (addWithTranslator) for the string. This can be very |
| 752 base.m_parsed, relative, relativeLength, | 760 // expensive for large URLs. However, since many URLs are generated from |
| 753 charsetConverter, &output, &m_parsed); | 761 // existing AtomicStrings (which already have their hashes computed), this |
| 762 // fast path is used if the input string is already canonicalized. | |
| 763 // | |
| 764 // Because this optimization does not apply to non-AtomicStrings, explicitly | |
| 765 // check that the input is Atomic before moving forward with it. This also | |
| 766 // gets around the problem that marking non-Atomic input as Atomic will render | |
| 767 // the string thread unsafe. | |
|
esprehn
2016/11/04 22:55:59
fwiw I don't think the thread safety is an issue,
Charlie Harrison
2016/11/05 02:23:13
The KURL is not thread safe, but the String passed
| |
| 768 if (!relative.isNull() && relative.impl()->isAtomic() && | |
| 769 StringView(output.data(), static_cast<unsigned>(output.length())) == | |
| 770 relative) { | |
| 771 m_string = relative; | |
| 772 } else { | |
| 773 m_string = AtomicString::fromUTF8(output.data(), output.length()); | |
| 774 } | |
| 754 | 775 |
| 755 // See FIXME in KURLPrivate in the header. If canonicalization has not | 776 initProtocolIsInHTTPFamily(); |
| 756 // changed the string, we can avoid an extra allocation by using assignment. | 777 initInnerURL(); |
| 757 m_string = AtomicString::fromUTF8(output.data(), output.length()); | |
| 758 } | 778 } |
| 759 | 779 |
| 760 void KURL::initInnerURL() { | 780 void KURL::initInnerURL() { |
| 761 if (!m_isValid) { | 781 if (!m_isValid) { |
| 762 m_innerURL.reset(); | 782 m_innerURL.reset(); |
| 763 return; | 783 return; |
| 764 } | 784 } |
| 765 if (url::Parsed* innerParsed = m_parsed.inner_parsed()) | 785 if (url::Parsed* innerParsed = m_parsed.inner_parsed()) |
| 766 m_innerURL = wrapUnique(new KURL( | 786 m_innerURL = wrapUnique(new KURL( |
| 767 ParsedURLString, | 787 ParsedURLString, |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 868 m_parsed = newParsed; | 888 m_parsed = newParsed; |
| 869 m_string = AtomicString::fromUTF8(output.data(), output.length()); | 889 m_string = AtomicString::fromUTF8(output.data(), output.length()); |
| 870 } | 890 } |
| 871 | 891 |
| 872 bool KURL::isSafeToSendToAnotherThread() const { | 892 bool KURL::isSafeToSendToAnotherThread() const { |
| 873 return m_string.isSafeToSendToAnotherThread() && | 893 return m_string.isSafeToSendToAnotherThread() && |
| 874 (!m_innerURL || m_innerURL->isSafeToSendToAnotherThread()); | 894 (!m_innerURL || m_innerURL->isSafeToSendToAnotherThread()); |
| 875 } | 895 } |
| 876 | 896 |
| 877 } // namespace blink | 897 } // namespace blink |
| OLD | NEW |