initial import
[vuplus_webkit] / Source / WebCore / platform / KURL.h
1 /*
2  * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2011 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 "PlatformString.h"
30 #include "URLString.h"
31 #include <wtf/HashMap.h>
32
33 #if USE(CF)
34 typedef const struct __CFURL* CFURLRef;
35 #endif
36
37 #if PLATFORM(MAC) || (PLATFORM(QT) && USE(QTKIT))
38 #ifdef __OBJC__
39 @class NSURL;
40 #else
41 class NSURL;
42 #endif
43 #endif
44
45 #if PLATFORM(QT)
46 QT_BEGIN_NAMESPACE
47 class QUrl;
48 QT_END_NAMESPACE
49 #endif
50
51 #if USE(GOOGLEURL)
52 #include "KURLGooglePrivate.h"
53 #endif
54
55 #if USE(JSC)
56 #include <runtime/UString.h>
57 #endif
58
59 namespace WebCore {
60
61 class TextEncoding;
62 struct KURLHash;
63
64 typedef HashMap<String, String> ParsedURLParameters;
65
66 enum ParsedURLStringTag { ParsedURLString };
67
68 class KURL {
69 public:
70     // Generates a URL which contains a null string.
71     KURL() { invalidate(); }
72
73     // The argument is an absolute URL string. The string is assumed to be output of KURL::string() called on a valid
74     // KURL object, or indiscernible from such.
75     // It is usually best to avoid repeatedly parsing a string, unless memory saving outweigh the possible slow-downs.
76     KURL(ParsedURLStringTag, const char*);
77     KURL(ParsedURLStringTag, const String&);
78     KURL(ParsedURLStringTag, const URLString&);
79 #if USE(GOOGLEURL)
80     KURL(WTF::HashTableDeletedValueType) : m_url(WTF::HashTableDeletedValue) { }
81 #else
82     KURL(WTF::HashTableDeletedValueType) : m_string(WTF::HashTableDeletedValue) { }
83 #endif
84     bool isHashTableDeletedValue() const { return string().isHashTableDeletedValue(); }
85
86     // Resolves the relative URL with the given base URL. If provided, the
87     // TextEncoding is used to encode non-ASCII characers. The base URL can be
88     // null or empty, in which case the relative URL will be interpreted as
89     // absolute.
90     // FIXME: If the base URL is invalid, this always creates an invalid
91     // URL. Instead I think it would be better to treat all invalid base URLs
92     // the same way we treate null and empty base URLs.
93     KURL(const KURL& base, const String& relative);
94     KURL(const KURL& base, const String& relative, const TextEncoding&);
95
96 #if USE(GOOGLEURL)
97     // For conversions from other structures that have already parsed and
98     // canonicalized the URL. The input must be exactly what KURL would have
99     // done with the same input.
100     KURL(const CString& canonicalSpec, const url_parse::Parsed&, bool isValid);
101 #endif
102
103     String strippedForUseAsReferrer() const;
104
105     // FIXME: The above functions should be harmonized so that passing a
106     // base of null or the empty string gives the same result as the
107     // standard String constructor.
108
109     // Makes a deep copy. Helpful only if you need to use a KURL on another
110     // thread.  Since the underlying StringImpl objects are immutable, there's
111     // no other reason to ever prefer copy() over plain old assignment.
112     KURL copy() const;
113
114     bool isNull() const;
115     bool isEmpty() const;
116     bool isValid() const;
117
118     // Returns true if this URL has a path. Note that "http://foo.com/" has a
119     // path of "/", so this function will return true. Only invalid or
120     // non-hierarchical (like "javascript:") URLs will have no path.
121     bool hasPath() const;
122
123     // Returns true if you can set the host and port for the URL.
124     // Non-hierarchical URLs don't have a host and port.
125     bool canSetHostOrPort() const { return isHierarchical(); }
126
127     bool canSetPathname() const { return isHierarchical(); }
128
129 #if USE(GOOGLEURL)
130     const String& string() const { return m_url.string(); }
131     URLString urlString() const { return URLString(m_url.string()); }
132 #else
133     const String& string() const { return m_string; }
134     URLString urlString() const { return URLString(m_string); }
135 #endif
136
137     String protocol() const;
138     String host() const;
139     unsigned short port() const;
140     bool hasPort() const;
141     String user() const;
142     String pass() const;
143     String path() const;
144     String lastPathComponent() const;
145     String query() const;
146     String fragmentIdentifier() const;
147     bool hasFragmentIdentifier() const;
148
149     void copyParsedQueryTo(ParsedURLParameters&) const;
150
151     String baseAsString() const;
152
153     // This function is only used by location.href. It's likely we shouldn't
154     // use it for that purpose, but more study is necessary before we remove it.
155     String deprecatedString() const;
156     String fileSystemPath() const;
157
158     // Returns true if the current URL's protocol is the same as the null-
159     // terminated ASCII argument. The argument must be lower-case.
160     bool protocolIs(const char*) const;
161     bool protocolIsData() const { return protocolIs("data"); }
162     bool protocolIsInHTTPFamily() const;
163     bool isLocalFile() const;
164
165     bool setProtocol(const String&);
166     void setHost(const String&);
167
168     void removePort();
169     void setPort(unsigned short);
170
171     // Input is like "foo.com" or "foo.com:8000".
172     void setHostAndPort(const String&);
173
174     void setUser(const String&);
175     void setPass(const String&);
176
177     // If you pass an empty path for HTTP or HTTPS URLs, the resulting path
178     // will be "/".
179     void setPath(const String&);
180
181     // The query may begin with a question mark, or, if not, one will be added
182     // for you. Setting the query to the empty string will leave a "?" in the
183     // URL (with nothing after it). To clear the query, pass a null string.
184     void setQuery(const String&);
185
186     void setFragmentIdentifier(const String&);
187     void removeFragmentIdentifier();
188
189     friend bool equalIgnoringFragmentIdentifier(const KURL&, const KURL&);
190
191     friend bool protocolHostAndPortAreEqual(const KURL&, const KURL&);
192
193     unsigned hostStart() const;
194     unsigned hostEnd() const;
195
196     unsigned pathStart() const;
197     unsigned pathEnd() const;
198     unsigned pathAfterLastSlash() const;
199
200     operator const String&() const { return string(); }
201
202 #if USE(CF)
203     KURL(CFURLRef);
204     CFURLRef createCFURL() const;
205 #endif
206
207 #if PLATFORM(MAC) || (PLATFORM(QT) && USE(QTKIT))
208     KURL(NSURL*);
209     operator NSURL*() const;
210 #endif
211 #ifdef __OBJC__
212     operator NSString*() const { return string(); }
213 #endif
214
215 #if PLATFORM(QT)
216     KURL(const QUrl&);
217     operator QUrl() const;
218 #endif
219
220 #if USE(GOOGLEURL)
221     // Getters for the parsed structure and its corresponding 8-bit string.
222     const url_parse::Parsed& parsed() const { return m_url.m_parsed; }
223     const CString& utf8String() const { return m_url.utf8String(); }
224 #endif
225
226 #ifndef NDEBUG
227     void print() const;
228 #endif
229
230     // FIXME: Remove this after changing all callers to use protocolIsInHTTPFamily.
231     bool protocolInHTTPFamily() const { return protocolIsInHTTPFamily(); }
232
233 private:
234     void invalidate();
235     bool isHierarchical() const;
236     static bool protocolIs(const String&, const char*);
237 #if USE(GOOGLEURL)
238     friend class KURLGooglePrivate;
239     void parse(const char* url, const String* originalString);  // KURLMac calls this.
240     void copyToBuffer(Vector<char, 512>& buffer) const;  // KURLCFNet uses this.
241     KURLGooglePrivate m_url;
242 #else  // !USE(GOOGLEURL)
243     void init(const KURL&, const String&, const TextEncoding&);
244     void copyToBuffer(Vector<char, 512>& buffer) const;
245
246     // Parses the given URL. The originalString parameter allows for an
247     // optimization: When the source is the same as the fixed-up string,
248     // it will use the passed-in string instead of allocating a new one.
249     void parse(const String&);
250     void parse(const char* url, const String* originalString);
251
252     String m_string;
253     bool m_isValid : 1;
254     bool m_protocolIsInHTTPFamily : 1;
255
256     int m_schemeEnd;
257     int m_userStart;
258     int m_userEnd;
259     int m_passwordEnd;
260     int m_hostEnd;
261     int m_portEnd;
262     int m_pathAfterLastSlash;
263     int m_pathEnd;
264     int m_queryEnd;
265     int m_fragmentEnd;
266 #endif
267 };
268
269 bool operator==(const KURL&, const KURL&);
270 bool operator==(const KURL&, const String&);
271 bool operator==(const String&, const KURL&);
272 bool operator!=(const KURL&, const KURL&);
273 bool operator!=(const KURL&, const String&);
274 bool operator!=(const String&, const KURL&);
275
276 bool equalIgnoringFragmentIdentifier(const KURL&, const KURL&);
277 bool protocolHostAndPortAreEqual(const KURL&, const KURL&);
278     
279 const KURL& blankURL();
280
281 // Functions to do URL operations on strings.
282 // These are operations that aren't faster on a parsed URL.
283 // These are also different from the KURL functions in that they don't require the string to be a valid and parsable URL.
284 // This is especially important because valid javascript URLs are not necessarily considered valid by KURL.
285
286 bool protocolIs(const String& url, const char* protocol);
287 bool protocolIsInHTTPFamily(const String& url);
288 bool protocolIsJavaScript(const String& url);
289
290 bool isDefaultPortForProtocol(unsigned short port, const String& protocol);
291 bool portAllowed(const KURL&); // Blacklist ports that should never be used for Web resources.
292
293 bool isValidProtocol(const String&);
294
295 String mimeTypeFromDataURL(const String& url);
296
297 // Unescapes the given string using URL escaping rules, given an optional
298 // encoding (defaulting to UTF-8 otherwise). DANGER: If the URL has "%00"
299 // in it, the resulting string will have embedded null characters!
300 String decodeURLEscapeSequences(const String&);
301 String decodeURLEscapeSequences(const String&, const TextEncoding&);
302
303 String encodeWithURLEscapeSequences(const String&);
304
305 // Inlines.
306
307 inline bool operator==(const KURL& a, const KURL& b)
308 {
309     return a.string() == b.string();
310 }
311
312 inline bool operator==(const KURL& a, const String& b)
313 {
314     return a.string() == b;
315 }
316
317 inline bool operator==(const String& a, const KURL& b)
318 {
319     return a == b.string();
320 }
321
322 inline bool operator!=(const KURL& a, const KURL& b)
323 {
324     return a.string() != b.string();
325 }
326
327 inline bool operator!=(const KURL& a, const String& b)
328 {
329     return a.string() != b;
330 }
331
332 inline bool operator!=(const String& a, const KURL& b)
333 {
334     return a != b.string();
335 }
336
337 #if !USE(GOOGLEURL)
338
339 // Inline versions of some non-GoogleURL functions so we can get inlining
340 // without having to have a lot of ugly ifdefs in the class definition.
341
342 inline bool KURL::isNull() const
343 {
344     return m_string.isNull();
345 }
346
347 inline bool KURL::isEmpty() const
348 {
349     return m_string.isEmpty();
350 }
351
352 inline bool KURL::isValid() const
353 {
354     return m_isValid;
355 }
356
357 inline bool KURL::hasPort() const
358 {
359     return m_hostEnd < m_portEnd;
360 }
361
362 inline bool KURL::protocolIsInHTTPFamily() const
363 {
364     return m_protocolIsInHTTPFamily;
365 }
366
367 inline unsigned KURL::hostStart() const
368 {
369     return (m_passwordEnd == m_userStart) ? m_passwordEnd : m_passwordEnd + 1;
370 }
371
372 inline unsigned KURL::hostEnd() const
373 {
374     return m_hostEnd;
375 }
376
377 inline unsigned KURL::pathStart() const
378 {
379     return m_portEnd;
380 }
381
382 inline unsigned KURL::pathEnd() const
383 {
384     return m_pathEnd;
385 }
386
387 inline unsigned KURL::pathAfterLastSlash() const
388 {
389     return m_pathAfterLastSlash;
390 }
391
392 #endif  // !USE(GOOGLEURL)
393
394 } // namespace WebCore
395
396 namespace WTF {
397
398     // KURLHash is the default hash for String
399     template<typename T> struct DefaultHash;
400     template<> struct DefaultHash<WebCore::KURL> {
401         typedef WebCore::KURLHash Hash;
402     };
403
404 } // namespace WTF
405
406 #endif // KURL_h