• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2004, 2007, 2008 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 
28 #if !USE(GOOGLEURL)
29 
30 #include "KURL.h"
31 
32 #include "CString.h"
33 #include "PlatformString.h"
34 #include "TextEncoding.h"
35 #include <wtf/StdLibExtras.h>
36 
37 #if USE(ICU_UNICODE)
38 #include <unicode/uidna.h>
39 #elif USE(QT4_UNICODE)
40 #include <QUrl>
41 #endif
42 
43 #include <stdio.h>
44 
45 using namespace std;
46 using namespace WTF;
47 
48 namespace WebCore {
49 
50 typedef Vector<char, 512> CharBuffer;
51 typedef Vector<UChar, 512> UCharBuffer;
52 
53 // FIXME: This file makes too much use of the + operator on String.
54 // We either have to optimize that operator so it doesn't involve
55 // so many allocations, or change this to use Vector<UChar> instead.
56 
57 enum URLCharacterClasses {
58     // alpha
59     SchemeFirstChar = 1 << 0,
60 
61     // ( alpha | digit | "+" | "-" | "." )
62     SchemeChar = 1 << 1,
63 
64     // mark        = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
65     // unreserved  = alphanum | mark
66     // ( unreserved | escaped | ";" | ":" | "&" | "=" | "+" | "$" | "," )
67     UserInfoChar = 1 << 2,
68 
69     // alnum | "." | "-" | "%"
70     // The above is what the specification says, but we are lenient to
71     // match existing practice and also allow:
72     // "_"
73     HostnameChar = 1 << 3,
74 
75     // hexdigit | ":" | "%"
76     IPv6Char = 1 << 4,
77 
78     // "#" | "?" | "/" | nul
79     PathSegmentEndChar = 1 << 5,
80 
81     // not allowed in path
82     BadChar = 1 << 6
83 };
84 
85 static const char hexDigits[17] = "0123456789ABCDEF";
86 
87 static const unsigned char characterClassTable[256] = {
88     /* 0 nul */ PathSegmentEndChar,    /* 1 soh */ BadChar,
89     /* 2 stx */ BadChar,    /* 3 etx */ BadChar,
90     /* 4 eot */ BadChar,    /* 5 enq */ BadChar,    /* 6 ack */ BadChar,    /* 7 bel */ BadChar,
91     /* 8 bs */ BadChar,     /* 9 ht */ BadChar,     /* 10 nl */ BadChar,    /* 11 vt */ BadChar,
92     /* 12 np */ BadChar,    /* 13 cr */ BadChar,    /* 14 so */ BadChar,    /* 15 si */ BadChar,
93     /* 16 dle */ BadChar,   /* 17 dc1 */ BadChar,   /* 18 dc2 */ BadChar,   /* 19 dc3 */ BadChar,
94     /* 20 dc4 */ BadChar,   /* 21 nak */ BadChar,   /* 22 syn */ BadChar,   /* 23 etb */ BadChar,
95     /* 24 can */ BadChar,   /* 25 em */ BadChar,    /* 26 sub */ BadChar,   /* 27 esc */ BadChar,
96     /* 28 fs */ BadChar,    /* 29 gs */ BadChar,    /* 30 rs */ BadChar,    /* 31 us */ BadChar,
97     /* 32 sp */ BadChar,    /* 33  ! */ UserInfoChar,
98     /* 34  " */ BadChar,    /* 35  # */ PathSegmentEndChar | BadChar,
99     /* 36  $ */ UserInfoChar,    /* 37  % */ UserInfoChar | HostnameChar | IPv6Char | BadChar,
100     /* 38  & */ UserInfoChar,    /* 39  ' */ UserInfoChar,
101     /* 40  ( */ UserInfoChar,    /* 41  ) */ UserInfoChar,
102     /* 42  * */ UserInfoChar,    /* 43  + */ SchemeChar | UserInfoChar,
103     /* 44  , */ UserInfoChar,
104     /* 45  - */ SchemeChar | UserInfoChar | HostnameChar,
105     /* 46  . */ SchemeChar | UserInfoChar | HostnameChar,
106     /* 47  / */ PathSegmentEndChar,
107     /* 48  0 */ SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
108     /* 49  1 */ SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
109     /* 50  2 */ SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
110     /* 51  3 */ SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
111     /* 52  4 */ SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
112     /* 53  5 */ SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
113     /* 54  6 */ SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
114     /* 55  7 */ SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
115     /* 56  8 */ SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
116     /* 57  9 */ SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
117     /* 58  : */ UserInfoChar | IPv6Char,    /* 59  ; */ UserInfoChar,
118     /* 60  < */ BadChar,    /* 61  = */ UserInfoChar,
119     /* 62  > */ BadChar,    /* 63  ? */ PathSegmentEndChar | BadChar,
120     /* 64  @ */ 0,
121     /* 65  A */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
122     /* 66  B */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
123     /* 67  C */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
124     /* 68  D */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
125     /* 69  E */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
126     /* 70  F */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
127     /* 71  G */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
128     /* 72  H */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
129     /* 73  I */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
130     /* 74  J */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
131     /* 75  K */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
132     /* 76  L */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
133     /* 77  M */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
134     /* 78  N */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
135     /* 79  O */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
136     /* 80  P */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
137     /* 81  Q */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
138     /* 82  R */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
139     /* 83  S */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
140     /* 84  T */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
141     /* 85  U */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
142     /* 86  V */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
143     /* 87  W */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
144     /* 88  X */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
145     /* 89  Y */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
146     /* 90  Z */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
147     /* 91  [ */ 0,
148     /* 92  \ */ 0,    /* 93  ] */ 0,
149     /* 94  ^ */ 0,
150     /* 95  _ */ UserInfoChar | HostnameChar,
151     /* 96  ` */ 0,
152     /* 97  a */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
153     /* 98  b */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
154     /* 99  c */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
155     /* 100  d */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
156     /* 101  e */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
157     /* 102  f */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | IPv6Char,
158     /* 103  g */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
159     /* 104  h */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
160     /* 105  i */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
161     /* 106  j */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
162     /* 107  k */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
163     /* 108  l */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
164     /* 109  m */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
165     /* 110  n */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
166     /* 111  o */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
167     /* 112  p */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
168     /* 113  q */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
169     /* 114  r */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
170     /* 115  s */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
171     /* 116  t */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
172     /* 117  u */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
173     /* 118  v */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
174     /* 119  w */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
175     /* 120  x */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
176     /* 121  y */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
177     /* 122  z */ SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
178     /* 123  { */ 0,
179     /* 124  | */ 0,   /* 125  } */ 0,   /* 126  ~ */ UserInfoChar,   /* 127 del */ BadChar,
180     /* 128 */ BadChar, /* 129 */ BadChar, /* 130 */ BadChar, /* 131 */ BadChar,
181     /* 132 */ BadChar, /* 133 */ BadChar, /* 134 */ BadChar, /* 135 */ BadChar,
182     /* 136 */ BadChar, /* 137 */ BadChar, /* 138 */ BadChar, /* 139 */ BadChar,
183     /* 140 */ BadChar, /* 141 */ BadChar, /* 142 */ BadChar, /* 143 */ BadChar,
184     /* 144 */ BadChar, /* 145 */ BadChar, /* 146 */ BadChar, /* 147 */ BadChar,
185     /* 148 */ BadChar, /* 149 */ BadChar, /* 150 */ BadChar, /* 151 */ BadChar,
186     /* 152 */ BadChar, /* 153 */ BadChar, /* 154 */ BadChar, /* 155 */ BadChar,
187     /* 156 */ BadChar, /* 157 */ BadChar, /* 158 */ BadChar, /* 159 */ BadChar,
188     /* 160 */ BadChar, /* 161 */ BadChar, /* 162 */ BadChar, /* 163 */ BadChar,
189     /* 164 */ BadChar, /* 165 */ BadChar, /* 166 */ BadChar, /* 167 */ BadChar,
190     /* 168 */ BadChar, /* 169 */ BadChar, /* 170 */ BadChar, /* 171 */ BadChar,
191     /* 172 */ BadChar, /* 173 */ BadChar, /* 174 */ BadChar, /* 175 */ BadChar,
192     /* 176 */ BadChar, /* 177 */ BadChar, /* 178 */ BadChar, /* 179 */ BadChar,
193     /* 180 */ BadChar, /* 181 */ BadChar, /* 182 */ BadChar, /* 183 */ BadChar,
194     /* 184 */ BadChar, /* 185 */ BadChar, /* 186 */ BadChar, /* 187 */ BadChar,
195     /* 188 */ BadChar, /* 189 */ BadChar, /* 190 */ BadChar, /* 191 */ BadChar,
196     /* 192 */ BadChar, /* 193 */ BadChar, /* 194 */ BadChar, /* 195 */ BadChar,
197     /* 196 */ BadChar, /* 197 */ BadChar, /* 198 */ BadChar, /* 199 */ BadChar,
198     /* 200 */ BadChar, /* 201 */ BadChar, /* 202 */ BadChar, /* 203 */ BadChar,
199     /* 204 */ BadChar, /* 205 */ BadChar, /* 206 */ BadChar, /* 207 */ BadChar,
200     /* 208 */ BadChar, /* 209 */ BadChar, /* 210 */ BadChar, /* 211 */ BadChar,
201     /* 212 */ BadChar, /* 213 */ BadChar, /* 214 */ BadChar, /* 215 */ BadChar,
202     /* 216 */ BadChar, /* 217 */ BadChar, /* 218 */ BadChar, /* 219 */ BadChar,
203     /* 220 */ BadChar, /* 221 */ BadChar, /* 222 */ BadChar, /* 223 */ BadChar,
204     /* 224 */ BadChar, /* 225 */ BadChar, /* 226 */ BadChar, /* 227 */ BadChar,
205     /* 228 */ BadChar, /* 229 */ BadChar, /* 230 */ BadChar, /* 231 */ BadChar,
206     /* 232 */ BadChar, /* 233 */ BadChar, /* 234 */ BadChar, /* 235 */ BadChar,
207     /* 236 */ BadChar, /* 237 */ BadChar, /* 238 */ BadChar, /* 239 */ BadChar,
208     /* 240 */ BadChar, /* 241 */ BadChar, /* 242 */ BadChar, /* 243 */ BadChar,
209     /* 244 */ BadChar, /* 245 */ BadChar, /* 246 */ BadChar, /* 247 */ BadChar,
210     /* 248 */ BadChar, /* 249 */ BadChar, /* 250 */ BadChar, /* 251 */ BadChar,
211     /* 252 */ BadChar, /* 253 */ BadChar, /* 254 */ BadChar, /* 255 */ BadChar
212 };
213 
214 static int copyPathRemovingDots(char* dst, const char* src, int srcStart, int srcEnd);
215 static void encodeRelativeString(const String& rel, const TextEncoding&, CharBuffer& ouput);
216 static String substituteBackslashes(const String&);
217 
isSchemeFirstChar(char c)218 static inline bool isSchemeFirstChar(char c) { return characterClassTable[static_cast<unsigned char>(c)] & SchemeFirstChar; }
isSchemeFirstChar(UChar c)219 static inline bool isSchemeFirstChar(UChar c) { return c <= 0xff && (characterClassTable[c] & SchemeFirstChar); }
isSchemeChar(char c)220 static inline bool isSchemeChar(char c) { return characterClassTable[static_cast<unsigned char>(c)] & SchemeChar; }
isSchemeChar(UChar c)221 static inline bool isSchemeChar(UChar c) { return c <= 0xff && (characterClassTable[c] & SchemeChar); }
isUserInfoChar(unsigned char c)222 static inline bool isUserInfoChar(unsigned char c) { return characterClassTable[c] & UserInfoChar; }
isHostnameChar(unsigned char c)223 static inline bool isHostnameChar(unsigned char c) { return characterClassTable[c] & HostnameChar; }
isIPv6Char(unsigned char c)224 static inline bool isIPv6Char(unsigned char c) { return characterClassTable[c] & IPv6Char; }
isPathSegmentEndChar(char c)225 static inline bool isPathSegmentEndChar(char c) { return characterClassTable[static_cast<unsigned char>(c)] & PathSegmentEndChar; }
isPathSegmentEndChar(UChar c)226 static inline bool isPathSegmentEndChar(UChar c) { return c <= 0xff && (characterClassTable[c] & PathSegmentEndChar); }
isBadChar(unsigned char c)227 static inline bool isBadChar(unsigned char c) { return characterClassTable[c] & BadChar; }
228 
hexDigitValue(UChar c)229 static inline int hexDigitValue(UChar c)
230 {
231     ASSERT(isASCIIHexDigit(c));
232     if (c < 'A')
233         return c - '0';
234     return (c - 'A' + 10) & 0xF; // handle both upper and lower case without a branch
235 }
236 
237 // Copies the source to the destination, assuming all the source characters are
238 // ASCII. The destination buffer must be large enough. Null characters are allowed
239 // in the source string, and no attempt is made to null-terminate the result.
copyASCII(const UChar * src,int length,char * dest)240 static void copyASCII(const UChar* src, int length, char* dest)
241 {
242     for (int i = 0; i < length; i++)
243         dest[i] = static_cast<char>(src[i]);
244 }
245 
appendASCII(const String & base,const char * rel,size_t len,CharBuffer & buffer)246 static void appendASCII(const String& base, const char* rel, size_t len, CharBuffer& buffer)
247 {
248     buffer.resize(base.length() + len + 1);
249     copyASCII(base.characters(), base.length(), buffer.data());
250     memcpy(buffer.data() + base.length(), rel, len);
251     buffer[buffer.size() - 1] = '\0';
252 }
253 
254 // FIXME: Move to PlatformString.h eventually.
255 // Returns the index of the first index in string |s| of any of the characters
256 // in |toFind|. |toFind| should be a null-terminated string, all characters up
257 // to the null will be searched. Returns int if not found.
findFirstOf(const UChar * s,int sLen,int startPos,const char * toFind)258 static int findFirstOf(const UChar* s, int sLen, int startPos, const char* toFind)
259 {
260     for (int i = startPos; i < sLen; i++) {
261         const char* cur = toFind;
262         while (*cur) {
263             if (s[i] == *(cur++))
264                 return i;
265         }
266     }
267     return -1;
268 }
269 
270 #ifndef NDEBUG
checkEncodedString(const String & url)271 static void checkEncodedString(const String& url)
272 {
273     for (unsigned i = 0; i < url.length(); ++i)
274         ASSERT(!(url[i] & ~0x7F));
275 
276     ASSERT(!url.length() || isSchemeFirstChar(url[0]));
277 }
278 #else
checkEncodedString(const String &)279 static inline void checkEncodedString(const String&)
280 {
281 }
282 #endif
283 
protocolIs(const String & string,const char * protocol)284 inline bool KURL::protocolIs(const String& string, const char* protocol)
285 {
286     return WebCore::protocolIs(string, protocol);
287 }
288 
invalidate()289 void KURL::invalidate()
290 {
291     m_isValid = false;
292     m_schemeEnd = 0;
293     m_userStart = 0;
294     m_userEnd = 0;
295     m_passwordEnd = 0;
296     m_hostEnd = 0;
297     m_portEnd = 0;
298     m_pathEnd = 0;
299     m_pathAfterLastSlash = 0;
300     m_queryEnd = 0;
301     m_fragmentEnd = 0;
302 }
303 
KURL(const char * url)304 KURL::KURL(const char* url)
305 {
306     parse(url, 0);
307     ASSERT(url == m_string);
308 }
309 
KURL(const String & url)310 KURL::KURL(const String& url)
311 {
312     checkEncodedString(url);
313 
314     parse(url);
315     ASSERT(url == m_string);
316 }
317 
KURL(const KURL & base,const String & relative)318 KURL::KURL(const KURL& base, const String& relative)
319 {
320     init(base, relative, UTF8Encoding());
321 }
322 
KURL(const KURL & base,const String & relative,const TextEncoding & encoding)323 KURL::KURL(const KURL& base, const String& relative, const TextEncoding& encoding)
324 {
325     // For UTF-{7,16,32}, we want to use UTF-8 for the query part as
326     // we do when submitting a form. A form with GET method
327     // has its contents added to a URL as query params and it makes sense
328     // to be consistent.
329     init(base, relative, encoding.encodingForFormSubmission());
330 }
331 
init(const KURL & base,const String & relative,const TextEncoding & encoding)332 void KURL::init(const KURL& base, const String& relative, const TextEncoding& encoding)
333 {
334     // Allow resolutions with a null or empty base URL, but not with any other invalid one.
335     // FIXME: Is this a good rule?
336     if (!base.m_isValid && !base.isEmpty()) {
337         m_string = relative;
338         invalidate();
339         return;
340     }
341 
342     // For compatibility with Win IE, treat backslashes as if they were slashes,
343     // as long as we're not dealing with javascript: or data: URLs.
344     String rel = relative;
345     if (rel.contains('\\') && !(protocolIs(rel, "javascript") || protocolIs(rel, "data")))
346         rel = substituteBackslashes(rel);
347 
348     String* originalString = &rel;
349 
350     bool allASCII = charactersAreAllASCII(rel.characters(), rel.length());
351     CharBuffer strBuffer;
352     char* str;
353     size_t len;
354     if (allASCII) {
355         len = rel.length();
356         strBuffer.resize(len + 1);
357         copyASCII(rel.characters(), len, strBuffer.data());
358         strBuffer[len] = 0;
359         str = strBuffer.data();
360     } else {
361         originalString = 0;
362         encodeRelativeString(rel, encoding, strBuffer);
363         str = strBuffer.data();
364         len = strlen(str);
365     }
366 
367     // Get rid of leading whitespace.
368     while (*str == ' ') {
369         originalString = 0;
370         str++;
371         --len;
372     }
373 
374     // Get rid of trailing whitespace.
375     while (len && str[len - 1] == ' ') {
376         originalString = 0;
377         str[--len] = '\0';
378     }
379 
380     // According to the RFC, the reference should be interpreted as an
381     // absolute URI if possible, using the "leftmost, longest"
382     // algorithm. If the URI reference is absolute it will have a
383     // scheme, meaning that it will have a colon before the first
384     // non-scheme element.
385     bool absolute = false;
386     char* p = str;
387     if (isSchemeFirstChar(*p)) {
388         ++p;
389         while (isSchemeChar(*p)) {
390             ++p;
391         }
392         if (*p == ':') {
393             if (p[1] != '/' && equalIgnoringCase(base.protocol(), String(str, p - str)) && base.isHierarchical()) {
394                 str = p + 1;
395                 originalString = 0;
396             } else
397                 absolute = true;
398         }
399     }
400 
401     CharBuffer parseBuffer;
402 
403     if (absolute) {
404         parse(str, originalString);
405     } else {
406         // If the base is empty or opaque (e.g. data: or javascript:), then the URL is invalid
407         // unless the relative URL is a single fragment.
408         if (!base.isHierarchical()) {
409             if (str[0] == '#') {
410                 appendASCII(base.m_string.left(base.m_queryEnd), str, len, parseBuffer);
411                 parse(parseBuffer.data(), 0);
412             } else {
413                 m_string = relative;
414                 invalidate();
415             }
416             return;
417         }
418 
419         switch (str[0]) {
420         case '\0':
421             // the reference must be empty - the RFC says this is a
422             // reference to the same document
423             *this = base;
424             break;
425         case '#': {
426             // must be fragment-only reference
427             appendASCII(base.m_string.left(base.m_queryEnd), str, len, parseBuffer);
428             parse(parseBuffer.data(), 0);
429             break;
430         }
431         case '?': {
432             // query-only reference, special case needed for non-URL results
433             appendASCII(base.m_string.left(base.m_pathEnd), str, len, parseBuffer);
434             parse(parseBuffer.data(), 0);
435             break;
436         }
437         case '/':
438             // must be net-path or absolute-path reference
439             if (str[1] == '/') {
440                 // net-path
441                 appendASCII(base.m_string.left(base.m_schemeEnd + 1), str, len, parseBuffer);
442                 parse(parseBuffer.data(), 0);
443             } else {
444                 // abs-path
445                 appendASCII(base.m_string.left(base.m_portEnd), str, len, parseBuffer);
446                 parse(parseBuffer.data(), 0);
447             }
448             break;
449         default:
450             {
451                 // must be relative-path reference
452 
453                 // Base part plus relative part plus one possible slash added in between plus terminating \0 byte.
454                 parseBuffer.resize(base.m_pathEnd + 1 + len + 1);
455 
456                 char* bufferPos = parseBuffer.data();
457 
458                 // first copy everything before the path from the base
459                 unsigned baseLength = base.m_string.length();
460                 const UChar* baseCharacters = base.m_string.characters();
461                 CharBuffer baseStringBuffer(baseLength);
462                 copyASCII(baseCharacters, baseLength, baseStringBuffer.data());
463                 const char* baseString = baseStringBuffer.data();
464                 const char* baseStringStart = baseString;
465                 const char* pathStart = baseStringStart + base.m_portEnd;
466                 while (baseStringStart < pathStart)
467                     *bufferPos++ = *baseStringStart++;
468                 char* bufferPathStart = bufferPos;
469 
470                 // now copy the base path
471                 const char* baseStringEnd = baseString + base.m_pathEnd;
472 
473                 // go back to the last slash
474                 while (baseStringEnd > baseStringStart && baseStringEnd[-1] != '/')
475                     baseStringEnd--;
476 
477                 if (baseStringEnd == baseStringStart) {
478                     // no path in base, add a path separator if necessary
479                     if (base.m_schemeEnd + 1 != base.m_pathEnd && *str && *str != '?' && *str != '#')
480                         *bufferPos++ = '/';
481                 } else {
482                     bufferPos += copyPathRemovingDots(bufferPos, baseStringStart, 0, baseStringEnd - baseStringStart);
483                 }
484 
485                 const char* relStringStart = str;
486                 const char* relStringPos = relStringStart;
487 
488                 while (*relStringPos && *relStringPos != '?' && *relStringPos != '#') {
489                     if (relStringPos[0] == '.' && bufferPos[-1] == '/') {
490                         if (isPathSegmentEndChar(relStringPos[1])) {
491                             // skip over "." segment
492                             relStringPos += 1;
493                             if (relStringPos[0] == '/')
494                                 relStringPos++;
495                             continue;
496                         } else if (relStringPos[1] == '.' && isPathSegmentEndChar(relStringPos[2])) {
497                             // skip over ".." segment and rewind the last segment
498                             // the RFC leaves it up to the app to decide what to do with excess
499                             // ".." segments - we choose to drop them since some web content
500                             // relies on this.
501                             relStringPos += 2;
502                             if (relStringPos[0] == '/')
503                                 relStringPos++;
504                             if (bufferPos > bufferPathStart + 1)
505                                 bufferPos--;
506                             while (bufferPos > bufferPathStart + 1  && bufferPos[-1] != '/')
507                                 bufferPos--;
508                             continue;
509                         }
510                     }
511 
512                     *bufferPos = *relStringPos;
513                     relStringPos++;
514                     bufferPos++;
515                 }
516 
517                 // all done with the path work, now copy any remainder
518                 // of the relative reference; this will also add a null terminator
519                 strcpy(bufferPos, relStringPos);
520 
521                 parse(parseBuffer.data(), 0);
522 
523                 ASSERT(strlen(parseBuffer.data()) + 1 <= parseBuffer.size());
524                 break;
525             }
526         }
527     }
528 }
529 
copy() const530 KURL KURL::copy() const
531 {
532     KURL result = *this;
533     result.m_string = result.m_string.copy();
534     return result;
535 }
536 
hasPath() const537 bool KURL::hasPath() const
538 {
539     return m_pathEnd != m_portEnd;
540 }
541 
lastPathComponent() const542 String KURL::lastPathComponent() const
543 {
544     if (!hasPath())
545         return String();
546 
547     int end = m_pathEnd - 1;
548     if (m_string[end] == '/')
549         --end;
550 
551     int start = m_string.reverseFind('/', end);
552     if (start < m_portEnd)
553         return String();
554     ++start;
555 
556     return m_string.substring(start, end - start + 1);
557 }
558 
protocol() const559 String KURL::protocol() const
560 {
561     return m_string.left(m_schemeEnd);
562 }
563 
host() const564 String KURL::host() const
565 {
566     int start = hostStart();
567     return decodeURLEscapeSequences(m_string.substring(start, m_hostEnd - start));
568 }
569 
port() const570 unsigned short KURL::port() const
571 {
572     if (m_hostEnd == m_portEnd)
573         return 0;
574 
575     int number = m_string.substring(m_hostEnd + 1, m_portEnd - m_hostEnd - 1).toInt();
576     if (number < 0 || number > 0xFFFF)
577         return 0;
578     return number;
579 }
580 
pass() const581 String KURL::pass() const
582 {
583     if (m_passwordEnd == m_userEnd)
584         return String();
585 
586     return decodeURLEscapeSequences(m_string.substring(m_userEnd + 1, m_passwordEnd - m_userEnd - 1));
587 }
588 
user() const589 String KURL::user() const
590 {
591     return decodeURLEscapeSequences(m_string.substring(m_userStart, m_userEnd - m_userStart));
592 }
593 
ref() const594 String KURL::ref() const
595 {
596     if (m_fragmentEnd == m_queryEnd)
597         return String();
598 
599     return m_string.substring(m_queryEnd + 1, m_fragmentEnd - (m_queryEnd + 1));
600 }
601 
hasRef() const602 bool KURL::hasRef() const
603 {
604     return m_fragmentEnd != m_queryEnd;
605 }
606 
607 #ifdef NDEBUG
608 
assertProtocolIsGood(const char *)609 static inline void assertProtocolIsGood(const char*)
610 {
611 }
612 
613 #else
614 
assertProtocolIsGood(const char * protocol)615 static void assertProtocolIsGood(const char* protocol)
616 {
617     const char* p = protocol;
618     while (*p) {
619         ASSERT(*p > ' ' && *p < 0x7F && !(*p >= 'A' && *p <= 'Z'));
620         ++p;
621     }
622 }
623 
624 #endif
625 
protocolIs(const char * protocol) const626 bool KURL::protocolIs(const char* protocol) const
627 {
628     // Do the comparison without making a new string object.
629     assertProtocolIsGood(protocol);
630     if (!m_isValid)
631         return false;
632     for (int i = 0; i < m_schemeEnd; ++i) {
633         if (!protocol[i] || toASCIILower(m_string[i]) != protocol[i])
634             return false;
635     }
636     return !protocol[m_schemeEnd]; // We should have consumed all characters in the argument.
637 }
638 
query() const639 String KURL::query() const
640 {
641     return m_string.substring(m_pathEnd, m_queryEnd - m_pathEnd);
642 }
643 
path() const644 String KURL::path() const
645 {
646     return decodeURLEscapeSequences(m_string.substring(m_portEnd, m_pathEnd - m_portEnd));
647 }
648 
setProtocol(const String & s)649 void KURL::setProtocol(const String& s)
650 {
651     // FIXME: Non-ASCII characters must be encoded and escaped to match parse() expectations,
652     // and to avoid changing more than just the protocol.
653 
654     if (!m_isValid) {
655         parse(s + ":" + m_string);
656         return;
657     }
658 
659     parse(s + m_string.substring(m_schemeEnd));
660 }
661 
setHost(const String & s)662 void KURL::setHost(const String& s)
663 {
664     if (!m_isValid)
665         return;
666 
667     // FIXME: Non-ASCII characters must be encoded and escaped to match parse() expectations,
668     // and to avoid changing more than just the host.
669 
670     bool slashSlashNeeded = m_userStart == m_schemeEnd + 1;
671 
672     parse(m_string.left(hostStart()) + (slashSlashNeeded ? "//" : "") + s + m_string.substring(m_hostEnd));
673 }
674 
setPort(unsigned short i)675 void KURL::setPort(unsigned short i)
676 {
677     if (!m_isValid)
678         return;
679 
680     // FIXME: Non-ASCII characters must be encoded and escaped to match parse() expectations,
681     // and to avoid changing more than just the port.
682 
683     bool colonNeeded = m_portEnd == m_hostEnd;
684     int portStart = (colonNeeded ? m_hostEnd : m_hostEnd + 1);
685 
686     parse(m_string.left(portStart) + (colonNeeded ? ":" : "") + String::number(i) + m_string.substring(m_portEnd));
687 }
688 
setHostAndPort(const String & hostAndPort)689 void KURL::setHostAndPort(const String& hostAndPort)
690 {
691     if (!m_isValid)
692         return;
693 
694     // FIXME: Non-ASCII characters must be encoded and escaped to match parse() expectations,
695     // and to avoid changing more than just host and port.
696 
697     bool slashSlashNeeded = m_userStart == m_schemeEnd + 1;
698 
699     parse(m_string.left(hostStart()) + (slashSlashNeeded ? "//" : "") + hostAndPort + m_string.substring(m_portEnd));
700 }
701 
setUser(const String & user)702 void KURL::setUser(const String& user)
703 {
704     if (!m_isValid)
705         return;
706 
707     // FIXME: Non-ASCII characters must be encoded and escaped to match parse() expectations,
708     // and to avoid changing more than just the user login.
709     String u;
710     int end = m_userEnd;
711     if (!user.isEmpty()) {
712         u = user;
713         if (m_userStart == m_schemeEnd + 1)
714             u = "//" + u;
715         // Add '@' if we didn't have one before.
716         if (end == m_hostEnd || (end == m_passwordEnd && m_string[end] != '@'))
717             u.append('@');
718     } else {
719         // Remove '@' if we now have neither user nor password.
720         if (m_userEnd == m_passwordEnd && end != m_hostEnd && m_string[end] == '@')
721             end += 1;
722     }
723     parse(m_string.left(m_userStart) + u + m_string.substring(end));
724 }
725 
setPass(const String & password)726 void KURL::setPass(const String& password)
727 {
728     if (!m_isValid)
729         return;
730 
731     // FIXME: Non-ASCII characters must be encoded and escaped to match parse() expectations,
732     // and to avoid changing more than just the user password.
733     String p;
734     int end = m_passwordEnd;
735     if (!password.isEmpty()) {
736         p = ":" + password + "@";
737         if (m_userEnd == m_schemeEnd + 1)
738             p = "//" + p;
739         // Eat the existing '@' since we are going to add our own.
740         if (end != m_hostEnd && m_string[end] == '@')
741             end += 1;
742     } else {
743         // Remove '@' if we now have neither user nor password.
744         if (m_userStart == m_userEnd && end != m_hostEnd && m_string[end] == '@')
745             end += 1;
746     }
747     parse(m_string.left(m_userEnd) + p + m_string.substring(end));
748 }
749 
setRef(const String & s)750 void KURL::setRef(const String& s)
751 {
752     if (!m_isValid)
753         return;
754 
755     // FIXME: Non-ASCII characters must be encoded and escaped to match parse() expectations.
756     parse(m_string.left(m_queryEnd) + (s.isNull() ? "" : "#" + s));
757 }
758 
removeRef()759 void KURL::removeRef()
760 {
761     if (!m_isValid)
762         return;
763     parse(m_string.left(m_queryEnd));
764 }
765 
setQuery(const String & query)766 void KURL::setQuery(const String& query)
767 {
768     if (!m_isValid)
769         return;
770 
771     // FIXME: '#' and non-ASCII characters must be encoded and escaped.
772     // Usually, the query is encoded using document encoding, not UTF-8, but we don't have
773     // access to the document in this function.
774     if ((query.isEmpty() || query[0] != '?') && !query.isNull())
775         parse(m_string.left(m_pathEnd) + "?" + query + m_string.substring(m_queryEnd));
776     else
777         parse(m_string.left(m_pathEnd) + query + m_string.substring(m_queryEnd));
778 
779 }
780 
setPath(const String & s)781 void KURL::setPath(const String& s)
782 {
783     if (!m_isValid)
784         return;
785 
786     // FIXME: encodeWithURLEscapeSequences does not correctly escape '#' and '?', so fragment and query parts
787     // may be inadvertently affected.
788     parse(m_string.left(m_portEnd) + encodeWithURLEscapeSequences(s) + m_string.substring(m_pathEnd));
789 }
790 
prettyURL() const791 String KURL::prettyURL() const
792 {
793     if (!m_isValid)
794         return m_string;
795 
796     Vector<UChar> result;
797 
798     append(result, protocol());
799     result.append(':');
800 
801     Vector<UChar> authority;
802 
803     if (m_hostEnd != m_passwordEnd) {
804         if (m_userEnd != m_userStart) {
805             append(authority, user());
806             authority.append('@');
807         }
808         append(authority, host());
809         if (port() != 0) {
810             authority.append(':');
811             append(authority, String::number(port()));
812         }
813     }
814 
815     if (!authority.isEmpty()) {
816         result.append('/');
817         result.append('/');
818         result.append(authority);
819     } else if (protocolIs("file")) {
820         result.append('/');
821         result.append('/');
822     }
823 
824     append(result, path());
825     append(result, query());
826 
827     if (m_fragmentEnd != m_queryEnd) {
828         result.append('#');
829         append(result, ref());
830     }
831 
832     return String::adopt(result);
833 }
834 
decodeURLEscapeSequences(const String & str)835 String decodeURLEscapeSequences(const String& str)
836 {
837     return decodeURLEscapeSequences(str, UTF8Encoding());
838 }
839 
decodeURLEscapeSequences(const String & str,const TextEncoding & encoding)840 String decodeURLEscapeSequences(const String& str, const TextEncoding& encoding)
841 {
842     Vector<UChar> result;
843 
844     CharBuffer buffer;
845 
846     int length = str.length();
847     int decodedPosition = 0;
848     int searchPosition = 0;
849     int encodedRunPosition;
850     while ((encodedRunPosition = str.find('%', searchPosition)) >= 0) {
851         // Find the sequence of %-escape codes.
852         int encodedRunEnd = encodedRunPosition;
853         while (length - encodedRunEnd >= 3
854                 && str[encodedRunEnd] == '%'
855                 && isASCIIHexDigit(str[encodedRunEnd + 1])
856                 && isASCIIHexDigit(str[encodedRunEnd + 2]))
857             encodedRunEnd += 3;
858         if (encodedRunEnd == encodedRunPosition) {
859             ++searchPosition;
860             continue;
861         }
862         searchPosition = encodedRunEnd;
863 
864         // Decode the %-escapes into bytes.
865         unsigned runLength = (encodedRunEnd - encodedRunPosition) / 3;
866         buffer.resize(runLength);
867         char* p = buffer.data();
868         const UChar* q = str.characters() + encodedRunPosition;
869         for (unsigned i = 0; i < runLength; ++i) {
870             *p++ = (hexDigitValue(q[1]) << 4) | hexDigitValue(q[2]);
871             q += 3;
872         }
873 
874         // Decode the bytes into Unicode characters.
875         String decoded = (encoding.isValid() ? encoding : UTF8Encoding()).decode(buffer.data(), p - buffer.data());
876         if (decoded.isEmpty())
877             continue;
878 
879         // Build up the string with what we just skipped and what we just decoded.
880         result.append(str.characters() + decodedPosition, encodedRunPosition - decodedPosition);
881         result.append(decoded.characters(), decoded.length());
882         decodedPosition = encodedRunEnd;
883     }
884 
885     result.append(str.characters() + decodedPosition, length - decodedPosition);
886 
887     return String::adopt(result);
888 }
889 
isLocalFile() const890 bool KURL::isLocalFile() const
891 {
892     // Including feed here might be a bad idea since drag and drop uses this check
893     // and including feed would allow feeds to potentially let someone's blog
894     // read the contents of the clipboard on a drag, even without a drop.
895     // Likewise with using the FrameLoader::shouldTreatURLAsLocal() function.
896     return protocolIs("file");
897 }
898 
appendEscapingBadChars(char * & buffer,const char * strStart,size_t length)899 static void appendEscapingBadChars(char*& buffer, const char* strStart, size_t length)
900 {
901     char* p = buffer;
902 
903     const char* str = strStart;
904     const char* strEnd = strStart + length;
905     while (str < strEnd) {
906         unsigned char c = *str++;
907         if (isBadChar(c)) {
908             if (c == '%' || c == '?') {
909                 *p++ = c;
910             } else if (c != 0x09 && c != 0x0a && c != 0x0d) {
911                 *p++ = '%';
912                 *p++ = hexDigits[c >> 4];
913                 *p++ = hexDigits[c & 0xF];
914             }
915         } else {
916             *p++ = c;
917         }
918     }
919 
920     buffer = p;
921 }
922 
923 // copy a path, accounting for "." and ".." segments
copyPathRemovingDots(char * dst,const char * src,int srcStart,int srcEnd)924 static int copyPathRemovingDots(char* dst, const char* src, int srcStart, int srcEnd)
925 {
926     char* bufferPathStart = dst;
927 
928     // empty path is a special case, and need not have a leading slash
929     if (srcStart != srcEnd) {
930         const char* baseStringStart = src + srcStart;
931         const char* baseStringEnd = src + srcEnd;
932         const char* baseStringPos = baseStringStart;
933 
934         // this code is unprepared for paths that do not begin with a
935         // slash and we should always have one in the source string
936         ASSERT(baseStringPos[0] == '/');
937 
938         // copy the leading slash into the destination
939         *dst = *baseStringPos;
940         baseStringPos++;
941         dst++;
942 
943         while (baseStringPos < baseStringEnd) {
944             if (baseStringPos[0] == '.' && dst[-1] == '/') {
945                 if (baseStringPos[1] == '/' || baseStringPos + 1 == baseStringEnd) {
946                     // skip over "." segment
947                     baseStringPos += 2;
948                     continue;
949                 } else if (baseStringPos[1] == '.' && (baseStringPos[2] == '/' ||
950                                        baseStringPos + 2 == baseStringEnd)) {
951                     // skip over ".." segment and rewind the last segment
952                     // the RFC leaves it up to the app to decide what to do with excess
953                     // ".." segments - we choose to drop them since some web content
954                     // relies on this.
955                     baseStringPos += 3;
956                     if (dst > bufferPathStart + 1)
957                         dst--;
958                     // Note that these two while blocks differ subtly.
959                     // The first helps to remove multiple adjoining slashes as we rewind.
960                     // The +1 to bufferPathStart in the first while block prevents eating a leading slash
961                     while (dst > bufferPathStart + 1 && dst[-1] == '/')
962                         dst--;
963                     while (dst > bufferPathStart && dst[-1] != '/')
964                         dst--;
965                     continue;
966                 }
967             }
968 
969             *dst = *baseStringPos;
970             baseStringPos++;
971             dst++;
972         }
973     }
974     *dst = '\0';
975     return dst - bufferPathStart;
976 }
977 
hasSlashDotOrDotDot(const char * str)978 static inline bool hasSlashDotOrDotDot(const char* str)
979 {
980     const unsigned char* p = reinterpret_cast<const unsigned char*>(str);
981     if (!*p)
982         return false;
983     unsigned char pc = *p;
984     while (unsigned char c = *++p) {
985         if (c == '.' && (pc == '/' || pc == '.'))
986             return true;
987         pc = c;
988     }
989     return false;
990 }
991 
matchLetter(char c,char lowercaseLetter)992 static inline bool matchLetter(char c, char lowercaseLetter)
993 {
994     return (c | 0x20) == lowercaseLetter;
995 }
996 
parse(const String & string)997 void KURL::parse(const String& string)
998 {
999     checkEncodedString(string);
1000 
1001     CharBuffer buffer(string.length() + 1);
1002     copyASCII(string.characters(), string.length(), buffer.data());
1003     buffer[string.length()] = '\0';
1004     parse(buffer.data(), &string);
1005 }
1006 
parse(const char * url,const String * originalString)1007 void KURL::parse(const char* url, const String* originalString)
1008 {
1009     if (!url || url[0] == '\0') {
1010         // valid URL must be non-empty
1011         m_string = originalString ? *originalString : url;
1012         invalidate();
1013         return;
1014     }
1015 
1016     if (!isSchemeFirstChar(url[0])) {
1017         // scheme must start with an alphabetic character
1018         m_string = originalString ? *originalString : url;
1019         invalidate();
1020         return;
1021     }
1022 
1023     int schemeEnd = 0;
1024     while (isSchemeChar(url[schemeEnd]))
1025         schemeEnd++;
1026 
1027     if (url[schemeEnd] != ':') {
1028         m_string = originalString ? *originalString : url;
1029         invalidate();
1030         return;
1031     }
1032 
1033     int userStart = schemeEnd + 1;
1034     int userEnd;
1035     int passwordStart;
1036     int passwordEnd;
1037     int hostStart;
1038     int hostEnd;
1039     int portStart;
1040     int portEnd;
1041 
1042     bool hierarchical = url[schemeEnd + 1] == '/';
1043 
1044     bool isFile = schemeEnd == 4
1045         && matchLetter(url[0], 'f')
1046         && matchLetter(url[1], 'i')
1047         && matchLetter(url[2], 'l')
1048         && matchLetter(url[3], 'e');
1049 
1050     bool isHTTPorHTTPS = matchLetter(url[0], 'h')
1051         && matchLetter(url[1], 't')
1052         && matchLetter(url[2], 't')
1053         && matchLetter(url[3], 'p')
1054         && (url[4] == ':' || (matchLetter(url[4], 's') && url[5] == ':'));
1055 
1056     if (hierarchical && url[schemeEnd + 2] == '/') {
1057         // The part after the scheme is either a net_path or an abs_path whose first path segment is empty.
1058         // Attempt to find an authority.
1059 
1060         // FIXME: Authority characters may be scanned twice, and it would be nice to be faster.
1061         userStart += 2;
1062         userEnd = userStart;
1063 
1064         int colonPos = 0;
1065         while (isUserInfoChar(url[userEnd])) {
1066             if (url[userEnd] == ':' && colonPos == 0)
1067                 colonPos = userEnd;
1068             userEnd++;
1069         }
1070 
1071         if (url[userEnd] == '@') {
1072             // actual end of the userinfo, start on the host
1073             if (colonPos != 0) {
1074                 passwordEnd = userEnd;
1075                 userEnd = colonPos;
1076                 passwordStart = colonPos + 1;
1077             } else
1078                 passwordStart = passwordEnd = userEnd;
1079 
1080             hostStart = passwordEnd + 1;
1081         } else if (url[userEnd] == '[' || isPathSegmentEndChar(url[userEnd])) {
1082             // hit the end of the authority, must have been no user
1083             // or looks like an IPv6 hostname
1084             // either way, try to parse it as a hostname
1085             userEnd = userStart;
1086             passwordStart = passwordEnd = userEnd;
1087             hostStart = userStart;
1088         } else {
1089             // invalid character
1090             m_string = originalString ? *originalString : url;
1091             invalidate();
1092             return;
1093         }
1094 
1095         hostEnd = hostStart;
1096 
1097         // IPV6 IP address
1098         if (url[hostEnd] == '[') {
1099             hostEnd++;
1100             while (isIPv6Char(url[hostEnd]))
1101                 hostEnd++;
1102             if (url[hostEnd] == ']')
1103                 hostEnd++;
1104             else {
1105                 // invalid character
1106                 m_string = originalString ? *originalString : url;
1107                 invalidate();
1108                 return;
1109             }
1110         } else {
1111             while (isHostnameChar(url[hostEnd]))
1112                 hostEnd++;
1113         }
1114 
1115         if (url[hostEnd] == ':') {
1116             portStart = portEnd = hostEnd + 1;
1117 
1118             // possible start of port
1119             portEnd = portStart;
1120             while (isASCIIDigit(url[portEnd]))
1121                 portEnd++;
1122         } else
1123             portStart = portEnd = hostEnd;
1124 
1125         if (!isPathSegmentEndChar(url[portEnd])) {
1126             // invalid character
1127             m_string = originalString ? *originalString : url;
1128             invalidate();
1129             return;
1130         }
1131 
1132         if (userStart == portEnd && !isHTTPorHTTPS && !isFile) {
1133             // No authority found, which means that this is not a net_path, but rather an abs_path whose first two
1134             // path segments are empty. For file, http and https only, an empty authority is allowed.
1135             userStart -= 2;
1136             userEnd = userStart;
1137             passwordStart = userEnd;
1138             passwordEnd = passwordStart;
1139             hostStart = passwordEnd;
1140             hostEnd = hostStart;
1141             portStart = hostEnd;
1142             portEnd = hostEnd;
1143         }
1144     } else {
1145         // the part after the scheme must be an opaque_part or an abs_path
1146         userEnd = userStart;
1147         passwordStart = passwordEnd = userEnd;
1148         hostStart = hostEnd = passwordEnd;
1149         portStart = portEnd = hostEnd;
1150     }
1151 
1152     int pathStart = portEnd;
1153     int pathEnd = pathStart;
1154     while (url[pathEnd] && url[pathEnd] != '?' && url[pathEnd] != '#')
1155         pathEnd++;
1156 
1157     int queryStart = pathEnd;
1158     int queryEnd = queryStart;
1159     if (url[queryStart] == '?') {
1160         while (url[queryEnd] && url[queryEnd] != '#')
1161             queryEnd++;
1162     }
1163 
1164     int fragmentStart = queryEnd;
1165     int fragmentEnd = fragmentStart;
1166     if (url[fragmentStart] == '#') {
1167         fragmentStart++;
1168         fragmentEnd = fragmentStart;
1169         while (url[fragmentEnd])
1170             fragmentEnd++;
1171     }
1172 
1173     // assemble it all, remembering the real ranges
1174 
1175     Vector<char, 4096> buffer(fragmentEnd * 3 + 1);
1176 
1177     char *p = buffer.data();
1178     const char *strPtr = url;
1179 
1180     // copy in the scheme
1181     const char *schemeEndPtr = url + schemeEnd;
1182     while (strPtr < schemeEndPtr)
1183         *p++ = *strPtr++;
1184     m_schemeEnd = p - buffer.data();
1185 
1186     bool hostIsLocalHost = portEnd - userStart == 9
1187         && matchLetter(url[userStart], 'l')
1188         && matchLetter(url[userStart+1], 'o')
1189         && matchLetter(url[userStart+2], 'c')
1190         && matchLetter(url[userStart+3], 'a')
1191         && matchLetter(url[userStart+4], 'l')
1192         && matchLetter(url[userStart+5], 'h')
1193         && matchLetter(url[userStart+6], 'o')
1194         && matchLetter(url[userStart+7], 's')
1195         && matchLetter(url[userStart+8], 't');
1196 
1197     // File URLs need a host part unless it is just file:// or file://localhost
1198     bool degenFilePath = pathStart == pathEnd && (hostStart == hostEnd || hostIsLocalHost);
1199 
1200     bool haveNonHostAuthorityPart = userStart != userEnd || passwordStart != passwordEnd || portStart != portEnd;
1201 
1202     // add ":" after scheme
1203     *p++ = ':';
1204 
1205     // if we have at least one authority part or a file URL - add "//" and authority
1206     if (isFile ? !degenFilePath : (haveNonHostAuthorityPart || hostStart != hostEnd)) {
1207         *p++ = '/';
1208         *p++ = '/';
1209 
1210         m_userStart = p - buffer.data();
1211 
1212         // copy in the user
1213         strPtr = url + userStart;
1214         const char* userEndPtr = url + userEnd;
1215         while (strPtr < userEndPtr)
1216             *p++ = *strPtr++;
1217         m_userEnd = p - buffer.data();
1218 
1219         // copy in the password
1220         if (passwordEnd != passwordStart) {
1221             *p++ = ':';
1222             strPtr = url + passwordStart;
1223             const char* passwordEndPtr = url + passwordEnd;
1224             while (strPtr < passwordEndPtr)
1225                 *p++ = *strPtr++;
1226         }
1227         m_passwordEnd = p - buffer.data();
1228 
1229         // If we had any user info, add "@"
1230         if (p - buffer.data() != m_userStart)
1231             *p++ = '@';
1232 
1233         // copy in the host, except in the case of a file URL with authority="localhost"
1234         if (!(isFile && hostIsLocalHost && !haveNonHostAuthorityPart)) {
1235             strPtr = url + hostStart;
1236             const char* hostEndPtr = url + hostEnd;
1237             while (strPtr < hostEndPtr)
1238                 *p++ = *strPtr++;
1239         }
1240         m_hostEnd = p - buffer.data();
1241 
1242         // copy in the port
1243         if (hostEnd != portStart) {
1244             *p++ = ':';
1245             strPtr = url + portStart;
1246             const char *portEndPtr = url + portEnd;
1247             while (strPtr < portEndPtr)
1248                 *p++ = *strPtr++;
1249         }
1250         m_portEnd = p - buffer.data();
1251     } else
1252         m_userStart = m_userEnd = m_passwordEnd = m_hostEnd = m_portEnd = p - buffer.data();
1253 
1254     // For canonicalization, ensure we have a '/' for no path.
1255     // Only do this for http and https.
1256     if (isHTTPorHTTPS && pathEnd - pathStart == 0)
1257         *p++ = '/';
1258 
1259     // add path, escaping bad characters
1260     if (!hierarchical || !hasSlashDotOrDotDot(url))
1261         appendEscapingBadChars(p, url + pathStart, pathEnd - pathStart);
1262     else {
1263         CharBuffer pathBuffer(pathEnd - pathStart + 1);
1264         size_t length = copyPathRemovingDots(pathBuffer.data(), url, pathStart, pathEnd);
1265         appendEscapingBadChars(p, pathBuffer.data(), length);
1266     }
1267 
1268     m_pathEnd = p - buffer.data();
1269 
1270     // Find the position after the last slash in the path, or
1271     // the position before the path if there are no slashes in it.
1272     int i;
1273     for (i = m_pathEnd; i > m_portEnd; --i) {
1274         if (buffer[i - 1] == '/')
1275             break;
1276     }
1277     m_pathAfterLastSlash = i;
1278 
1279     // add query, escaping bad characters
1280     appendEscapingBadChars(p, url + queryStart, queryEnd - queryStart);
1281     m_queryEnd = p - buffer.data();
1282 
1283     // add fragment, escaping bad characters
1284     if (fragmentEnd != queryEnd) {
1285         *p++ = '#';
1286         appendEscapingBadChars(p, url + fragmentStart, fragmentEnd - fragmentStart);
1287     }
1288     m_fragmentEnd = p - buffer.data();
1289 
1290     ASSERT(p - buffer.data() <= static_cast<int>(buffer.size()));
1291 
1292     // If we didn't end up actually changing the original string and
1293     // it was already in a String, reuse it to avoid extra allocation.
1294     if (originalString && strncmp(buffer.data(), url, m_fragmentEnd) == 0)
1295         m_string = *originalString;
1296     else
1297         m_string = String(buffer.data(), m_fragmentEnd);
1298 
1299     m_isValid = true;
1300 }
1301 
equalIgnoringRef(const KURL & a,const KURL & b)1302 bool equalIgnoringRef(const KURL& a, const KURL& b)
1303 {
1304     if (a.m_queryEnd != b.m_queryEnd)
1305         return false;
1306     unsigned queryLength = a.m_queryEnd;
1307     for (unsigned i = 0; i < queryLength; ++i)
1308         if (a.string()[i] != b.string()[i])
1309             return false;
1310     return true;
1311 }
1312 
protocolHostAndPortAreEqual(const KURL & a,const KURL & b)1313 bool protocolHostAndPortAreEqual(const KURL& a, const KURL& b)
1314 {
1315     if (a.m_schemeEnd != b.m_schemeEnd)
1316         return false;
1317     int hostStartA = a.hostStart();
1318     int hostStartB = b.hostStart();
1319     if (a.m_hostEnd - hostStartA != b.m_hostEnd - hostStartB)
1320         return false;
1321 
1322     // Check the scheme
1323     for (int i = 0; i < a.m_schemeEnd; ++i)
1324         if (a.string()[i] != b.string()[i])
1325             return false;
1326 
1327     // And the host
1328     for (int i = hostStartA; i < a.m_hostEnd; ++i)
1329         if (a.string()[i] != b.string()[i])
1330             return false;
1331 
1332     if (a.port() != b.port())
1333         return false;
1334 
1335     return true;
1336 }
1337 
1338 
encodeWithURLEscapeSequences(const String & notEncodedString)1339 String encodeWithURLEscapeSequences(const String& notEncodedString)
1340 {
1341     CString asUTF8 = notEncodedString.utf8();
1342 
1343     CharBuffer buffer(asUTF8.length() * 3 + 1);
1344     char* p = buffer.data();
1345 
1346     const char* str = asUTF8.data();
1347     const char* strEnd = str + asUTF8.length();
1348     while (str < strEnd) {
1349         unsigned char c = *str++;
1350         if (isBadChar(c)) {
1351             *p++ = '%';
1352             *p++ = hexDigits[c >> 4];
1353             *p++ = hexDigits[c & 0xF];
1354         } else
1355             *p++ = c;
1356     }
1357 
1358     ASSERT(p - buffer.data() <= static_cast<int>(buffer.size()));
1359 
1360     return String(buffer.data(), p - buffer.data());
1361 }
1362 
1363 // Appends the punycoded hostname identified by the given string and length to
1364 // the output buffer. The result will not be null terminated.
appendEncodedHostname(UCharBuffer & buffer,const UChar * str,unsigned strLen)1365 static void appendEncodedHostname(UCharBuffer& buffer, const UChar* str, unsigned strLen)
1366 {
1367     // Needs to be big enough to hold an IDN-encoded name.
1368     // For host names bigger than this, we won't do IDN encoding, which is almost certainly OK.
1369     const unsigned hostnameBufferLength = 2048;
1370 
1371     if (strLen > hostnameBufferLength || charactersAreAllASCII(str, strLen)) {
1372         buffer.append(str, strLen);
1373         return;
1374     }
1375 
1376 #if USE(ICU_UNICODE)
1377     UChar hostnameBuffer[hostnameBufferLength];
1378     UErrorCode error = U_ZERO_ERROR;
1379     int32_t numCharactersConverted = uidna_IDNToASCII(str, strLen, hostnameBuffer,
1380         hostnameBufferLength, UIDNA_ALLOW_UNASSIGNED, 0, &error);
1381     if (error == U_ZERO_ERROR)
1382         buffer.append(hostnameBuffer, numCharactersConverted);
1383 #elif USE(QT4_UNICODE)
1384     QByteArray result = QUrl::toAce(String(str, strLen));
1385     buffer.append(result.constData(), result.length());
1386 #endif
1387 }
1388 
findHostnamesInMailToURL(const UChar * str,int strLen,Vector<pair<int,int>> & nameRanges)1389 static void findHostnamesInMailToURL(const UChar* str, int strLen, Vector<pair<int, int> >& nameRanges)
1390 {
1391     // In a mailto: URL, host names come after a '@' character and end with a '>' or ',' or '?' or end of string character.
1392     // Skip quoted strings so that characters in them don't confuse us.
1393     // When we find a '?' character, we are past the part of the URL that contains host names.
1394 
1395     nameRanges.clear();
1396 
1397     int p = 0;
1398     while (1) {
1399         // Find start of host name or of quoted string.
1400         int hostnameOrStringStart = findFirstOf(str, strLen, p, "\"@?");
1401         if (hostnameOrStringStart == -1)
1402             return;
1403         UChar c = str[hostnameOrStringStart];
1404         p = hostnameOrStringStart + 1;
1405 
1406         if (c == '?')
1407             return;
1408 
1409         if (c == '@') {
1410             // Find end of host name.
1411             int hostnameStart = p;
1412             int hostnameEnd = findFirstOf(str, strLen, p, ">,?");
1413             bool done;
1414             if (hostnameEnd == -1) {
1415                 hostnameEnd = strLen;
1416                 done = true;
1417             } else {
1418                 p = hostnameEnd;
1419                 done = false;
1420             }
1421 
1422             nameRanges.append(make_pair(hostnameStart, hostnameEnd));
1423 
1424             if (done)
1425                 return;
1426         } else {
1427             // Skip quoted string.
1428             ASSERT(c == '"');
1429             while (1) {
1430                 int escapedCharacterOrStringEnd = findFirstOf(str, strLen, p, "\"\\");
1431                 if (escapedCharacterOrStringEnd == -1)
1432                     return;
1433 
1434                 c = str[escapedCharacterOrStringEnd];
1435                 p = escapedCharacterOrStringEnd + 1;
1436 
1437                 // If we are the end of the string, then break from the string loop back to the host name loop.
1438                 if (c == '"')
1439                     break;
1440 
1441                 // Skip escaped character.
1442                 ASSERT(c == '\\');
1443                 if (p == strLen)
1444                     return;
1445 
1446                 ++p;
1447             }
1448         }
1449     }
1450 }
1451 
findHostnameInHierarchicalURL(const UChar * str,int strLen,int & startOffset,int & endOffset)1452 static bool findHostnameInHierarchicalURL(const UChar* str, int strLen, int& startOffset, int& endOffset)
1453 {
1454     // Find the host name in a hierarchical URL.
1455     // It comes after a "://" sequence, with scheme characters preceding, and
1456     // this should be the first colon in the string.
1457     // It ends with the end of the string or a ":" or a path segment ending character.
1458     // If there is a "@" character, the host part is just the part after the "@".
1459     int separator = findFirstOf(str, strLen, 0, ":");
1460     if (separator == -1 || separator + 2 >= strLen ||
1461         str[separator + 1] != '/' || str[separator + 2] != '/')
1462         return false;
1463 
1464     // Check that all characters before the :// are valid scheme characters.
1465     if (!isSchemeFirstChar(str[0]))
1466         return false;
1467     for (int i = 1; i < separator; ++i) {
1468         if (!isSchemeChar(str[i]))
1469             return false;
1470     }
1471 
1472     // Start after the separator.
1473     int authorityStart = separator + 3;
1474 
1475     // Find terminating character.
1476     int hostnameEnd = strLen;
1477     for (int i = authorityStart; i < strLen; ++i) {
1478         UChar c = str[i];
1479         if (c == ':' || (isPathSegmentEndChar(c) && c != 0)) {
1480             hostnameEnd = i;
1481             break;
1482         }
1483     }
1484 
1485     // Find "@" for the start of the host name.
1486     int userInfoTerminator = findFirstOf(str, strLen, authorityStart, "@");
1487     int hostnameStart;
1488     if (userInfoTerminator == -1 || userInfoTerminator > hostnameEnd)
1489         hostnameStart = authorityStart;
1490     else
1491         hostnameStart = userInfoTerminator + 1;
1492 
1493     startOffset = hostnameStart;
1494     endOffset = hostnameEnd;
1495     return true;
1496 }
1497 
1498 // Converts all hostnames found in the given input to punycode, preserving the
1499 // rest of the URL unchanged. The output will NOT be null-terminated.
encodeHostnames(const String & str,UCharBuffer & output)1500 static void encodeHostnames(const String& str, UCharBuffer& output)
1501 {
1502     output.clear();
1503 
1504     if (protocolIs(str, "mailto")) {
1505         Vector<pair<int, int> > hostnameRanges;
1506         findHostnamesInMailToURL(str.characters(), str.length(), hostnameRanges);
1507         int n = hostnameRanges.size();
1508         int p = 0;
1509         for (int i = 0; i < n; ++i) {
1510             const pair<int, int>& r = hostnameRanges[i];
1511             output.append(&str.characters()[p], r.first - p);
1512             appendEncodedHostname(output, &str.characters()[r.first], r.second - r.first);
1513             p = r.second;
1514         }
1515         // This will copy either everything after the last hostname, or the
1516         // whole thing if there is no hostname.
1517         output.append(&str.characters()[p], str.length() - p);
1518     } else {
1519         int hostStart, hostEnd;
1520         if (findHostnameInHierarchicalURL(str.characters(), str.length(), hostStart, hostEnd)) {
1521             output.append(str.characters(), hostStart); // Before hostname.
1522             appendEncodedHostname(output, &str.characters()[hostStart], hostEnd - hostStart);
1523             output.append(&str.characters()[hostEnd], str.length() - hostEnd); // After hostname.
1524         } else {
1525             // No hostname to encode, return the input.
1526             output.append(str.characters(), str.length());
1527         }
1528     }
1529 }
1530 
encodeRelativeString(const String & rel,const TextEncoding & encoding,CharBuffer & output)1531 static void encodeRelativeString(const String& rel, const TextEncoding& encoding, CharBuffer& output)
1532 {
1533     UCharBuffer s;
1534     encodeHostnames(rel, s);
1535 
1536     TextEncoding pathEncoding(UTF8Encoding()); // Path is always encoded as UTF-8; other parts may depend on the scheme.
1537 
1538     int pathEnd = -1;
1539     if (encoding != pathEncoding && encoding.isValid() && !protocolIs(rel, "mailto") && !protocolIs(rel, "data") && !protocolIs(rel, "javascript")) {
1540         // Find the first instance of either # or ?, keep pathEnd at -1 otherwise.
1541         pathEnd = findFirstOf(s.data(), s.size(), 0, "#?");
1542     }
1543 
1544     if (pathEnd == -1) {
1545         CString decoded = pathEncoding.encode(s.data(), s.size(), URLEncodedEntitiesForUnencodables);
1546         output.resize(decoded.length());
1547         memcpy(output.data(), decoded.data(), decoded.length());
1548     } else {
1549         CString pathDecoded = pathEncoding.encode(s.data(), pathEnd, URLEncodedEntitiesForUnencodables);
1550         // Unencodable characters in URLs are represented by converting
1551         // them to XML entities and escaping non-alphanumeric characters.
1552         CString otherDecoded = encoding.encode(s.data() + pathEnd, s.size() - pathEnd, URLEncodedEntitiesForUnencodables);
1553 
1554         output.resize(pathDecoded.length() + otherDecoded.length());
1555         memcpy(output.data(), pathDecoded.data(), pathDecoded.length());
1556         memcpy(output.data() + pathDecoded.length(), otherDecoded.data(), otherDecoded.length());
1557     }
1558     output.append('\0'); // null-terminate the output.
1559 }
1560 
substituteBackslashes(const String & string)1561 static String substituteBackslashes(const String& string)
1562 {
1563     int questionPos = string.find('?');
1564     int hashPos = string.find('#');
1565     int pathEnd;
1566 
1567     if (hashPos >= 0 && (questionPos < 0 || questionPos > hashPos))
1568         pathEnd = hashPos;
1569     else if (questionPos >= 0)
1570         pathEnd = questionPos;
1571     else
1572         pathEnd = string.length();
1573 
1574     return string.left(pathEnd).replace('\\','/') + string.substring(pathEnd);
1575 }
1576 
isHierarchical() const1577 bool KURL::isHierarchical() const
1578 {
1579     if (!m_isValid)
1580         return false;
1581     ASSERT(m_string[m_schemeEnd] == ':');
1582     return m_string[m_schemeEnd + 1] == '/';
1583 }
1584 
copyToBuffer(CharBuffer & buffer) const1585 void KURL::copyToBuffer(CharBuffer& buffer) const
1586 {
1587     // FIXME: This throws away the high bytes of all the characters in the string!
1588     // That's fine for a valid URL, which is all ASCII, but not for invalid URLs.
1589     buffer.resize(m_string.length());
1590     copyASCII(m_string.characters(), m_string.length(), buffer.data());
1591 }
1592 
protocolIs(const String & url,const char * protocol)1593 bool protocolIs(const String& url, const char* protocol)
1594 {
1595     // Do the comparison without making a new string object.
1596     assertProtocolIsGood(protocol);
1597     for (int i = 0; ; ++i) {
1598         if (!protocol[i])
1599             return url[i] == ':';
1600         if (toASCIILower(url[i]) != protocol[i])
1601             return false;
1602     }
1603 }
1604 
mimeTypeFromDataURL(const String & url)1605 String mimeTypeFromDataURL(const String& url)
1606 {
1607     ASSERT(protocolIs(url, "data"));
1608     int index = url.find(';');
1609     if (index == -1)
1610         index = url.find(',');
1611     if (index != -1) {
1612         int len = index - 5;
1613         if (len > 0)
1614             return url.substring(5, len);
1615         return "text/plain"; // Data URLs with no MIME type are considered text/plain.
1616     }
1617     return "";
1618 }
1619 
blankURL()1620 const KURL& blankURL()
1621 {
1622     DEFINE_STATIC_LOCAL(KURL, staticBlankURL, ("about:blank"));
1623     return staticBlankURL;
1624 }
1625 
1626 #ifndef NDEBUG
print() const1627 void KURL::print() const
1628 {
1629     printf("%s\n", m_string.utf8().data());
1630 }
1631 #endif
1632 
1633 }
1634 
1635 #endif  // !USE(GOOGLEURL)
1636