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"
32 typedef const struct __CFURL* CFURLRef;
50 #include "GoogleURLPrivate.h"
58 // FIXME: Our terminology here is a bit inconsistent. We refer to the part
59 // after the "#" as the "fragment" in some places and the "ref" in others.
60 // We should fix the terminology to match the URL and URI RFCs as closely
61 // as possible to resolve this.
65 // Generates a URL which contains a null string.
66 KURL() { invalidate(); }
68 // The argument is an absolute URL string. The string is assumed to be
70 // FIXME: This constructor has a special case for strings that start with
71 // "/", prepending "file://" to such strings; it would be good to get
72 // rid of that special case.
73 explicit KURL(const char*);
75 // The argument is an absolute URL string. The string is assumed to be
77 // FIXME: This constructor has a special case for strings that start with
78 // "/", prepending "file://" to such strings; it would be good to get
79 // rid of that special case.
80 explicit KURL(const String&);
82 // Resolves the relative URL with the given base URL. If provided, the
83 // TextEncoding is used to encode non-ASCII characers. The base URL can be
84 // null or empty, in which case the relative URL will be interpreted as
86 // FIXME: If the base URL is invalid, this always creates an invalid
87 // URL. Instead I think it would be better to treat all invalid base URLs
88 // the same way we treate null and empty base URLs.
89 KURL(const KURL& base, const String& relative);
90 KURL(const KURL& base, const String& relative, const TextEncoding&);
93 // For conversions for other structures that have already parsed and
94 // canonicalized the URL. The input must be exactly what KURL would have
95 // done with the same input.
96 KURL(const char* canonicalSpec, int canonicalSpecLen,
97 const url_parse::Parsed& parsed, bool isValid);
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;
119 const String& string() const { return m_url.string(); }
121 const String& string() const { return m_string; }
124 String protocol() const;
126 unsigned short port() const;
130 String lastPathComponent() const;
131 String query() const; // Includes the "?".
132 String ref() const; // Does *not* include the "#".
135 String prettyURL() const;
136 String fileSystemPath() const;
138 // Returns true if the current URL's protocol is the same as the null-
139 // terminated ASCII argument. The argument must be lower-case.
140 bool protocolIs(const char*) const;
141 bool isLocalFile() const;
143 void setProtocol(const String&);
144 void setHost(const String&);
146 // Setting the port to 0 will clear any port from the URL.
147 void setPort(unsigned short);
149 // Input is like "foo.com" or "foo.com:8000".
150 void setHostAndPort(const String&);
152 void setUser(const String&);
153 void setPass(const String&);
155 // If you pass an empty path for HTTP or HTTPS URLs, the resulting path
157 void setPath(const String&);
159 // The query may begin with a question mark, or, if not, one will be added
160 // for you. Setting the query to the empty string will leave a "?" in the
161 // URL (with nothing after it). To clear the query, pass a null string.
162 void setQuery(const String&);
164 void setRef(const String&);
167 friend bool equalIgnoringRef(const KURL&, const KURL&);
169 friend bool protocolHostAndPortAreEqual(const KURL&, const KURL&);
171 unsigned hostStart() const;
172 unsigned hostEnd() const;
174 unsigned pathStart() const;
175 unsigned pathEnd() const;
176 unsigned pathAfterLastSlash() const;
177 operator const String&() const { return string(); }
179 operator JSC::UString() const { return string(); }
184 CFURLRef createCFURL() const;
189 operator NSURL*() const;
192 operator NSString*() const { return string(); }
197 operator QUrl() const;
201 // Getters for the parsed structure and its corresponding 8-bit string.
202 const url_parse::Parsed& parsed() const { return m_url.m_parsed; }
203 const CString& utf8String() const { return m_url.utf8String(); }
212 bool isHierarchical() const;
213 static bool protocolIs(const String&, const char*);
215 friend class GoogleURLPrivate;
216 void parse(const char* url, const String* originalString); // KURLMac calls this.
217 void copyToBuffer(Vector<char, 512>& buffer) const; // KURLCFNet uses this.
218 GoogleURLPrivate m_url;
219 #else // !USE(GOOGLEURL)
220 void init(const KURL&, const String&, const TextEncoding&);
221 void copyToBuffer(Vector<char, 512>& buffer) const;
223 // Parses the given URL. The originalString parameter allows for an
224 // optimization: When the source is the same as the fixed-up string,
225 // it will use the passed-in string instead of allocating a new one.
226 void parse(const String&);
227 void parse(const char* url, const String* originalString);
237 int m_pathAfterLastSlash;
244 bool operator==(const KURL&, const KURL&);
245 bool operator==(const KURL&, const String&);
246 bool operator==(const String&, const KURL&);
247 bool operator!=(const KURL&, const KURL&);
248 bool operator!=(const KURL&, const String&);
249 bool operator!=(const String&, const KURL&);
251 bool equalIgnoringRef(const KURL&, const KURL&);
252 bool protocolHostAndPortAreEqual(const KURL&, const KURL&);
254 const KURL& blankURL();
256 // Functions to do URL operations on strings.
257 // These are operations that aren't faster on a parsed URL.
259 bool protocolIs(const String& url, const char* protocol);
261 String mimeTypeFromDataURL(const String& url);
263 // Unescapes the given string using URL escaping rules, given an optional
264 // encoding (defaulting to UTF-8 otherwise). DANGER: If the URL has "%00"
265 // in it, the resulting string will have embedded null characters!
266 String decodeURLEscapeSequences(const String&);
267 String decodeURLEscapeSequences(const String&, const TextEncoding&);
269 String encodeWithURLEscapeSequences(const String&);
273 inline bool operator==(const KURL& a, const KURL& b)
275 return a.string() == b.string();
278 inline bool operator==(const KURL& a, const String& b)
280 return a.string() == b;
283 inline bool operator==(const String& a, const KURL& b)
285 return a == b.string();
288 inline bool operator!=(const KURL& a, const KURL& b)
290 return a.string() != b.string();
293 inline bool operator!=(const KURL& a, const String& b)
295 return a.string() != b;
298 inline bool operator!=(const String& a, const KURL& b)
300 return a != b.string();
305 // Inline versions of some non-GoogleURL functions so we can get inlining
306 // without having to have a lot of ugly ifdefs in the class definition.
308 inline bool KURL::isNull() const
310 return m_string.isNull();
313 inline bool KURL::isEmpty() const
315 return m_string.isEmpty();
318 inline bool KURL::isValid() const
323 inline unsigned KURL::hostStart() const
325 return (m_passwordEnd == m_userStart) ? m_passwordEnd : m_passwordEnd + 1;
328 inline unsigned KURL::hostEnd() const
333 inline unsigned KURL::pathStart() const
338 inline unsigned KURL::pathEnd() const
343 inline unsigned KURL::pathAfterLastSlash() const
345 return m_pathAfterLastSlash;
348 #endif // !USE(GOOGLEURL)
350 } // namespace WebCore
354 // KURLHash is the default hash for String
355 template<typename T> struct DefaultHash;
356 template<> struct DefaultHash<WebCore::KURL> {
357 typedef WebCore::KURLHash Hash;