2 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 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;
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&);
98 // For conversions for other structures that have already parsed and
99 // canonicalized the URL. The input must be exactly what KURL would have
100 // done with the same input.
101 KURL(const CString& canonicalSpec,
102 const url_parse::Parsed& parsed, bool isValid);
105 String strippedForUseAsReferrer() const;
107 // FIXME: The above functions should be harmonized so that passing a
108 // base of null or the empty string gives the same result as the
109 // standard String constructor.
111 // Makes a deep copy. Helpful only if you need to use a KURL on another
112 // thread. Since the underlying StringImpl objects are immutable, there's
113 // no other reason to ever prefer copy() over plain old assignment.
117 bool isEmpty() const;
118 bool isValid() const;
120 // Returns true if this URL has a path. Note that "http://foo.com/" has a
121 // path of "/", so this function will return true. Only invalid or
122 // non-hierarchical (like "javascript:") URLs will have no path.
123 bool hasPath() const;
125 // Returns true if you can set the host and port for the URL.
126 // Non-hierarchical URLs don't have a host and port.
127 bool canSetHostOrPort() const { return isHierarchical(); }
129 bool canSetPathname() const { return isHierarchical(); }
132 const String& string() const { return m_url.string(); }
133 URLString urlString() const { return URLString(m_url.string()); }
135 const String& string() const { return m_string; }
136 URLString urlString() const { return URLString(m_string); }
139 String protocol() const;
141 unsigned short port() const;
142 bool hasPort() const;
146 String lastPathComponent() const;
147 String query() const;
148 String fragmentIdentifier() const;
149 bool hasFragmentIdentifier() const;
151 void copyParsedQueryTo(ParsedURLParameters&) const;
153 String baseAsString() const;
155 String prettyURL() 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 protocolInHTTPFamily() 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;
199 operator const String&() const { return string(); }
203 CFURLRef createCFURL() const;
208 operator NSURL*() const;
211 operator NSString*() const { return string(); }
216 operator QUrl() const;
220 // Getters for the parsed structure and its corresponding 8-bit string.
221 const url_parse::Parsed& parsed() const { return m_url.m_parsed; }
222 const CString& utf8String() const { return m_url.utf8String(); }
231 bool isHierarchical() const;
232 static bool protocolIs(const String&, const char*);
234 friend class KURLGooglePrivate;
235 void parse(const char* url, const String* originalString); // KURLMac calls this.
236 void copyToBuffer(Vector<char, 512>& buffer) const; // KURLCFNet uses this.
237 KURLGooglePrivate m_url;
238 #else // !USE(GOOGLEURL)
239 void init(const KURL&, const String&, const TextEncoding&);
240 void copyToBuffer(Vector<char, 512>& buffer) const;
242 // Parses the given URL. The originalString parameter allows for an
243 // optimization: When the source is the same as the fixed-up string,
244 // it will use the passed-in string instead of allocating a new one.
245 void parse(const String&);
246 void parse(const char* url, const String* originalString);
250 bool m_protocolInHTTPFamily : 1;
258 int m_pathAfterLastSlash;
265 bool operator==(const KURL&, const KURL&);
266 bool operator==(const KURL&, const String&);
267 bool operator==(const String&, const KURL&);
268 bool operator!=(const KURL&, const KURL&);
269 bool operator!=(const KURL&, const String&);
270 bool operator!=(const String&, const KURL&);
272 bool equalIgnoringFragmentIdentifier(const KURL&, const KURL&);
273 bool protocolHostAndPortAreEqual(const KURL&, const KURL&);
275 const KURL& blankURL();
277 // Functions to do URL operations on strings.
278 // These are operations that aren't faster on a parsed URL.
279 // These are also different from the KURL functions in that they don't require the string to be a valid and parsable URL.
280 // This is especially important because valid javascript URLs are not necessarily considered valid by KURL.
282 bool protocolIs(const String& url, const char* protocol);
283 bool protocolIsJavaScript(const String& url);
285 bool isDefaultPortForProtocol(unsigned short port, const String& protocol);
286 bool portAllowed(const KURL&); // Blacklist ports that should never be used for Web resources.
288 bool isValidProtocol(const String&);
290 String mimeTypeFromDataURL(const String& url);
292 // Unescapes the given string using URL escaping rules, given an optional
293 // encoding (defaulting to UTF-8 otherwise). DANGER: If the URL has "%00"
294 // in it, the resulting string will have embedded null characters!
295 String decodeURLEscapeSequences(const String&);
296 String decodeURLEscapeSequences(const String&, const TextEncoding&);
298 String encodeWithURLEscapeSequences(const String&);
302 inline bool operator==(const KURL& a, const KURL& b)
304 return a.string() == b.string();
307 inline bool operator==(const KURL& a, const String& b)
309 return a.string() == b;
312 inline bool operator==(const String& a, const KURL& b)
314 return a == b.string();
317 inline bool operator!=(const KURL& a, const KURL& b)
319 return a.string() != b.string();
322 inline bool operator!=(const KURL& a, const String& b)
324 return a.string() != b;
327 inline bool operator!=(const String& a, const KURL& b)
329 return a != b.string();
334 // Inline versions of some non-GoogleURL functions so we can get inlining
335 // without having to have a lot of ugly ifdefs in the class definition.
337 inline bool KURL::isNull() const
339 return m_string.isNull();
342 inline bool KURL::isEmpty() const
344 return m_string.isEmpty();
347 inline bool KURL::isValid() const
352 inline bool KURL::hasPort() const
354 return m_hostEnd < m_portEnd;
357 inline bool KURL::protocolInHTTPFamily() const
359 return m_protocolInHTTPFamily;
362 inline unsigned KURL::hostStart() const
364 return (m_passwordEnd == m_userStart) ? m_passwordEnd : m_passwordEnd + 1;
367 inline unsigned KURL::hostEnd() const
372 inline unsigned KURL::pathStart() const
377 inline unsigned KURL::pathEnd() const
382 inline unsigned KURL::pathAfterLastSlash() const
384 return m_pathAfterLastSlash;
387 #endif // !USE(GOOGLEURL)
389 } // namespace WebCore
393 // KURLHash is the default hash for String
394 template<typename T> struct DefaultHash;
395 template<> struct DefaultHash<WebCore::KURL> {
396 typedef WebCore::KURLHash Hash;