2 * Copyright (C) 2016 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
28 #include "TextEncoding.h"
30 #include <wtf/Forward.h>
34 template<typename CharacterType> class CodePointIterator;
38 WEBCORE_EXPORT URLParser(const String&, const URL& = { }, const TextEncoding& = UTF8Encoding());
39 URL result() { return m_url; }
41 WEBCORE_EXPORT static bool allValuesEqual(const URL&, const URL&);
42 WEBCORE_EXPORT static bool internalValuesConsistent(const URL&);
44 WEBCORE_EXPORT static bool enabled();
45 WEBCORE_EXPORT static void setEnabled(bool);
47 typedef Vector<std::pair<String, String>> URLEncodedForm;
48 static URLEncodedForm parseURLEncodedForm(StringView);
49 static String serialize(const URLEncodedForm&);
52 static Optional<uint16_t> defaultPortForProtocol(StringView);
53 friend Optional<uint16_t> defaultPortForProtocol(StringView);
56 Vector<LChar> m_asciiBuffer;
57 bool m_urlIsSpecial { false };
58 bool m_hostHasPercentOrNonASCII { false };
60 const void* m_inputBegin { nullptr };
62 bool m_didSeeSyntaxViolation { false };
63 const static size_t defaultInlineBufferSize = 2048;
65 template<typename CharacterType> void parse(const CharacterType*, const unsigned length, const URL&, const TextEncoding&);
66 template<typename CharacterType> void parseAuthority(CodePointIterator<CharacterType>);
67 template<typename CharacterType> bool parseHostAndPort(CodePointIterator<CharacterType>);
68 template<typename CharacterType> bool parsePort(CodePointIterator<CharacterType>&);
71 enum class ReportSyntaxViolation { No, Yes };
72 template<typename CharacterType, ReportSyntaxViolation reportSyntaxViolation = ReportSyntaxViolation::Yes>
73 void advance(CodePointIterator<CharacterType>& iterator) { advance<CharacterType, reportSyntaxViolation>(iterator, iterator); }
74 template<typename CharacterType, ReportSyntaxViolation = ReportSyntaxViolation::Yes>
75 void advance(CodePointIterator<CharacterType>&, const CodePointIterator<CharacterType>& iteratorForSyntaxViolationPosition);
76 template<typename CharacterType> bool takesTwoAdvancesUntilEnd(CodePointIterator<CharacterType>);
77 template<typename CharacterType> void syntaxViolation(const CodePointIterator<CharacterType>&);
78 template<typename CharacterType> bool isPercentEncodedDot(CodePointIterator<CharacterType>);
79 template<typename CharacterType> bool isWindowsDriveLetter(CodePointIterator<CharacterType>);
80 template<typename CharacterType> bool isSingleDotPathSegment(CodePointIterator<CharacterType>);
81 template<typename CharacterType> bool isDoubleDotPathSegment(CodePointIterator<CharacterType>);
82 template<typename CharacterType> bool shouldCopyFileURL(CodePointIterator<CharacterType>);
83 template<typename CharacterType> bool checkLocalhostCodePoint(CodePointIterator<CharacterType>&, UChar32);
84 template<typename CharacterType> bool isAtLocalhost(CodePointIterator<CharacterType>);
85 bool isLocalhost(StringView);
86 template<typename CharacterType> void consumeSingleDotPathSegment(CodePointIterator<CharacterType>&);
87 template<typename CharacterType> void consumeDoubleDotPathSegment(CodePointIterator<CharacterType>&);
88 template<typename CharacterType> void appendWindowsDriveLetter(CodePointIterator<CharacterType>&);
89 template<typename CharacterType> size_t currentPosition(const CodePointIterator<CharacterType>&);
90 template<typename UnsignedIntegerType> void appendNumberToASCIIBuffer(UnsignedIntegerType);
91 template<bool(*isInCodeSet)(UChar32), typename CharacterType> void utf8PercentEncode(const CodePointIterator<CharacterType>&);
92 template<typename CharacterType> void utf8QueryEncode(const CodePointIterator<CharacterType>&);
93 template<typename CharacterType> Optional<Vector<LChar, defaultInlineBufferSize>> domainToASCII(const String&, const CodePointIterator<CharacterType>& iteratorForSyntaxViolationPosition);
94 template<typename CharacterType> Vector<LChar, defaultInlineBufferSize> percentDecode(const LChar*, size_t, const CodePointIterator<CharacterType>& iteratorForSyntaxViolationPosition);
95 static Vector<LChar, defaultInlineBufferSize> percentDecode(const LChar*, size_t);
96 static Optional<String> formURLDecode(StringView input);
97 static bool hasInvalidDomainCharacter(const Vector<LChar, defaultInlineBufferSize>&);
98 void percentEncodeByte(uint8_t);
99 void appendToASCIIBuffer(UChar32);
100 void appendToASCIIBuffer(const char*, size_t);
101 void appendToASCIIBuffer(const LChar* characters, size_t size) { appendToASCIIBuffer(reinterpret_cast<const char*>(characters), size); }
102 template<typename CharacterType> void encodeQuery(const Vector<UChar>& source, const TextEncoding&, CodePointIterator<CharacterType>);
103 void copyASCIIStringUntil(const String&, size_t length);
104 StringView parsedDataView(size_t start, size_t length);
106 using IPv4Address = uint32_t;
107 void serializeIPv4(IPv4Address);
108 template<typename CharacterType> Optional<IPv4Address> parseIPv4Host(CodePointIterator<CharacterType>);
109 template<typename CharacterType> Optional<uint32_t> parseIPv4Piece(CodePointIterator<CharacterType>&, bool& syntaxViolation);
110 using IPv6Address = std::array<uint16_t, 8>;
111 template<typename CharacterType> Optional<IPv6Address> parseIPv6Host(CodePointIterator<CharacterType>);
112 template<typename CharacterType> Optional<uint32_t> parseIPv4PieceInsideIPv6(CodePointIterator<CharacterType>&);
113 template<typename CharacterType> Optional<IPv4Address> parseIPv4AddressInsideIPv6(CodePointIterator<CharacterType>);
114 void serializeIPv6Piece(uint16_t piece);
115 void serializeIPv6(IPv6Address);
118 template<typename CharacterType> void copyURLPartsUntil(const URL& base, URLPart, const CodePointIterator<CharacterType>&, bool& isUTF8Encoding);
119 static size_t urlLengthUntilPart(const URL&, URLPart);