• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2011, 2012 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 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.
24  */
25 
26 #ifndef KURL_h
27 #define KURL_h
28 
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>
36 
37 namespace WTF {
38 class TextEncoding;
39 }
40 
41 namespace WebCore {
42 
43 struct KURLHash;
44 
45 enum ParsedURLStringTag { ParsedURLString };
46 
47 class PLATFORM_EXPORT KURL {
48 public:
49     KURL();
50     KURL(const KURL&);
51     KURL& operator=(const KURL&);
52 
53 #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES)
54     KURL(KURL&&);
55     KURL& operator=(KURL&&);
56 #endif
57 
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);
64 
65     // Creates an isolated URL object suitable for sending to another thread.
66     static KURL createIsolated(ParsedURLStringTag, const String&);
67 
isHashTableDeletedValue()68     bool isHashTableDeletedValue() const { return string().isHashTableDeletedValue(); }
69 
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
73     // absolute.
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&);
79 
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);
84 
85     String strippedForUseAsReferrer() const;
86 
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.
90 
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.
94     KURL copy() const;
95 
96     bool isNull() const;
97     bool isEmpty() const;
98     bool isValid() const;
99 
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;
104 
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.
canSetHostOrPort()107     bool canSetHostOrPort() const { return isHierarchical(); }
108 
canSetPathname()109     bool canSetPathname() const { return isHierarchical(); }
110     bool isHierarchical() const;
111 
string()112     const String& string() const { return m_string; }
113 
114     String elidedString() const;
115 
116     String protocol() const;
117     String host() const;
118     unsigned short port() const;
119     bool hasPort() const;
120     String user() const;
121     String pass() const;
122     String path() const;
123     String lastPathComponent() const;
124     String query() const;
125     String fragmentIdentifier() const;
126     bool hasFragmentIdentifier() const;
127 
128     String baseAsString() const;
129 
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;
protocolIsData()133     bool protocolIsData() const { return protocolIs("data"); }
134     // This includes at least about:blank and about:srcdoc.
protocolIsAbout()135     bool protocolIsAbout() const { return protocolIs("about"); }
136     bool protocolIsInHTTPFamily() const;
137     bool isLocalFile() const;
138     bool isAboutBlankURL() const; // Is exactly about:blank.
139 
140     bool setProtocol(const String&);
141     void setHost(const String&);
142 
143     void removePort();
144     void setPort(unsigned short);
145     void setPort(const String&);
146 
147     // Input is like "foo.com" or "foo.com:8000".
148     void setHostAndPort(const String&);
149 
150     void setUser(const String&);
151     void setPass(const String&);
152 
153     // If you pass an empty path for HTTP or HTTPS URLs, the resulting path
154     // will be "/".
155     void setPath(const String&);
156 
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&);
161 
162     void setFragmentIdentifier(const String&);
163     void removeFragmentIdentifier();
164 
165     PLATFORM_EXPORT friend bool equalIgnoringFragmentIdentifier(const KURL&, const KURL&);
166 
167     unsigned hostStart() const;
168     unsigned hostEnd() const;
169 
170     unsigned pathStart() const;
171     unsigned pathEnd() const;
172     unsigned pathAfterLastSlash() const;
173 
174     operator const String&() const { return string(); }
175 
parsed()176     const url::Parsed& parsed() const { return m_parsed; }
177 
innerURL()178     const KURL* innerURL() const { return m_innerURL.get(); }
179 
180 #ifndef NDEBUG
181     void print() const;
182 #endif
183 
184     bool isSafeToSendToAnotherThread() const;
185 
186 private:
187     void init(const KURL& base, const String& relative, const WTF::TextEncoding* queryEncoding);
188 
189     String componentString(const url::Component&) const;
190     String stringForInvalidComponent() const;
191 
192     template<typename CHAR>
193     void replaceComponents(const url::Replacements<CHAR>&);
194 
195     template <typename CHAR>
196     void init(const KURL& base, const CHAR* relative, int relativeLength, const WTF::TextEncoding* queryEncoding);
197     void initInnerURL();
198     void initProtocolIsInHTTPFamily();
199 
200     bool m_isValid;
201     bool m_protocolIsInHTTPFamily;
202     url::Parsed m_parsed;
203     String m_string;
204     OwnPtr<KURL> m_innerURL;
205 };
206 
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&);
213 
214 PLATFORM_EXPORT bool equalIgnoringFragmentIdentifier(const KURL&, const KURL&);
215 
216 PLATFORM_EXPORT const KURL& blankURL();
217 
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.
222 
223 PLATFORM_EXPORT bool protocolIs(const String& url, const char* protocol);
224 PLATFORM_EXPORT bool protocolIsJavaScript(const String& url);
225 
226 PLATFORM_EXPORT bool isValidProtocol(const String&);
227 
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&);
233 
234 PLATFORM_EXPORT String encodeWithURLEscapeSequences(const String&);
235 
236 // Inlines.
237 
238 inline bool operator==(const KURL& a, const KURL& b)
239 {
240     return a.string() == b.string();
241 }
242 
243 inline bool operator==(const KURL& a, const String& b)
244 {
245     return a.string() == b;
246 }
247 
248 inline bool operator==(const String& a, const KURL& b)
249 {
250     return a == b.string();
251 }
252 
253 inline bool operator!=(const KURL& a, const KURL& b)
254 {
255     return a.string() != b.string();
256 }
257 
258 inline bool operator!=(const KURL& a, const String& b)
259 {
260     return a.string() != b;
261 }
262 
263 inline bool operator!=(const String& a, const KURL& b)
264 {
265     return a != b.string();
266 }
267 
268 } // namespace WebCore
269 
270 namespace WTF {
271 
272 // KURLHash is the default hash for String
273 template<typename T> struct DefaultHash;
274 template<> struct DefaultHash<WebCore::KURL> {
275     typedef WebCore::KURLHash Hash;
276 };
277 
278 } // namespace WTF
279 
280 #endif // KURL_h
281