• 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:
KURL()49     KURL()
50         : m_isValid(false)
51         , m_protocolIsInHTTPFamily(false)
52     {
53     }
54 
55     KURL(const KURL&);
56     KURL& operator=(const KURL&);
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_parse::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     bool protocolIsInHTTPFamily() const;
135     bool isLocalFile() const;
136     bool isBlankURL() const;
137 
138     bool setProtocol(const String&);
139     void setHost(const String&);
140 
141     void removePort();
142     void setPort(unsigned short);
143     void setPort(const String&);
144 
145     // Input is like "foo.com" or "foo.com:8000".
146     void setHostAndPort(const String&);
147 
148     void setUser(const String&);
149     void setPass(const String&);
150 
151     // If you pass an empty path for HTTP or HTTPS URLs, the resulting path
152     // will be "/".
153     void setPath(const String&);
154 
155     // The query may begin with a question mark, or, if not, one will be added
156     // for you. Setting the query to the empty string will leave a "?" in the
157     // URL (with nothing after it). To clear the query, pass a null string.
158     void setQuery(const String&);
159 
160     void setFragmentIdentifier(const String&);
161     void removeFragmentIdentifier();
162 
163     PLATFORM_EXPORT friend bool equalIgnoringFragmentIdentifier(const KURL&, const KURL&);
164 
165     unsigned hostStart() const;
166     unsigned hostEnd() const;
167 
168     unsigned pathStart() const;
169     unsigned pathEnd() const;
170     unsigned pathAfterLastSlash() const;
171 
172     operator const String&() const { return string(); }
173 
parsed()174     const url_parse::Parsed& parsed() const { return m_parsed; }
175 
innerURL()176     const KURL* innerURL() const { return m_innerURL.get(); }
177 
178 #ifndef NDEBUG
179     void print() const;
180 #endif
181 
182     bool isSafeToSendToAnotherThread() const;
183 
184 private:
185     void init(const KURL& base, const String& relative, const WTF::TextEncoding* queryEncoding);
186 
187     String componentString(const url_parse::Component&) const;
188     String stringForInvalidComponent() const;
189 
190     template<typename CHAR>
191     void replaceComponents(const url_canon::Replacements<CHAR>&);
192 
193     template <typename CHAR>
194     void init(const KURL& base, const CHAR* relative, int relativeLength, const WTF::TextEncoding* queryEncoding);
195     void initInnerURL();
196     void initProtocolIsInHTTPFamily();
197 
198     bool m_isValid;
199     bool m_protocolIsInHTTPFamily;
200     url_parse::Parsed m_parsed;
201     String m_string;
202     OwnPtr<KURL> m_innerURL;
203 };
204 
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&);
211 
212 PLATFORM_EXPORT bool equalIgnoringFragmentIdentifier(const KURL&, const KURL&);
213 
214 PLATFORM_EXPORT const KURL& blankURL();
215 
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.
220 
221 PLATFORM_EXPORT bool protocolIs(const String& url, const char* protocol);
222 PLATFORM_EXPORT bool protocolIsJavaScript(const String& url);
223 
224 PLATFORM_EXPORT bool isValidProtocol(const String&);
225 
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&);
231 
232 PLATFORM_EXPORT String encodeWithURLEscapeSequences(const String&);
233 
234 // Inlines.
235 
236 inline bool operator==(const KURL& a, const KURL& b)
237 {
238     return a.string() == b.string();
239 }
240 
241 inline bool operator==(const KURL& a, const String& b)
242 {
243     return a.string() == b;
244 }
245 
246 inline bool operator==(const String& a, const KURL& b)
247 {
248     return a == b.string();
249 }
250 
251 inline bool operator!=(const KURL& a, const KURL& b)
252 {
253     return a.string() != b.string();
254 }
255 
256 inline bool operator!=(const KURL& a, const String& b)
257 {
258     return a.string() != b;
259 }
260 
261 inline bool operator!=(const String& a, const KURL& b)
262 {
263     return a != b.string();
264 }
265 
266 } // namespace WebCore
267 
268 namespace WTF {
269 
270 // KURLHash is the default hash for String
271 template<typename T> struct DefaultHash;
272 template<> struct DefaultHash<WebCore::KURL> {
273     typedef WebCore::KURLHash Hash;
274 };
275 
276 } // namespace WTF
277 
278 #endif // KURL_h
279