• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2004, 2007, 2008, 2011, 2012 Apple Inc. All rights reserved.
3  * Copyright (C) 2012 Research In Motion Limited. All rights reserved.
4  * Copyright (C) 2008, 2009, 2011 Google Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "config.h"
29 #include "platform/weborigin/KURL.h"
30 
31 #include "platform/weborigin/KnownPorts.h"
32 #include "wtf/StdLibExtras.h"
33 #include "wtf/text/CString.h"
34 #include "wtf/text/StringHash.h"
35 #include "wtf/text/StringUTF8Adaptor.h"
36 #include "wtf/text/TextEncoding.h"
37 #include <algorithm>
38 #include <url/url_util.h>
39 #ifndef NDEBUG
40 #include <stdio.h>
41 #endif
42 
43 namespace WebCore {
44 
45 static const int maximumValidPortNumber = 0xFFFE;
46 static const int invalidPortNumber = 0xFFFF;
47 
assertProtocolIsGood(const char * protocol)48 static void assertProtocolIsGood(const char* protocol)
49 {
50 #ifndef NDEBUG
51     const char* p = protocol;
52     while (*p) {
53         ASSERT(*p > ' ' && *p < 0x7F && !(*p >= 'A' && *p <= 'Z'));
54         ++p;
55     }
56 #endif
57 }
58 
59 // Note: You must ensure that |spec| is a valid canonicalized URL before calling this function.
asURLChar8Subtle(const String & spec)60 static const char* asURLChar8Subtle(const String& spec)
61 {
62     ASSERT(spec.is8Bit());
63     // characters8 really return characters in Latin-1, but because we canonicalize
64     // URL strings, we know that everything before the fragment identifier will
65     // actually be ASCII, which means this cast is safe as long as you don't look
66     // at the fragment component.
67     return reinterpret_cast<const char*>(spec.characters8());
68 }
69 
70 // Returns the characters for the given string, or a pointer to a static empty
71 // string if the input string is null. This will always ensure we have a non-
72 // null character pointer since ReplaceComponents has special meaning for null.
charactersOrEmpty(const StringUTF8Adaptor & string)73 static const char* charactersOrEmpty(const StringUTF8Adaptor& string)
74 {
75     static const char zero = 0;
76     return string.data() ? string.data() : &zero;
77 }
78 
isSchemeFirstChar(char c)79 static bool isSchemeFirstChar(char c)
80 {
81     return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
82 }
83 
isSchemeChar(char c)84 static bool isSchemeChar(char c)
85 {
86     return isSchemeFirstChar(c) || (c >= '0' && c <= '9') || c == '.' || c == '-' || c == '+';
87 }
88 
isUnicodeEncoding(const WTF::TextEncoding * encoding)89 static bool isUnicodeEncoding(const WTF::TextEncoding* encoding)
90 {
91     return encoding->encodingForFormSubmission() == UTF8Encoding();
92 }
93 
94 namespace {
95 
96 class KURLCharsetConverter : public url_canon::CharsetConverter {
97 public:
98     // The encoding parameter may be 0, but in this case the object must not be called.
KURLCharsetConverter(const WTF::TextEncoding * encoding)99     explicit KURLCharsetConverter(const WTF::TextEncoding* encoding)
100         : m_encoding(encoding)
101     {
102     }
103 
ConvertFromUTF16(const url_parse::UTF16Char * input,int inputLength,url_canon::CanonOutput * output)104     virtual void ConvertFromUTF16(const url_parse::UTF16Char* input, int inputLength, url_canon::CanonOutput* output)
105     {
106         CString encoded = m_encoding->normalizeAndEncode(String(input, inputLength), WTF::URLEncodedEntitiesForUnencodables);
107         output->Append(encoded.data(), static_cast<int>(encoded.length()));
108     }
109 
110 private:
111     const WTF::TextEncoding* m_encoding;
112 };
113 
114 } // namespace
115 
isValidProtocol(const String & protocol)116 bool isValidProtocol(const String& protocol)
117 {
118     // RFC3986: ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
119     if (protocol.isEmpty())
120         return false;
121     if (!isSchemeFirstChar(protocol[0]))
122         return false;
123     unsigned protocolLength = protocol.length();
124     for (unsigned i = 1; i < protocolLength; i++) {
125         if (!isSchemeChar(protocol[i]))
126             return false;
127     }
128     return true;
129 }
130 
strippedForUseAsReferrer() const131 String KURL::strippedForUseAsReferrer() const
132 {
133     if (isBlankURL() || protocolIs("data") || protocolIs("javascript"))
134         return String();
135 
136     KURL referrer(*this);
137     referrer.setUser(String());
138     referrer.setPass(String());
139     referrer.removeFragmentIdentifier();
140     return referrer.string();
141 }
142 
isLocalFile() const143 bool KURL::isLocalFile() const
144 {
145     // Including feed here might be a bad idea since drag and drop uses this check
146     // and including feed would allow feeds to potentially let someone's blog
147     // read the contents of the clipboard on a drag, even without a drop.
148     // Likewise with using the FrameLoader::shouldTreatURLAsLocal() function.
149     return protocolIs("file");
150 }
151 
protocolIsJavaScript(const String & url)152 bool protocolIsJavaScript(const String& url)
153 {
154     return protocolIs(url, "javascript");
155 }
156 
blankURL()157 const KURL& blankURL()
158 {
159     DEFINE_STATIC_LOCAL(KURL, staticBlankURL, (ParsedURLString, "about:blank"));
160     return staticBlankURL;
161 }
162 
isBlankURL() const163 bool KURL::isBlankURL() const
164 {
165     return protocolIs("about");
166 }
167 
elidedString() const168 String KURL::elidedString() const
169 {
170     if (string().length() <= 1024)
171         return string();
172 
173     return string().left(511) + "..." + string().right(510);
174 }
175 
176 // Initializes with a string representing an absolute URL. No encoding
177 // information is specified. This generally happens when a KURL is converted
178 // to a string and then converted back. In this case, the URL is already
179 // canonical and in proper escaped form so needs no encoding. We treat it as
180 // UTF-8 just in case.
KURL(ParsedURLStringTag,const String & url)181 KURL::KURL(ParsedURLStringTag, const String& url)
182 {
183     if (!url.isNull())
184         init(KURL(), url, 0);
185     else {
186         // WebCore expects us to preserve the nullness of strings when this
187         // constructor is used. In all other cases, it expects a non-null
188         // empty string, which is what init() will create.
189         m_isValid = false;
190         m_protocolIsInHTTPFamily = false;
191     }
192 }
193 
createIsolated(ParsedURLStringTag,const String & url)194 KURL KURL::createIsolated(ParsedURLStringTag, const String& url)
195 {
196     // FIXME: We should be able to skip this extra copy and created an
197     // isolated KURL more efficiently.
198     return KURL(ParsedURLString, url).copy();
199 }
200 
201 // Constructs a new URL given a base URL and a possibly relative input URL.
202 // This assumes UTF-8 encoding.
KURL(const KURL & base,const String & relative)203 KURL::KURL(const KURL& base, const String& relative)
204 {
205     init(base, relative, 0);
206 }
207 
208 // Constructs a new URL given a base URL and a possibly relative input URL.
209 // Any query portion of the relative URL will be encoded in the given encoding.
KURL(const KURL & base,const String & relative,const WTF::TextEncoding & encoding)210 KURL::KURL(const KURL& base, const String& relative, const WTF::TextEncoding& encoding)
211 {
212     init(base, relative, &encoding.encodingForFormSubmission());
213 }
214 
KURL(const AtomicString & canonicalString,const url_parse::Parsed & parsed,bool isValid)215 KURL::KURL(const AtomicString& canonicalString, const url_parse::Parsed& parsed, bool isValid)
216     : m_isValid(isValid)
217     , m_protocolIsInHTTPFamily(false)
218     , m_parsed(parsed)
219     , m_string(canonicalString)
220 {
221     initProtocolIsInHTTPFamily();
222     initInnerURL();
223 }
224 
KURL(WTF::HashTableDeletedValueType)225 KURL::KURL(WTF::HashTableDeletedValueType)
226     : m_isValid(false)
227     , m_protocolIsInHTTPFamily(false)
228     , m_string(WTF::HashTableDeletedValue)
229 {
230 }
231 
KURL(const KURL & other)232 KURL::KURL(const KURL& other)
233     : m_isValid(other.m_isValid)
234     , m_protocolIsInHTTPFamily(other.m_protocolIsInHTTPFamily)
235     , m_parsed(other.m_parsed)
236     , m_string(other.m_string)
237 {
238     if (other.m_innerURL.get())
239         m_innerURL = adoptPtr(new KURL(other.m_innerURL->copy()));
240 }
241 
operator =(const KURL & other)242 KURL& KURL::operator=(const KURL& other)
243 {
244     m_isValid = other.m_isValid;
245     m_protocolIsInHTTPFamily = other.m_protocolIsInHTTPFamily;
246     m_parsed = other.m_parsed;
247     m_string = other.m_string;
248     if (other.m_innerURL)
249         m_innerURL = adoptPtr(new KURL(other.m_innerURL->copy()));
250     else
251         m_innerURL.clear();
252     return *this;
253 }
254 
copy() const255 KURL KURL::copy() const
256 {
257     KURL result;
258     result.m_isValid = m_isValid;
259     result.m_protocolIsInHTTPFamily = m_protocolIsInHTTPFamily;
260     result.m_parsed = m_parsed;
261     result.m_string = m_string.isolatedCopy();
262     if (result.m_innerURL)
263         result.m_innerURL = adoptPtr(new KURL(m_innerURL->copy()));
264     return result;
265 }
266 
isNull() const267 bool KURL::isNull() const
268 {
269     return m_string.isNull();
270 }
271 
isEmpty() const272 bool KURL::isEmpty() const
273 {
274     return m_string.isEmpty();
275 }
276 
isValid() const277 bool KURL::isValid() const
278 {
279     return m_isValid;
280 }
281 
hasPort() const282 bool KURL::hasPort() const
283 {
284     return hostEnd() < pathStart();
285 }
286 
protocolIsInHTTPFamily() const287 bool KURL::protocolIsInHTTPFamily() const
288 {
289     return m_protocolIsInHTTPFamily;
290 }
291 
hasPath() const292 bool KURL::hasPath() const
293 {
294     // Note that http://www.google.com/" has a path, the path is "/". This can
295     // return false only for invalid or nonstandard URLs.
296     return m_parsed.path.len >= 0;
297 }
298 
299 // We handle "parameters" separated by a semicolon, while KURL.cpp does not,
300 // which can lead to different results in some cases.
lastPathComponent() const301 String KURL::lastPathComponent() const
302 {
303     if (!m_isValid)
304         return stringForInvalidComponent();
305     ASSERT(!m_string.isNull());
306 
307     // When the output ends in a slash, WebCore has different expectations than
308     // the GoogleURL library. For "/foo/bar/" the library will return the empty
309     // string, but WebCore wants "bar".
310     url_parse::Component path = m_parsed.path;
311     if (path.len > 0 && m_string[path.end() - 1] == '/')
312         path.len--;
313 
314     url_parse::Component file;
315     if (m_string.is8Bit())
316         url_parse::ExtractFileName(asURLChar8Subtle(m_string), path, &file);
317     else
318         url_parse::ExtractFileName(m_string.characters16(), path, &file);
319 
320     // Bug: https://bugs.webkit.org/show_bug.cgi?id=21015 this function returns
321     // a null string when the path is empty, which we duplicate here.
322     if (!file.is_nonempty())
323         return String();
324     return componentString(file);
325 }
326 
protocol() const327 String KURL::protocol() const
328 {
329     return componentString(m_parsed.scheme);
330 }
331 
host() const332 String KURL::host() const
333 {
334     return componentString(m_parsed.host);
335 }
336 
337 // Returns 0 when there is no port.
338 //
339 // We treat URL's with out-of-range port numbers as invalid URLs, and they will
340 // be rejected by the canonicalizer. KURL.cpp will allow them in parsing, but
341 // return invalidPortNumber from this port() function, so we mirror that behavior here.
port() const342 unsigned short KURL::port() const
343 {
344     if (!m_isValid || m_parsed.port.len <= 0)
345         return 0;
346     ASSERT(!m_string.isNull());
347     int port = m_string.is8Bit() ?
348         url_parse::ParsePort(asURLChar8Subtle(m_string), m_parsed.port) :
349         url_parse::ParsePort(m_string.characters16(), m_parsed.port);
350     ASSERT(port != url_parse::PORT_UNSPECIFIED); // Checked port.len <= 0 before.
351 
352     if (port == url_parse::PORT_INVALID || port > maximumValidPortNumber) // Mimic KURL::port()
353         port = invalidPortNumber;
354 
355     return static_cast<unsigned short>(port);
356 }
357 
pass() const358 String KURL::pass() const
359 {
360     // Bug: https://bugs.webkit.org/show_bug.cgi?id=21015 this function returns
361     // a null string when the password is empty, which we duplicate here.
362     if (!m_parsed.password.is_nonempty())
363         return String();
364     return componentString(m_parsed.password);
365 }
366 
user() const367 String KURL::user() const
368 {
369     return componentString(m_parsed.username);
370 }
371 
fragmentIdentifier() const372 String KURL::fragmentIdentifier() const
373 {
374     // Empty but present refs ("foo.com/bar#") should result in the empty
375     // string, which componentString will produce. Nonexistent refs
376     // should be the null string.
377     if (!m_parsed.ref.is_valid())
378         return String();
379     return componentString(m_parsed.ref);
380 }
381 
hasFragmentIdentifier() const382 bool KURL::hasFragmentIdentifier() const
383 {
384     return m_parsed.ref.len >= 0;
385 }
386 
baseAsString() const387 String KURL::baseAsString() const
388 {
389     // FIXME: There is probably a more efficient way to do this?
390     return m_string.left(pathAfterLastSlash());
391 }
392 
query() const393 String KURL::query() const
394 {
395     if (m_parsed.query.len >= 0)
396         return componentString(m_parsed.query);
397 
398     // Bug: https://bugs.webkit.org/show_bug.cgi?id=21015 this function returns
399     // an empty string when the query is empty rather than a null (not sure
400     // which is right).
401     // Returns a null if the query is not specified, instead of empty.
402     if (m_parsed.query.is_valid())
403         return emptyString();
404     return String();
405 }
406 
path() const407 String KURL::path() const
408 {
409     return componentString(m_parsed.path);
410 }
411 
setProtocol(const String & protocol)412 bool KURL::setProtocol(const String& protocol)
413 {
414     // Firefox and IE remove everything after the first ':'.
415     int separatorPosition = protocol.find(':');
416     String newProtocol = protocol.substring(0, separatorPosition);
417     StringUTF8Adaptor newProtocolUTF8(newProtocol);
418 
419     // If KURL is given an invalid scheme, it returns failure without modifying
420     // the URL at all. This is in contrast to most other setters which modify
421     // the URL and set "m_isValid."
422     url_canon::RawCanonOutputT<char> canonProtocol;
423     url_parse::Component protocolComponent;
424     if (!url_canon::CanonicalizeScheme(newProtocolUTF8.data(), url_parse::Component(0, newProtocolUTF8.length()), &canonProtocol, &protocolComponent)
425         || !protocolComponent.is_nonempty())
426         return false;
427 
428     url_canon::Replacements<char> replacements;
429     replacements.SetScheme(charactersOrEmpty(newProtocolUTF8), url_parse::Component(0, newProtocolUTF8.length()));
430     replaceComponents(replacements);
431 
432     // isValid could be false but we still return true here. This is because
433     // WebCore or JS scripts can build up a URL by setting individual
434     // components, and a JS exception is based on the return value of this
435     // function. We want to throw the exception and stop the script only when
436     // its trying to set a bad protocol, and not when it maybe just hasn't
437     // finished building up its final scheme.
438     return true;
439 }
440 
setHost(const String & host)441 void KURL::setHost(const String& host)
442 {
443     StringUTF8Adaptor hostUTF8(host);
444     url_canon::Replacements<char> replacements;
445     replacements.SetHost(charactersOrEmpty(hostUTF8), url_parse::Component(0, hostUTF8.length()));
446     replaceComponents(replacements);
447 }
448 
parsePortFromStringPosition(const String & value,unsigned portStart)449 static String parsePortFromStringPosition(const String& value, unsigned portStart)
450 {
451     // "008080junk" needs to be treated as port "8080" and "000" as "0".
452     size_t length = value.length();
453     unsigned portEnd = portStart;
454     while (isASCIIDigit(value[portEnd]) && portEnd < length)
455         ++portEnd;
456     while (value[portStart] == '0' && portStart < portEnd - 1)
457         ++portStart;
458 
459     // Required for backwards compat.
460     // https://www.w3.org/Bugs/Public/show_bug.cgi?id=23463
461     if (portStart == portEnd)
462         return "0";
463 
464     return value.substring(portStart, portEnd - portStart);
465 }
466 
setHostAndPort(const String & hostAndPort)467 void KURL::setHostAndPort(const String& hostAndPort)
468 {
469     size_t separator = hostAndPort.find(':');
470     if (!separator)
471         return;
472 
473     if (separator == kNotFound) {
474         url_canon::Replacements<char> replacements;
475         StringUTF8Adaptor hostUTF8(hostAndPort);
476         replacements.SetHost(charactersOrEmpty(hostUTF8), url_parse::Component(0, hostUTF8.length()));
477         replaceComponents(replacements);
478         return;
479     }
480 
481     String host = hostAndPort.substring(0, separator);
482     String port = parsePortFromStringPosition(hostAndPort, separator + 1);
483 
484     StringUTF8Adaptor hostUTF8(host);
485     StringUTF8Adaptor portUTF8(port);
486 
487     url_canon::Replacements<char> replacements;
488     replacements.SetHost(charactersOrEmpty(hostUTF8), url_parse::Component(0, hostUTF8.length()));
489     replacements.SetPort(charactersOrEmpty(portUTF8), url_parse::Component(0, portUTF8.length()));
490     replaceComponents(replacements);
491 }
492 
removePort()493 void KURL::removePort()
494 {
495     if (!hasPort())
496         return;
497     url_canon::Replacements<char> replacements;
498     replacements.ClearPort();
499     replaceComponents(replacements);
500 }
501 
setPort(const String & port)502 void KURL::setPort(const String& port)
503 {
504     String parsedPort = parsePortFromStringPosition(port, 0);
505     setPort(parsedPort.toUInt());
506 }
507 
setPort(unsigned short port)508 void KURL::setPort(unsigned short port)
509 {
510     if (isDefaultPortForProtocol(port, protocol())) {
511         removePort();
512         return;
513     }
514 
515     String portString = String::number(port);
516     ASSERT(portString.is8Bit());
517 
518     url_canon::Replacements<char> replacements;
519     replacements.SetPort(reinterpret_cast<const char*>(portString.characters8()), url_parse::Component(0, portString.length()));
520     replaceComponents(replacements);
521 }
522 
setUser(const String & user)523 void KURL::setUser(const String& user)
524 {
525     // This function is commonly called to clear the username, which we
526     // normally don't have, so we optimize this case.
527     if (user.isEmpty() && !m_parsed.username.is_valid())
528         return;
529 
530     // The canonicalizer will clear any usernames that are empty, so we
531     // don't have to explicitly call ClearUsername() here.
532     StringUTF8Adaptor userUTF8(user);
533     url_canon::Replacements<char> replacements;
534     replacements.SetUsername(charactersOrEmpty(userUTF8), url_parse::Component(0, userUTF8.length()));
535     replaceComponents(replacements);
536 }
537 
setPass(const String & pass)538 void KURL::setPass(const String& pass)
539 {
540     // This function is commonly called to clear the password, which we
541     // normally don't have, so we optimize this case.
542     if (pass.isEmpty() && !m_parsed.password.is_valid())
543         return;
544 
545     // The canonicalizer will clear any passwords that are empty, so we
546     // don't have to explicitly call ClearUsername() here.
547     StringUTF8Adaptor passUTF8(pass);
548     url_canon::Replacements<char> replacements;
549     replacements.SetPassword(charactersOrEmpty(passUTF8), url_parse::Component(0, passUTF8.length()));
550     replaceComponents(replacements);
551 }
552 
setFragmentIdentifier(const String & fragment)553 void KURL::setFragmentIdentifier(const String& fragment)
554 {
555     // This function is commonly called to clear the ref, which we
556     // normally don't have, so we optimize this case.
557     if (fragment.isNull() && !m_parsed.ref.is_valid())
558         return;
559 
560     StringUTF8Adaptor fragmentUTF8(fragment);
561 
562     url_canon::Replacements<char> replacements;
563     if (fragment.isNull())
564         replacements.ClearRef();
565     else
566         replacements.SetRef(charactersOrEmpty(fragmentUTF8), url_parse::Component(0, fragmentUTF8.length()));
567     replaceComponents(replacements);
568 }
569 
removeFragmentIdentifier()570 void KURL::removeFragmentIdentifier()
571 {
572     url_canon::Replacements<char> replacements;
573     replacements.ClearRef();
574     replaceComponents(replacements);
575 }
576 
setQuery(const String & query)577 void KURL::setQuery(const String& query)
578 {
579     StringUTF8Adaptor queryUTF8(query);
580     url_canon::Replacements<char> replacements;
581     if (query.isNull()) {
582         // KURL.cpp sets to null to clear any query.
583         replacements.ClearQuery();
584     } else if (query.length() > 0 && query[0] == '?') {
585         // WebCore expects the query string to begin with a question mark, but
586         // GoogleURL doesn't. So we trim off the question mark when setting.
587         replacements.SetQuery(charactersOrEmpty(queryUTF8), url_parse::Component(1, queryUTF8.length() - 1));
588     } else {
589         // When set with the empty string or something that doesn't begin with
590         // a question mark, KURL.cpp will add a question mark for you. The only
591         // way this isn't compatible is if you call this function with an empty
592         // string. KURL.cpp will leave a '?' with nothing following it in the
593         // URL, whereas we'll clear it.
594         // FIXME We should eliminate this difference.
595         replacements.SetQuery(charactersOrEmpty(queryUTF8), url_parse::Component(0, queryUTF8.length()));
596     }
597     replaceComponents(replacements);
598 }
599 
setPath(const String & path)600 void KURL::setPath(const String& path)
601 {
602     // Empty paths will be canonicalized to "/", so we don't have to worry
603     // about calling ClearPath().
604     StringUTF8Adaptor pathUTF8(path);
605     url_canon::Replacements<char> replacements;
606     replacements.SetPath(charactersOrEmpty(pathUTF8), url_parse::Component(0, pathUTF8.length()));
607     replaceComponents(replacements);
608 }
609 
decodeURLEscapeSequences(const String & string)610 String decodeURLEscapeSequences(const String& string)
611 {
612     return decodeURLEscapeSequences(string, UTF8Encoding());
613 }
614 
615 // In KURL.cpp's implementation, this is called by every component getter.
616 // It will unescape every character, including '\0'. This is scary, and may
617 // cause security holes. We never call this function for components, and
618 // just return the ASCII versions instead.
619 //
620 // This function is also used to decode javascript: URLs and as a general
621 // purpose unescaping function.
622 //
623 // FIXME These should be merged to the KURL.cpp implementation.
decodeURLEscapeSequences(const String & string,const WTF::TextEncoding & encoding)624 String decodeURLEscapeSequences(const String& string, const WTF::TextEncoding& encoding)
625 {
626     // FIXME We can probably use KURL.cpp's version of this function
627     // without modification. However, I'm concerned about
628     // https://bugs.webkit.org/show_bug.cgi?id=20559 so am keeping this old
629     // custom code for now. Using their version will also fix the bug that
630     // we ignore the encoding.
631     //
632     // FIXME b/1350291: This does not get called very often. We just convert
633     // first to 8-bit UTF-8, then unescape, then back to 16-bit. This kind of
634     // sucks, and we don't use the encoding properly, which will make some
635     // obscure anchor navigations fail.
636     StringUTF8Adaptor stringUTF8(string);
637     url_canon::RawCanonOutputT<url_parse::UTF16Char> unescaped;
638     url_util::DecodeURLEscapeSequences(stringUTF8.data(), stringUTF8.length(), &unescaped);
639     return StringImpl::create8BitIfPossible(reinterpret_cast<UChar*>(unescaped.data()), unescaped.length());
640 }
641 
encodeWithURLEscapeSequences(const String & notEncodedString)642 String encodeWithURLEscapeSequences(const String& notEncodedString)
643 {
644     CString utf8 = UTF8Encoding().normalizeAndEncode(notEncodedString, WTF::URLEncodedEntitiesForUnencodables);
645 
646     url_canon::RawCanonOutputT<char> buffer;
647     int inputLength = utf8.length();
648     if (buffer.length() < inputLength * 3)
649         buffer.Resize(inputLength * 3);
650 
651     url_util::EncodeURIComponent(utf8.data(), inputLength, &buffer);
652     String escaped(buffer.data(), buffer.length());
653     // Unescape '/'; it's safe and much prettier.
654     escaped.replace("%2F", "/");
655     return escaped;
656 }
657 
isHierarchical() const658 bool KURL::isHierarchical() const
659 {
660     if (m_string.isNull() || !m_parsed.scheme.is_nonempty())
661         return false;
662     return m_string.is8Bit() ?
663         url_util::IsStandard(asURLChar8Subtle(m_string), m_parsed.scheme) :
664         url_util::IsStandard(m_string.characters16(), m_parsed.scheme);
665 }
666 
667 #ifndef NDEBUG
print() const668 void KURL::print() const
669 {
670     printf("%s\n", m_string.utf8().data());
671 }
672 #endif
673 
equalIgnoringFragmentIdentifier(const KURL & a,const KURL & b)674 bool equalIgnoringFragmentIdentifier(const KURL& a, const KURL& b)
675 {
676     // Compute the length of each URL without its ref. Note that the reference
677     // begin (if it exists) points to the character *after* the '#', so we need
678     // to subtract one.
679     int aLength = a.m_string.length();
680     if (a.m_parsed.ref.len >= 0)
681         aLength = a.m_parsed.ref.begin - 1;
682 
683     int bLength = b.m_string.length();
684     if (b.m_parsed.ref.len >= 0)
685         bLength = b.m_parsed.ref.begin - 1;
686 
687     if (aLength != bLength)
688         return false;
689 
690     const String& aString = a.m_string;
691     const String& bString = b.m_string;
692     // FIXME: Abstraction this into a function in WTFString.h.
693     for (int i = 0; i < aLength; ++i) {
694         if (aString[i] != bString[i])
695             return false;
696     }
697     return true;
698 }
699 
hostStart() const700 unsigned KURL::hostStart() const
701 {
702     return m_parsed.CountCharactersBefore(url_parse::Parsed::HOST, false);
703 }
704 
hostEnd() const705 unsigned KURL::hostEnd() const
706 {
707     return m_parsed.CountCharactersBefore(url_parse::Parsed::PORT, true);
708 }
709 
pathStart() const710 unsigned KURL::pathStart() const
711 {
712     return m_parsed.CountCharactersBefore(url_parse::Parsed::PATH, false);
713 }
714 
pathEnd() const715 unsigned KURL::pathEnd() const
716 {
717     return m_parsed.CountCharactersBefore(url_parse::Parsed::QUERY, true);
718 }
719 
pathAfterLastSlash() const720 unsigned KURL::pathAfterLastSlash() const
721 {
722     if (m_string.isNull())
723         return 0;
724     if (!m_isValid || !m_parsed.path.is_valid())
725         return m_parsed.CountCharactersBefore(url_parse::Parsed::PATH, false);
726     url_parse::Component filename;
727     if (m_string.is8Bit())
728         url_parse::ExtractFileName(asURLChar8Subtle(m_string), m_parsed.path, &filename);
729     else
730         url_parse::ExtractFileName(m_string.characters16(), m_parsed.path, &filename);
731     return filename.begin;
732 }
733 
protocolIs(const String & url,const char * protocol)734 bool protocolIs(const String& url, const char* protocol)
735 {
736     assertProtocolIsGood(protocol);
737     if (url.isNull())
738         return false;
739     if (url.is8Bit())
740         return url_util::FindAndCompareScheme(asURLChar8Subtle(url), url.length(), protocol, 0);
741     return url_util::FindAndCompareScheme(url.characters16(), url.length(), protocol, 0);
742 }
743 
init(const KURL & base,const String & relative,const WTF::TextEncoding * queryEncoding)744 void KURL::init(const KURL& base, const String& relative, const WTF::TextEncoding* queryEncoding)
745 {
746     if (!relative.isNull() && relative.is8Bit()) {
747         StringUTF8Adaptor relativeUTF8(relative);
748         init(base, relativeUTF8.data(), relativeUTF8.length(), queryEncoding);
749     } else
750         init(base, relative.characters16(), relative.length(), queryEncoding);
751     initProtocolIsInHTTPFamily();
752     initInnerURL();
753 }
754 
755 template <typename CHAR>
init(const KURL & base,const CHAR * relative,int relativeLength,const WTF::TextEncoding * queryEncoding)756 void KURL::init(const KURL& base, const CHAR* relative, int relativeLength, const WTF::TextEncoding* queryEncoding)
757 {
758     // As a performance optimization, we do not use the charset converter
759     // if encoding is UTF-8 or other Unicode encodings. Note that this is
760     // per HTML5 2.5.3 (resolving URL). The URL canonicalizer will be more
761     // efficient with no charset converter object because it can do UTF-8
762     // internally with no extra copies.
763 
764     // We feel free to make the charset converter object every time since it's
765     // just a wrapper around a reference.
766     KURLCharsetConverter charsetConverterObject(queryEncoding);
767     KURLCharsetConverter* charsetConverter = (!queryEncoding || isUnicodeEncoding(queryEncoding)) ? 0 : &charsetConverterObject;
768 
769     StringUTF8Adaptor baseUTF8(base.string());
770 
771     url_canon::RawCanonOutputT<char> output;
772     m_isValid = url_util::ResolveRelative(baseUTF8.data(), baseUTF8.length(), base.m_parsed, relative, relativeLength, charsetConverter, &output, &m_parsed);
773 
774     // See FIXME in KURLPrivate in the header. If canonicalization has not
775     // changed the string, we can avoid an extra allocation by using assignment.
776     m_string = AtomicString::fromUTF8(output.data(), output.length());
777 }
778 
initInnerURL()779 void KURL::initInnerURL()
780 {
781     if (!m_isValid) {
782         m_innerURL.clear();
783         return;
784     }
785     if (url_parse::Parsed* innerParsed = m_parsed.inner_parsed())
786         m_innerURL = adoptPtr(new KURL(ParsedURLString, m_string.substring(innerParsed->scheme.begin, innerParsed->Length() - innerParsed->scheme.begin)));
787     else
788         m_innerURL.clear();
789 }
790 
791 template<typename CHAR>
internalProtocolIs(const url_parse::Component & scheme,const CHAR * spec,const char * protocol)792 bool internalProtocolIs(const url_parse::Component& scheme, const CHAR* spec, const char* protocol)
793 {
794     const CHAR* begin = spec + scheme.begin;
795     const CHAR* end = begin + scheme.len;
796 
797     while (begin != end && *protocol) {
798         ASSERT(toASCIILower(*protocol) == *protocol);
799         if (toASCIILower(*begin++) != *protocol++)
800             return false;
801     }
802 
803     // Both strings are equal (ignoring case) if and only if all of the characters were equal,
804     // and the end of both has been reached.
805     return begin == end && !*protocol;
806 }
807 
808 template<typename CHAR>
checkIfProtocolIsInHTTPFamily(const url_parse::Component & scheme,const CHAR * spec)809 bool checkIfProtocolIsInHTTPFamily(const url_parse::Component& scheme, const CHAR* spec)
810 {
811     if (scheme.len == 4)
812         return internalProtocolIs(scheme, spec, "http");
813     if (scheme.len == 5)
814         return internalProtocolIs(scheme, spec, "https");
815     return false;
816 }
817 
initProtocolIsInHTTPFamily()818 void KURL::initProtocolIsInHTTPFamily()
819 {
820     if (!m_isValid) {
821         m_protocolIsInHTTPFamily = false;
822         return;
823     }
824 
825     ASSERT(!m_string.isNull());
826     m_protocolIsInHTTPFamily = m_string.is8Bit() ?
827         checkIfProtocolIsInHTTPFamily(m_parsed.scheme, m_string.characters8()) :
828         checkIfProtocolIsInHTTPFamily(m_parsed.scheme, m_string.characters16());
829 }
830 
protocolIs(const char * protocol) const831 bool KURL::protocolIs(const char* protocol) const
832 {
833     assertProtocolIsGood(protocol);
834 
835     // JavaScript URLs are "valid" and should be executed even if KURL decides they are invalid.
836     // The free function protocolIsJavaScript() should be used instead.
837     // FIXME: Chromium code needs to be fixed for this assert to be enabled. ASSERT(strcmp(protocol, "javascript"));
838 
839     if (m_string.isNull() || m_parsed.scheme.len <= 0)
840         return *protocol == '\0';
841 
842     return m_string.is8Bit() ?
843         internalProtocolIs(m_parsed.scheme, m_string.characters8(), protocol) :
844         internalProtocolIs(m_parsed.scheme, m_string.characters16(), protocol);
845 }
846 
stringForInvalidComponent() const847 String KURL::stringForInvalidComponent() const
848 {
849     if (m_string.isNull())
850         return String();
851     return emptyString();
852 }
853 
componentString(const url_parse::Component & component) const854 String KURL::componentString(const url_parse::Component& component) const
855 {
856     if (!m_isValid || component.len <= 0)
857         return stringForInvalidComponent();
858     // begin and len are in terms of bytes which do not match
859     // if string() is UTF-16 and input contains non-ASCII characters.
860     // However, the only part in urlString that can contain non-ASCII
861     // characters is 'ref' at the end of the string. In that case,
862     // begin will always match the actual value and len (in terms of
863     // byte) will be longer than what's needed by 'mid'. However, mid
864     // truncates len to avoid go past the end of a string so that we can
865     // get away without doing anything here.
866     return string().substring(component.begin, component.len);
867 }
868 
869 template<typename CHAR>
replaceComponents(const url_canon::Replacements<CHAR> & replacements)870 void KURL::replaceComponents(const url_canon::Replacements<CHAR>& replacements)
871 {
872     url_canon::RawCanonOutputT<char> output;
873     url_parse::Parsed newParsed;
874 
875     StringUTF8Adaptor utf8(m_string);
876     m_isValid = url_util::ReplaceComponents(utf8.data(), utf8.length(), m_parsed, replacements, 0, &output, &newParsed);
877 
878     m_parsed = newParsed;
879     m_string = AtomicString::fromUTF8(output.data(), output.length());
880 }
881 
isSafeToSendToAnotherThread() const882 bool KURL::isSafeToSendToAnotherThread() const
883 {
884     return m_string.isSafeToSendToAnotherThread()
885         && (!m_innerURL || m_innerURL->isSafeToSendToAnotherThread());
886 }
887 
888 } // namespace WebCore
889