2 * Copyright (C) 2004, 2007, 2008, 2011 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. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "DecodeEscapeSequences.h"
30 #include "TextEncoding.h"
32 #include <wtf/HashMap.h>
33 #include <wtf/HexNumber.h>
34 #include <wtf/StdLibExtras.h>
35 #include <wtf/text/CString.h>
36 #include <wtf/text/StringBuilder.h>
37 #include <wtf/text/StringHash.h>
40 #include <unicode/uidna.h>
41 #elif USE(QT4_UNICODE)
43 #elif USE(GLIB_UNICODE)
48 // FIXME: This file makes too much use of the + operator on String.
49 // We either have to optimize that operator so it doesn't involve
50 // so many allocations, or change this to use StringBuffer instead.
57 typedef Vector<char, 512> CharBuffer;
58 typedef Vector<UChar, 512> UCharBuffer;
60 static const unsigned maximumValidPortNumber = 0xFFFE;
61 static const unsigned invalidPortNumber = 0xFFFF;
63 static inline bool isLetterMatchIgnoringCase(UChar character, char lowercaseLetter)
65 ASSERT(isASCIILower(lowercaseLetter));
66 return (character | 0x20) == lowercaseLetter;
71 static inline bool isLetterMatchIgnoringCase(char character, char lowercaseLetter)
73 ASSERT(isASCIILower(lowercaseLetter));
74 return (character | 0x20) == lowercaseLetter;
77 enum URLCharacterClasses {
79 SchemeFirstChar = 1 << 0,
81 // ( alpha | digit | "+" | "-" | "." )
84 // mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
85 // unreserved = alphanum | mark
86 // ( unreserved | escaped | ";" | ":" | "&" | "=" | "+" | "$" | "," )
87 UserInfoChar = 1 << 2,
89 // alnum | "." | "-" | "%"
90 // The above is what the specification says, but we are lenient to
91 // match existing practice and also allow:
93 HostnameChar = 1 << 3,
95 // hexdigit | ":" | "%"
98 // "#" | "?" | "/" | nul
99 PathSegmentEndChar = 1 << 5,
101 // not allowed in path
105 static const unsigned char characterClassTable[256] = {
106 /* 0 nul */ PathSegmentEndChar, /* 1 soh */ BadChar,
107 /* 2 stx */ BadChar, /* 3 etx */ BadChar,
108 /* 4 eot */ BadChar, /* 5 enq */ BadChar, /* 6 ack */ BadChar, /* 7 bel */ BadChar,
109 /* 8 bs */ BadChar, /* 9 ht */ BadChar, /* 10 nl */ BadChar, /* 11 vt */ BadChar,
110 /* 12 np */ BadChar, /* 13 cr */ BadChar, /* 14 so */ BadChar, /* 15 si */ BadChar,
111 /* 16 dle */ BadChar, /* 17 dc1 */ BadChar, /* 18 dc2 */ BadChar, /* 19 dc3 */ BadChar,
112 /* 20 dc4 */ BadChar, /* 21 nak */ BadChar, /* 22 syn */ BadChar, /* 23 etb */ BadChar,
113 /* 24 can */ BadChar, /* 25 em */ BadChar, /* 26 sub */ BadChar, /* 27 esc */ BadChar,
114 /* 28 fs */ BadChar, /* 29 gs */ BadChar, /* 30 rs */ BadChar, /* 31 us */ BadChar,
115 /* 32 sp */ BadChar, /* 33 ! */ UserInfoChar,
116 /* 34 " */ BadChar, /* 35 # */ PathSegmentEndChar | BadChar,
117 /* 36 $ */ UserInfoChar, /* 37 % */ UserInfoChar | HostnameChar | IPv6Char | BadChar,
118 /* 38 & */ UserInfoChar, /* 39 ' */ UserInfoChar,
119 /* 40 ( */ UserInfoChar, /* 41 ) */ UserInfoChar,
120 /* 42 * */ UserInfoChar, /* 43 + */ SchemeChar | UserInfoChar,
121 /* 44 , */ UserInfoChar,
122 /* 45 - */ SchemeChar | UserInfoChar | HostnameChar,
123 /* 46 . */ SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
124 /* 47 / */ PathSegmentEndChar,
125 /* 48 0 */ SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
126 /* 49 1 */ SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
127 /* 50 2 */ SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
128 /* 51 3 */ SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
129 /* 52 4 */ SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
130 /* 53 5 */ SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
131 /* 54 6 */ SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
132 /* 55 7 */ SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
133 /* 56 8 */ SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
134 /* 57 9 */ SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
135 /* 58 : */ UserInfoChar | IPv6Char, /* 59 ; */ UserInfoChar,
136 /* 60 < */ BadChar, /* 61 = */ UserInfoChar,
137 /* 62 > */ BadChar, /* 63 ? */ PathSegmentEndChar | BadChar,
139 /* 65 A */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
140 /* 66 B */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
141 /* 67 C */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
142 /* 68 D */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
143 /* 69 E */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
144 /* 70 F */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
145 /* 71 G */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
146 /* 72 H */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
147 /* 73 I */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
148 /* 74 J */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
149 /* 75 K */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
150 /* 76 L */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
151 /* 77 M */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
152 /* 78 N */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
153 /* 79 O */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
154 /* 80 P */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
155 /* 81 Q */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
156 /* 82 R */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
157 /* 83 S */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
158 /* 84 T */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
159 /* 85 U */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
160 /* 86 V */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
161 /* 87 W */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
162 /* 88 X */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
163 /* 89 Y */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
164 /* 90 Z */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
166 /* 92 \ */ 0, /* 93 ] */ 0,
168 /* 95 _ */ UserInfoChar | HostnameChar,
170 /* 97 a */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
171 /* 98 b */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
172 /* 99 c */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
173 /* 100 d */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
174 /* 101 e */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
175 /* 102 f */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
176 /* 103 g */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
177 /* 104 h */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
178 /* 105 i */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
179 /* 106 j */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
180 /* 107 k */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
181 /* 108 l */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
182 /* 109 m */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
183 /* 110 n */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
184 /* 111 o */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
185 /* 112 p */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
186 /* 113 q */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
187 /* 114 r */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
188 /* 115 s */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
189 /* 116 t */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
190 /* 117 u */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
191 /* 118 v */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
192 /* 119 w */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
193 /* 120 x */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
194 /* 121 y */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
195 /* 122 z */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
197 /* 124 | */ 0, /* 125 } */ 0, /* 126 ~ */ UserInfoChar, /* 127 del */ BadChar,
198 /* 128 */ BadChar, /* 129 */ BadChar, /* 130 */ BadChar, /* 131 */ BadChar,
199 /* 132 */ BadChar, /* 133 */ BadChar, /* 134 */ BadChar, /* 135 */ BadChar,
200 /* 136 */ BadChar, /* 137 */ BadChar, /* 138 */ BadChar, /* 139 */ BadChar,
201 /* 140 */ BadChar, /* 141 */ BadChar, /* 142 */ BadChar, /* 143 */ BadChar,
202 /* 144 */ BadChar, /* 145 */ BadChar, /* 146 */ BadChar, /* 147 */ BadChar,
203 /* 148 */ BadChar, /* 149 */ BadChar, /* 150 */ BadChar, /* 151 */ BadChar,
204 /* 152 */ BadChar, /* 153 */ BadChar, /* 154 */ BadChar, /* 155 */ BadChar,
205 /* 156 */ BadChar, /* 157 */ BadChar, /* 158 */ BadChar, /* 159 */ BadChar,
206 /* 160 */ BadChar, /* 161 */ BadChar, /* 162 */ BadChar, /* 163 */ BadChar,
207 /* 164 */ BadChar, /* 165 */ BadChar, /* 166 */ BadChar, /* 167 */ BadChar,
208 /* 168 */ BadChar, /* 169 */ BadChar, /* 170 */ BadChar, /* 171 */ BadChar,
209 /* 172 */ BadChar, /* 173 */ BadChar, /* 174 */ BadChar, /* 175 */ BadChar,
210 /* 176 */ BadChar, /* 177 */ BadChar, /* 178 */ BadChar, /* 179 */ BadChar,
211 /* 180 */ BadChar, /* 181 */ BadChar, /* 182 */ BadChar, /* 183 */ BadChar,
212 /* 184 */ BadChar, /* 185 */ BadChar, /* 186 */ BadChar, /* 187 */ BadChar,
213 /* 188 */ BadChar, /* 189 */ BadChar, /* 190 */ BadChar, /* 191 */ BadChar,
214 /* 192 */ BadChar, /* 193 */ BadChar, /* 194 */ BadChar, /* 195 */ BadChar,
215 /* 196 */ BadChar, /* 197 */ BadChar, /* 198 */ BadChar, /* 199 */ BadChar,
216 /* 200 */ BadChar, /* 201 */ BadChar, /* 202 */ BadChar, /* 203 */ BadChar,
217 /* 204 */ BadChar, /* 205 */ BadChar, /* 206 */ BadChar, /* 207 */ BadChar,
218 /* 208 */ BadChar, /* 209 */ BadChar, /* 210 */ BadChar, /* 211 */ BadChar,
219 /* 212 */ BadChar, /* 213 */ BadChar, /* 214 */ BadChar, /* 215 */ BadChar,
220 /* 216 */ BadChar, /* 217 */ BadChar, /* 218 */ BadChar, /* 219 */ BadChar,
221 /* 220 */ BadChar, /* 221 */ BadChar, /* 222 */ BadChar, /* 223 */ BadChar,
222 /* 224 */ BadChar, /* 225 */ BadChar, /* 226 */ BadChar, /* 227 */ BadChar,
223 /* 228 */ BadChar, /* 229 */ BadChar, /* 230 */ BadChar, /* 231 */ BadChar,
224 /* 232 */ BadChar, /* 233 */ BadChar, /* 234 */ BadChar, /* 235 */ BadChar,
225 /* 236 */ BadChar, /* 237 */ BadChar, /* 238 */ BadChar, /* 239 */ BadChar,
226 /* 240 */ BadChar, /* 241 */ BadChar, /* 242 */ BadChar, /* 243 */ BadChar,
227 /* 244 */ BadChar, /* 245 */ BadChar, /* 246 */ BadChar, /* 247 */ BadChar,
228 /* 248 */ BadChar, /* 249 */ BadChar, /* 250 */ BadChar, /* 251 */ BadChar,
229 /* 252 */ BadChar, /* 253 */ BadChar, /* 254 */ BadChar, /* 255 */ BadChar
232 static int copyPathRemovingDots(char* dst, const char* src, int srcStart, int srcEnd);
233 static void encodeRelativeString(const String& rel, const TextEncoding&, CharBuffer& ouput);
234 static String substituteBackslashes(const String&);
236 static inline bool isSchemeFirstChar(char c) { return characterClassTable[static_cast<unsigned char>(c)] & SchemeFirstChar; }
237 static inline bool isSchemeFirstChar(UChar c) { return c <= 0xff && (characterClassTable[c] & SchemeFirstChar); }
238 static inline bool isSchemeChar(char c) { return characterClassTable[static_cast<unsigned char>(c)] & SchemeChar; }
239 static inline bool isSchemeChar(UChar c) { return c <= 0xff && (characterClassTable[c] & SchemeChar); }
240 static inline bool isUserInfoChar(unsigned char c) { return characterClassTable[c] & UserInfoChar; }
241 static inline bool isHostnameChar(unsigned char c) { return characterClassTable[c] & HostnameChar; }
242 static inline bool isIPv6Char(unsigned char c) { return characterClassTable[c] & IPv6Char; }
243 static inline bool isPathSegmentEndChar(char c) { return characterClassTable[static_cast<unsigned char>(c)] & PathSegmentEndChar; }
244 static inline bool isPathSegmentEndChar(UChar c) { return c <= 0xff && (characterClassTable[c] & PathSegmentEndChar); }
245 static inline bool isBadChar(unsigned char c) { return characterClassTable[c] & BadChar; }
247 static inline bool isSchemeCharacterMatchIgnoringCase(char character, char schemeCharacter)
249 ASSERT(isSchemeChar(character));
250 ASSERT(schemeCharacter & 0x20);
251 ASSERT(isASCIILower(schemeCharacter) || (!isASCIIUpper(schemeCharacter) && isSchemeChar(schemeCharacter)));
252 return (character | 0x20) == schemeCharacter;
255 // Copies the source to the destination, assuming all the source characters are
256 // ASCII. The destination buffer must be large enough. Null characters are allowed
257 // in the source string, and no attempt is made to null-terminate the result.
258 static void copyASCII(const UChar* src, int length, char* dest)
260 for (int i = 0; i < length; i++)
261 dest[i] = static_cast<char>(src[i]);
264 static void appendASCII(const String& base, const char* rel, size_t len, CharBuffer& buffer)
266 buffer.resize(base.length() + len + 1);
267 copyASCII(base.characters(), base.length(), buffer.data());
268 memcpy(buffer.data() + base.length(), rel, len);
269 buffer[buffer.size() - 1] = '\0';
272 // FIXME: Move to PlatformString.h eventually.
273 // Returns the index of the first index in string |s| of any of the characters
274 // in |toFind|. |toFind| should be a null-terminated string, all characters up
275 // to the null will be searched. Returns int if not found.
276 static int findFirstOf(const UChar* s, int sLen, int startPos, const char* toFind)
278 for (int i = startPos; i < sLen; i++) {
279 const char* cur = toFind;
281 if (s[i] == *(cur++))
289 static void checkEncodedString(const String& url)
291 for (unsigned i = 0; i < url.length(); ++i)
292 ASSERT(!(url[i] & ~0x7F));
294 ASSERT(!url.length() || isSchemeFirstChar(url[0]));
297 static inline void checkEncodedString(const String&)
302 inline bool KURL::protocolIs(const String& string, const char* protocol)
304 return WebCore::protocolIs(string, protocol);
307 void KURL::invalidate()
310 m_protocolIsInHTTPFamily = false;
318 m_pathAfterLastSlash = 0;
323 KURL::KURL(ParsedURLStringTag, const char* url)
326 ASSERT(url == m_string);
329 KURL::KURL(ParsedURLStringTag, const String& url)
332 ASSERT(url == m_string);
335 KURL::KURL(ParsedURLStringTag, const URLString& url)
338 ASSERT(url.string() == m_string);
341 KURL::KURL(const KURL& base, const String& relative)
343 init(base, relative, UTF8Encoding());
346 KURL::KURL(const KURL& base, const String& relative, const TextEncoding& encoding)
348 // For UTF-{7,16,32}, we want to use UTF-8 for the query part as
349 // we do when submitting a form. A form with GET method
350 // has its contents added to a URL as query params and it makes sense
352 init(base, relative, encoding.encodingForFormSubmission());
355 static bool shouldTrimFromURL(unsigned char c)
357 // Browsers ignore leading/trailing whitespace and control
358 // characters from URLs. Note that c is an *unsigned* char here
359 // so this comparison should only catch control characters.
363 void KURL::init(const KURL& base, const String& relative, const TextEncoding& encoding)
365 // Allow resolutions with a null or empty base URL, but not with any other invalid one.
366 // FIXME: Is this a good rule?
367 if (!base.m_isValid && !base.isEmpty()) {
373 // For compatibility with Win IE, treat backslashes as if they were slashes,
374 // as long as we're not dealing with javascript: or data: URLs.
375 String rel = relative;
376 if (rel.contains('\\') && !(protocolIsJavaScript(rel) || protocolIs(rel, "data")))
377 rel = substituteBackslashes(rel);
379 String* originalString = &rel;
381 bool allASCII = charactersAreAllASCII(rel.characters(), rel.length());
382 CharBuffer strBuffer;
387 strBuffer.resize(len + 1);
388 copyASCII(rel.characters(), len, strBuffer.data());
390 str = strBuffer.data();
393 encodeRelativeString(rel, encoding, strBuffer);
394 str = strBuffer.data();
398 // Get rid of leading whitespace and control characters.
399 while (len && shouldTrimFromURL(*str)) {
405 // Get rid of trailing whitespace and control characters.
406 while (len && shouldTrimFromURL(str[len - 1])) {
411 // According to the RFC, the reference should be interpreted as an
412 // absolute URI if possible, using the "leftmost, longest"
413 // algorithm. If the URI reference is absolute it will have a
414 // scheme, meaning that it will have a colon before the first
415 // non-scheme element.
416 bool absolute = false;
418 if (isSchemeFirstChar(*p)) {
420 while (isSchemeChar(*p)) {
424 if (p[1] != '/' && equalIgnoringCase(base.protocol(), String(str, p - str)) && base.isHierarchical()) {
432 CharBuffer parseBuffer;
435 parse(str, originalString);
437 // If the base is empty or opaque (e.g. data: or javascript:), then the URL is invalid
438 // unless the relative URL is a single fragment.
439 if (!base.isHierarchical()) {
441 appendASCII(base.m_string.left(base.m_queryEnd), str, len, parseBuffer);
442 parse(parseBuffer.data(), 0);
452 // The reference is empty, so this is a reference to the same document with any fragment identifier removed.
454 removeFragmentIdentifier();
457 // must be fragment-only reference
458 appendASCII(base.m_string.left(base.m_queryEnd), str, len, parseBuffer);
459 parse(parseBuffer.data(), 0);
463 // query-only reference, special case needed for non-URL results
464 appendASCII(base.m_string.left(base.m_pathEnd), str, len, parseBuffer);
465 parse(parseBuffer.data(), 0);
469 // must be net-path or absolute-path reference
472 appendASCII(base.m_string.left(base.m_schemeEnd + 1), str, len, parseBuffer);
473 parse(parseBuffer.data(), 0);
476 appendASCII(base.m_string.left(base.m_portEnd), str, len, parseBuffer);
477 parse(parseBuffer.data(), 0);
482 // must be relative-path reference
484 // Base part plus relative part plus one possible slash added in between plus terminating \0 byte.
485 parseBuffer.resize(base.m_pathEnd + 1 + len + 1);
487 char* bufferPos = parseBuffer.data();
489 // first copy everything before the path from the base
490 unsigned baseLength = base.m_string.length();
491 const UChar* baseCharacters = base.m_string.characters();
492 CharBuffer baseStringBuffer(baseLength);
493 copyASCII(baseCharacters, baseLength, baseStringBuffer.data());
494 const char* baseString = baseStringBuffer.data();
495 const char* baseStringStart = baseString;
496 const char* pathStart = baseStringStart + base.m_portEnd;
497 while (baseStringStart < pathStart)
498 *bufferPos++ = *baseStringStart++;
499 char* bufferPathStart = bufferPos;
501 // now copy the base path
502 const char* baseStringEnd = baseString + base.m_pathEnd;
504 // go back to the last slash
505 while (baseStringEnd > baseStringStart && baseStringEnd[-1] != '/')
508 if (baseStringEnd == baseStringStart) {
509 // no path in base, add a path separator if necessary
510 if (base.m_schemeEnd + 1 != base.m_pathEnd && *str && *str != '?' && *str != '#')
513 bufferPos += copyPathRemovingDots(bufferPos, baseStringStart, 0, baseStringEnd - baseStringStart);
516 const char* relStringStart = str;
517 const char* relStringPos = relStringStart;
519 while (*relStringPos && *relStringPos != '?' && *relStringPos != '#') {
520 if (relStringPos[0] == '.' && bufferPos[-1] == '/') {
521 if (isPathSegmentEndChar(relStringPos[1])) {
522 // skip over "." segment
524 if (relStringPos[0] == '/')
527 } else if (relStringPos[1] == '.' && isPathSegmentEndChar(relStringPos[2])) {
528 // skip over ".." segment and rewind the last segment
529 // the RFC leaves it up to the app to decide what to do with excess
530 // ".." segments - we choose to drop them since some web content
533 if (relStringPos[0] == '/')
535 if (bufferPos > bufferPathStart + 1)
537 while (bufferPos > bufferPathStart + 1 && bufferPos[-1] != '/')
543 *bufferPos = *relStringPos;
548 // all done with the path work, now copy any remainder
549 // of the relative reference; this will also add a null terminator
550 strcpy(bufferPos, relStringPos);
552 parse(parseBuffer.data(), 0);
554 ASSERT(strlen(parseBuffer.data()) + 1 <= parseBuffer.size());
561 KURL KURL::copy() const
564 result.m_string = result.m_string.crossThreadString();
568 bool KURL::hasPath() const
570 return m_pathEnd != m_portEnd;
573 String KURL::lastPathComponent() const
578 unsigned end = m_pathEnd - 1;
579 if (m_string[end] == '/')
582 size_t start = m_string.reverseFind('/', end);
583 if (start < static_cast<unsigned>(m_portEnd))
587 return m_string.substring(start, end - start + 1);
590 String KURL::protocol() const
592 return m_string.left(m_schemeEnd);
595 String KURL::host() const
597 int start = hostStart();
598 return decodeURLEscapeSequences(m_string.substring(start, m_hostEnd - start));
601 unsigned short KURL::port() const
603 // We return a port of 0 if there is no port specified. This can happen in two situations:
604 // 1) The URL contains no colon after the host name and before the path component of the URL.
605 // 2) The URL contains a colon but there's no port number before the path component of the URL begins.
606 if (m_hostEnd == m_portEnd || m_hostEnd == m_portEnd - 1)
609 const UChar* stringData = m_string.characters();
611 unsigned number = charactersToUIntStrict(stringData + m_hostEnd + 1, m_portEnd - m_hostEnd - 1, &ok);
612 if (!ok || number > maximumValidPortNumber)
613 return invalidPortNumber;
617 String KURL::pass() const
619 if (m_passwordEnd == m_userEnd)
622 return decodeURLEscapeSequences(m_string.substring(m_userEnd + 1, m_passwordEnd - m_userEnd - 1));
625 String KURL::user() const
627 return decodeURLEscapeSequences(m_string.substring(m_userStart, m_userEnd - m_userStart));
630 String KURL::fragmentIdentifier() const
632 if (m_fragmentEnd == m_queryEnd)
635 return m_string.substring(m_queryEnd + 1, m_fragmentEnd - (m_queryEnd + 1));
638 bool KURL::hasFragmentIdentifier() const
640 return m_fragmentEnd != m_queryEnd;
643 void KURL::copyParsedQueryTo(ParsedURLParameters& parameters) const
645 const UChar* pos = m_string.characters() + m_pathEnd + 1;
646 const UChar* end = m_string.characters() + m_queryEnd;
648 const UChar* parameterStart = pos;
649 while (pos < end && *pos != '&')
651 const UChar* parameterEnd = pos;
656 if (parameterStart == parameterEnd)
658 const UChar* nameStart = parameterStart;
659 const UChar* equalSign = parameterStart;
660 while (equalSign < parameterEnd && *equalSign != '=')
662 if (equalSign == nameStart)
664 String name(nameStart, equalSign - nameStart);
665 String value = equalSign == parameterEnd ? String() : String(equalSign + 1, parameterEnd - equalSign - 1);
666 parameters.set(name, value);
670 String KURL::baseAsString() const
672 return m_string.left(m_pathAfterLastSlash);
677 static inline void assertProtocolIsGood(const char*)
683 static void assertProtocolIsGood(const char* protocol)
685 const char* p = protocol;
687 ASSERT(*p > ' ' && *p < 0x7F && !(*p >= 'A' && *p <= 'Z'));
694 bool KURL::protocolIs(const char* protocol) const
696 assertProtocolIsGood(protocol);
698 // JavaScript URLs are "valid" and should be executed even if KURL decides they are invalid.
699 // The free function protocolIsJavaScript() should be used instead.
700 ASSERT(!equalIgnoringCase(protocol, String("javascript")));
705 // Do the comparison without making a new string object.
706 for (int i = 0; i < m_schemeEnd; ++i) {
707 if (!protocol[i] || !isSchemeCharacterMatchIgnoringCase(m_string[i], protocol[i]))
710 return !protocol[m_schemeEnd]; // We should have consumed all characters in the argument.
713 String KURL::query() const
715 if (m_queryEnd == m_pathEnd)
718 return m_string.substring(m_pathEnd + 1, m_queryEnd - (m_pathEnd + 1));
721 String KURL::path() const
723 return decodeURLEscapeSequences(m_string.substring(m_portEnd, m_pathEnd - m_portEnd));
726 bool KURL::setProtocol(const String& s)
728 // Firefox and IE remove everything after the first ':'.
729 size_t separatorPosition = s.find(':');
730 String newProtocol = s.substring(0, separatorPosition);
732 if (!isValidProtocol(newProtocol))
736 parse(newProtocol + ":" + m_string);
740 parse(newProtocol + m_string.substring(m_schemeEnd));
744 void KURL::setHost(const String& s)
749 // FIXME: Non-ASCII characters must be encoded and escaped to match parse() expectations,
750 // and to avoid changing more than just the host.
752 bool slashSlashNeeded = m_userStart == m_schemeEnd + 1;
754 parse(m_string.left(hostStart()) + (slashSlashNeeded ? "//" : "") + s + m_string.substring(m_hostEnd));
757 void KURL::removePort()
759 if (m_hostEnd == m_portEnd)
761 parse(m_string.left(m_hostEnd) + m_string.substring(m_portEnd));
764 void KURL::setPort(unsigned short i)
769 bool colonNeeded = m_portEnd == m_hostEnd;
770 int portStart = (colonNeeded ? m_hostEnd : m_hostEnd + 1);
772 parse(m_string.left(portStart) + (colonNeeded ? ":" : "") + String::number(i) + m_string.substring(m_portEnd));
775 void KURL::setHostAndPort(const String& hostAndPort)
780 // FIXME: Non-ASCII characters must be encoded and escaped to match parse() expectations,
781 // and to avoid changing more than just host and port.
783 bool slashSlashNeeded = m_userStart == m_schemeEnd + 1;
785 parse(m_string.left(hostStart()) + (slashSlashNeeded ? "//" : "") + hostAndPort + m_string.substring(m_portEnd));
788 void KURL::setUser(const String& user)
793 // FIXME: Non-ASCII characters must be encoded and escaped to match parse() expectations,
794 // and to avoid changing more than just the user login.
797 if (!user.isEmpty()) {
799 if (m_userStart == m_schemeEnd + 1)
801 // Add '@' if we didn't have one before.
802 if (end == m_hostEnd || (end == m_passwordEnd && m_string[end] != '@'))
805 // Remove '@' if we now have neither user nor password.
806 if (m_userEnd == m_passwordEnd && end != m_hostEnd && m_string[end] == '@')
809 parse(m_string.left(m_userStart) + u + m_string.substring(end));
812 void KURL::setPass(const String& password)
817 // FIXME: Non-ASCII characters must be encoded and escaped to match parse() expectations,
818 // and to avoid changing more than just the user password.
820 int end = m_passwordEnd;
821 if (!password.isEmpty()) {
822 p = ":" + password + "@";
823 if (m_userEnd == m_schemeEnd + 1)
825 // Eat the existing '@' since we are going to add our own.
826 if (end != m_hostEnd && m_string[end] == '@')
829 // Remove '@' if we now have neither user nor password.
830 if (m_userStart == m_userEnd && end != m_hostEnd && m_string[end] == '@')
833 parse(m_string.left(m_userEnd) + p + m_string.substring(end));
836 void KURL::setFragmentIdentifier(const String& s)
841 // FIXME: Non-ASCII characters must be encoded and escaped to match parse() expectations.
842 parse(m_string.left(m_queryEnd) + "#" + s);
845 void KURL::removeFragmentIdentifier()
849 parse(m_string.left(m_queryEnd));
852 void KURL::setQuery(const String& query)
857 // FIXME: '#' and non-ASCII characters must be encoded and escaped.
858 // Usually, the query is encoded using document encoding, not UTF-8, but we don't have
859 // access to the document in this function.
860 if ((query.isEmpty() || query[0] != '?') && !query.isNull())
861 parse(m_string.left(m_pathEnd) + "?" + query + m_string.substring(m_queryEnd));
863 parse(m_string.left(m_pathEnd) + query + m_string.substring(m_queryEnd));
867 void KURL::setPath(const String& s)
872 // FIXME: encodeWithURLEscapeSequences does not correctly escape '#' and '?', so fragment and query parts
873 // may be inadvertently affected.
875 if (path.isEmpty() || path[0] != '/')
878 parse(m_string.left(m_portEnd) + encodeWithURLEscapeSequences(path) + m_string.substring(m_pathEnd));
881 String KURL::deprecatedString() const
886 StringBuilder result;
888 result.append(protocol());
891 StringBuilder authority;
893 if (m_hostEnd != m_passwordEnd) {
894 if (m_userEnd != m_userStart) {
895 authority.append(user());
896 authority.append('@');
898 authority.append(host());
900 authority.append(':');
901 authority.append(String::number(port()));
905 if (!authority.isEmpty()) {
908 result.append(authority.characters(), authority.length());
909 } else if (protocolIs("file")) {
914 result.append(path());
916 if (m_pathEnd != m_queryEnd) {
918 result.append(query());
921 if (m_fragmentEnd != m_queryEnd) {
923 result.append(fragmentIdentifier());
926 return result.toString();
929 String decodeURLEscapeSequences(const String& string)
931 return decodeEscapeSequences<URLEscapeSequence>(string, UTF8Encoding());
934 String decodeURLEscapeSequences(const String& string, const TextEncoding& encoding)
936 return decodeEscapeSequences<URLEscapeSequence>(string, encoding);
939 // Caution: This function does not bounds check.
940 static void appendEscapedChar(char*& buffer, unsigned char c)
943 placeByteAsHex(c, buffer);
946 static void appendEscapingBadChars(char*& buffer, const char* strStart, size_t length)
950 const char* str = strStart;
951 const char* strEnd = strStart + length;
952 while (str < strEnd) {
953 unsigned char c = *str++;
955 if (c == '%' || c == '?')
957 else if (c != 0x09 && c != 0x0a && c != 0x0d)
958 appendEscapedChar(p, c);
966 static void escapeAndAppendFragment(char*& buffer, const char* strStart, size_t length)
970 const char* str = strStart;
971 const char* strEnd = strStart + length;
972 while (str < strEnd) {
973 unsigned char c = *str++;
974 // Strip CR, LF and Tab from fragments, per:
975 // https://bugs.webkit.org/show_bug.cgi?id=8770
976 if (c == 0x09 || c == 0x0a || c == 0x0d)
979 // Chrome and IE allow non-ascii characters in fragments, however doing
980 // so would hit an ASSERT in checkEncodedString, so for now we don't.
981 if (c < 0x20 || c >= 127) {
982 appendEscapedChar(p, c);
991 // copy a path, accounting for "." and ".." segments
992 static int copyPathRemovingDots(char* dst, const char* src, int srcStart, int srcEnd)
994 char* bufferPathStart = dst;
996 // empty path is a special case, and need not have a leading slash
997 if (srcStart != srcEnd) {
998 const char* baseStringStart = src + srcStart;
999 const char* baseStringEnd = src + srcEnd;
1000 const char* baseStringPos = baseStringStart;
1002 // this code is unprepared for paths that do not begin with a
1003 // slash and we should always have one in the source string
1004 ASSERT(baseStringPos[0] == '/');
1006 // copy the leading slash into the destination
1007 *dst = *baseStringPos;
1011 while (baseStringPos < baseStringEnd) {
1012 if (baseStringPos[0] == '.' && dst[-1] == '/') {
1013 if (baseStringPos[1] == '/' || baseStringPos + 1 == baseStringEnd) {
1014 // skip over "." segment
1017 } else if (baseStringPos[1] == '.' && (baseStringPos[2] == '/' ||
1018 baseStringPos + 2 == baseStringEnd)) {
1019 // skip over ".." segment and rewind the last segment
1020 // the RFC leaves it up to the app to decide what to do with excess
1021 // ".." segments - we choose to drop them since some web content
1024 if (dst > bufferPathStart + 1)
1026 while (dst > bufferPathStart && dst[-1] != '/')
1032 *dst = *baseStringPos;
1038 return dst - bufferPathStart;
1041 static inline bool hasSlashDotOrDotDot(const char* str)
1043 const unsigned char* p = reinterpret_cast<const unsigned char*>(str);
1046 unsigned char pc = *p;
1047 while (unsigned char c = *++p) {
1048 if (c == '.' && (pc == '/' || pc == '.'))
1055 void KURL::parse(const String& string)
1057 checkEncodedString(string);
1059 CharBuffer buffer(string.length() + 1);
1060 copyASCII(string.characters(), string.length(), buffer.data());
1061 buffer[string.length()] = '\0';
1062 parse(buffer.data(), &string);
1065 static inline bool equal(const char* a, size_t lenA, const char* b, size_t lenB)
1069 return !strncmp(a, b, lenA);
1072 // List of default schemes is taken from google-url:
1073 // http://code.google.com/p/google-url/source/browse/trunk/src/url_canon_stdurl.cc#120
1074 static inline bool isDefaultPortForScheme(const char* port, size_t portLength, const char* scheme, size_t schemeLength)
1076 // This switch is theoretically a performance optimization. It came over when
1077 // the code was moved from google-url, but may be removed later.
1078 switch (schemeLength) {
1080 return equal("ws", 2, scheme, schemeLength) && equal("80", 2, port, portLength);
1082 if (equal("ftp", 3, scheme, schemeLength))
1083 return equal("21", 2, port, portLength);
1084 if (equal("wss", 3, scheme, schemeLength))
1085 return equal("443", 3, port, portLength);
1088 return equal("http", 4, scheme, schemeLength) && equal("80", 2, port, portLength);
1090 return equal("https", 5, scheme, schemeLength) && equal("443", 3, port, portLength);
1092 return equal("gopher", 6, scheme, schemeLength) && equal("70", 2, port, portLength);
1097 static inline bool hostPortIsEmptyButCredentialsArePresent(int hostStart, int portEnd, char userEndChar)
1099 return userEndChar == '@' && hostStart == portEnd;
1102 static bool isNonFileHierarchicalScheme(const char* scheme, size_t schemeLength)
1104 switch (schemeLength) {
1106 return equal("ws", 2, scheme, schemeLength);
1108 return equal("ftp", 3, scheme, schemeLength) || equal("wss", 3, scheme, schemeLength);
1110 return equal("http", 4, scheme, schemeLength);
1112 return equal("https", 5, scheme, schemeLength);
1114 return equal("gopher", 6, scheme, schemeLength);
1119 void KURL::parse(const char* url, const String* originalString)
1121 if (!url || url[0] == '\0') {
1122 // valid URL must be non-empty
1123 m_string = originalString ? *originalString : url;
1128 if (!isSchemeFirstChar(url[0])) {
1129 // scheme must start with an alphabetic character
1130 m_string = originalString ? *originalString : url;
1136 while (isSchemeChar(url[schemeEnd]))
1139 if (url[schemeEnd] != ':') {
1140 m_string = originalString ? *originalString : url;
1145 int userStart = schemeEnd + 1;
1154 bool hierarchical = url[schemeEnd + 1] == '/';
1155 bool hasSecondSlash = hierarchical && url[schemeEnd + 2] == '/';
1157 bool isFile = schemeEnd == 4
1158 && isLetterMatchIgnoringCase(url[0], 'f')
1159 && isLetterMatchIgnoringCase(url[1], 'i')
1160 && isLetterMatchIgnoringCase(url[2], 'l')
1161 && isLetterMatchIgnoringCase(url[3], 'e');
1163 m_protocolIsInHTTPFamily = isLetterMatchIgnoringCase(url[0], 'h')
1164 && isLetterMatchIgnoringCase(url[1], 't')
1165 && isLetterMatchIgnoringCase(url[2], 't')
1166 && isLetterMatchIgnoringCase(url[3], 'p')
1167 && (url[4] == ':' || (isLetterMatchIgnoringCase(url[4], 's') && url[5] == ':'));
1169 if ((hierarchical && hasSecondSlash) || isNonFileHierarchicalScheme(url, schemeEnd)) {
1170 // The part after the scheme is either a net_path or an abs_path whose first path segment is empty.
1171 // Attempt to find an authority.
1172 // FIXME: Authority characters may be scanned twice, and it would be nice to be faster.
1178 userEnd = userStart;
1181 while (isUserInfoChar(url[userEnd])) {
1182 if (url[userEnd] == ':' && colonPos == 0)
1187 if (url[userEnd] == '@') {
1188 // actual end of the userinfo, start on the host
1189 if (colonPos != 0) {
1190 passwordEnd = userEnd;
1192 passwordStart = colonPos + 1;
1194 passwordStart = passwordEnd = userEnd;
1196 hostStart = passwordEnd + 1;
1197 } else if (url[userEnd] == '[' || isPathSegmentEndChar(url[userEnd])) {
1198 // hit the end of the authority, must have been no user
1199 // or looks like an IPv6 hostname
1200 // either way, try to parse it as a hostname
1201 userEnd = userStart;
1202 passwordStart = passwordEnd = userEnd;
1203 hostStart = userStart;
1205 // invalid character
1206 m_string = originalString ? *originalString : url;
1211 hostEnd = hostStart;
1214 if (url[hostEnd] == '[') {
1216 while (isIPv6Char(url[hostEnd]))
1218 if (url[hostEnd] == ']')
1221 // invalid character
1222 m_string = originalString ? *originalString : url;
1227 while (isHostnameChar(url[hostEnd]))
1231 if (url[hostEnd] == ':') {
1232 portStart = portEnd = hostEnd + 1;
1234 // possible start of port
1235 portEnd = portStart;
1236 while (isASCIIDigit(url[portEnd]))
1239 portStart = portEnd = hostEnd;
1241 if (!isPathSegmentEndChar(url[portEnd])) {
1242 // invalid character
1243 m_string = originalString ? *originalString : url;
1248 if (hostPortIsEmptyButCredentialsArePresent(hostStart, portEnd, url[userEnd])) {
1249 // in this circumstance, act as if there is an erroneous hostname containing an '@'
1250 userEnd = userStart;
1251 hostStart = userEnd;
1254 if (userStart == portEnd && !m_protocolIsInHTTPFamily && !isFile) {
1255 // No authority found, which means that this is not a net_path, but rather an abs_path whose first two
1256 // path segments are empty. For file, http and https only, an empty authority is allowed.
1258 userEnd = userStart;
1259 passwordStart = userEnd;
1260 passwordEnd = passwordStart;
1261 hostStart = passwordEnd;
1262 hostEnd = hostStart;
1263 portStart = hostEnd;
1267 // the part after the scheme must be an opaque_part or an abs_path
1268 userEnd = userStart;
1269 passwordStart = passwordEnd = userEnd;
1270 hostStart = hostEnd = passwordEnd;
1271 portStart = portEnd = hostEnd;
1274 int pathStart = portEnd;
1275 int pathEnd = pathStart;
1276 while (url[pathEnd] && url[pathEnd] != '?' && url[pathEnd] != '#')
1279 int queryStart = pathEnd;
1280 int queryEnd = queryStart;
1281 if (url[queryStart] == '?') {
1282 while (url[queryEnd] && url[queryEnd] != '#')
1286 int fragmentStart = queryEnd;
1287 int fragmentEnd = fragmentStart;
1288 if (url[fragmentStart] == '#') {
1290 fragmentEnd = fragmentStart;
1291 while (url[fragmentEnd])
1295 // assemble it all, remembering the real ranges
1297 Vector<char, 4096> buffer(fragmentEnd * 3 + 1);
1299 char *p = buffer.data();
1300 const char *strPtr = url;
1302 // copy in the scheme
1303 const char *schemeEndPtr = url + schemeEnd;
1304 while (strPtr < schemeEndPtr)
1305 *p++ = toASCIILower(*strPtr++);
1306 m_schemeEnd = p - buffer.data();
1308 bool hostIsLocalHost = portEnd - userStart == 9
1309 && isLetterMatchIgnoringCase(url[userStart], 'l')
1310 && isLetterMatchIgnoringCase(url[userStart+1], 'o')
1311 && isLetterMatchIgnoringCase(url[userStart+2], 'c')
1312 && isLetterMatchIgnoringCase(url[userStart+3], 'a')
1313 && isLetterMatchIgnoringCase(url[userStart+4], 'l')
1314 && isLetterMatchIgnoringCase(url[userStart+5], 'h')
1315 && isLetterMatchIgnoringCase(url[userStart+6], 'o')
1316 && isLetterMatchIgnoringCase(url[userStart+7], 's')
1317 && isLetterMatchIgnoringCase(url[userStart+8], 't');
1319 // File URLs need a host part unless it is just file:// or file://localhost
1320 bool degenFilePath = pathStart == pathEnd && (hostStart == hostEnd || hostIsLocalHost);
1322 bool haveNonHostAuthorityPart = userStart != userEnd || passwordStart != passwordEnd || portStart != portEnd;
1324 // add ":" after scheme
1327 // if we have at least one authority part or a file URL - add "//" and authority
1328 if (isFile ? !degenFilePath : (haveNonHostAuthorityPart || hostStart != hostEnd)) {
1332 m_userStart = p - buffer.data();
1335 strPtr = url + userStart;
1336 const char* userEndPtr = url + userEnd;
1337 while (strPtr < userEndPtr)
1339 m_userEnd = p - buffer.data();
1341 // copy in the password
1342 if (passwordEnd != passwordStart) {
1344 strPtr = url + passwordStart;
1345 const char* passwordEndPtr = url + passwordEnd;
1346 while (strPtr < passwordEndPtr)
1349 m_passwordEnd = p - buffer.data();
1351 // If we had any user info, add "@"
1352 if (p - buffer.data() != m_userStart)
1355 // copy in the host, except in the case of a file URL with authority="localhost"
1356 if (!(isFile && hostIsLocalHost && !haveNonHostAuthorityPart)) {
1357 strPtr = url + hostStart;
1358 const char* hostEndPtr = url + hostEnd;
1359 while (strPtr < hostEndPtr)
1362 m_hostEnd = p - buffer.data();
1364 // Copy in the port if the URL has one (and it's not default).
1365 if (hostEnd != portStart) {
1366 const char* portStr = url + portStart;
1367 size_t portLength = portEnd - portStart;
1368 if (portLength && !isDefaultPortForScheme(portStr, portLength, buffer.data(), m_schemeEnd)) {
1370 const char* portEndPtr = url + portEnd;
1371 while (portStr < portEndPtr)
1375 m_portEnd = p - buffer.data();
1377 m_userStart = m_userEnd = m_passwordEnd = m_hostEnd = m_portEnd = p - buffer.data();
1379 // For canonicalization, ensure we have a '/' for no path.
1380 // Do this only for URL with protocol http or https.
1381 if (m_protocolIsInHTTPFamily && pathEnd == pathStart)
1384 // add path, escaping bad characters
1385 if (!hierarchical || !hasSlashDotOrDotDot(url))
1386 appendEscapingBadChars(p, url + pathStart, pathEnd - pathStart);
1388 CharBuffer pathBuffer(pathEnd - pathStart + 1);
1389 size_t length = copyPathRemovingDots(pathBuffer.data(), url, pathStart, pathEnd);
1390 appendEscapingBadChars(p, pathBuffer.data(), length);
1393 m_pathEnd = p - buffer.data();
1395 // Find the position after the last slash in the path, or
1396 // the position before the path if there are no slashes in it.
1398 for (i = m_pathEnd; i > m_portEnd; --i) {
1399 if (buffer[i - 1] == '/')
1402 m_pathAfterLastSlash = i;
1404 // add query, escaping bad characters
1405 appendEscapingBadChars(p, url + queryStart, queryEnd - queryStart);
1406 m_queryEnd = p - buffer.data();
1408 // add fragment, escaping bad characters
1409 if (fragmentEnd != queryEnd) {
1411 escapeAndAppendFragment(p, url + fragmentStart, fragmentEnd - fragmentStart);
1413 m_fragmentEnd = p - buffer.data();
1415 ASSERT(p - buffer.data() <= static_cast<int>(buffer.size()));
1417 // If we didn't end up actually changing the original string and
1418 // it was already in a String, reuse it to avoid extra allocation.
1419 if (originalString && originalString->length() == static_cast<unsigned>(m_fragmentEnd) && strncmp(buffer.data(), url, m_fragmentEnd) == 0)
1420 m_string = *originalString;
1422 m_string = String(buffer.data(), m_fragmentEnd);
1427 bool equalIgnoringFragmentIdentifier(const KURL& a, const KURL& b)
1429 if (a.m_queryEnd != b.m_queryEnd)
1431 unsigned queryLength = a.m_queryEnd;
1432 for (unsigned i = 0; i < queryLength; ++i)
1433 if (a.string()[i] != b.string()[i])
1438 bool protocolHostAndPortAreEqual(const KURL& a, const KURL& b)
1440 if (a.m_schemeEnd != b.m_schemeEnd)
1443 int hostStartA = a.hostStart();
1444 int hostLengthA = a.hostEnd() - hostStartA;
1445 int hostStartB = b.hostStart();
1446 int hostLengthB = b.hostEnd() - b.hostStart();
1447 if (hostLengthA != hostLengthB)
1451 for (int i = 0; i < a.m_schemeEnd; ++i)
1452 if (a.string()[i] != b.string()[i])
1456 for (int i = 0; i < hostLengthA; ++i)
1457 if (a.string()[hostStartA + i] != b.string()[hostStartB + i])
1460 if (a.port() != b.port())
1466 String encodeWithURLEscapeSequences(const String& notEncodedString)
1468 CString asUTF8 = notEncodedString.utf8();
1470 CharBuffer buffer(asUTF8.length() * 3 + 1);
1471 char* p = buffer.data();
1473 const char* str = asUTF8.data();
1474 const char* strEnd = str + asUTF8.length();
1475 while (str < strEnd) {
1476 unsigned char c = *str++;
1478 appendEscapedChar(p, c);
1483 ASSERT(p - buffer.data() <= static_cast<int>(buffer.size()));
1485 return String(buffer.data(), p - buffer.data());
1488 // Appends the punycoded hostname identified by the given string and length to
1489 // the output buffer. The result will not be null terminated.
1490 static void appendEncodedHostname(UCharBuffer& buffer, const UChar* str, unsigned strLen)
1492 // Needs to be big enough to hold an IDN-encoded name.
1493 // For host names bigger than this, we won't do IDN encoding, which is almost certainly OK.
1494 const unsigned hostnameBufferLength = 2048;
1496 if (strLen > hostnameBufferLength || charactersAreAllASCII(str, strLen)) {
1497 buffer.append(str, strLen);
1501 #if USE(ICU_UNICODE)
1502 UChar hostnameBuffer[hostnameBufferLength];
1503 UErrorCode error = U_ZERO_ERROR;
1504 int32_t numCharactersConverted = uidna_IDNToASCII(str, strLen, hostnameBuffer,
1505 hostnameBufferLength, UIDNA_ALLOW_UNASSIGNED, 0, &error);
1506 if (error == U_ZERO_ERROR)
1507 buffer.append(hostnameBuffer, numCharactersConverted);
1508 #elif USE(QT4_UNICODE)
1509 QByteArray result = QUrl::toAce(String(str, strLen));
1510 buffer.append(result.constData(), result.length());
1511 #elif USE(GLIB_UNICODE)
1512 GOwnPtr<gchar> utf8Hostname;
1513 GOwnPtr<GError> utf8Err;
1514 utf8Hostname.set(g_utf16_to_utf8(str, strLen, 0, 0, &utf8Err.outPtr()));
1518 GOwnPtr<gchar> encodedHostname;
1519 encodedHostname.set(g_hostname_to_ascii(utf8Hostname.get()));
1520 if (!encodedHostname)
1523 buffer.append(encodedHostname.get(), strlen(encodedHostname.get()));
1527 static void findHostnamesInMailToURL(const UChar* str, int strLen, Vector<pair<int, int> >& nameRanges)
1529 // In a mailto: URL, host names come after a '@' character and end with a '>' or ',' or '?' or end of string character.
1530 // Skip quoted strings so that characters in them don't confuse us.
1531 // When we find a '?' character, we are past the part of the URL that contains host names.
1537 // Find start of host name or of quoted string.
1538 int hostnameOrStringStart = findFirstOf(str, strLen, p, "\"@?");
1539 if (hostnameOrStringStart == -1)
1541 UChar c = str[hostnameOrStringStart];
1542 p = hostnameOrStringStart + 1;
1548 // Find end of host name.
1549 int hostnameStart = p;
1550 int hostnameEnd = findFirstOf(str, strLen, p, ">,?");
1552 if (hostnameEnd == -1) {
1553 hostnameEnd = strLen;
1560 nameRanges.append(make_pair(hostnameStart, hostnameEnd));
1565 // Skip quoted string.
1568 int escapedCharacterOrStringEnd = findFirstOf(str, strLen, p, "\"\\");
1569 if (escapedCharacterOrStringEnd == -1)
1572 c = str[escapedCharacterOrStringEnd];
1573 p = escapedCharacterOrStringEnd + 1;
1575 // If we are the end of the string, then break from the string loop back to the host name loop.
1579 // Skip escaped character.
1590 static bool findHostnameInHierarchicalURL(const UChar* str, int strLen, int& startOffset, int& endOffset)
1592 // Find the host name in a hierarchical URL.
1593 // It comes after a "://" sequence, with scheme characters preceding, and
1594 // this should be the first colon in the string.
1595 // It ends with the end of the string or a ":" or a path segment ending character.
1596 // If there is a "@" character, the host part is just the part after the "@".
1597 int separator = findFirstOf(str, strLen, 0, ":");
1598 if (separator == -1 || separator + 2 >= strLen ||
1599 str[separator + 1] != '/' || str[separator + 2] != '/')
1602 // Check that all characters before the :// are valid scheme characters.
1603 if (!isSchemeFirstChar(str[0]))
1605 for (int i = 1; i < separator; ++i) {
1606 if (!isSchemeChar(str[i]))
1610 // Start after the separator.
1611 int authorityStart = separator + 3;
1613 // Find terminating character.
1614 int hostnameEnd = strLen;
1615 for (int i = authorityStart; i < strLen; ++i) {
1617 if (c == ':' || (isPathSegmentEndChar(c) && c != 0)) {
1623 // Find "@" for the start of the host name.
1624 int userInfoTerminator = findFirstOf(str, strLen, authorityStart, "@");
1626 if (userInfoTerminator == -1 || userInfoTerminator > hostnameEnd)
1627 hostnameStart = authorityStart;
1629 hostnameStart = userInfoTerminator + 1;
1631 startOffset = hostnameStart;
1632 endOffset = hostnameEnd;
1636 // Converts all hostnames found in the given input to punycode, preserving the
1637 // rest of the URL unchanged. The output will NOT be null-terminated.
1638 static void encodeHostnames(const String& str, UCharBuffer& output)
1642 if (protocolIs(str, "mailto")) {
1643 Vector<pair<int, int> > hostnameRanges;
1644 findHostnamesInMailToURL(str.characters(), str.length(), hostnameRanges);
1645 int n = hostnameRanges.size();
1647 for (int i = 0; i < n; ++i) {
1648 const pair<int, int>& r = hostnameRanges[i];
1649 output.append(&str.characters()[p], r.first - p);
1650 appendEncodedHostname(output, &str.characters()[r.first], r.second - r.first);
1653 // This will copy either everything after the last hostname, or the
1654 // whole thing if there is no hostname.
1655 output.append(&str.characters()[p], str.length() - p);
1657 int hostStart, hostEnd;
1658 if (findHostnameInHierarchicalURL(str.characters(), str.length(), hostStart, hostEnd)) {
1659 output.append(str.characters(), hostStart); // Before hostname.
1660 appendEncodedHostname(output, &str.characters()[hostStart], hostEnd - hostStart);
1661 output.append(&str.characters()[hostEnd], str.length() - hostEnd); // After hostname.
1663 // No hostname to encode, return the input.
1664 output.append(str.characters(), str.length());
1669 static void encodeRelativeString(const String& rel, const TextEncoding& encoding, CharBuffer& output)
1672 encodeHostnames(rel, s);
1674 TextEncoding pathEncoding(UTF8Encoding()); // Path is always encoded as UTF-8; other parts may depend on the scheme.
1677 if (encoding != pathEncoding && encoding.isValid() && !protocolIs(rel, "mailto") && !protocolIs(rel, "data") && !protocolIsJavaScript(rel)) {
1678 // Find the first instance of either # or ?, keep pathEnd at -1 otherwise.
1679 pathEnd = findFirstOf(s.data(), s.size(), 0, "#?");
1682 if (pathEnd == -1) {
1683 CString decoded = pathEncoding.encode(s.data(), s.size(), URLEncodedEntitiesForUnencodables);
1684 output.resize(decoded.length());
1685 memcpy(output.data(), decoded.data(), decoded.length());
1687 CString pathDecoded = pathEncoding.encode(s.data(), pathEnd, URLEncodedEntitiesForUnencodables);
1688 // Unencodable characters in URLs are represented by converting
1689 // them to XML entities and escaping non-alphanumeric characters.
1690 CString otherDecoded = encoding.encode(s.data() + pathEnd, s.size() - pathEnd, URLEncodedEntitiesForUnencodables);
1692 output.resize(pathDecoded.length() + otherDecoded.length());
1693 memcpy(output.data(), pathDecoded.data(), pathDecoded.length());
1694 memcpy(output.data() + pathDecoded.length(), otherDecoded.data(), otherDecoded.length());
1696 output.append('\0'); // null-terminate the output.
1699 static String substituteBackslashes(const String& string)
1701 size_t questionPos = string.find('?');
1702 size_t hashPos = string.find('#');
1705 if (hashPos != notFound && (questionPos == notFound || questionPos > hashPos))
1707 else if (questionPos != notFound)
1708 pathEnd = questionPos;
1710 pathEnd = string.length();
1712 return string.left(pathEnd).replace('\\','/') + string.substring(pathEnd);
1715 bool KURL::isHierarchical() const
1719 ASSERT(m_string[m_schemeEnd] == ':');
1720 return m_string[m_schemeEnd + 1] == '/';
1723 void KURL::copyToBuffer(CharBuffer& buffer) const
1725 // FIXME: This throws away the high bytes of all the characters in the string!
1726 // That's fine for a valid URL, which is all ASCII, but not for invalid URLs.
1727 buffer.resize(m_string.length());
1728 copyASCII(m_string.characters(), m_string.length(), buffer.data());
1731 bool protocolIs(const String& url, const char* protocol)
1733 // Do the comparison without making a new string object.
1734 assertProtocolIsGood(protocol);
1735 for (int i = 0; ; ++i) {
1737 return url[i] == ':';
1738 if (!isLetterMatchIgnoringCase(url[i], protocol[i]))
1743 bool isValidProtocol(const String& protocol)
1745 // RFC3986: ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
1746 if (protocol.isEmpty())
1748 if (!isSchemeFirstChar(protocol[0]))
1750 unsigned protocolLength = protocol.length();
1751 for (unsigned i = 1; i < protocolLength; i++) {
1752 if (!isSchemeChar(protocol[i]))
1759 void KURL::print() const
1761 printf("%s\n", m_string.utf8().data());
1765 #endif // !USE(GOOGLEURL)
1767 String KURL::strippedForUseAsReferrer() const
1769 KURL referrer(*this);
1770 referrer.setUser(String());
1771 referrer.setPass(String());
1772 referrer.removeFragmentIdentifier();
1773 return referrer.string();
1776 bool KURL::isLocalFile() const
1778 // Including feed here might be a bad idea since drag and drop uses this check
1779 // and including feed would allow feeds to potentially let someone's blog
1780 // read the contents of the clipboard on a drag, even without a drop.
1781 // Likewise with using the FrameLoader::shouldTreatURLAsLocal() function.
1782 return protocolIs("file");
1785 bool protocolIsJavaScript(const String& url)
1787 return protocolIs(url, "javascript");
1790 const KURL& blankURL()
1792 DEFINE_STATIC_LOCAL(KURL, staticBlankURL, (ParsedURLString, "about:blank"));
1793 return staticBlankURL;
1796 bool isDefaultPortForProtocol(unsigned short port, const String& protocol)
1798 if (protocol.isEmpty())
1801 typedef HashMap<String, unsigned, CaseFoldingHash> DefaultPortsMap;
1802 DEFINE_STATIC_LOCAL(DefaultPortsMap, defaultPorts, ());
1803 if (defaultPorts.isEmpty()) {
1804 defaultPorts.set("http", 80);
1805 defaultPorts.set("https", 443);
1806 defaultPorts.set("ftp", 21);
1807 defaultPorts.set("ftps", 990);
1809 return defaultPorts.get(protocol) == port;
1812 bool portAllowed(const KURL& url)
1814 unsigned short port = url.port();
1816 // Since most URLs don't have a port, return early for the "no port" case.
1820 // This blocked port list matches the port blocking that Mozilla implements.
1821 // See http://www.mozilla.org/projects/netlib/PortBanning.html for more information.
1822 static const unsigned short blockedPortList[] = {
1856 135, // loc-srv / epmap
1862 512, // print / exec
1879 3659, // apple-sasl / PasswordServer [Apple addition]
1882 6665, // Alternate IRC [Apple addition]
1883 6666, // Alternate IRC [Apple addition]
1884 6667, // Standard IRC [Apple addition]
1885 6668, // Alternate IRC [Apple addition]
1886 6669, // Alternate IRC [Apple addition]
1887 invalidPortNumber, // Used to block all invalid port numbers
1889 const unsigned short* const blockedPortListEnd = blockedPortList + WTF_ARRAY_LENGTH(blockedPortList);
1892 // The port list must be sorted for binary_search to work.
1893 static bool checkedPortList = false;
1894 if (!checkedPortList) {
1895 for (const unsigned short* p = blockedPortList; p != blockedPortListEnd - 1; ++p)
1896 ASSERT(*p < *(p + 1));
1897 checkedPortList = true;
1901 // If the port is not in the blocked port list, allow it.
1902 if (!binary_search(blockedPortList, blockedPortListEnd, port))
1905 // Allow ports 21 and 22 for FTP URLs, as Mozilla does.
1906 if ((port == 21 || port == 22) && url.protocolIs("ftp"))
1909 // Allow any port number in a file URL, since the port number is ignored.
1910 if (url.protocolIs("file"))
1916 String mimeTypeFromDataURL(const String& url)
1918 ASSERT(protocolIs(url, "data"));
1919 size_t index = url.find(';');
1920 if (index == notFound)
1921 index = url.find(',');
1922 if (index != notFound) {
1924 return url.substring(5, index - 5);
1925 return "text/plain"; // Data URLs with no MIME type are considered text/plain.
1930 bool protocolIsInHTTPFamily(const String& url)
1932 unsigned length = url.length();
1933 const UChar* characters = url.characters();
1935 && isLetterMatchIgnoringCase(characters[0], 'h')
1936 && isLetterMatchIgnoringCase(characters[1], 't')
1937 && isLetterMatchIgnoringCase(characters[2], 't')
1938 && isLetterMatchIgnoringCase(characters[3], 'p')
1939 && (characters[4] == ':'
1940 || (isLetterMatchIgnoringCase(characters[4], 's') && length > 5 && characters[5] == ':'));