Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: src/gurl.h

Issue 14090005: Modify the headers in src/ to forward to url/*.h (Closed) Base URL: http://google-url.googlecode.com/svn/trunk
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | src/url_canon.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2007, Google Inc. 1 // Copyright 2007, Google Inc.
2 // All rights reserved. 2 // All rights reserved.
3 // 3 //
4 // Redistribution and use in source and binary forms, with or without 4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are 5 // modification, are permitted provided that the following conditions are
6 // met: 6 // met:
7 // 7 //
8 // * Redistributions of source code must retain the above copyright 8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer. 9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above 10 // * Redistributions in binary form must reproduce the above
(...skipping 12 matching lines...) Expand all
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 29
30 #ifndef GOOGLEURL_SRC_GURL_H__ 30 #ifndef GOOGLEURL_SRC_GURL_H__
31 #define GOOGLEURL_SRC_GURL_H__ 31 #define GOOGLEURL_SRC_GURL_H__
32 32
33 #include <iosfwd> 33 #include "url/gurl.h"
34 #include <string>
35
36 #include "base/string16.h"
37 #include "googleurl/src/url_canon.h"
38 #include "googleurl/src/url_canon_stdstring.h"
39 #include "googleurl/src/url_common.h"
40 #include "googleurl/src/url_parse.h"
41
42 class GURL {
43 public:
44 typedef url_canon::StdStringReplacements<std::string> Replacements;
45 typedef url_canon::StdStringReplacements<string16> ReplacementsW;
46
47 // Creates an empty, invalid URL.
48 GURL_API GURL();
49
50 // Copy construction is relatively inexpensive, with most of the time going
51 // to reallocating the string. It does not re-parse.
52 GURL_API GURL(const GURL& other);
53
54 // The narrow version requires the input be UTF-8. Invalid UTF-8 input will
55 // result in an invalid URL.
56 //
57 // The wide version should also take an encoding parameter so we know how to
58 // encode the query parameters. It is probably sufficient for the narrow
59 // version to assume the query parameter encoding should be the same as the
60 // input encoding.
61 GURL_API explicit GURL(const std::string& url_string
62 /*, output_param_encoding*/);
63 GURL_API explicit GURL(const string16& url_string
64 /*, output_param_encoding*/);
65
66 // Constructor for URLs that have already been parsed and canonicalized. This
67 // is used for conversions from KURL, for example. The caller must supply all
68 // information associated with the URL, which must be correct and consistent.
69 GURL_API GURL(const char* canonical_spec, size_t canonical_spec_len,
70 const url_parse::Parsed& parsed, bool is_valid);
71
72 GURL_API ~GURL();
73
74 GURL_API GURL& operator=(const GURL& other);
75
76 // Returns true when this object represents a valid parsed URL. When not
77 // valid, other functions will still succeed, but you will not get canonical
78 // data out in the format you may be expecting. Instead, we keep something
79 // "reasonable looking" so that the user can see how it's busted if
80 // displayed to them.
81 bool is_valid() const {
82 return is_valid_;
83 }
84
85 // Returns true if the URL is zero-length. Note that empty URLs are also
86 // invalid, and is_valid() will return false for them. This is provided
87 // because some users may want to treat the empty case differently.
88 bool is_empty() const {
89 return spec_.empty();
90 }
91
92 // Returns the raw spec, i.e., the full text of the URL, in canonical UTF-8,
93 // if the URL is valid. If the URL is not valid, this will assert and return
94 // the empty string (for safety in release builds, to keep them from being
95 // misused which might be a security problem).
96 //
97 // The URL will be ASCII except the reference fragment, which may be UTF-8.
98 // It is guaranteed to be valid UTF-8.
99 //
100 // The exception is for empty() URLs (which are !is_valid()) but this will
101 // return the empty string without asserting.
102 //
103 // Used invalid_spec() below to get the unusable spec of an invalid URL. This
104 // separation is designed to prevent errors that may cause security problems
105 // that could result from the mistaken use of an invalid URL.
106 GURL_API const std::string& spec() const;
107
108 // Returns the potentially invalid spec for a the URL. This spec MUST NOT be
109 // modified or sent over the network. It is designed to be displayed in error
110 // messages to the user, as the apperance of the spec may explain the error.
111 // If the spec is valid, the valid spec will be returned.
112 //
113 // The returned string is guaranteed to be valid UTF-8.
114 const std::string& possibly_invalid_spec() const {
115 return spec_;
116 }
117
118 // Getter for the raw parsed structure. This allows callers to locate parts
119 // of the URL within the spec themselves. Most callers should consider using
120 // the individual component getters below.
121 //
122 // The returned parsed structure will reference into the raw spec, which may
123 // or may not be valid. If you are using this to index into the spec, BE
124 // SURE YOU ARE USING possibly_invalid_spec() to get the spec, and that you
125 // don't do anything "important" with invalid specs.
126 const url_parse::Parsed& parsed_for_possibly_invalid_spec() const {
127 return parsed_;
128 }
129
130 // Defiant equality operator!
131 bool operator==(const GURL& other) const {
132 return spec_ == other.spec_;
133 }
134 bool operator!=(const GURL& other) const {
135 return spec_ != other.spec_;
136 }
137
138 // Allows GURL to used as a key in STL (for example, a std::set or std::map).
139 bool operator<(const GURL& other) const {
140 return spec_ < other.spec_;
141 }
142
143 // Resolves a URL that's possibly relative to this object's URL, and returns
144 // it. Absolute URLs are also handled according to the rules of URLs on web
145 // pages.
146 //
147 // It may be impossible to resolve the URLs properly. If the input is not
148 // "standard" (SchemeIsStandard() == false) and the input looks relative, we
149 // can't resolve it. In these cases, the result will be an empty, invalid
150 // GURL.
151 //
152 // The result may also be a nonempty, invalid URL if the input has some kind
153 // of encoding error. In these cases, we will try to construct a "good" URL
154 // that may have meaning to the user, but it will be marked invalid.
155 //
156 // It is an error to resolve a URL relative to an invalid URL. The result
157 // will be the empty URL.
158 GURL_API GURL Resolve(const std::string& relative) const;
159 GURL_API GURL Resolve(const string16& relative) const;
160
161 // Like Resolve() above but takes a character set encoder which will be used
162 // for any query text specified in the input. The charset converter parameter
163 // may be NULL, in which case it will be treated as UTF-8.
164 //
165 // TODO(brettw): These should be replaced with versions that take something
166 // more friendly than a raw CharsetConverter (maybe like an ICU character set
167 // name).
168 GURL_API GURL ResolveWithCharsetConverter(
169 const std::string& relative,
170 url_canon::CharsetConverter* charset_converter) const;
171 GURL_API GURL ResolveWithCharsetConverter(
172 const string16& relative,
173 url_canon::CharsetConverter* charset_converter) const;
174
175 // Creates a new GURL by replacing the current URL's components with the
176 // supplied versions. See the Replacements class in url_canon.h for more.
177 //
178 // These are not particularly quick, so avoid doing mutations when possible.
179 // Prefer the 8-bit version when possible.
180 //
181 // It is an error to replace components of an invalid URL. The result will
182 // be the empty URL.
183 //
184 // Note that we use the more general url_canon::Replacements type to give
185 // callers extra flexibility rather than our override.
186 GURL_API GURL ReplaceComponents(
187 const url_canon::Replacements<char>& replacements) const;
188 GURL_API GURL ReplaceComponents(
189 const url_canon::Replacements<char16>& replacements) const;
190
191 // A helper function that is equivalent to replacing the path with a slash
192 // and clearing out everything after that. We sometimes need to know just the
193 // scheme and the authority. If this URL is not a standard URL (it doesn't
194 // have the regular authority and path sections), then the result will be
195 // an empty, invalid GURL. Note that this *does* work for file: URLs, which
196 // some callers may want to filter out before calling this.
197 //
198 // It is an error to get an empty path on an invalid URL. The result
199 // will be the empty URL.
200 GURL_API GURL GetWithEmptyPath() const;
201
202 // A helper function to return a GURL containing just the scheme, host,
203 // and port from a URL. Equivalent to clearing any username and password,
204 // replacing the path with a slash, and clearing everything after that. If
205 // this URL is not a standard URL, then the result will be an empty,
206 // invalid GURL. If the URL has neither username nor password, this
207 // degenerates to GetWithEmptyPath().
208 //
209 // It is an error to get the origin of an invalid URL. The result
210 // will be the empty URL.
211 GURL_API GURL GetOrigin() const;
212
213 // Returns true if the scheme for the current URL is a known "standard"
214 // scheme. Standard schemes have an authority and a path section. This
215 // includes file: and filesystem:, which some callers may want to filter out
216 // explicitly by calling SchemeIsFile[System].
217 GURL_API bool IsStandard() const;
218
219 // Returns true if the given parameter (should be lower-case ASCII to match
220 // the canonicalized scheme) is the scheme for this URL. This call is more
221 // efficient than getting the scheme and comparing it because no copies or
222 // object constructions are done.
223 GURL_API bool SchemeIs(const char* lower_ascii_scheme) const;
224
225 // We often need to know if this is a file URL. File URLs are "standard", but
226 // are often treated separately by some programs.
227 bool SchemeIsFile() const {
228 return SchemeIs("file");
229 }
230
231 // FileSystem URLs need to be treated differently in some cases.
232 bool SchemeIsFileSystem() const {
233 return SchemeIs("filesystem");
234 }
235
236 // If the scheme indicates a secure connection
237 bool SchemeIsSecure() const {
238 return SchemeIs("https") || SchemeIs("wss") ||
239 (SchemeIsFileSystem() && inner_url() && inner_url()->SchemeIsSecure());
240 }
241
242 // Returns true if the hostname is an IP address. Note: this function isn't
243 // as cheap as a simple getter because it re-parses the hostname to verify.
244 // This currently identifies only IPv4 addresses (bug 822685).
245 GURL_API bool HostIsIPAddress() const;
246
247 // Getters for various components of the URL. The returned string will be
248 // empty if the component is empty or is not present.
249 std::string scheme() const { // Not including the colon. See also SchemeIs.
250 return ComponentString(parsed_.scheme);
251 }
252 std::string username() const {
253 return ComponentString(parsed_.username);
254 }
255 std::string password() const {
256 return ComponentString(parsed_.password);
257 }
258 // Note that this may be a hostname, an IPv4 address, or an IPv6 literal
259 // surrounded by square brackets, like "[2001:db8::1]". To exclude these
260 // brackets, use HostNoBrackets() below.
261 std::string host() const {
262 return ComponentString(parsed_.host);
263 }
264 std::string port() const { // Returns -1 if "default"
265 return ComponentString(parsed_.port);
266 }
267 std::string path() const { // Including first slash following host
268 return ComponentString(parsed_.path);
269 }
270 std::string query() const { // Stuff following '?'
271 return ComponentString(parsed_.query);
272 }
273 std::string ref() const { // Stuff following '#'
274 return ComponentString(parsed_.ref);
275 }
276
277 // Existance querying. These functions will return true if the corresponding
278 // URL component exists in this URL. Note that existance is different than
279 // being nonempty. http://www.google.com/? has a query that just happens to
280 // be empty, and has_query() will return true.
281 bool has_scheme() const {
282 return parsed_.scheme.len >= 0;
283 }
284 bool has_username() const {
285 return parsed_.username.len >= 0;
286 }
287 bool has_password() const {
288 return parsed_.password.len >= 0;
289 }
290 bool has_host() const {
291 // Note that hosts are special, absense of host means length 0.
292 return parsed_.host.len > 0;
293 }
294 bool has_port() const {
295 return parsed_.port.len >= 0;
296 }
297 bool has_path() const {
298 // Note that http://www.google.com/" has a path, the path is "/". This can
299 // return false only for invalid or nonstandard URLs.
300 return parsed_.path.len >= 0;
301 }
302 bool has_query() const {
303 return parsed_.query.len >= 0;
304 }
305 bool has_ref() const {
306 return parsed_.ref.len >= 0;
307 }
308
309 // Returns a parsed version of the port. Can also be any of the special
310 // values defined in Parsed for ExtractPort.
311 GURL_API int IntPort() const;
312
313 // Returns the port number of the url, or the default port number.
314 // If the scheme has no concept of port (or unknown default) returns
315 // PORT_UNSPECIFIED.
316 GURL_API int EffectiveIntPort() const;
317
318 // Extracts the filename portion of the path and returns it. The filename
319 // is everything after the last slash in the path. This may be empty.
320 GURL_API std::string ExtractFileName() const;
321
322 // Returns the path that should be sent to the server. This is the path,
323 // parameter, and query portions of the URL. It is guaranteed to be ASCII.
324 GURL_API std::string PathForRequest() const;
325
326 // Returns the host, excluding the square brackets surrounding IPv6 address
327 // literals. This can be useful for passing to getaddrinfo().
328 GURL_API std::string HostNoBrackets() const;
329
330 // Returns true if this URL's host matches or is in the same domain as
331 // the given input string. For example if this URL was "www.google.com",
332 // this would match "com", "google.com", and "www.google.com
333 // (input domain should be lower-case ASCII to match the canonicalized
334 // scheme). This call is more efficient than getting the host and check
335 // whether host has the specific domain or not because no copies or
336 // object constructions are done.
337 //
338 // If function DomainIs has parameter domain_len, which means the parameter
339 // lower_ascii_domain does not gurantee to terminate with NULL character.
340 GURL_API bool DomainIs(const char* lower_ascii_domain, int domain_len) const;
341
342 // If function DomainIs only has parameter lower_ascii_domain, which means
343 // domain string should be terminate with NULL character.
344 bool DomainIs(const char* lower_ascii_domain) const {
345 return DomainIs(lower_ascii_domain,
346 static_cast<int>(strlen(lower_ascii_domain)));
347 }
348
349 // Swaps the contents of this GURL object with the argument without doing
350 // any memory allocations.
351 GURL_API void Swap(GURL* other);
352
353 // Returns a reference to a singleton empty GURL. This object is for callers
354 // who return references but don't have anything to return in some cases.
355 // This function may be called from any thread.
356 GURL_API static const GURL& EmptyGURL();
357
358 // Returns the inner URL of a nested URL [currently only non-null for
359 // filesystem: URLs].
360 const GURL* inner_url() const {
361 return inner_url_;
362 }
363
364 private:
365 // Returns the substring of the input identified by the given component.
366 std::string ComponentString(const url_parse::Component& comp) const {
367 if (comp.len <= 0)
368 return std::string();
369 return std::string(spec_, comp.begin, comp.len);
370 }
371
372 // The actual text of the URL, in canonical ASCII form.
373 std::string spec_;
374
375 // Set when the given URL is valid. Otherwise, we may still have a spec and
376 // components, but they may not identify valid resources (for example, an
377 // invalid port number, invalid characters in the scheme, etc.).
378 bool is_valid_;
379
380 // Identified components of the canonical spec.
381 url_parse::Parsed parsed_;
382
383 // Used for nested schemes [currently only filesystem:].
384 GURL* inner_url_;
385
386 // TODO bug 684583: Add encoding for query params.
387 };
388
389 // Stream operator so GURL can be used in assertion statements.
390 GURL_API std::ostream& operator<<(std::ostream& out, const GURL& url);
391 34
392 #endif // GOOGLEURL_SRC_GURL_H__ 35 #endif // GOOGLEURL_SRC_GURL_H__
OLDNEW
« no previous file with comments | « no previous file | src/url_canon.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698