blob: 09ab212c5841861e130101f9bdab5ca7e77d0688 [file] [log] [blame]
[email protected]51bcc5d2013-04-24 01:41:371// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
[email protected]e7bba5f82013-04-10 20:10:524
avic0c60312015-12-21 21:03:505#include "url/gurl.h"
6
7#include <stddef.h>
[email protected]e7bba5f82013-04-10 20:10:528
9#include <algorithm>
10#include <ostream>
11
[email protected]e7bba5f82013-04-10 20:10:5212#include "base/logging.h"
qyearsley7ffaa682015-08-03 07:03:4913#include "base/strings/string_piece.h"
brettwbc17d2c82015-06-09 22:39:0814#include "base/strings/string_util.h"
dskiba3bc10ee82017-02-01 01:22:1915#include "base/trace_event/memory_usage_estimator.h"
[email protected]318076b2013-04-18 21:19:4516#include "url/url_canon_stdstring.h"
17#include "url/url_util.h"
[email protected]e7bba5f82013-04-10 20:10:5218
avic0c60312015-12-21 21:03:5019#ifdef WIN32
20#include <windows.h>
21#else
22#include <pthread.h>
23#endif
24
[email protected]e7bba5f82013-04-10 20:10:5225namespace {
26
[email protected]e7bba5f82013-04-10 20:10:5227static std::string* empty_string = NULL;
28static GURL* empty_gurl = NULL;
29
30#ifdef WIN32
31
32// Returns a static reference to an empty string for returning a reference
33// when there is no underlying string.
34const std::string& EmptyStringForGURL() {
35 // Avoid static object construction/destruction on startup/shutdown.
36 if (!empty_string) {
37 // Create the string. Be careful that we don't break in the case that this
38 // is being called from multiple threads. Statics are not threadsafe.
39 std::string* new_empty_string = new std::string;
40 if (InterlockedCompareExchangePointer(
41 reinterpret_cast<PVOID*>(&empty_string), new_empty_string, NULL)) {
42 // The old value was non-NULL, so no replacement was done. Another
43 // thread did the initialization out from under us.
44 delete new_empty_string;
45 }
46 }
47 return *empty_string;
48}
49
50#else
51
52static pthread_once_t empty_string_once = PTHREAD_ONCE_INIT;
53static pthread_once_t empty_gurl_once = PTHREAD_ONCE_INIT;
54
55void EmptyStringForGURLOnce(void) {
56 empty_string = new std::string;
57}
58
59const std::string& EmptyStringForGURL() {
60 // Avoid static object construction/destruction on startup/shutdown.
61 pthread_once(&empty_string_once, EmptyStringForGURLOnce);
62 return *empty_string;
63}
64
65#endif // WIN32
66
qyearsley2bc727d2015-08-14 20:17:1567} // namespace
[email protected]e7bba5f82013-04-10 20:10:5268
[email protected]e05d81f2013-10-22 21:20:3169GURL::GURL() : is_valid_(false) {
[email protected]e7bba5f82013-04-10 20:10:5270}
71
72GURL::GURL(const GURL& other)
73 : spec_(other.spec_),
74 is_valid_(other.is_valid_),
[email protected]e05d81f2013-10-22 21:20:3175 parsed_(other.parsed_) {
[email protected]e7bba5f82013-04-10 20:10:5276 if (other.inner_url_)
[email protected]e05d81f2013-10-22 21:20:3177 inner_url_.reset(new GURL(*other.inner_url_));
[email protected]e7bba5f82013-04-10 20:10:5278 // Valid filesystem urls should always have an inner_url_.
79 DCHECK(!is_valid_ || !SchemeIsFileSystem() || inner_url_);
80}
81
sclittle376085b32017-03-14 21:08:4182GURL::GURL(GURL&& other)
83 : spec_(std::move(other.spec_)),
84 is_valid_(other.is_valid_),
85 parsed_(other.parsed_),
86 inner_url_(std::move(other.inner_url_)) {
87 other.is_valid_ = false;
88 other.parsed_ = url::Parsed();
89}
90
brettwdfbcc3b2016-01-20 01:49:1791GURL::GURL(base::StringPiece url_string) {
[email protected]369e84f72013-11-23 01:53:5292 InitCanonical(url_string, true);
[email protected]e7bba5f82013-04-10 20:10:5293}
94
brettwdfbcc3b2016-01-20 01:49:1795GURL::GURL(base::StringPiece16 url_string) {
[email protected]369e84f72013-11-23 01:53:5296 InitCanonical(url_string, true);
97}
98
99GURL::GURL(const std::string& url_string, RetainWhiteSpaceSelector) {
brettwdfbcc3b2016-01-20 01:49:17100 InitCanonical(base::StringPiece(url_string), false);
[email protected]e7bba5f82013-04-10 20:10:52101}
102
[email protected]0318f922014-04-22 00:09:23103GURL::GURL(const char* canonical_spec,
104 size_t canonical_spec_len,
105 const url::Parsed& parsed,
106 bool is_valid)
[email protected]e7bba5f82013-04-10 20:10:52107 : spec_(canonical_spec, canonical_spec_len),
108 is_valid_(is_valid),
[email protected]e05d81f2013-10-22 21:20:31109 parsed_(parsed) {
[email protected]19b61f972013-07-26 13:30:09110 InitializeFromCanonicalSpec();
111}
112
[email protected]0318f922014-04-22 00:09:23113GURL::GURL(std::string canonical_spec, const url::Parsed& parsed, bool is_valid)
ki.stfucebea5e2016-06-04 07:05:36114 : spec_(std::move(canonical_spec)), is_valid_(is_valid), parsed_(parsed) {
[email protected]19b61f972013-07-26 13:30:09115 InitializeFromCanonicalSpec();
116}
117
[email protected]369e84f72013-11-23 01:53:52118template<typename STR>
brettwdfbcc3b2016-01-20 01:49:17119void GURL::InitCanonical(base::BasicStringPiece<STR> input_spec,
120 bool trim_path_end) {
[email protected]0318f922014-04-22 00:09:23121 url::StdStringCanonOutput output(&spec_);
122 is_valid_ = url::Canonicalize(
[email protected]369e84f72013-11-23 01:53:52123 input_spec.data(), static_cast<int>(input_spec.length()), trim_path_end,
124 NULL, &output, &parsed_);
125
126 output.Complete(); // Must be done before using string.
127 if (is_valid_ && SchemeIsFileSystem()) {
128 inner_url_.reset(new GURL(spec_.data(), parsed_.Length(),
129 *parsed_.inner_parsed(), true));
130 }
csharrison475851da2016-12-17 02:19:42131 // Valid URLs always have non-empty specs.
132 DCHECK(!is_valid_ || !spec_.empty());
[email protected]369e84f72013-11-23 01:53:52133}
134
[email protected]19b61f972013-07-26 13:30:09135void GURL::InitializeFromCanonicalSpec() {
[email protected]e7bba5f82013-04-10 20:10:52136 if (is_valid_ && SchemeIsFileSystem()) {
[email protected]e05d81f2013-10-22 21:20:31137 inner_url_.reset(
138 new GURL(spec_.data(), parsed_.Length(),
139 *parsed_.inner_parsed(), true));
[email protected]e7bba5f82013-04-10 20:10:52140 }
141
142#ifndef NDEBUG
143 // For testing purposes, check that the parsed canonical URL is identical to
144 // what we would have produced. Skip checking for invalid URLs have no meaning
qyearsley2bc727d2015-08-14 20:17:15145 // and we can't always canonicalize then reproducibly.
[email protected]e7bba5f82013-04-10 20:10:52146 if (is_valid_) {
csharrison475851da2016-12-17 02:19:42147 DCHECK(!spec_.empty());
[email protected]0318f922014-04-22 00:09:23148 url::Component scheme;
[email protected]369e84f72013-11-23 01:53:52149 // We can't do this check on the inner_url of a filesystem URL, as
150 // canonical_spec actually points to the start of the outer URL, so we'd
151 // end up with infinite recursion in this constructor.
[email protected]0318f922014-04-22 00:09:23152 if (!url::FindAndCompareScheme(spec_.data(), spec_.length(),
[email protected]08dc7052014-06-18 07:57:49153 url::kFileSystemScheme, &scheme) ||
[email protected]19b61f972013-07-26 13:30:09154 scheme.begin == parsed_.scheme.begin) {
[email protected]369e84f72013-11-23 01:53:52155 // We need to retain trailing whitespace on path URLs, as the |parsed_|
156 // spec we originally received may legitimately contain trailing white-
157 // space on the path or components e.g. if the #ref has been
158 // removed from a "foo:hello #ref" URL (see http://crbug.com/291747).
159 GURL test_url(spec_, RETAIN_TRAILING_PATH_WHITEPACE);
[email protected]e7bba5f82013-04-10 20:10:52160
161 DCHECK(test_url.is_valid_ == is_valid_);
162 DCHECK(test_url.spec_ == spec_);
163
164 DCHECK(test_url.parsed_.scheme == parsed_.scheme);
165 DCHECK(test_url.parsed_.username == parsed_.username);
166 DCHECK(test_url.parsed_.password == parsed_.password);
167 DCHECK(test_url.parsed_.host == parsed_.host);
168 DCHECK(test_url.parsed_.port == parsed_.port);
169 DCHECK(test_url.parsed_.path == parsed_.path);
170 DCHECK(test_url.parsed_.query == parsed_.query);
171 DCHECK(test_url.parsed_.ref == parsed_.ref);
172 }
173 }
174#endif
175}
176
177GURL::~GURL() {
[email protected]e7bba5f82013-04-10 20:10:52178}
179
sclittle376085b32017-03-14 21:08:41180GURL& GURL::operator=(const GURL& other) {
181 spec_ = other.spec_;
182 is_valid_ = other.is_valid_;
183 parsed_ = other.parsed_;
184
185 if (!other.inner_url_)
186 inner_url_.reset();
187 else if (inner_url_)
188 *inner_url_ = *other.inner_url_;
189 else
190 inner_url_.reset(new GURL(*other.inner_url_));
191
192 return *this;
193}
194
195GURL& GURL::operator=(GURL&& other) {
196 spec_ = std::move(other.spec_);
197 is_valid_ = other.is_valid_;
198 parsed_ = other.parsed_;
199 inner_url_ = std::move(other.inner_url_);
200
201 other.is_valid_ = false;
202 other.parsed_ = url::Parsed();
[email protected]e7bba5f82013-04-10 20:10:52203 return *this;
204}
205
206const std::string& GURL::spec() const {
207 if (is_valid_ || spec_.empty())
208 return spec_;
209
210 DCHECK(false) << "Trying to get the spec of an invalid URL!";
211 return EmptyStringForGURL();
212}
213
dcheng3a088772014-12-06 09:58:21214bool GURL::operator<(const GURL& other) const {
215 return spec_ < other.spec_;
216}
217
218bool GURL::operator>(const GURL& other) const {
219 return spec_ > other.spec_;
220}
221
[email protected]e7bba5f82013-04-10 20:10:52222// Note: code duplicated below (it's inconvenient to use a template here).
mkwst45f25db2015-07-21 04:03:50223GURL GURL::Resolve(const std::string& relative) const {
[email protected]e7bba5f82013-04-10 20:10:52224 // Not allowed for invalid URLs.
225 if (!is_valid_)
226 return GURL();
227
228 GURL result;
[email protected]0318f922014-04-22 00:09:23229 url::StdStringCanonOutput output(&result.spec_);
[email protected]0318f922014-04-22 00:09:23230 if (!url::ResolveRelative(spec_.data(), static_cast<int>(spec_.length()),
231 parsed_, relative.data(),
232 static_cast<int>(relative.length()),
mkwst45f25db2015-07-21 04:03:50233 nullptr, &output, &result.parsed_)) {
[email protected]e7bba5f82013-04-10 20:10:52234 // Error resolving, return an empty URL.
235 return GURL();
236 }
237
238 output.Complete();
239 result.is_valid_ = true;
240 if (result.SchemeIsFileSystem()) {
[email protected]e05d81f2013-10-22 21:20:31241 result.inner_url_.reset(
242 new GURL(result.spec_.data(), result.parsed_.Length(),
243 *result.parsed_.inner_parsed(), true));
[email protected]e7bba5f82013-04-10 20:10:52244 }
245 return result;
246}
247
248// Note: code duplicated above (it's inconvenient to use a template here).
mkwst45f25db2015-07-21 04:03:50249GURL GURL::Resolve(const base::string16& relative) const {
[email protected]e7bba5f82013-04-10 20:10:52250 // Not allowed for invalid URLs.
251 if (!is_valid_)
252 return GURL();
253
254 GURL result;
[email protected]0318f922014-04-22 00:09:23255 url::StdStringCanonOutput output(&result.spec_);
[email protected]0318f922014-04-22 00:09:23256 if (!url::ResolveRelative(spec_.data(), static_cast<int>(spec_.length()),
257 parsed_, relative.data(),
258 static_cast<int>(relative.length()),
mkwst45f25db2015-07-21 04:03:50259 nullptr, &output, &result.parsed_)) {
[email protected]e7bba5f82013-04-10 20:10:52260 // Error resolving, return an empty URL.
261 return GURL();
262 }
263
264 output.Complete();
265 result.is_valid_ = true;
266 if (result.SchemeIsFileSystem()) {
[email protected]e05d81f2013-10-22 21:20:31267 result.inner_url_.reset(
268 new GURL(result.spec_.data(), result.parsed_.Length(),
269 *result.parsed_.inner_parsed(), true));
[email protected]e7bba5f82013-04-10 20:10:52270 }
271 return result;
272}
273
274// Note: code duplicated below (it's inconvenient to use a template here).
275GURL GURL::ReplaceComponents(
[email protected]0318f922014-04-22 00:09:23276 const url::Replacements<char>& replacements) const {
[email protected]e7bba5f82013-04-10 20:10:52277 GURL result;
278
279 // Not allowed for invalid URLs.
280 if (!is_valid_)
281 return GURL();
282
[email protected]0318f922014-04-22 00:09:23283 url::StdStringCanonOutput output(&result.spec_);
[email protected]0318f922014-04-22 00:09:23284 result.is_valid_ = url::ReplaceComponents(
[email protected]e7bba5f82013-04-10 20:10:52285 spec_.data(), static_cast<int>(spec_.length()), parsed_, replacements,
286 NULL, &output, &result.parsed_);
287
288 output.Complete();
289 if (result.is_valid_ && result.SchemeIsFileSystem()) {
mmenke73cea7e4a2016-06-13 19:04:57290 result.inner_url_.reset(new GURL(result.spec_.data(),
291 result.parsed_.Length(),
[email protected]e05d81f2013-10-22 21:20:31292 *result.parsed_.inner_parsed(), true));
[email protected]e7bba5f82013-04-10 20:10:52293 }
294 return result;
295}
296
297// Note: code duplicated above (it's inconvenient to use a template here).
298GURL GURL::ReplaceComponents(
[email protected]0318f922014-04-22 00:09:23299 const url::Replacements<base::char16>& replacements) const {
[email protected]e7bba5f82013-04-10 20:10:52300 GURL result;
301
302 // Not allowed for invalid URLs.
303 if (!is_valid_)
304 return GURL();
305
[email protected]0318f922014-04-22 00:09:23306 url::StdStringCanonOutput output(&result.spec_);
[email protected]0318f922014-04-22 00:09:23307 result.is_valid_ = url::ReplaceComponents(
[email protected]e7bba5f82013-04-10 20:10:52308 spec_.data(), static_cast<int>(spec_.length()), parsed_, replacements,
309 NULL, &output, &result.parsed_);
310
311 output.Complete();
312 if (result.is_valid_ && result.SchemeIsFileSystem()) {
mmenke73cea7e4a2016-06-13 19:04:57313 result.inner_url_.reset(new GURL(result.spec_.data(),
314 result.parsed_.Length(),
[email protected]e05d81f2013-10-22 21:20:31315 *result.parsed_.inner_parsed(), true));
[email protected]e7bba5f82013-04-10 20:10:52316 }
317 return result;
318}
319
320GURL GURL::GetOrigin() const {
321 // This doesn't make sense for invalid or nonstandard URLs, so return
qyearsley2bc727d2015-08-14 20:17:15322 // the empty URL.
[email protected]e7bba5f82013-04-10 20:10:52323 if (!is_valid_ || !IsStandard())
324 return GURL();
325
326 if (SchemeIsFileSystem())
327 return inner_url_->GetOrigin();
328
[email protected]0318f922014-04-22 00:09:23329 url::Replacements<char> replacements;
[email protected]e7bba5f82013-04-10 20:10:52330 replacements.ClearUsername();
331 replacements.ClearPassword();
332 replacements.ClearPath();
333 replacements.ClearQuery();
334 replacements.ClearRef();
335
336 return ReplaceComponents(replacements);
337}
338
[email protected]6b775ee2014-03-20 20:27:25339GURL GURL::GetAsReferrer() const {
lizeb5120f6dc2016-02-19 09:29:44340 if (!SchemeIsValidForReferrer())
jochen42450392014-11-24 19:47:22341 return GURL();
342
343 if (!has_ref() && !has_username() && !has_password())
[email protected]6b775ee2014-03-20 20:27:25344 return GURL(*this);
345
[email protected]0318f922014-04-22 00:09:23346 url::Replacements<char> replacements;
[email protected]6b775ee2014-03-20 20:27:25347 replacements.ClearRef();
348 replacements.ClearUsername();
349 replacements.ClearPassword();
350 return ReplaceComponents(replacements);
351}
352
[email protected]e7bba5f82013-04-10 20:10:52353GURL GURL::GetWithEmptyPath() const {
354 // This doesn't make sense for invalid or nonstandard URLs, so return
355 // the empty URL.
356 if (!is_valid_ || !IsStandard())
357 return GURL();
358
359 // We could optimize this since we know that the URL is canonical, and we are
360 // appending a canonical path, so avoiding re-parsing.
361 GURL other(*this);
362 if (parsed_.path.len == 0)
363 return other;
364
365 // Clear everything after the path.
366 other.parsed_.query.reset();
367 other.parsed_.ref.reset();
368
369 // Set the path, since the path is longer than one, we can just set the
370 // first character and resize.
371 other.spec_[other.parsed_.path.begin] = '/';
372 other.parsed_.path.len = 1;
373 other.spec_.resize(other.parsed_.path.begin + 1);
374 return other;
375}
376
377bool GURL::IsStandard() const {
[email protected]0318f922014-04-22 00:09:23378 return url::IsStandard(spec_.data(), parsed_.scheme);
[email protected]e7bba5f82013-04-10 20:10:52379}
380
clamy12bca18b2017-02-10 15:33:07381bool GURL::IsAboutBlank() const {
382 if (!SchemeIs(url::kAboutScheme))
383 return false;
384
385 if (has_host() || has_username() || has_password() || has_port())
386 return false;
387
388 if (path() != url::kAboutBlankPath && path() != url::kAboutBlankWithHashPath)
389 return false;
390
391 return true;
392}
393
brettwadc846882015-09-25 01:16:22394bool GURL::SchemeIs(base::StringPiece lower_ascii_scheme) const {
395 DCHECK(base::IsStringASCII(lower_ascii_scheme));
396 DCHECK(base::ToLowerASCII(lower_ascii_scheme) == lower_ascii_scheme);
397
[email protected]e7bba5f82013-04-10 20:10:52398 if (parsed_.scheme.len <= 0)
brettwadc846882015-09-25 01:16:22399 return lower_ascii_scheme.empty();
400 return scheme_piece() == lower_ascii_scheme;
[email protected]e7bba5f82013-04-10 20:10:52401}
402
[email protected]91f5689032013-08-22 01:43:33403bool GURL::SchemeIsHTTPOrHTTPS() const {
[email protected]9d5877e2014-06-02 07:34:35404 return SchemeIs(url::kHttpScheme) || SchemeIs(url::kHttpsScheme);
[email protected]91f5689032013-08-22 01:43:33405}
406
lizeb5120f6dc2016-02-19 09:29:44407bool GURL::SchemeIsValidForReferrer() const {
408 return is_valid_ && IsReferrerScheme(spec_.data(), parsed_.scheme);
409}
410
[email protected]9690b992013-11-22 07:40:46411bool GURL::SchemeIsWSOrWSS() const {
[email protected]9d5877e2014-06-02 07:34:35412 return SchemeIs(url::kWsScheme) || SchemeIs(url::kWssScheme);
[email protected]9690b992013-11-22 07:40:46413}
414
[email protected]e7bba5f82013-04-10 20:10:52415int GURL::IntPort() const {
416 if (parsed_.port.is_nonempty())
[email protected]0318f922014-04-22 00:09:23417 return url::ParsePort(spec_.data(), parsed_.port);
418 return url::PORT_UNSPECIFIED;
[email protected]e7bba5f82013-04-10 20:10:52419}
420
421int GURL::EffectiveIntPort() const {
422 int int_port = IntPort();
[email protected]0318f922014-04-22 00:09:23423 if (int_port == url::PORT_UNSPECIFIED && IsStandard())
424 return url::DefaultPortForScheme(spec_.data() + parsed_.scheme.begin,
425 parsed_.scheme.len);
[email protected]e7bba5f82013-04-10 20:10:52426 return int_port;
427}
428
429std::string GURL::ExtractFileName() const {
[email protected]0318f922014-04-22 00:09:23430 url::Component file_component;
431 url::ExtractFileName(spec_.data(), parsed_.path, &file_component);
[email protected]e7bba5f82013-04-10 20:10:52432 return ComponentString(file_component);
433}
434
435std::string GURL::PathForRequest() const {
qyearsley2bc727d2015-08-14 20:17:15436 DCHECK(parsed_.path.len > 0)
437 << "Canonical path for requests should be non-empty";
[email protected]e7bba5f82013-04-10 20:10:52438 if (parsed_.ref.len >= 0) {
qyearsley2bc727d2015-08-14 20:17:15439 // Clip off the reference when it exists. The reference starts after the
440 // #-sign, so we have to subtract one to also remove it.
[email protected]e7bba5f82013-04-10 20:10:52441 return std::string(spec_, parsed_.path.begin,
442 parsed_.ref.begin - parsed_.path.begin - 1);
443 }
444 // Compute the actual path length, rather than depending on the spec's
qyearsley2bc727d2015-08-14 20:17:15445 // terminator. If we're an inner_url, our spec continues on into our outer
446 // URL's path/query/ref.
[email protected]e7bba5f82013-04-10 20:10:52447 int path_len = parsed_.path.len;
448 if (parsed_.query.is_valid())
449 path_len = parsed_.query.end() - parsed_.path.begin;
450
451 return std::string(spec_, parsed_.path.begin, path_len);
452}
453
454std::string GURL::HostNoBrackets() const {
455 // If host looks like an IPv6 literal, strip the square brackets.
[email protected]0318f922014-04-22 00:09:23456 url::Component h(parsed_.host);
[email protected]e7bba5f82013-04-10 20:10:52457 if (h.len >= 2 && spec_[h.begin] == '[' && spec_[h.end() - 1] == ']') {
458 h.begin++;
459 h.len -= 2;
460 }
461 return ComponentString(h);
462}
463
[email protected]5f50c5d2013-10-24 19:05:17464std::string GURL::GetContent() const {
465 return is_valid_ ? ComponentString(parsed_.GetContent()) : std::string();
466}
467
[email protected]e7bba5f82013-04-10 20:10:52468bool GURL::HostIsIPAddress() const {
csharrison475851da2016-12-17 02:19:42469 return is_valid_ && url::HostIsIPAddress(host_piece());
[email protected]e7bba5f82013-04-10 20:10:52470}
471
472#ifdef WIN32
473
474const GURL& GURL::EmptyGURL() {
475 // Avoid static object construction/destruction on startup/shutdown.
476 if (!empty_gurl) {
477 // Create the string. Be careful that we don't break in the case that this
478 // is being called from multiple threads.
479 GURL* new_empty_gurl = new GURL;
480 if (InterlockedCompareExchangePointer(
481 reinterpret_cast<PVOID*>(&empty_gurl), new_empty_gurl, NULL)) {
482 // The old value was non-NULL, so no replacement was done. Another
483 // thread did the initialization out from under us.
484 delete new_empty_gurl;
485 }
486 }
487 return *empty_gurl;
488}
489
490#else
491
492void EmptyGURLOnce(void) {
493 empty_gurl = new GURL;
494}
495
496const GURL& GURL::EmptyGURL() {
497 // Avoid static object construction/destruction on startup/shutdown.
498 pthread_once(&empty_gurl_once, EmptyGURLOnce);
499 return *empty_gurl;
500}
501
502#endif // WIN32
503
qyearsley7ffaa682015-08-03 07:03:49504bool GURL::DomainIs(base::StringPiece lower_ascii_domain) const {
pkalinnikov054f4032016-08-31 10:54:17505 if (!is_valid_)
[email protected]e7bba5f82013-04-10 20:10:52506 return false;
507
pkalinnikov054f4032016-08-31 10:54:17508 // FileSystem URLs have empty host_piece, so check this first.
[email protected]e7bba5f82013-04-10 20:10:52509 if (SchemeIsFileSystem() && inner_url_)
qyearsley7ffaa682015-08-03 07:03:49510 return inner_url_->DomainIs(lower_ascii_domain);
pkalinnikov054f4032016-08-31 10:54:17511 return url::DomainIs(host_piece(), lower_ascii_domain);
[email protected]e7bba5f82013-04-10 20:10:52512}
513
arthursonzogni9c873d8c2017-02-08 17:58:05514bool GURL::EqualsIgnoringRef(const GURL& other) const {
515 int ref_position = parsed_.CountCharactersBefore(url::Parsed::REF, true);
516 int ref_position_other =
517 other.parsed_.CountCharactersBefore(url::Parsed::REF, true);
518 return base::StringPiece(spec_).substr(0, ref_position) ==
519 base::StringPiece(other.spec_).substr(0, ref_position_other);
520}
521
[email protected]e7bba5f82013-04-10 20:10:52522void GURL::Swap(GURL* other) {
523 spec_.swap(other->spec_);
524 std::swap(is_valid_, other->is_valid_);
525 std::swap(parsed_, other->parsed_);
[email protected]e05d81f2013-10-22 21:20:31526 inner_url_.swap(other->inner_url_);
[email protected]e7bba5f82013-04-10 20:10:52527}
528
dskiba3bc10ee82017-02-01 01:22:19529size_t GURL::EstimateMemoryUsage() const {
530 return base::trace_event::EstimateMemoryUsage(spec_) +
531 base::trace_event::EstimateMemoryUsage(inner_url_) +
532 (parsed_.inner_parsed() ? sizeof(url::Parsed) : 0);
533}
534
[email protected]e7bba5f82013-04-10 20:10:52535std::ostream& operator<<(std::ostream& out, const GURL& url) {
536 return out << url.possibly_invalid_spec();
537}
csharrisonebeca8e2016-10-18 02:35:36538
539bool operator==(const GURL& x, const GURL& y) {
540 return x.possibly_invalid_spec() == y.possibly_invalid_spec();
541}
542
543bool operator!=(const GURL& x, const GURL& y) {
544 return !(x == y);
545}
546
547bool operator==(const GURL& x, const base::StringPiece& spec) {
cfredric370250a2016-11-15 22:38:56548 DCHECK_EQ(GURL(spec).possibly_invalid_spec(), spec);
csharrisonebeca8e2016-10-18 02:35:36549 return x.possibly_invalid_spec() == spec;
550}
551
552bool operator!=(const GURL& x, const base::StringPiece& spec) {
553 return !(x == spec);
554}