1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
/*
* Copyright (C) 2003, 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef KURL_h
#define KURL_h
#include "DeprecatedString.h"
#include "PlatformString.h"
#include <wtf/Platform.h>
#ifdef USE_GOOGLE_URL_LIBRARY
#include "CString.h"
#include "googleurl/src/url_canon.h"
#include "googleurl/src/url_parse.h"
#endif
#if PLATFORM(CF)
typedef const struct __CFURL * CFURLRef;
#endif
#if PLATFORM(MAC)
#ifdef __OBJC__
@class NSURL;
#else
class NSURL;
#endif
#endif
#if PLATFORM(QT)
class QUrl;
#endif
namespace WebCore {
class KURL;
class TextEncoding;
bool operator==(const KURL&, const KURL&);
inline bool operator!=(const KURL &a, const KURL &b) { return !(a == b); }
bool equalIgnoringRef(const KURL&, const KURL&);
class KURL {
public:
KURL();
KURL(const char*);
KURL(const KURL&, const DeprecatedString&);
KURL(const KURL&, const DeprecatedString&, const TextEncoding&);
KURL(const DeprecatedString&);
#if PLATFORM(MAC)
KURL(NSURL*);
#endif
#if PLATFORM(CF)
KURL(CFURLRef);
#endif
#if PLATFORM(QT)
KURL(const QUrl&);
#endif
#ifdef USE_GOOGLE_URL_LIBRARY
// For conversions for other structures that have already parsed and
// canonicalized the URL. The input must be exactly what KURL would have
// done with the same input.
KURL(const char* canonical_spec, int canonical_spec_len,
const url_parse::Parsed& parsed, bool is_valid);
#endif
bool isValid() const { return m_isValid; }
bool hasPath() const;
#ifdef USE_GOOGLE_URL_LIBRARY
bool isNull() const { return m_url.utf8String().isNull(); }
bool isEmpty() const { return m_url.utf8String().length() == 0; }
const String& string() const { return m_url.string(); }
DeprecatedString deprecatedString() const { return m_url.deprecatedString(); }
#else
bool isNull() const { return urlString.isNull(); }
bool isEmpty() const { return urlString.isEmpty(); }
String string() const { return urlString; }
const DeprecatedString& deprecatedString() const { return urlString; }
#endif
DeprecatedString protocol() const;
DeprecatedString host() const;
unsigned short int port() const;
DeprecatedString user() const;
DeprecatedString pass() const;
DeprecatedString path() const;
DeprecatedString lastPathComponent() const;
DeprecatedString query() const;
DeprecatedString ref() const;
bool hasRef() const;
DeprecatedString encodedHtmlRef() const { return ref(); }
void setProtocol(const DeprecatedString &);
void setHost(const DeprecatedString &);
void setPort(unsigned short int);
void setHostAndPort(const DeprecatedString&);
void setUser(const DeprecatedString &);
void setPass(const DeprecatedString &);
void setPath(const DeprecatedString &);
void setQuery(const DeprecatedString &);
void setRef(const DeprecatedString &);
DeprecatedString prettyURL() const;
#if PLATFORM(CF)
CFURLRef createCFURL() const;
#endif
#if PLATFORM(MAC)
NSURL *getNSURL() const;
#endif
#if PLATFORM(QT)
operator QUrl() const;
#endif
bool isLocalFile() const;
String fileSystemPath() const;
static DeprecatedString decode_string(const DeprecatedString&);
static DeprecatedString decode_string(const DeprecatedString&, const TextEncoding&);
static DeprecatedString encode_string(const DeprecatedString&);
friend bool operator==(const KURL &, const KURL &);
#ifdef USE_GOOGLE_URL_LIBRARY
// Getters for the parsed structure and its corresponding 8-bit string.
const url_parse::Parsed& parsed() const { return m_parsed; }
const CString& utf8String() const { return m_url.utf8String(); }
#endif
#ifndef NDEBUG
void print() const;
#endif
private:
bool isHierarchical() const;
bool m_isValid;
#ifdef USE_GOOGLE_URL_LIBRARY
// Initializes the object. This will call through to one of the backend
// initializers below depending on whether the string's internal
// representation is 8 or 16 bit.
void init(const KURL& base, const DeprecatedString& relative,
const TextEncoding* query_encoding);
// Backend initializers. The query encoding parameters are optional and can
// be NULL (this implies UTF-8). These initializers require that the object
// has just been created and the strings are NULL. Do not call on an
// already-constructed object.
void init(const KURL& base, const char* rel, int rel_len,
const TextEncoding* query_encoding);
void init(const KURL& base, const UChar* rel, int rel_len,
const TextEncoding* query_encoding);
// Returns the substring of the input identified by the given component.
DeprecatedString componentString(const url_parse::Component& comp) const;
// Replaces the given components, modifying the current URL. The current
// URL must be valid.
typedef url_canon::Replacements<wchar_t> Replacements;
void replaceComponents(const Replacements& replacements);
// Returns true if the scheme matches the given lowercase ASCII scheme.
bool schemeIs(const char* lower_ascii_scheme) const;
// We store the URL in UTF-8 (really, ASCII except for the ref which can be
// UTF-8). WebKit wants Strings and (for now) DeprecatedStrings out of us.
// We create the conversion of the strings on demand and cache the results
// to speed things up.
class URLString {
public:
URLString();
// Setters for the data. Using the ASCII version when you know the
// data is ASCII will be slightly more efficient. The UTF-8 version
// will always be correct if the caller is unsure.
void setUtf8(const char* data, int data_len);
void setAscii(const char* data, int data_len);
// TODO(brettw) we can support an additional optimization when KURL
// class is supported only for Strings. Make this buffer support both
// optinal Strings and UTF-8 data. This way, we can use the optimization
// from the original KURL which uses = with the original string when
// canonicalization did not change it. This allows the strings to share
// a buffer internally, and saves a malloc.
// Getters for the data.
const CString& utf8String() const { return m_utf8; }
const String& string() const;
const DeprecatedString deprecatedString() const;
private:
CString m_utf8;
// Set to true when the caller set us using the ASCII setter. We can
// be more efficient when we know there is no UTF-8 to worry about.
// This flag is currently always correct, but should be changed to be a
// hint (see setUtf8).
bool m_utf8IsASCII;
mutable bool m_stringIsValid;
mutable String m_string;
mutable bool m_deprecatedStringIsValid;
mutable DeprecatedString m_deprecatedString;
};
URLString m_url;
url_parse::Parsed m_parsed; // Indexes into the UTF-8 version of the string.
#else
void init(const KURL&, const DeprecatedString&, const TextEncoding&);
void parse(const char *url, const DeprecatedString *originalString);
DeprecatedString urlString;
int schemeEndPos;
int userStartPos;
int userEndPos;
int passwordEndPos;
int hostEndPos;
int portEndPos;
int pathEndPos;
int queryEndPos;
int fragmentEndPos;
#endif
friend bool equalIgnoringRef(const KURL& a, const KURL& b);
};
}
#endif // KURL_h
|