• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 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 
31 #if PLATFORM(CF)
32 typedef const struct __CFURL* CFURLRef;
33 #endif
34 
35 #if PLATFORM(MAC)
36 #ifdef __OBJC__
37 @class NSURL;
38 #else
39 class NSURL;
40 #endif
41 #endif
42 
43 #if PLATFORM(QT)
44 QT_BEGIN_NAMESPACE
45 class QUrl;
46 QT_END_NAMESPACE
47 #endif
48 
49 #if USE(GOOGLEURL)
50 #include "GoogleURLPrivate.h"
51 #endif
52 
53 namespace WebCore {
54 
55 class TextEncoding;
56 struct KURLHash;
57 
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.
62 
63 class KURL {
64 public:
65     // Generates a URL which contains a null string.
KURL()66     KURL() { invalidate(); }
67 
68     // The argument is an absolute URL string. The string is assumed to be
69     // an already encoded (ASCII-only) valid absolute URL.
70     explicit KURL(const char*);
71     explicit KURL(const String&);
72 
73     // Resolves the relative URL with the given base URL. If provided, the
74     // TextEncoding is used to encode non-ASCII characers. The base URL can be
75     // null or empty, in which case the relative URL will be interpreted as
76     // absolute.
77     // FIXME: If the base URL is invalid, this always creates an invalid
78     // URL. Instead I think it would be better to treat all invalid base URLs
79     // the same way we treate null and empty base URLs.
80     KURL(const KURL& base, const String& relative);
81     KURL(const KURL& base, const String& relative, const TextEncoding&);
82 
83 #if USE(GOOGLEURL)
84     // For conversions for other structures that have already parsed and
85     // canonicalized the URL. The input must be exactly what KURL would have
86     // done with the same input.
87     KURL(const char* canonicalSpec, int canonicalSpecLen,
88          const url_parse::Parsed& parsed, bool isValid);
89 #endif
90 
91     // FIXME: The above functions should be harmonized so that passing a
92     // base of null or the empty string gives the same result as the
93     // standard String constructor.
94 
95     // Makes a deep copy. Helpful only if you need to use a KURL on another
96     // thread.  Since the underlying StringImpl objects are immutable, there's
97     // no other reason to ever prefer copy() over plain old assignment.
98     KURL copy() const;
99 
100     bool isNull() const;
101     bool isEmpty() const;
102     bool isValid() const;
103 
104     // Returns true if this URL has a path. Note that "http://foo.com/" has a
105     // path of "/", so this function will return true. Only invalid or
106     // non-hierarchical (like "javascript:") URLs will have no path.
107     bool hasPath() const;
108 
109 #if USE(GOOGLEURL)
string()110     const String& string() const { return m_url.string(); }
111 #else
string()112     const String& string() const { return m_string; }
113 #endif
114 
115     String protocol() const;
116     String host() const;
117     unsigned short port() const;
118     String user() const;
119     String pass() const;
120     String path() const;
121     String lastPathComponent() const;
122     String query() const; // Includes the "?".
123     String ref() const; // Does *not* include the "#".
124     bool hasRef() const;
125 
126     String prettyURL() const;
127     String fileSystemPath() const;
128 
129     // Returns true if the current URL's protocol is the same as the null-
130     // terminated ASCII argument. The argument must be lower-case.
131     bool protocolIs(const char*) const;
132     bool isLocalFile() const;
133 
134     void setProtocol(const String&);
135     void setHost(const String&);
136 
137     // Setting the port to 0 will clear any port from the URL.
138     void setPort(unsigned short);
139 
140     // Input is like "foo.com" or "foo.com:8000".
141     void setHostAndPort(const String&);
142 
143     void setUser(const String&);
144     void setPass(const String&);
145 
146     // If you pass an empty path for HTTP or HTTPS URLs, the resulting path
147     // will be "/".
148     void setPath(const String&);
149 
150     // The query may begin with a question mark, or, if not, one will be added
151     // for you. Setting the query to the empty string will leave a "?" in the
152     // URL (with nothing after it). To clear the query, pass a null string.
153     void setQuery(const String&);
154 
155     void setRef(const String&);
156     void removeRef();
157 
158     friend bool equalIgnoringRef(const KURL&, const KURL&);
159 
160     friend bool protocolHostAndPortAreEqual(const KURL&, const KURL&);
161 
162     unsigned hostStart() const;
163     unsigned hostEnd() const;
164 
165     unsigned pathStart() const;
166     unsigned pathEnd() const;
167     unsigned pathAfterLastSlash() const;
168     operator const String&() const { return string(); }
169 #if USE(JSC)
UString()170     operator JSC::UString() const { return string(); }
171 #endif
172 
173 #if PLATFORM(CF)
174     KURL(CFURLRef);
175     CFURLRef createCFURL() const;
176 #endif
177 
178 #if PLATFORM(MAC)
179     KURL(NSURL*);
180     operator NSURL*() const;
181 #endif
182 #ifdef __OBJC__
183     operator NSString*() const { return string(); }
184 #endif
185 
186 #if PLATFORM(QT)
187     KURL(const QUrl&);
188     operator QUrl() const;
189 #endif
190 
191 #if USE(GOOGLEURL)
192     // Getters for the parsed structure and its corresponding 8-bit string.
parsed()193     const url_parse::Parsed& parsed() const { return m_url.m_parsed; }
utf8String()194     const CString& utf8String() const { return m_url.utf8String(); }
195 #endif
196 
197 #ifndef NDEBUG
198     void print() const;
199 #endif
200 
201 private:
202     void invalidate();
203     bool isHierarchical() const;
204     static bool protocolIs(const String&, const char*);
205 #if USE(GOOGLEURL)
206     friend class GoogleURLPrivate;
207     void parse(const char* url, const String* originalString);  // KURLMac calls this.
208     void copyToBuffer(Vector<char, 512>& buffer) const;  // KURLCFNet uses this.
209     GoogleURLPrivate m_url;
210 #else  // !USE(GOOGLEURL)
211     void init(const KURL&, const String&, const TextEncoding&);
212     void copyToBuffer(Vector<char, 512>& buffer) const;
213 
214     // Parses the given URL. The originalString parameter allows for an
215     // optimization: When the source is the same as the fixed-up string,
216     // it will use the passed-in string instead of allocating a new one.
217     void parse(const String&);
218     void parse(const char* url, const String* originalString);
219 
220     String m_string;
221     bool m_isValid;
222     int m_schemeEnd;
223     int m_userStart;
224     int m_userEnd;
225     int m_passwordEnd;
226     int m_hostEnd;
227     int m_portEnd;
228     int m_pathAfterLastSlash;
229     int m_pathEnd;
230     int m_queryEnd;
231     int m_fragmentEnd;
232 #endif
233 };
234 
235 bool operator==(const KURL&, const KURL&);
236 bool operator==(const KURL&, const String&);
237 bool operator==(const String&, const KURL&);
238 bool operator!=(const KURL&, const KURL&);
239 bool operator!=(const KURL&, const String&);
240 bool operator!=(const String&, const KURL&);
241 
242 bool equalIgnoringRef(const KURL&, const KURL&);
243 bool protocolHostAndPortAreEqual(const KURL&, const KURL&);
244 
245 const KURL& blankURL();
246 
247 // Functions to do URL operations on strings.
248 // These are operations that aren't faster on a parsed URL.
249 
250 bool protocolIs(const String& url, const char* protocol);
251 
252 String mimeTypeFromDataURL(const String& url);
253 
254 // Unescapes the given string using URL escaping rules, given an optional
255 // encoding (defaulting to UTF-8 otherwise). DANGER: If the URL has "%00"
256 // in it, the resulting string will have embedded null characters!
257 String decodeURLEscapeSequences(const String&);
258 String decodeURLEscapeSequences(const String&, const TextEncoding&);
259 
260 String encodeWithURLEscapeSequences(const String&);
261 
262 // Inlines.
263 
264 inline bool operator==(const KURL& a, const KURL& b)
265 {
266     return a.string() == b.string();
267 }
268 
269 inline bool operator==(const KURL& a, const String& b)
270 {
271     return a.string() == b;
272 }
273 
274 inline bool operator==(const String& a, const KURL& b)
275 {
276     return a == b.string();
277 }
278 
279 inline bool operator!=(const KURL& a, const KURL& b)
280 {
281     return a.string() != b.string();
282 }
283 
284 inline bool operator!=(const KURL& a, const String& b)
285 {
286     return a.string() != b;
287 }
288 
289 inline bool operator!=(const String& a, const KURL& b)
290 {
291     return a != b.string();
292 }
293 
294 #if !USE(GOOGLEURL)
295 
296 // Inline versions of some non-GoogleURL functions so we can get inlining
297 // without having to have a lot of ugly ifdefs in the class definition.
298 
isNull()299 inline bool KURL::isNull() const
300 {
301     return m_string.isNull();
302 }
303 
isEmpty()304 inline bool KURL::isEmpty() const
305 {
306     return m_string.isEmpty();
307 }
308 
isValid()309 inline bool KURL::isValid() const
310 {
311     return m_isValid;
312 }
313 
hostStart()314 inline unsigned KURL::hostStart() const
315 {
316     return (m_passwordEnd == m_userStart) ? m_passwordEnd : m_passwordEnd + 1;
317 }
318 
hostEnd()319 inline unsigned KURL::hostEnd() const
320 {
321     return m_hostEnd;
322 }
323 
pathStart()324 inline unsigned KURL::pathStart() const
325 {
326     return m_portEnd;
327 }
328 
pathEnd()329 inline unsigned KURL::pathEnd() const
330 {
331     return m_pathEnd;
332 }
333 
pathAfterLastSlash()334 inline unsigned KURL::pathAfterLastSlash() const
335 {
336     return m_pathAfterLastSlash;
337 }
338 
339 #endif  // !USE(GOOGLEURL)
340 
341 } // namespace WebCore
342 
343 namespace WTF {
344 
345     // KURLHash is the default hash for String
346     template<typename T> struct DefaultHash;
347     template<> struct DefaultHash<WebCore::KURL> {
348         typedef WebCore::KURLHash Hash;
349     };
350 
351 } // namespace WTF
352 
353 #endif // KURL_h
354