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 "KURLWTFURLImpl.h"
30 #include <wtf/Forward.h>
31 #include <wtf/HashMap.h>
32 #include <wtf/text/WTFString.h>
35 typedef const struct __CFURL* CFURLRef;
38 #if PLATFORM(MAC) || (PLATFORM(QT) && USE(QTKIT))
49 #include "KURLGooglePrivate.h"
57 enum ParsedURLStringTag { ParsedURLString };
61 // Generates a URL which contains a null string.
62 KURL() { invalidate(); }
64 // The argument is an absolute URL string. The string is assumed to be output of KURL::string() called on a valid
65 // KURL object, or indiscernible from such.
66 // It is usually best to avoid repeatedly parsing a string, unless memory saving outweigh the possible slow-downs.
67 KURL(ParsedURLStringTag, const String&);
69 explicit KURL(WTF::HashTableDeletedValueType) : m_url(WTF::HashTableDeletedValue) { }
71 explicit KURL(WTF::HashTableDeletedValueType) : m_urlImpl(WTF::HashTableDeletedValue) { }
73 explicit KURL(WTF::HashTableDeletedValueType) : m_string(WTF::HashTableDeletedValue) { }
76 bool isHashTableDeletedValue() const { return string().isHashTableDeletedValue(); }
78 bool isHashTableDeletedValue() const { return m_urlImpl.isHashTableDeletedValue(); }
81 // Resolves the relative URL with the given base URL. If provided, the
82 // TextEncoding is used to encode non-ASCII characers. The base URL can be
83 // null or empty, in which case the relative URL will be interpreted as
85 // FIXME: If the base URL is invalid, this always creates an invalid
86 // URL. Instead I think it would be better to treat all invalid base URLs
87 // the same way we treate null and empty base URLs.
88 KURL(const KURL& base, const String& relative);
89 KURL(const KURL& base, const String& relative, const TextEncoding&);
92 // For conversions from other structures that have already parsed and
93 // canonicalized the URL. The input must be exactly what KURL would have
94 // done with the same input.
95 KURL(const CString& canonicalSpec, const url_parse::Parsed&, bool isValid);
98 String strippedForUseAsReferrer() const;
100 // FIXME: The above functions should be harmonized so that passing a
101 // base of null or the empty string gives the same result as the
102 // standard String constructor.
104 // Makes a deep copy. Helpful only if you need to use a KURL on another
105 // thread. Since the underlying StringImpl objects are immutable, there's
106 // no other reason to ever prefer copy() over plain old assignment.
110 bool isEmpty() const;
111 bool isValid() const;
113 // Returns true if this URL has a path. Note that "http://foo.com/" has a
114 // path of "/", so this function will return true. Only invalid or
115 // non-hierarchical (like "javascript:") URLs will have no path.
116 bool hasPath() const;
118 // Returns true if you can set the host and port for the URL.
119 // Non-hierarchical URLs don't have a host and port.
120 bool canSetHostOrPort() const { return isHierarchical(); }
122 bool canSetPathname() const { return isHierarchical(); }
125 const String& string() const { return m_url.string(); }
127 // FIXME: Split this in URLString and InvalidURLString, get rid of the implicit conversions.
128 const String& string() const;
130 const String& string() const { return m_string; }
133 String protocol() const;
135 unsigned short port() const;
136 bool hasPort() const;
140 String lastPathComponent() const;
141 String query() const;
142 String fragmentIdentifier() const;
143 bool hasFragmentIdentifier() const;
145 String baseAsString() const;
147 String fileSystemPath() const;
149 // Returns true if the current URL's protocol is the same as the null-
150 // terminated ASCII argument. The argument must be lower-case.
151 bool protocolIs(const char*) const;
152 bool protocolIsData() const { return protocolIs("data"); }
153 bool protocolIsInHTTPFamily() const;
154 bool isLocalFile() const;
155 bool isBlankURL() const;
157 bool setProtocol(const String&);
158 void setHost(const String&);
161 void setPort(unsigned short);
163 // Input is like "foo.com" or "foo.com:8000".
164 void setHostAndPort(const String&);
166 void setUser(const String&);
167 void setPass(const String&);
169 // If you pass an empty path for HTTP or HTTPS URLs, the resulting path
171 void setPath(const String&);
173 // The query may begin with a question mark, or, if not, one will be added
174 // for you. Setting the query to the empty string will leave a "?" in the
175 // URL (with nothing after it). To clear the query, pass a null string.
176 void setQuery(const String&);
178 void setFragmentIdentifier(const String&);
179 void removeFragmentIdentifier();
181 friend bool equalIgnoringFragmentIdentifier(const KURL&, const KURL&);
183 friend bool protocolHostAndPortAreEqual(const KURL&, const KURL&);
185 unsigned hostStart() const;
186 unsigned hostEnd() const;
188 unsigned pathStart() const;
189 unsigned pathEnd() const;
190 unsigned pathAfterLastSlash() const;
192 operator const String&() const { return string(); }
196 CFURLRef createCFURL() const;
199 #if PLATFORM(MAC) || (PLATFORM(QT) && USE(QTKIT))
201 operator NSURL*() const;
204 operator NSString*() const { return string(); }
209 operator QUrl() const;
213 // Getters for the parsed structure and its corresponding 8-bit string.
214 const url_parse::Parsed& parsed() const { return m_url.m_parsed; }
215 const CString& utf8String() const { return m_url.utf8String(); }
220 const KURL* innerURL() const { return m_url.innerURL(); }
222 const KURL* innerURL() const { return 0; }
229 void reportMemoryUsage(MemoryObjectInfo*) const;
230 bool isSafeToSendToAnotherThread() const;
234 bool isHierarchical() const;
235 static bool protocolIs(const String&, const char*);
237 friend class KURLGooglePrivate;
238 KURLGooglePrivate m_url;
240 RefPtr<KURLWTFURLImpl> m_urlImpl;
241 #else // !USE(GOOGLEURL)
242 void init(const KURL&, const String&, const TextEncoding&);
243 void copyToBuffer(Vector<char, 512>& buffer) const;
245 // Parses the given URL. The originalString parameter allows for an
246 // optimization: When the source is the same as the fixed-up string,
247 // it will use the passed-in string instead of allocating a new one.
248 void parse(const String&);
249 void parse(const char* url, const String* originalString = 0);
253 bool m_protocolIsInHTTPFamily : 1;
261 int m_pathAfterLastSlash;
268 bool operator==(const KURL&, const KURL&);
269 bool operator==(const KURL&, const String&);
270 bool operator==(const String&, const KURL&);
271 bool operator!=(const KURL&, const KURL&);
272 bool operator!=(const KURL&, const String&);
273 bool operator!=(const String&, const KURL&);
275 bool equalIgnoringFragmentIdentifier(const KURL&, const KURL&);
276 bool protocolHostAndPortAreEqual(const KURL&, const KURL&);
278 const KURL& blankURL();
280 // Functions to do URL operations on strings.
281 // These are operations that aren't faster on a parsed URL.
282 // These are also different from the KURL functions in that they don't require the string to be a valid and parsable URL.
283 // This is especially important because valid javascript URLs are not necessarily considered valid by KURL.
285 bool protocolIs(const String& url, const char* protocol);
286 bool protocolIsJavaScript(const String& url);
288 bool isDefaultPortForProtocol(unsigned short port, const String& protocol);
289 bool portAllowed(const KURL&); // Blacklist ports that should never be used for Web resources.
291 bool isValidProtocol(const String&);
293 String mimeTypeFromDataURL(const String& url);
294 String mimeTypeFromURL(const KURL&);
296 // Unescapes the given string using URL escaping rules, given an optional
297 // encoding (defaulting to UTF-8 otherwise). DANGER: If the URL has "%00"
298 // in it, the resulting string will have embedded null characters!
299 String decodeURLEscapeSequences(const String&);
300 String decodeURLEscapeSequences(const String&, const TextEncoding&);
302 String encodeWithURLEscapeSequences(const String&);
306 inline bool operator==(const KURL& a, const KURL& b)
308 return a.string() == b.string();
311 inline bool operator==(const KURL& a, const String& b)
313 return a.string() == b;
316 inline bool operator==(const String& a, const KURL& b)
318 return a == b.string();
321 inline bool operator!=(const KURL& a, const KURL& b)
323 return a.string() != b.string();
326 inline bool operator!=(const KURL& a, const String& b)
328 return a.string() != b;
331 inline bool operator!=(const String& a, const KURL& b)
333 return a != b.string();
336 #if !USE(GOOGLEURL) && !USE(WTFURL)
338 // Inline versions of some non-GoogleURL functions so we can get inlining
339 // without having to have a lot of ugly ifdefs in the class definition.
341 inline bool KURL::isNull() const
343 return m_string.isNull();
346 inline bool KURL::isEmpty() const
348 return m_string.isEmpty();
351 inline bool KURL::isValid() const
356 inline bool KURL::hasPath() const
358 return m_pathEnd != m_portEnd;
361 inline bool KURL::hasPort() const
363 return m_hostEnd < m_portEnd;
366 inline bool KURL::protocolIsInHTTPFamily() const
368 return m_protocolIsInHTTPFamily;
371 inline unsigned KURL::hostStart() const
373 return (m_passwordEnd == m_userStart) ? m_passwordEnd : m_passwordEnd + 1;
376 inline unsigned KURL::hostEnd() const
381 inline unsigned KURL::pathStart() const
386 inline unsigned KURL::pathEnd() const
391 inline unsigned KURL::pathAfterLastSlash() const
393 return m_pathAfterLastSlash;
396 #endif // !USE(GOOGLEURL) && !USE(WTFURL)
398 } // namespace WebCore
402 // KURLHash is the default hash for String
403 template<typename T> struct DefaultHash;
404 template<> struct DefaultHash<WebCore::KURL> {
405 typedef WebCore::KURLHash Hash;