initial import
[vuplus_webkit] / Source / WebCore / platform / KURL.cpp
1 /*
2  * Copyright (C) 2004, 2007, 2008, 2011 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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. 
24  */
25
26 #include "config.h"
27 #include "KURL.h"
28
29 #include "DecodeEscapeSequences.h"
30 #include "TextEncoding.h"
31 #include <stdio.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>
38
39 #if USE(ICU_UNICODE)
40 #include <unicode/uidna.h>
41 #elif USE(QT4_UNICODE)
42 #include <QUrl>
43 #elif USE(GLIB_UNICODE)
44 #include <glib.h>
45 #include "GOwnPtr.h"
46 #endif
47
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.
51
52 using namespace std;
53 using namespace WTF;
54
55 namespace WebCore {
56
57 typedef Vector<char, 512> CharBuffer;
58 typedef Vector<UChar, 512> UCharBuffer;
59
60 static const unsigned maximumValidPortNumber = 0xFFFE;
61 static const unsigned invalidPortNumber = 0xFFFF;
62
63 static inline bool isLetterMatchIgnoringCase(UChar character, char lowercaseLetter)
64 {
65     ASSERT(isASCIILower(lowercaseLetter));
66     return (character | 0x20) == lowercaseLetter;
67 }
68
69 #if !USE(GOOGLEURL)
70
71 static inline bool isLetterMatchIgnoringCase(char character, char lowercaseLetter)
72 {
73     ASSERT(isASCIILower(lowercaseLetter));
74     return (character | 0x20) == lowercaseLetter;
75 }
76
77 enum URLCharacterClasses {
78     // alpha 
79     SchemeFirstChar = 1 << 0,
80
81     // ( alpha | digit | "+" | "-" | "." )
82     SchemeChar = 1 << 1,
83
84     // mark        = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
85     // unreserved  = alphanum | mark
86     // ( unreserved | escaped | ";" | ":" | "&" | "=" | "+" | "$" | "," )
87     UserInfoChar = 1 << 2,
88
89     // alnum | "." | "-" | "%"
90     // The above is what the specification says, but we are lenient to
91     // match existing practice and also allow:
92     // "_"
93     HostnameChar = 1 << 3,
94
95     // hexdigit | ":" | "%"
96     IPv6Char = 1 << 4,
97
98     // "#" | "?" | "/" | nul
99     PathSegmentEndChar = 1 << 5,
100
101     // not allowed in path
102     BadChar = 1 << 6
103 };
104
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,
138     /* 64  @ */ 0,
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,
165     /* 91  [ */ 0,
166     /* 92  \ */ 0,    /* 93  ] */ 0,
167     /* 94  ^ */ 0,
168     /* 95  _ */ UserInfoChar | HostnameChar,
169     /* 96  ` */ 0,
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, 
196     /* 123  { */ 0,
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
230 };
231
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&);
235
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; }
246     
247 static inline bool isSchemeCharacterMatchIgnoringCase(char character, char schemeCharacter)
248 {
249     ASSERT(isSchemeChar(character));
250     ASSERT(schemeCharacter & 0x20);
251     ASSERT(isASCIILower(schemeCharacter) || (!isASCIIUpper(schemeCharacter) && isSchemeChar(schemeCharacter)));
252     return (character | 0x20) == schemeCharacter;
253 }
254
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)
259 {
260     for (int i = 0; i < length; i++)
261         dest[i] = static_cast<char>(src[i]);
262 }
263
264 static void appendASCII(const String& base, const char* rel, size_t len, CharBuffer& buffer)
265 {
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';
270 }
271
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)
277 {
278     for (int i = startPos; i < sLen; i++) {
279         const char* cur = toFind;
280         while (*cur) {
281             if (s[i] == *(cur++))
282                 return i;
283         }
284     }
285     return -1;
286 }
287
288 #ifndef NDEBUG
289 static void checkEncodedString(const String& url)
290 {
291     for (unsigned i = 0; i < url.length(); ++i)
292         ASSERT(!(url[i] & ~0x7F));
293
294     ASSERT(!url.length() || isSchemeFirstChar(url[0]));
295 }
296 #else
297 static inline void checkEncodedString(const String&)
298 {
299 }
300 #endif
301
302 inline bool KURL::protocolIs(const String& string, const char* protocol)
303 {
304     return WebCore::protocolIs(string, protocol);
305 }
306
307 void KURL::invalidate()
308 {
309     m_isValid = false;
310     m_protocolIsInHTTPFamily = false;
311     m_schemeEnd = 0;
312     m_userStart = 0;
313     m_userEnd = 0;
314     m_passwordEnd = 0;
315     m_hostEnd = 0;
316     m_portEnd = 0;
317     m_pathEnd = 0;
318     m_pathAfterLastSlash = 0;
319     m_queryEnd = 0;
320     m_fragmentEnd = 0;
321 }
322
323 KURL::KURL(ParsedURLStringTag, const char* url)
324 {
325     parse(url, 0);
326     ASSERT(url == m_string);
327 }
328
329 KURL::KURL(ParsedURLStringTag, const String& url)
330 {
331     parse(url);
332     ASSERT(url == m_string);
333 }
334
335 KURL::KURL(ParsedURLStringTag, const URLString& url)
336 {
337     parse(url.string());
338     ASSERT(url.string() == m_string);
339 }
340
341 KURL::KURL(const KURL& base, const String& relative)
342 {
343     init(base, relative, UTF8Encoding());
344 }
345
346 KURL::KURL(const KURL& base, const String& relative, const TextEncoding& encoding)
347 {
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
351     // to be consistent.
352     init(base, relative, encoding.encodingForFormSubmission());
353 }
354
355 static bool shouldTrimFromURL(unsigned char c)
356 {
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.
360     return c <= ' ';
361 }
362
363 void KURL::init(const KURL& base, const String& relative, const TextEncoding& encoding)
364 {
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()) {
368         m_string = relative;
369         invalidate();
370         return;
371     }
372
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);
378
379     String* originalString = &rel;
380
381     bool allASCII = charactersAreAllASCII(rel.characters(), rel.length());
382     CharBuffer strBuffer;
383     char* str;
384     size_t len;
385     if (allASCII) {
386         len = rel.length();
387         strBuffer.resize(len + 1);
388         copyASCII(rel.characters(), len, strBuffer.data());
389         strBuffer[len] = 0;
390         str = strBuffer.data();
391     } else {
392         originalString = 0;
393         encodeRelativeString(rel, encoding, strBuffer);
394         str = strBuffer.data();
395         len = strlen(str);
396     }
397
398     // Get rid of leading whitespace and control characters.
399     while (len && shouldTrimFromURL(*str)) {
400         originalString = 0;
401         str++;
402         --len;
403     }
404
405     // Get rid of trailing whitespace and control characters.
406     while (len && shouldTrimFromURL(str[len - 1])) {
407         originalString = 0;
408         str[--len] = '\0';
409     }
410
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;
417     char* p = str;
418     if (isSchemeFirstChar(*p)) {
419         ++p;
420         while (isSchemeChar(*p)) {
421             ++p;
422         }
423         if (*p == ':') {
424             if (p[1] != '/' && equalIgnoringCase(base.protocol(), String(str, p - str)) && base.isHierarchical()) {
425                 str = p + 1;
426                 originalString = 0;
427             } else
428                 absolute = true;
429         }
430     }
431
432     CharBuffer parseBuffer;
433
434     if (absolute) {
435         parse(str, originalString);
436     } else {
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()) {
440             if (str[0] == '#') {
441                 appendASCII(base.m_string.left(base.m_queryEnd), str, len, parseBuffer);
442                 parse(parseBuffer.data(), 0);
443             } else {
444                 m_string = relative;
445                 invalidate();
446             }
447             return;
448         }
449
450         switch (str[0]) {
451         case '\0':
452             // The reference is empty, so this is a reference to the same document with any fragment identifier removed.
453             *this = base;
454             removeFragmentIdentifier();
455             break;
456         case '#': {
457             // must be fragment-only reference
458             appendASCII(base.m_string.left(base.m_queryEnd), str, len, parseBuffer);
459             parse(parseBuffer.data(), 0);
460             break;
461         }
462         case '?': {
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);
466             break;
467         }
468         case '/':
469             // must be net-path or absolute-path reference
470             if (str[1] == '/') {
471                 // net-path
472                 appendASCII(base.m_string.left(base.m_schemeEnd + 1), str, len, parseBuffer);
473                 parse(parseBuffer.data(), 0);
474             } else {
475                 // abs-path
476                 appendASCII(base.m_string.left(base.m_portEnd), str, len, parseBuffer);
477                 parse(parseBuffer.data(), 0);
478             }
479             break;
480         default:
481             {
482                 // must be relative-path reference
483
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);
486
487                 char* bufferPos = parseBuffer.data();
488
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;
500
501                 // now copy the base path
502                 const char* baseStringEnd = baseString + base.m_pathEnd;
503
504                 // go back to the last slash
505                 while (baseStringEnd > baseStringStart && baseStringEnd[-1] != '/')
506                     baseStringEnd--;
507
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 != '#')
511                         *bufferPos++ = '/';
512                 } else {
513                     bufferPos += copyPathRemovingDots(bufferPos, baseStringStart, 0, baseStringEnd - baseStringStart);
514                 }
515
516                 const char* relStringStart = str;
517                 const char* relStringPos = relStringStart;
518
519                 while (*relStringPos && *relStringPos != '?' && *relStringPos != '#') {
520                     if (relStringPos[0] == '.' && bufferPos[-1] == '/') {
521                         if (isPathSegmentEndChar(relStringPos[1])) {
522                             // skip over "." segment
523                             relStringPos += 1;
524                             if (relStringPos[0] == '/')
525                                 relStringPos++;
526                             continue;
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
531                             // relies on this.
532                             relStringPos += 2;
533                             if (relStringPos[0] == '/')
534                                 relStringPos++;
535                             if (bufferPos > bufferPathStart + 1)
536                                 bufferPos--;
537                             while (bufferPos > bufferPathStart + 1  && bufferPos[-1] != '/')
538                                 bufferPos--;
539                             continue;
540                         }
541                     }
542
543                     *bufferPos = *relStringPos;
544                     relStringPos++;
545                     bufferPos++;
546                 }
547
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);
551
552                 parse(parseBuffer.data(), 0);
553
554                 ASSERT(strlen(parseBuffer.data()) + 1 <= parseBuffer.size());
555                 break;
556             }
557         }
558     }
559 }
560
561 KURL KURL::copy() const
562 {
563     KURL result = *this;
564     result.m_string = result.m_string.crossThreadString();
565     return result;
566 }
567
568 bool KURL::hasPath() const
569 {
570     return m_pathEnd != m_portEnd;
571 }
572
573 String KURL::lastPathComponent() const
574 {
575     if (!hasPath())
576         return String();
577
578     unsigned end = m_pathEnd - 1;
579     if (m_string[end] == '/')
580         --end;
581
582     size_t start = m_string.reverseFind('/', end);
583     if (start < static_cast<unsigned>(m_portEnd))
584         return String();
585     ++start;
586
587     return m_string.substring(start, end - start + 1);
588 }
589
590 String KURL::protocol() const
591 {
592     return m_string.left(m_schemeEnd);
593 }
594
595 String KURL::host() const
596 {
597     int start = hostStart();
598     return decodeURLEscapeSequences(m_string.substring(start, m_hostEnd - start));
599 }
600
601 unsigned short KURL::port() const
602 {
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)
607         return 0;
608
609     const UChar* stringData = m_string.characters();
610     bool ok = false;
611     unsigned number = charactersToUIntStrict(stringData + m_hostEnd + 1, m_portEnd - m_hostEnd - 1, &ok);
612     if (!ok || number > maximumValidPortNumber)
613         return invalidPortNumber;
614     return number;
615 }
616
617 String KURL::pass() const
618 {
619     if (m_passwordEnd == m_userEnd)
620         return String();
621
622     return decodeURLEscapeSequences(m_string.substring(m_userEnd + 1, m_passwordEnd - m_userEnd - 1)); 
623 }
624
625 String KURL::user() const
626 {
627     return decodeURLEscapeSequences(m_string.substring(m_userStart, m_userEnd - m_userStart));
628 }
629
630 String KURL::fragmentIdentifier() const
631 {
632     if (m_fragmentEnd == m_queryEnd)
633         return String();
634
635     return m_string.substring(m_queryEnd + 1, m_fragmentEnd - (m_queryEnd + 1));
636 }
637
638 bool KURL::hasFragmentIdentifier() const
639 {
640     return m_fragmentEnd != m_queryEnd;
641 }
642
643 void KURL::copyParsedQueryTo(ParsedURLParameters& parameters) const
644 {
645     const UChar* pos = m_string.characters() + m_pathEnd + 1;
646     const UChar* end = m_string.characters() + m_queryEnd;
647     while (pos < end) {
648         const UChar* parameterStart = pos;
649         while (pos < end && *pos != '&')
650             ++pos;
651         const UChar* parameterEnd = pos;
652         if (pos < end) {
653             ASSERT(*pos == '&');
654             ++pos;
655         }
656         if (parameterStart == parameterEnd)
657             continue;
658         const UChar* nameStart = parameterStart;
659         const UChar* equalSign = parameterStart;
660         while (equalSign < parameterEnd && *equalSign != '=')
661             ++equalSign;
662         if (equalSign == nameStart)
663             continue;
664         String name(nameStart, equalSign - nameStart);
665         String value = equalSign == parameterEnd ? String() : String(equalSign + 1, parameterEnd - equalSign - 1);
666         parameters.set(name, value);
667     }
668 }
669
670 String KURL::baseAsString() const
671 {
672     return m_string.left(m_pathAfterLastSlash);
673 }
674
675 #ifdef NDEBUG
676
677 static inline void assertProtocolIsGood(const char*)
678 {
679 }
680
681 #else
682
683 static void assertProtocolIsGood(const char* protocol)
684 {
685     const char* p = protocol;
686     while (*p) {
687         ASSERT(*p > ' ' && *p < 0x7F && !(*p >= 'A' && *p <= 'Z'));
688         ++p;
689     }
690 }
691
692 #endif
693
694 bool KURL::protocolIs(const char* protocol) const
695 {
696     assertProtocolIsGood(protocol);
697
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")));
701
702     if (!m_isValid)
703         return false;
704
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]))
708             return false;
709     }
710     return !protocol[m_schemeEnd]; // We should have consumed all characters in the argument.
711 }
712
713 String KURL::query() const
714 {
715     if (m_queryEnd == m_pathEnd)
716         return String();
717
718     return m_string.substring(m_pathEnd + 1, m_queryEnd - (m_pathEnd + 1)); 
719 }
720
721 String KURL::path() const
722 {
723     return decodeURLEscapeSequences(m_string.substring(m_portEnd, m_pathEnd - m_portEnd)); 
724 }
725
726 bool KURL::setProtocol(const String& s)
727 {
728     // Firefox and IE remove everything after the first ':'.
729     size_t separatorPosition = s.find(':');
730     String newProtocol = s.substring(0, separatorPosition);
731
732     if (!isValidProtocol(newProtocol))
733         return false;
734
735     if (!m_isValid) {
736         parse(newProtocol + ":" + m_string);
737         return true;
738     }
739
740     parse(newProtocol + m_string.substring(m_schemeEnd));
741     return true;
742 }
743
744 void KURL::setHost(const String& s)
745 {
746     if (!m_isValid)
747         return;
748
749     // FIXME: Non-ASCII characters must be encoded and escaped to match parse() expectations,
750     // and to avoid changing more than just the host.
751
752     bool slashSlashNeeded = m_userStart == m_schemeEnd + 1;
753
754     parse(m_string.left(hostStart()) + (slashSlashNeeded ? "//" : "") + s + m_string.substring(m_hostEnd));
755 }
756
757 void KURL::removePort()
758 {
759     if (m_hostEnd == m_portEnd)
760         return;
761     parse(m_string.left(m_hostEnd) + m_string.substring(m_portEnd));
762 }
763
764 void KURL::setPort(unsigned short i)
765 {
766     if (!m_isValid)
767         return;
768
769     bool colonNeeded = m_portEnd == m_hostEnd;
770     int portStart = (colonNeeded ? m_hostEnd : m_hostEnd + 1);
771
772     parse(m_string.left(portStart) + (colonNeeded ? ":" : "") + String::number(i) + m_string.substring(m_portEnd));
773 }
774
775 void KURL::setHostAndPort(const String& hostAndPort)
776 {
777     if (!m_isValid)
778         return;
779
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.
782
783     bool slashSlashNeeded = m_userStart == m_schemeEnd + 1;
784
785     parse(m_string.left(hostStart()) + (slashSlashNeeded ? "//" : "") + hostAndPort + m_string.substring(m_portEnd));
786 }
787
788 void KURL::setUser(const String& user)
789 {
790     if (!m_isValid)
791         return;
792
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.
795     String u;
796     int end = m_userEnd;
797     if (!user.isEmpty()) {
798         u = user;
799         if (m_userStart == m_schemeEnd + 1)
800             u = "//" + u;
801         // Add '@' if we didn't have one before.
802         if (end == m_hostEnd || (end == m_passwordEnd && m_string[end] != '@'))
803             u.append('@');
804     } else {
805         // Remove '@' if we now have neither user nor password.
806         if (m_userEnd == m_passwordEnd && end != m_hostEnd && m_string[end] == '@')
807             end += 1;
808     }
809     parse(m_string.left(m_userStart) + u + m_string.substring(end));
810 }
811
812 void KURL::setPass(const String& password)
813 {
814     if (!m_isValid)
815         return;
816
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.
819     String p;
820     int end = m_passwordEnd;
821     if (!password.isEmpty()) {
822         p = ":" + password + "@";
823         if (m_userEnd == m_schemeEnd + 1)
824             p = "//" + p;
825         // Eat the existing '@' since we are going to add our own.
826         if (end != m_hostEnd && m_string[end] == '@')
827             end += 1;
828     } else {
829         // Remove '@' if we now have neither user nor password.
830         if (m_userStart == m_userEnd && end != m_hostEnd && m_string[end] == '@')
831             end += 1;
832     }
833     parse(m_string.left(m_userEnd) + p + m_string.substring(end));
834 }
835
836 void KURL::setFragmentIdentifier(const String& s)
837 {
838     if (!m_isValid)
839         return;
840
841     // FIXME: Non-ASCII characters must be encoded and escaped to match parse() expectations.
842     parse(m_string.left(m_queryEnd) + "#" + s);
843 }
844
845 void KURL::removeFragmentIdentifier()
846 {
847     if (!m_isValid)
848         return;
849     parse(m_string.left(m_queryEnd));
850 }
851     
852 void KURL::setQuery(const String& query)
853 {
854     if (!m_isValid)
855         return;
856
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));
862     else
863         parse(m_string.left(m_pathEnd) + query + m_string.substring(m_queryEnd));
864
865 }
866
867 void KURL::setPath(const String& s)
868 {
869     if (!m_isValid)
870         return;
871
872     // FIXME: encodeWithURLEscapeSequences does not correctly escape '#' and '?', so fragment and query parts
873     // may be inadvertently affected.
874     String path = s;
875     if (path.isEmpty() || path[0] != '/')
876         path = "/" + path;
877
878     parse(m_string.left(m_portEnd) + encodeWithURLEscapeSequences(path) + m_string.substring(m_pathEnd));
879 }
880
881 String KURL::deprecatedString() const
882 {
883     if (!m_isValid)
884         return m_string;
885
886     StringBuilder result;
887
888     result.append(protocol());
889     result.append(':');
890
891     StringBuilder authority;
892
893     if (m_hostEnd != m_passwordEnd) {
894         if (m_userEnd != m_userStart) {
895             authority.append(user());
896             authority.append('@');
897         }
898         authority.append(host());
899         if (hasPort()) {
900             authority.append(':');
901             authority.append(String::number(port()));
902         }
903     }
904
905     if (!authority.isEmpty()) {
906         result.append('/');
907         result.append('/');
908         result.append(authority.characters(), authority.length());
909     } else if (protocolIs("file")) {
910         result.append('/');
911         result.append('/');
912     }
913
914     result.append(path());
915
916     if (m_pathEnd != m_queryEnd) {
917         result.append('?');
918         result.append(query());
919     }
920
921     if (m_fragmentEnd != m_queryEnd) {
922         result.append('#');
923         result.append(fragmentIdentifier());
924     }
925
926     return result.toString();
927 }
928
929 String decodeURLEscapeSequences(const String& string)
930 {
931     return decodeEscapeSequences<URLEscapeSequence>(string, UTF8Encoding());
932 }
933
934 String decodeURLEscapeSequences(const String& string, const TextEncoding& encoding)
935 {
936     return decodeEscapeSequences<URLEscapeSequence>(string, encoding);
937 }
938
939 // Caution: This function does not bounds check.
940 static void appendEscapedChar(char*& buffer, unsigned char c)
941 {
942     *buffer++ = '%';
943     placeByteAsHex(c, buffer);
944 }
945
946 static void appendEscapingBadChars(char*& buffer, const char* strStart, size_t length)
947 {
948     char* p = buffer;
949
950     const char* str = strStart;
951     const char* strEnd = strStart + length;
952     while (str < strEnd) {
953         unsigned char c = *str++;
954         if (isBadChar(c)) {
955             if (c == '%' || c == '?')
956                 *p++ = c;
957             else if (c != 0x09 && c != 0x0a && c != 0x0d)
958                 appendEscapedChar(p, c);
959         } else
960             *p++ = c;
961     }
962
963     buffer = p;
964 }
965
966 static void escapeAndAppendFragment(char*& buffer, const char* strStart, size_t length)
967 {
968     char* p = buffer;
969
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)
977             continue;
978
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);
983             continue;
984         }
985         *p++ = c;
986     }
987
988     buffer = p;
989 }
990
991 // copy a path, accounting for "." and ".." segments
992 static int copyPathRemovingDots(char* dst, const char* src, int srcStart, int srcEnd)
993 {
994     char* bufferPathStart = dst;
995
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;
1001
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] == '/');
1005
1006         // copy the leading slash into the destination
1007         *dst = *baseStringPos;
1008         baseStringPos++;
1009         dst++;
1010
1011         while (baseStringPos < baseStringEnd) {
1012             if (baseStringPos[0] == '.' && dst[-1] == '/') {
1013                 if (baseStringPos[1] == '/' || baseStringPos + 1 == baseStringEnd) {
1014                     // skip over "." segment
1015                     baseStringPos += 2;
1016                     continue;
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
1022                     // relies on this.
1023                     baseStringPos += 3;
1024                     if (dst > bufferPathStart + 1)
1025                         dst--;
1026                     while (dst > bufferPathStart && dst[-1] != '/')
1027                         dst--;
1028                     continue;
1029                 }
1030             }
1031
1032             *dst = *baseStringPos;
1033             baseStringPos++;
1034             dst++;
1035         }
1036     }
1037     *dst = '\0';
1038     return dst - bufferPathStart;
1039 }
1040
1041 static inline bool hasSlashDotOrDotDot(const char* str)
1042 {
1043     const unsigned char* p = reinterpret_cast<const unsigned char*>(str);
1044     if (!*p)
1045         return false;
1046     unsigned char pc = *p;
1047     while (unsigned char c = *++p) {
1048         if (c == '.' && (pc == '/' || pc == '.'))
1049             return true;
1050         pc = c;
1051     }
1052     return false;
1053 }
1054
1055 void KURL::parse(const String& string)
1056 {
1057     checkEncodedString(string);
1058
1059     CharBuffer buffer(string.length() + 1);
1060     copyASCII(string.characters(), string.length(), buffer.data());
1061     buffer[string.length()] = '\0';
1062     parse(buffer.data(), &string);
1063 }
1064
1065 static inline bool equal(const char* a, size_t lenA, const char* b, size_t lenB)
1066 {
1067     if (lenA != lenB)
1068         return false;
1069     return !strncmp(a, b, lenA);
1070 }
1071
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)
1075 {
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) {
1079     case 2:
1080         return equal("ws", 2, scheme, schemeLength) && equal("80", 2, port, portLength);
1081     case 3:
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);
1086         break;
1087     case 4:
1088         return equal("http", 4, scheme, schemeLength) && equal("80", 2, port, portLength);
1089     case 5:
1090         return equal("https", 5, scheme, schemeLength) && equal("443", 3, port, portLength);
1091     case 6:
1092         return equal("gopher", 6, scheme, schemeLength) && equal("70", 2, port, portLength);
1093     }
1094     return false;
1095 }
1096
1097 static inline bool hostPortIsEmptyButCredentialsArePresent(int hostStart, int portEnd, char userEndChar)
1098 {
1099     return userEndChar == '@' && hostStart == portEnd;
1100 }
1101
1102 static bool isNonFileHierarchicalScheme(const char* scheme, size_t schemeLength)
1103 {
1104     switch (schemeLength) {
1105     case 2:
1106         return equal("ws", 2, scheme, schemeLength);
1107     case 3:
1108         return equal("ftp", 3, scheme, schemeLength) || equal("wss", 3, scheme, schemeLength);
1109     case 4:
1110         return equal("http", 4, scheme, schemeLength);
1111     case 5:
1112         return equal("https", 5, scheme, schemeLength);
1113     case 6:
1114         return equal("gopher", 6, scheme, schemeLength);
1115     }
1116     return false;
1117 }
1118
1119 void KURL::parse(const char* url, const String* originalString)
1120 {
1121     if (!url || url[0] == '\0') {
1122         // valid URL must be non-empty
1123         m_string = originalString ? *originalString : url;
1124         invalidate();
1125         return;
1126     }
1127
1128     if (!isSchemeFirstChar(url[0])) {
1129         // scheme must start with an alphabetic character
1130         m_string = originalString ? *originalString : url;
1131         invalidate();
1132         return;
1133     }
1134
1135     int schemeEnd = 0;
1136     while (isSchemeChar(url[schemeEnd]))
1137         schemeEnd++;
1138
1139     if (url[schemeEnd] != ':') {
1140         m_string = originalString ? *originalString : url;
1141         invalidate();
1142         return;
1143     }
1144
1145     int userStart = schemeEnd + 1;
1146     int userEnd;
1147     int passwordStart;
1148     int passwordEnd;
1149     int hostStart;
1150     int hostEnd;
1151     int portStart;
1152     int portEnd;
1153
1154     bool hierarchical = url[schemeEnd + 1] == '/';
1155     bool hasSecondSlash = hierarchical && url[schemeEnd + 2] == '/';
1156
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');
1162
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] == ':'));
1168
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.
1173
1174         if (hierarchical)
1175             userStart++;
1176         if (hasSecondSlash)
1177             userStart++;
1178         userEnd = userStart;
1179
1180         int colonPos = 0;
1181         while (isUserInfoChar(url[userEnd])) {
1182             if (url[userEnd] == ':' && colonPos == 0)
1183                 colonPos = userEnd;
1184             userEnd++;
1185         }
1186
1187         if (url[userEnd] == '@') {
1188             // actual end of the userinfo, start on the host
1189             if (colonPos != 0) {
1190                 passwordEnd = userEnd;
1191                 userEnd = colonPos;
1192                 passwordStart = colonPos + 1;
1193             } else
1194                 passwordStart = passwordEnd = userEnd;
1195
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;
1204         } else {
1205             // invalid character
1206             m_string = originalString ? *originalString : url;
1207             invalidate();
1208             return;
1209         }
1210
1211         hostEnd = hostStart;
1212
1213         // IPV6 IP address
1214         if (url[hostEnd] == '[') {
1215             hostEnd++;
1216             while (isIPv6Char(url[hostEnd]))
1217                 hostEnd++;
1218             if (url[hostEnd] == ']')
1219                 hostEnd++;
1220             else {
1221                 // invalid character
1222                 m_string = originalString ? *originalString : url;
1223                 invalidate();
1224                 return;
1225             }
1226         } else {
1227             while (isHostnameChar(url[hostEnd]))
1228                 hostEnd++;
1229         }
1230         
1231         if (url[hostEnd] == ':') {
1232             portStart = portEnd = hostEnd + 1;
1233  
1234             // possible start of port
1235             portEnd = portStart;
1236             while (isASCIIDigit(url[portEnd]))
1237                 portEnd++;
1238         } else
1239             portStart = portEnd = hostEnd;
1240
1241         if (!isPathSegmentEndChar(url[portEnd])) {
1242             // invalid character
1243             m_string = originalString ? *originalString : url;
1244             invalidate();
1245             return;
1246         }
1247
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;
1252         }
1253
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.
1257             userStart -= 2;
1258             userEnd = userStart;
1259             passwordStart = userEnd;
1260             passwordEnd = passwordStart;
1261             hostStart = passwordEnd;
1262             hostEnd = hostStart;
1263             portStart = hostEnd;
1264             portEnd = hostEnd;
1265         }
1266     } else {
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;
1272     }
1273
1274     int pathStart = portEnd;
1275     int pathEnd = pathStart;
1276     while (url[pathEnd] && url[pathEnd] != '?' && url[pathEnd] != '#')
1277         pathEnd++;
1278
1279     int queryStart = pathEnd;
1280     int queryEnd = queryStart;
1281     if (url[queryStart] == '?') {
1282         while (url[queryEnd] && url[queryEnd] != '#')
1283             queryEnd++;
1284     }
1285
1286     int fragmentStart = queryEnd;
1287     int fragmentEnd = fragmentStart;
1288     if (url[fragmentStart] == '#') {
1289         fragmentStart++;
1290         fragmentEnd = fragmentStart;
1291         while (url[fragmentEnd])
1292             fragmentEnd++;
1293     }
1294
1295     // assemble it all, remembering the real ranges
1296
1297     Vector<char, 4096> buffer(fragmentEnd * 3 + 1);
1298
1299     char *p = buffer.data();
1300     const char *strPtr = url;
1301
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();
1307
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');
1318
1319     // File URLs need a host part unless it is just file:// or file://localhost
1320     bool degenFilePath = pathStart == pathEnd && (hostStart == hostEnd || hostIsLocalHost);
1321
1322     bool haveNonHostAuthorityPart = userStart != userEnd || passwordStart != passwordEnd || portStart != portEnd;
1323
1324     // add ":" after scheme
1325     *p++ = ':';
1326
1327     // if we have at least one authority part or a file URL - add "//" and authority
1328     if (isFile ? !degenFilePath : (haveNonHostAuthorityPart || hostStart != hostEnd)) {
1329         *p++ = '/';
1330         *p++ = '/';
1331
1332         m_userStart = p - buffer.data();
1333
1334         // copy in the user
1335         strPtr = url + userStart;
1336         const char* userEndPtr = url + userEnd;
1337         while (strPtr < userEndPtr)
1338             *p++ = *strPtr++;
1339         m_userEnd = p - buffer.data();
1340
1341         // copy in the password
1342         if (passwordEnd != passwordStart) {
1343             *p++ = ':';
1344             strPtr = url + passwordStart;
1345             const char* passwordEndPtr = url + passwordEnd;
1346             while (strPtr < passwordEndPtr)
1347                 *p++ = *strPtr++;
1348         }
1349         m_passwordEnd = p - buffer.data();
1350
1351         // If we had any user info, add "@"
1352         if (p - buffer.data() != m_userStart)
1353             *p++ = '@';
1354
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)
1360                 *p++ = *strPtr++;
1361         }
1362         m_hostEnd = p - buffer.data();
1363
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)) {
1369                 *p++ = ':';
1370                 const char* portEndPtr = url + portEnd;
1371                 while (portStr < portEndPtr)
1372                     *p++ = *portStr++;
1373             }
1374         }
1375         m_portEnd = p - buffer.data();
1376     } else
1377         m_userStart = m_userEnd = m_passwordEnd = m_hostEnd = m_portEnd = p - buffer.data();
1378
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)
1382         *p++ = '/';
1383
1384     // add path, escaping bad characters
1385     if (!hierarchical || !hasSlashDotOrDotDot(url))
1386         appendEscapingBadChars(p, url + pathStart, pathEnd - pathStart);
1387     else {
1388         CharBuffer pathBuffer(pathEnd - pathStart + 1);
1389         size_t length = copyPathRemovingDots(pathBuffer.data(), url, pathStart, pathEnd);
1390         appendEscapingBadChars(p, pathBuffer.data(), length);
1391     }
1392
1393     m_pathEnd = p - buffer.data();
1394
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.
1397     int i;
1398     for (i = m_pathEnd; i > m_portEnd; --i) {
1399         if (buffer[i - 1] == '/')
1400             break;
1401     }
1402     m_pathAfterLastSlash = i;
1403
1404     // add query, escaping bad characters
1405     appendEscapingBadChars(p, url + queryStart, queryEnd - queryStart);
1406     m_queryEnd = p - buffer.data();
1407
1408     // add fragment, escaping bad characters
1409     if (fragmentEnd != queryEnd) {
1410         *p++ = '#';
1411         escapeAndAppendFragment(p, url + fragmentStart, fragmentEnd - fragmentStart);
1412     }
1413     m_fragmentEnd = p - buffer.data();
1414
1415     ASSERT(p - buffer.data() <= static_cast<int>(buffer.size()));
1416
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;
1421     else
1422         m_string = String(buffer.data(), m_fragmentEnd);
1423
1424     m_isValid = true;
1425 }
1426
1427 bool equalIgnoringFragmentIdentifier(const KURL& a, const KURL& b)
1428 {
1429     if (a.m_queryEnd != b.m_queryEnd)
1430         return false;
1431     unsigned queryLength = a.m_queryEnd;
1432     for (unsigned i = 0; i < queryLength; ++i)
1433         if (a.string()[i] != b.string()[i])
1434             return false;
1435     return true;
1436 }
1437
1438 bool protocolHostAndPortAreEqual(const KURL& a, const KURL& b)
1439 {
1440     if (a.m_schemeEnd != b.m_schemeEnd)
1441         return false;
1442
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)
1448         return false;
1449
1450     // Check the scheme
1451     for (int i = 0; i < a.m_schemeEnd; ++i)
1452         if (a.string()[i] != b.string()[i])
1453             return false;
1454
1455     // And the host
1456     for (int i = 0; i < hostLengthA; ++i)
1457         if (a.string()[hostStartA + i] != b.string()[hostStartB + i])
1458             return false;
1459
1460     if (a.port() != b.port())
1461         return false;
1462
1463     return true;
1464 }
1465
1466 String encodeWithURLEscapeSequences(const String& notEncodedString)
1467 {
1468     CString asUTF8 = notEncodedString.utf8();
1469
1470     CharBuffer buffer(asUTF8.length() * 3 + 1);
1471     char* p = buffer.data();
1472
1473     const char* str = asUTF8.data();
1474     const char* strEnd = str + asUTF8.length();
1475     while (str < strEnd) {
1476         unsigned char c = *str++;
1477         if (isBadChar(c))
1478             appendEscapedChar(p, c);
1479         else
1480             *p++ = c;
1481     }
1482
1483     ASSERT(p - buffer.data() <= static_cast<int>(buffer.size()));
1484
1485     return String(buffer.data(), p - buffer.data());
1486 }
1487
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)
1491 {
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;
1495
1496     if (strLen > hostnameBufferLength || charactersAreAllASCII(str, strLen)) {
1497         buffer.append(str, strLen);
1498         return;
1499     }
1500
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()));
1515     if (utf8Err)
1516         return;
1517
1518     GOwnPtr<gchar> encodedHostname;
1519     encodedHostname.set(g_hostname_to_ascii(utf8Hostname.get()));
1520     if (!encodedHostname) 
1521         return;
1522
1523     buffer.append(encodedHostname.get(), strlen(encodedHostname.get()));
1524 #endif
1525 }
1526
1527 static void findHostnamesInMailToURL(const UChar* str, int strLen, Vector<pair<int, int> >& nameRanges)
1528 {
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.
1532
1533     nameRanges.clear();
1534
1535     int p = 0;
1536     while (1) {
1537         // Find start of host name or of quoted string.
1538         int hostnameOrStringStart = findFirstOf(str, strLen, p, "\"@?");
1539         if (hostnameOrStringStart == -1)
1540             return;
1541         UChar c = str[hostnameOrStringStart];
1542         p = hostnameOrStringStart + 1;
1543
1544         if (c == '?')
1545             return;
1546
1547         if (c == '@') {
1548             // Find end of host name.
1549             int hostnameStart = p;
1550             int hostnameEnd = findFirstOf(str, strLen, p, ">,?");
1551             bool done;
1552             if (hostnameEnd == -1) {
1553                 hostnameEnd = strLen;
1554                 done = true;
1555             } else {
1556                 p = hostnameEnd;
1557                 done = false;
1558             }
1559
1560             nameRanges.append(make_pair(hostnameStart, hostnameEnd));
1561
1562             if (done)
1563                 return;
1564         } else {
1565             // Skip quoted string.
1566             ASSERT(c == '"');
1567             while (1) {
1568                 int escapedCharacterOrStringEnd = findFirstOf(str, strLen, p, "\"\\");
1569                 if (escapedCharacterOrStringEnd == -1)
1570                     return;
1571
1572                 c = str[escapedCharacterOrStringEnd];
1573                 p = escapedCharacterOrStringEnd + 1;
1574
1575                 // If we are the end of the string, then break from the string loop back to the host name loop.
1576                 if (c == '"')
1577                     break;
1578
1579                 // Skip escaped character.
1580                 ASSERT(c == '\\');
1581                 if (p == strLen)
1582                     return;
1583
1584                 ++p;
1585             }
1586         }
1587     }
1588 }
1589
1590 static bool findHostnameInHierarchicalURL(const UChar* str, int strLen, int& startOffset, int& endOffset)
1591 {
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] != '/')
1600         return false;
1601
1602     // Check that all characters before the :// are valid scheme characters.
1603     if (!isSchemeFirstChar(str[0]))
1604         return false;
1605     for (int i = 1; i < separator; ++i) {
1606         if (!isSchemeChar(str[i]))
1607             return false;
1608     }
1609
1610     // Start after the separator.
1611     int authorityStart = separator + 3;
1612
1613     // Find terminating character.
1614     int hostnameEnd = strLen;
1615     for (int i = authorityStart; i < strLen; ++i) {
1616         UChar c = str[i];
1617         if (c == ':' || (isPathSegmentEndChar(c) && c != 0)) {
1618             hostnameEnd = i;
1619             break;
1620         }
1621     }
1622
1623     // Find "@" for the start of the host name.
1624     int userInfoTerminator = findFirstOf(str, strLen, authorityStart, "@");
1625     int hostnameStart;
1626     if (userInfoTerminator == -1 || userInfoTerminator > hostnameEnd)
1627         hostnameStart = authorityStart;
1628     else
1629         hostnameStart = userInfoTerminator + 1;
1630
1631     startOffset = hostnameStart;
1632     endOffset = hostnameEnd;
1633     return true;
1634 }
1635
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)
1639 {
1640     output.clear();
1641
1642     if (protocolIs(str, "mailto")) {
1643         Vector<pair<int, int> > hostnameRanges;
1644         findHostnamesInMailToURL(str.characters(), str.length(), hostnameRanges);
1645         int n = hostnameRanges.size();
1646         int p = 0;
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);
1651             p = r.second;
1652         }
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);
1656     } else {
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.
1662         } else {
1663             // No hostname to encode, return the input.
1664             output.append(str.characters(), str.length());
1665         }
1666     }
1667 }
1668
1669 static void encodeRelativeString(const String& rel, const TextEncoding& encoding, CharBuffer& output)
1670 {
1671     UCharBuffer s;
1672     encodeHostnames(rel, s);
1673
1674     TextEncoding pathEncoding(UTF8Encoding()); // Path is always encoded as UTF-8; other parts may depend on the scheme.
1675
1676     int pathEnd = -1;
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, "#?");
1680     }
1681
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());
1686     } else {
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);
1691
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());
1695     }
1696     output.append('\0'); // null-terminate the output.
1697 }
1698
1699 static String substituteBackslashes(const String& string)
1700 {
1701     size_t questionPos = string.find('?');
1702     size_t hashPos = string.find('#');
1703     unsigned pathEnd;
1704
1705     if (hashPos != notFound && (questionPos == notFound || questionPos > hashPos))
1706         pathEnd = hashPos;
1707     else if (questionPos != notFound)
1708         pathEnd = questionPos;
1709     else
1710         pathEnd = string.length();
1711
1712     return string.left(pathEnd).replace('\\','/') + string.substring(pathEnd);
1713 }
1714
1715 bool KURL::isHierarchical() const
1716 {
1717     if (!m_isValid)
1718         return false;
1719     ASSERT(m_string[m_schemeEnd] == ':');
1720     return m_string[m_schemeEnd + 1] == '/';
1721 }
1722
1723 void KURL::copyToBuffer(CharBuffer& buffer) const
1724 {
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());
1729 }
1730
1731 bool protocolIs(const String& url, const char* protocol)
1732 {
1733     // Do the comparison without making a new string object.
1734     assertProtocolIsGood(protocol);
1735     for (int i = 0; ; ++i) {
1736         if (!protocol[i])
1737             return url[i] == ':';
1738         if (!isLetterMatchIgnoringCase(url[i], protocol[i]))
1739             return false;
1740     }
1741 }
1742
1743 bool isValidProtocol(const String& protocol)
1744 {
1745     // RFC3986: ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
1746     if (protocol.isEmpty())
1747         return false;
1748     if (!isSchemeFirstChar(protocol[0]))
1749         return false;
1750     unsigned protocolLength = protocol.length();
1751     for (unsigned i = 1; i < protocolLength; i++) {
1752         if (!isSchemeChar(protocol[i]))
1753             return false;
1754     }
1755     return true;
1756 }
1757
1758 #ifndef NDEBUG
1759 void KURL::print() const
1760 {
1761     printf("%s\n", m_string.utf8().data());
1762 }
1763 #endif
1764
1765 #endif // !USE(GOOGLEURL)
1766
1767 String KURL::strippedForUseAsReferrer() const
1768 {
1769     KURL referrer(*this);
1770     referrer.setUser(String());
1771     referrer.setPass(String());
1772     referrer.removeFragmentIdentifier();
1773     return referrer.string();
1774 }
1775
1776 bool KURL::isLocalFile() const
1777 {
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");
1783 }
1784
1785 bool protocolIsJavaScript(const String& url)
1786 {
1787     return protocolIs(url, "javascript");
1788 }
1789
1790 const KURL& blankURL()
1791 {
1792     DEFINE_STATIC_LOCAL(KURL, staticBlankURL, (ParsedURLString, "about:blank"));
1793     return staticBlankURL;
1794 }
1795
1796 bool isDefaultPortForProtocol(unsigned short port, const String& protocol)
1797 {
1798     if (protocol.isEmpty())
1799         return false;
1800
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);
1808     }
1809     return defaultPorts.get(protocol) == port;
1810 }
1811
1812 bool portAllowed(const KURL& url)
1813 {
1814     unsigned short port = url.port();
1815
1816     // Since most URLs don't have a port, return early for the "no port" case.
1817     if (!port)
1818         return true;
1819
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[] = {
1823         1,    // tcpmux
1824         7,    // echo
1825         9,    // discard
1826         11,   // systat
1827         13,   // daytime
1828         15,   // netstat
1829         17,   // qotd
1830         19,   // chargen
1831         20,   // FTP-data
1832         21,   // FTP-control
1833         22,   // SSH
1834         23,   // telnet
1835         25,   // SMTP
1836         37,   // time
1837         42,   // name
1838         43,   // nicname
1839         53,   // domain
1840         77,   // priv-rjs
1841         79,   // finger
1842         87,   // ttylink
1843         95,   // supdup
1844         101,  // hostriame
1845         102,  // iso-tsap
1846         103,  // gppitnp
1847         104,  // acr-nema
1848         109,  // POP2
1849         110,  // POP3
1850         111,  // sunrpc
1851         113,  // auth
1852         115,  // SFTP
1853         117,  // uucp-path
1854         119,  // nntp
1855         123,  // NTP
1856         135,  // loc-srv / epmap
1857         139,  // netbios
1858         143,  // IMAP2
1859         179,  // BGP
1860         389,  // LDAP
1861         465,  // SMTP+SSL
1862         512,  // print / exec
1863         513,  // login
1864         514,  // shell
1865         515,  // printer
1866         526,  // tempo
1867         530,  // courier
1868         531,  // Chat
1869         532,  // netnews
1870         540,  // UUCP
1871         556,  // remotefs
1872         563,  // NNTP+SSL
1873         587,  // ESMTP
1874         601,  // syslog-conn
1875         636,  // LDAP+SSL
1876         993,  // IMAP+SSL
1877         995,  // POP3+SSL
1878         2049, // NFS
1879         3659, // apple-sasl / PasswordServer [Apple addition]
1880         4045, // lockd
1881         6000, // X11
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
1888     };
1889     const unsigned short* const blockedPortListEnd = blockedPortList + WTF_ARRAY_LENGTH(blockedPortList);
1890
1891 #ifndef NDEBUG
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;
1898     }
1899 #endif
1900
1901     // If the port is not in the blocked port list, allow it.
1902     if (!binary_search(blockedPortList, blockedPortListEnd, port))
1903         return true;
1904
1905     // Allow ports 21 and 22 for FTP URLs, as Mozilla does.
1906     if ((port == 21 || port == 22) && url.protocolIs("ftp"))
1907         return true;
1908
1909     // Allow any port number in a file URL, since the port number is ignored.
1910     if (url.protocolIs("file"))
1911         return true;
1912
1913     return false;
1914 }
1915
1916 String mimeTypeFromDataURL(const String& url)
1917 {
1918     ASSERT(protocolIs(url, "data"));
1919     size_t index = url.find(';');
1920     if (index == notFound)
1921         index = url.find(',');
1922     if (index != notFound) {
1923         if (index > 5)
1924             return url.substring(5, index - 5);
1925         return "text/plain"; // Data URLs with no MIME type are considered text/plain.
1926     }
1927     return "";
1928 }
1929
1930 bool protocolIsInHTTPFamily(const String& url)
1931 {
1932     unsigned length = url.length();
1933     const UChar* characters = url.characters();
1934     return length > 4
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] == ':'));
1941 }
1942
1943 }