]>
git.saurik.com Git - iphone-api.git/blob - WebCore/KURL.h
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
69 // an already encoded (ASCII-only) valid absolute URL.
70 explicit KURL(const char*);
71 explicit KURL(const String
&);
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
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
&);
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
);
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.
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.
101 bool isEmpty() const;
102 bool isValid() const;
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;
110 const String
& string() const { return m_url
.string(); }
112 const String
& string() const { return m_string
; }
115 String
protocol() const;
117 unsigned short port() const;
121 String
lastPathComponent() const;
122 String
query() const; // Includes the "?".
123 String
ref() const; // Does *not* include the "#".
126 String
prettyURL() const;
127 String
fileSystemPath() const;
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;
134 void setProtocol(const String
&);
135 void setHost(const String
&);
137 // Setting the port to 0 will clear any port from the URL.
138 void setPort(unsigned short);
140 // Input is like "foo.com" or "foo.com:8000".
141 void setHostAndPort(const String
&);
143 void setUser(const String
&);
144 void setPass(const String
&);
146 // If you pass an empty path for HTTP or HTTPS URLs, the resulting path
148 void setPath(const String
&);
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
&);
155 void setRef(const String
&);
158 friend bool equalIgnoringRef(const KURL
&, const KURL
&);
160 friend bool protocolHostAndPortAreEqual(const KURL
&, const KURL
&);
162 unsigned hostStart() const;
163 unsigned hostEnd() const;
165 unsigned pathStart() const;
166 unsigned pathEnd() const;
167 unsigned pathAfterLastSlash() const;
168 operator const String
&() const { return string(); }
170 operator JSC::UString() const { return string(); }
175 CFURLRef
createCFURL() const;
180 operator NSURL
*() const;
183 operator NSString
*() const { return string(); }
188 operator QUrl() const;
192 // Getters for the parsed structure and its corresponding 8-bit string.
193 const url_parse::Parsed
& parsed() const { return m_url
.m_parsed
; }
194 const CString
& utf8String() const { return m_url
.utf8String(); }
203 bool isHierarchical() const;
204 static bool protocolIs(const String
&, const char*);
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;
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
);
228 int m_pathAfterLastSlash
;
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
&);
242 bool equalIgnoringRef(const KURL
&, const KURL
&);
243 bool protocolHostAndPortAreEqual(const KURL
&, const KURL
&);
245 const KURL
& blankURL();
247 // Functions to do URL operations on strings.
248 // These are operations that aren't faster on a parsed URL.
250 bool protocolIs(const String
& url
, const char* protocol
);
252 String
mimeTypeFromDataURL(const String
& url
);
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
&);
260 String
encodeWithURLEscapeSequences(const String
&);
264 inline bool operator==(const KURL
& a
, const KURL
& b
)
266 return a
.string() == b
.string();
269 inline bool operator==(const KURL
& a
, const String
& b
)
271 return a
.string() == b
;
274 inline bool operator==(const String
& a
, const KURL
& b
)
276 return a
== b
.string();
279 inline bool operator!=(const KURL
& a
, const KURL
& b
)
281 return a
.string() != b
.string();
284 inline bool operator!=(const KURL
& a
, const String
& b
)
286 return a
.string() != b
;
289 inline bool operator!=(const String
& a
, const KURL
& b
)
291 return a
!= b
.string();
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.
299 inline bool KURL::isNull() const
301 return m_string
.isNull();
304 inline bool KURL::isEmpty() const
306 return m_string
.isEmpty();
309 inline bool KURL::isValid() const
314 inline unsigned KURL::hostStart() const
316 return (m_passwordEnd
== m_userStart
) ? m_passwordEnd
: m_passwordEnd
+ 1;
319 inline unsigned KURL::hostEnd() const
324 inline unsigned KURL::pathStart() const
329 inline unsigned KURL::pathEnd() const
334 inline unsigned KURL::pathAfterLastSlash() const
336 return m_pathAfterLastSlash
;
339 #endif // !USE(GOOGLEURL)
341 } // namespace WebCore
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
;