2 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2011, 2012 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "platform/PlatformExport.h"
30 #include "wtf/Forward.h"
31 #include "wtf/HashTableDeletedValueType.h"
32 #include "wtf/OwnPtr.h"
33 #include "wtf/text/WTFString.h"
34 #include <url/third_party/mozilla/url_parse.h>
35 #include <url/url_canon.h>
45 enum ParsedURLStringTag { ParsedURLString };
47 class PLATFORM_EXPORT KURL {
51 KURL& operator=(const KURL&);
53 #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES)
55 KURL& operator=(KURL&&);
58 // The argument is an absolute URL string. The string is assumed to be
59 // output of KURL::string() called on a valid KURL object, or indiscernible
60 // from such. It is usually best to avoid repeatedly parsing a string,
61 // unless memory saving outweigh the possible slow-downs.
62 KURL(ParsedURLStringTag, const String&);
63 explicit KURL(WTF::HashTableDeletedValueType);
65 // Creates an isolated URL object suitable for sending to another thread.
66 static KURL createIsolated(ParsedURLStringTag, const String&);
68 bool isHashTableDeletedValue() const { return string().isHashTableDeletedValue(); }
70 // Resolves the relative URL with the given base URL. If provided, the
71 // TextEncoding is used to encode non-ASCII characers. The base URL can be
72 // null or empty, in which case the relative URL will be interpreted as
74 // FIXME: If the base URL is invalid, this always creates an invalid
75 // URL. Instead I think it would be better to treat all invalid base URLs
76 // the same way we treate null and empty base URLs.
77 KURL(const KURL& base, const String& relative);
78 KURL(const KURL& base, const String& relative, const WTF::TextEncoding&);
80 // For conversions from other structures that have already parsed and
81 // canonicalized the URL. The input must be exactly what KURL would have
82 // done with the same input.
83 KURL(const AtomicString& canonicalString, const url::Parsed&, bool isValid);
85 String strippedForUseAsReferrer() const;
87 // FIXME: The above functions should be harmonized so that passing a
88 // base of null or the empty string gives the same result as the
89 // standard String constructor.
91 // Makes a deep copy. Helpful only if you need to use a KURL on another
92 // thread. Since the underlying StringImpl objects are immutable, there's
93 // no other reason to ever prefer copy() over plain old assignment.
100 // Returns true if this URL has a path. Note that "http://foo.com/" has a
101 // path of "/", so this function will return true. Only invalid or
102 // non-hierarchical (like "javascript:") URLs will have no path.
103 bool hasPath() const;
105 // Returns true if you can set the host and port for the URL.
106 // Non-hierarchical URLs don't have a host and port.
107 bool canSetHostOrPort() const { return isHierarchical(); }
109 bool canSetPathname() const { return isHierarchical(); }
110 bool isHierarchical() const;
112 const String& string() const { return m_string; }
114 String elidedString() const;
116 String protocol() const;
118 unsigned short port() const;
119 bool hasPort() const;
123 String lastPathComponent() const;
124 String query() const;
125 String fragmentIdentifier() const;
126 bool hasFragmentIdentifier() const;
128 String baseAsString() const;
130 // Returns true if the current URL's protocol is the same as the null-
131 // terminated ASCII argument. The argument must be lower-case.
132 bool protocolIs(const char*) const;
133 bool protocolIsData() const { return protocolIs("data"); }
134 // This includes at least about:blank and about:srcdoc.
135 bool protocolIsAbout() const { return protocolIs("about"); }
136 bool protocolIsInHTTPFamily() const;
137 bool isLocalFile() const;
138 bool isAboutBlankURL() const; // Is exactly about:blank.
140 bool setProtocol(const String&);
141 void setHost(const String&);
144 void setPort(unsigned short);
145 void setPort(const String&);
147 // Input is like "foo.com" or "foo.com:8000".
148 void setHostAndPort(const String&);
150 void setUser(const String&);
151 void setPass(const String&);
153 // If you pass an empty path for HTTP or HTTPS URLs, the resulting path
155 void setPath(const String&);
157 // The query may begin with a question mark, or, if not, one will be added
158 // for you. Setting the query to the empty string will leave a "?" in the
159 // URL (with nothing after it). To clear the query, pass a null string.
160 void setQuery(const String&);
162 void setFragmentIdentifier(const String&);
163 void removeFragmentIdentifier();
165 PLATFORM_EXPORT friend bool equalIgnoringFragmentIdentifier(const KURL&, const KURL&);
167 unsigned hostStart() const;
168 unsigned hostEnd() const;
170 unsigned pathStart() const;
171 unsigned pathEnd() const;
172 unsigned pathAfterLastSlash() const;
174 operator const String&() const { return string(); }
176 const url::Parsed& parsed() const { return m_parsed; }
178 const KURL* innerURL() const { return m_innerURL.get(); }
184 bool isSafeToSendToAnotherThread() const;
187 void init(const KURL& base, const String& relative, const WTF::TextEncoding* queryEncoding);
189 String componentString(const url::Component&) const;
190 String stringForInvalidComponent() const;
192 template<typename CHAR>
193 void replaceComponents(const url::Replacements<CHAR>&);
195 template <typename CHAR>
196 void init(const KURL& base, const CHAR* relative, int relativeLength, const WTF::TextEncoding* queryEncoding);
198 void initProtocolIsInHTTPFamily();
201 bool m_protocolIsInHTTPFamily;
202 url::Parsed m_parsed;
204 OwnPtr<KURL> m_innerURL;
207 PLATFORM_EXPORT bool operator==(const KURL&, const KURL&);
208 PLATFORM_EXPORT bool operator==(const KURL&, const String&);
209 PLATFORM_EXPORT bool operator==(const String&, const KURL&);
210 PLATFORM_EXPORT bool operator!=(const KURL&, const KURL&);
211 PLATFORM_EXPORT bool operator!=(const KURL&, const String&);
212 PLATFORM_EXPORT bool operator!=(const String&, const KURL&);
214 PLATFORM_EXPORT bool equalIgnoringFragmentIdentifier(const KURL&, const KURL&);
216 PLATFORM_EXPORT const KURL& blankURL();
218 // Functions to do URL operations on strings.
219 // These are operations that aren't faster on a parsed URL.
220 // These are also different from the KURL functions in that they don't require the string to be a valid and parsable URL.
221 // This is especially important because valid javascript URLs are not necessarily considered valid by KURL.
223 PLATFORM_EXPORT bool protocolIs(const String& url, const char* protocol);
224 PLATFORM_EXPORT bool protocolIsJavaScript(const String& url);
226 PLATFORM_EXPORT bool isValidProtocol(const String&);
228 // Unescapes the given string using URL escaping rules, given an optional
229 // encoding (defaulting to UTF-8 otherwise). DANGER: If the URL has "%00"
230 // in it, the resulting string will have embedded null characters!
231 PLATFORM_EXPORT String decodeURLEscapeSequences(const String&);
232 PLATFORM_EXPORT String decodeURLEscapeSequences(const String&, const WTF::TextEncoding&);
234 PLATFORM_EXPORT String encodeWithURLEscapeSequences(const String&);
238 inline bool operator==(const KURL& a, const KURL& b)
240 return a.string() == b.string();
243 inline bool operator==(const KURL& a, const String& b)
245 return a.string() == b;
248 inline bool operator==(const String& a, const KURL& b)
250 return a == b.string();
253 inline bool operator!=(const KURL& a, const KURL& b)
255 return a.string() != b.string();
258 inline bool operator!=(const KURL& a, const String& b)
260 return a.string() != b;
263 inline bool operator!=(const String& a, const KURL& b)
265 return a != b.string();
272 // KURLHash is the default hash for String
273 template<> struct DefaultHash<blink::KURL> {
274 typedef blink::KURLHash Hash;