2 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2011 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 "PlatformString.h"
30 #include "URLString.h"
31 #include <wtf/HashMap.h>
34 typedef const struct __CFURL* CFURLRef;
37 #if PLATFORM(MAC) || (PLATFORM(QT) && USE(QTKIT))
52 #include "KURLGooglePrivate.h"
56 #include <runtime/UString.h>
64 typedef HashMap<String, String> ParsedURLParameters;
66 enum ParsedURLStringTag { ParsedURLString };
70 // Generates a URL which contains a null string.
71 KURL() { invalidate(); }
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&);
80 KURL(WTF::HashTableDeletedValueType) : m_url(WTF::HashTableDeletedValue) { }
82 KURL(WTF::HashTableDeletedValueType) : m_string(WTF::HashTableDeletedValue) { }
84 bool isHashTableDeletedValue() const { return string().isHashTableDeletedValue(); }
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
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&);
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);
103 String strippedForUseAsReferrer() const;
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.
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.
115 bool isEmpty() const;
116 bool isValid() const;
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;
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(); }
127 bool canSetPathname() const { return isHierarchical(); }
130 const String& string() const { return m_url.string(); }
131 URLString urlString() const { return URLString(m_url.string()); }
133 const String& string() const { return m_string; }
134 URLString urlString() const { return URLString(m_string); }
137 String protocol() const;
139 unsigned short port() const;
140 bool hasPort() const;
144 String lastPathComponent() const;
145 String query() const;
146 String fragmentIdentifier() const;
147 bool hasFragmentIdentifier() const;
149 void copyParsedQueryTo(ParsedURLParameters&) const;
151 String baseAsString() const;
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;
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;
165 bool setProtocol(const String&);
166 void setHost(const String&);
169 void setPort(unsigned short);
171 // Input is like "foo.com" or "foo.com:8000".
172 void setHostAndPort(const String&);
174 void setUser(const String&);
175 void setPass(const String&);
177 // If you pass an empty path for HTTP or HTTPS URLs, the resulting path
179 void setPath(const String&);
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&);
186 void setFragmentIdentifier(const String&);
187 void removeFragmentIdentifier();
189 friend bool equalIgnoringFragmentIdentifier(const KURL&, const KURL&);
191 friend bool protocolHostAndPortAreEqual(const KURL&, const KURL&);
193 unsigned hostStart() const;
194 unsigned hostEnd() const;
196 unsigned pathStart() const;
197 unsigned pathEnd() const;
198 unsigned pathAfterLastSlash() const;
200 operator const String&() const { return string(); }
204 CFURLRef createCFURL() const;
207 #if PLATFORM(MAC) || (PLATFORM(QT) && USE(QTKIT))
209 operator NSURL*() const;
212 operator NSString*() const { return string(); }
217 operator QUrl() const;
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(); }
230 // FIXME: Remove this after changing all callers to use protocolIsInHTTPFamily.
231 bool protocolInHTTPFamily() const { return protocolIsInHTTPFamily(); }
235 bool isHierarchical() const;
236 static bool protocolIs(const String&, const char*);
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;
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);
254 bool m_protocolIsInHTTPFamily : 1;
262 int m_pathAfterLastSlash;
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&);
276 bool equalIgnoringFragmentIdentifier(const KURL&, const KURL&);
277 bool protocolHostAndPortAreEqual(const KURL&, const KURL&);
279 const KURL& blankURL();
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.
286 bool protocolIs(const String& url, const char* protocol);
287 bool protocolIsInHTTPFamily(const String& url);
288 bool protocolIsJavaScript(const String& url);
290 bool isDefaultPortForProtocol(unsigned short port, const String& protocol);
291 bool portAllowed(const KURL&); // Blacklist ports that should never be used for Web resources.
293 bool isValidProtocol(const String&);
295 String mimeTypeFromDataURL(const String& url);
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&);
303 String encodeWithURLEscapeSequences(const String&);
307 inline bool operator==(const KURL& a, const KURL& b)
309 return a.string() == b.string();
312 inline bool operator==(const KURL& a, const String& b)
314 return a.string() == b;
317 inline bool operator==(const String& a, const KURL& b)
319 return a == b.string();
322 inline bool operator!=(const KURL& a, const KURL& b)
324 return a.string() != b.string();
327 inline bool operator!=(const KURL& a, const String& b)
329 return a.string() != b;
332 inline bool operator!=(const String& a, const KURL& b)
334 return a != b.string();
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.
342 inline bool KURL::isNull() const
344 return m_string.isNull();
347 inline bool KURL::isEmpty() const
349 return m_string.isEmpty();
352 inline bool KURL::isValid() const
357 inline bool KURL::hasPort() const
359 return m_hostEnd < m_portEnd;
362 inline bool KURL::protocolIsInHTTPFamily() const
364 return m_protocolIsInHTTPFamily;
367 inline unsigned KURL::hostStart() const
369 return (m_passwordEnd == m_userStart) ? m_passwordEnd : m_passwordEnd + 1;
372 inline unsigned KURL::hostEnd() const
377 inline unsigned KURL::pathStart() const
382 inline unsigned KURL::pathEnd() const
387 inline unsigned KURL::pathAfterLastSlash() const
389 return m_pathAfterLastSlash;
392 #endif // !USE(GOOGLEURL)
394 } // namespace WebCore
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;