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&);
53 Vector<LChar> m_asciiBuffer;
54 bool m_urlIsSpecial { false };
55 bool m_hostHasPercentOrNonASCII { false };
57 const void* m_inputBegin { nullptr };
59 bool m_didSeeSyntaxViolation { false };
60 const static size_t defaultInlineBufferSize = 2048;
62 template<typename CharacterType> void parse(const CharacterType*, const unsigned length, const URL&, const TextEncoding&);
63 template<typename CharacterType> void parseAuthority(CodePointIterator<CharacterType>);
64 template<typename CharacterType> bool parseHostAndPort(CodePointIterator<CharacterType>);
65 template<typename CharacterType> bool parsePort(CodePointIterator<CharacterType>&);
68 enum class ReportSyntaxViolation { No, Yes };
69 template<typename CharacterType, ReportSyntaxViolation reportSyntaxViolation = ReportSyntaxViolation::Yes>
70 void advance(CodePointIterator<CharacterType>& iterator) { advance<CharacterType, reportSyntaxViolation>(iterator, iterator); }
71 template<typename CharacterType, ReportSyntaxViolation = ReportSyntaxViolation::Yes>
72 void advance(CodePointIterator<CharacterType>&, const CodePointIterator<CharacterType>& iteratorForSyntaxViolationPosition);
73 template<typename CharacterType> bool takesTwoAdvancesUntilEnd(CodePointIterator<CharacterType>);
74 template<typename CharacterType> void syntaxViolation(const CodePointIterator<CharacterType>&);
75 template<typename CharacterType> bool isPercentEncodedDot(CodePointIterator<CharacterType>);
76 template<typename CharacterType> bool isWindowsDriveLetter(CodePointIterator<CharacterType>);
77 template<typename CharacterType> bool isSingleDotPathSegment(CodePointIterator<CharacterType>);
78 template<typename CharacterType> bool isDoubleDotPathSegment(CodePointIterator<CharacterType>);
79 template<typename CharacterType> bool shouldCopyFileURL(CodePointIterator<CharacterType>);
80 template<typename CharacterType> bool checkLocalhostCodePoint(CodePointIterator<CharacterType>&, UChar32);
81 template<typename CharacterType> bool isAtLocalhost(CodePointIterator<CharacterType>);
82 bool isLocalhost(StringView);
83 template<typename CharacterType> void consumeSingleDotPathSegment(CodePointIterator<CharacterType>&);
84 template<typename CharacterType> void consumeDoubleDotPathSegment(CodePointIterator<CharacterType>&);
85 template<typename CharacterType> void appendWindowsDriveLetter(CodePointIterator<CharacterType>&);
86 template<typename CharacterType> size_t currentPosition(const CodePointIterator<CharacterType>&);
87 template<typename UnsignedIntegerType> void appendNumberToASCIIBuffer(UnsignedIntegerType);
88 template<bool(*isInCodeSet)(UChar32), typename CharacterType> void utf8PercentEncode(const CodePointIterator<CharacterType>&);
89 template<typename CharacterType> void utf8QueryEncode(const CodePointIterator<CharacterType>&);
90 template<typename CharacterType> Optional<Vector<LChar, defaultInlineBufferSize>> domainToASCII(const String&, const CodePointIterator<CharacterType>& iteratorForSyntaxViolationPosition);
91 template<typename CharacterType> Vector<LChar, defaultInlineBufferSize> percentDecode(const LChar*, size_t, const CodePointIterator<CharacterType>& iteratorForSyntaxViolationPosition);
92 static Vector<LChar, defaultInlineBufferSize> percentDecode(const LChar*, size_t);
93 static Optional<String> formURLDecode(StringView input);
94 static bool hasInvalidDomainCharacter(const Vector<LChar, defaultInlineBufferSize>&);
95 void percentEncodeByte(uint8_t);
96 void appendToASCIIBuffer(UChar32);
97 void appendToASCIIBuffer(const char*, size_t);
98 void appendToASCIIBuffer(const LChar* characters, size_t size) { appendToASCIIBuffer(reinterpret_cast<const char*>(characters), size); }
99 template<typename CharacterType> void encodeQuery(const Vector<UChar>& source, const TextEncoding&, CodePointIterator<CharacterType>);
100 void copyASCIIStringUntil(const String&, size_t length);
101 StringView parsedDataView(size_t start, size_t length);
103 using IPv4Address = uint32_t;
104 void serializeIPv4(IPv4Address);
105 template<typename CharacterType> Optional<IPv4Address> parseIPv4Host(CodePointIterator<CharacterType>);
106 template<typename CharacterType> Optional<uint32_t> parseIPv4Piece(CodePointIterator<CharacterType>&, bool& syntaxViolation);
107 using IPv6Address = std::array<uint16_t, 8>;
108 template<typename CharacterType> Optional<IPv6Address> parseIPv6Host(CodePointIterator<CharacterType>);
109 template<typename CharacterType> Optional<uint32_t> parseIPv4PieceInsideIPv6(CodePointIterator<CharacterType>&);
110 template<typename CharacterType> Optional<IPv4Address> parseIPv4AddressInsideIPv6(CodePointIterator<CharacterType>);
111 void serializeIPv6Piece(uint16_t piece);
112 void serializeIPv6(IPv6Address);
115 template<typename CharacterType> void copyURLPartsUntil(const URL& base, URLPart, const CodePointIterator<CharacterType>&, bool& isUTF8Encoding);
116 static size_t urlLengthUntilPart(const URL&, URLPart);