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
{
49 // This must be called during initialization (before we create
51 static void initialize();
55 KURL
& operator=(const KURL
&);
57 // The argument is an absolute URL string. The string is assumed to be
58 // output of KURL::string() called on a valid KURL object, or indiscernible
59 // from such. It is usually best to avoid repeatedly parsing a string,
60 // unless memory saving outweigh the possible slow-downs.
61 KURL(ParsedURLStringTag
, const String
&);
62 explicit KURL(WTF::HashTableDeletedValueType
);
64 // Creates an isolated URL object suitable for sending to another thread.
65 static KURL
createIsolated(ParsedURLStringTag
, const String
&);
67 bool isHashTableDeletedValue() const { return string().isHashTableDeletedValue(); }
69 // Resolves the relative URL with the given base URL. If provided, the
70 // TextEncoding is used to encode non-ASCII characers. The base URL can be
71 // null or empty, in which case the relative URL will be interpreted as
73 // FIXME: If the base URL is invalid, this always creates an invalid
74 // URL. Instead I think it would be better to treat all invalid base URLs
75 // the same way we treate null and empty base URLs.
76 KURL(const KURL
& base
, const String
& relative
);
77 KURL(const KURL
& base
, const String
& relative
, const WTF::TextEncoding
&);
79 // For conversions from other structures that have already parsed and
80 // canonicalized the URL. The input must be exactly what KURL would have
81 // done with the same input.
82 KURL(const AtomicString
& canonicalString
, const url::Parsed
&, bool isValid
);
86 String
strippedForUseAsReferrer() const;
87 String
strippedForUseAsHref() const;
89 // FIXME: The above functions should be harmonized so that passing a
90 // base of null or the empty string gives the same result as the
91 // standard String constructor.
93 // Makes a deep copy. Helpful only if you need to use a KURL on another
94 // thread. Since the underlying StringImpl objects are immutable, there's
95 // no other reason to ever prefer copy() over plain old assignment.
100 bool isValid() const;
102 // Returns true if this URL has a path. Note that "http://foo.com/" has a
103 // path of "/", so this function will return true. Only invalid or
104 // non-hierarchical (like "javascript:") URLs will have no path.
105 bool hasPath() const;
107 // Returns true if you can set the host and port for the URL.
108 // Non-hierarchical URLs don't have a host and port.
109 bool canSetHostOrPort() const { return isHierarchical(); }
111 bool canSetPathname() const { return isHierarchical(); }
112 bool isHierarchical() const;
114 const String
& string() const { return m_string
; }
116 String
elidedString() const;
118 String
protocol() const;
120 unsigned short port() const;
121 bool hasPort() const;
125 String
lastPathComponent() const;
126 String
query() const;
127 String
fragmentIdentifier() const;
128 bool hasFragmentIdentifier() const;
130 String
baseAsString() const;
132 // Returns true if the current URL's protocol is the same as the null-
133 // terminated ASCII argument. The argument must be lower-case.
134 bool protocolIs(const char*) const;
135 bool protocolIsData() const { return protocolIs("data"); }
136 // This includes at least about:blank and about:srcdoc.
137 bool protocolIsAbout() const { return protocolIs("about"); }
138 bool protocolIsInHTTPFamily() const;
139 bool isLocalFile() const;
140 bool isAboutBlankURL() const; // Is exactly about:blank.
142 bool setProtocol(const String
&);
143 void setHost(const String
&);
146 void setPort(unsigned short);
147 void setPort(const String
&);
149 // Input is like "foo.com" or "foo.com:8000".
150 void setHostAndPort(const String
&);
152 void setUser(const String
&);
153 void setPass(const String
&);
155 // If you pass an empty path for HTTP or HTTPS URLs, the resulting path
157 void setPath(const String
&);
159 // The query may begin with a question mark, or, if not, one will be added
160 // for you. Setting the query to the empty string will leave a "?" in the
161 // URL (with nothing after it). To clear the query, pass a null string.
162 void setQuery(const String
&);
164 void setFragmentIdentifier(const String
&);
165 void removeFragmentIdentifier();
167 PLATFORM_EXPORT
friend bool equalIgnoringFragmentIdentifier(const KURL
&, const KURL
&);
169 unsigned hostStart() const;
170 unsigned hostEnd() const;
172 unsigned pathStart() const;
173 unsigned pathEnd() const;
174 unsigned pathAfterLastSlash() const;
176 operator const String
&() const { return string(); }
178 const url::Parsed
& parsed() const { return m_parsed
; }
180 const KURL
* innerURL() const { return m_innerURL
.get(); }
182 bool isSafeToSendToAnotherThread() const;
185 void init(const KURL
& base
, const String
& relative
, const WTF::TextEncoding
* queryEncoding
);
187 String
componentString(const url::Component
&) const;
188 String
stringForInvalidComponent() const;
190 template<typename CHAR
>
191 void replaceComponents(const url::Replacements
<CHAR
>&);
193 template <typename CHAR
>
194 void init(const KURL
& base
, const CHAR
* relative
, int relativeLength
, const WTF::TextEncoding
* queryEncoding
);
196 void initProtocolIsInHTTPFamily();
199 bool m_protocolIsInHTTPFamily
;
200 url::Parsed m_parsed
;
202 OwnPtr
<KURL
> m_innerURL
;
205 PLATFORM_EXPORT
bool operator==(const KURL
&, const KURL
&);
206 PLATFORM_EXPORT
bool operator==(const KURL
&, const String
&);
207 PLATFORM_EXPORT
bool operator==(const String
&, const KURL
&);
208 PLATFORM_EXPORT
bool operator!=(const KURL
&, const KURL
&);
209 PLATFORM_EXPORT
bool operator!=(const KURL
&, const String
&);
210 PLATFORM_EXPORT
bool operator!=(const String
&, const KURL
&);
212 PLATFORM_EXPORT
bool equalIgnoringFragmentIdentifier(const KURL
&, const KURL
&);
214 PLATFORM_EXPORT
const KURL
& blankURL();
216 // Functions to do URL operations on strings.
217 // These are operations that aren't faster on a parsed URL.
218 // These are also different from the KURL functions in that they don't require the string to be a valid and parsable URL.
219 // This is especially important because valid javascript URLs are not necessarily considered valid by KURL.
221 PLATFORM_EXPORT
bool protocolIs(const String
& url
, const char* protocol
);
222 PLATFORM_EXPORT
bool protocolIsJavaScript(const String
& url
);
224 PLATFORM_EXPORT
bool isValidProtocol(const String
&);
226 // Unescapes the given string using URL escaping rules, given an optional
227 // encoding (defaulting to UTF-8 otherwise). DANGER: If the URL has "%00"
228 // in it, the resulting string will have embedded null characters!
229 PLATFORM_EXPORT String
decodeURLEscapeSequences(const String
&);
230 PLATFORM_EXPORT String
decodeURLEscapeSequences(const String
&, const WTF::TextEncoding
&);
232 PLATFORM_EXPORT String
encodeWithURLEscapeSequences(const String
&);
236 inline bool operator==(const KURL
& a
, const KURL
& b
)
238 return a
.string() == b
.string();
241 inline bool operator==(const KURL
& a
, const String
& b
)
243 return a
.string() == b
;
246 inline bool operator==(const String
& a
, const KURL
& b
)
248 return a
== b
.string();
251 inline bool operator!=(const KURL
& a
, const KURL
& b
)
253 return a
.string() != b
.string();
256 inline bool operator!=(const KURL
& a
, const String
& b
)
258 return a
.string() != b
;
261 inline bool operator!=(const String
& a
, const KURL
& b
)
263 return a
!= b
.string();
270 // KURLHash is the default hash for String
271 template<> struct DefaultHash
<blink::KURL
> {
272 typedef blink::KURLHash Hash
;