1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "url/gurl.h"
6
7 #include <stddef.h>
8
9 #include <algorithm>
10 #include <memory>
11 #include <ostream>
12 #include <utility>
13
14 #include "base/check_op.h"
15 #include "base/no_destructor.h"
16 #include "base/strings/string_piece.h"
17 #include "base/strings/string_util.h"
18 #include "base/trace_event/base_tracing.h"
19 #include "base/trace_event/memory_usage_estimator.h"
20 #include "url/url_canon_stdstring.h"
21 #include "url/url_util.h"
22
GURL()23 GURL::GURL() : is_valid_(false) {
24 }
25
GURL(const GURL & other)26 GURL::GURL(const GURL& other)
27 : spec_(other.spec_),
28 is_valid_(other.is_valid_),
29 parsed_(other.parsed_) {
30 if (other.inner_url_)
31 inner_url_ = std::make_unique<GURL>(*other.inner_url_);
32 // Valid filesystem urls should always have an inner_url_.
33 DCHECK(!is_valid_ || !SchemeIsFileSystem() || inner_url_);
34 }
35
GURL(GURL && other)36 GURL::GURL(GURL&& other) noexcept
37 : spec_(std::move(other.spec_)),
38 is_valid_(other.is_valid_),
39 parsed_(other.parsed_),
40 inner_url_(std::move(other.inner_url_)) {
41 other.is_valid_ = false;
42 other.parsed_ = url::Parsed();
43 }
44
GURL(base::StringPiece url_string)45 GURL::GURL(base::StringPiece url_string) {
46 InitCanonical(url_string, true);
47 }
48
GURL(base::StringPiece16 url_string)49 GURL::GURL(base::StringPiece16 url_string) {
50 InitCanonical(url_string, true);
51 }
52
GURL(const std::string & url_string,RetainWhiteSpaceSelector)53 GURL::GURL(const std::string& url_string, RetainWhiteSpaceSelector) {
54 InitCanonical(url_string, false);
55 }
56
GURL(const char * canonical_spec,size_t canonical_spec_len,const url::Parsed & parsed,bool is_valid)57 GURL::GURL(const char* canonical_spec,
58 size_t canonical_spec_len,
59 const url::Parsed& parsed,
60 bool is_valid)
61 : spec_(canonical_spec, canonical_spec_len),
62 is_valid_(is_valid),
63 parsed_(parsed) {
64 InitializeFromCanonicalSpec();
65 }
66
GURL(std::string canonical_spec,const url::Parsed & parsed,bool is_valid)67 GURL::GURL(std::string canonical_spec, const url::Parsed& parsed, bool is_valid)
68 : spec_(std::move(canonical_spec)), is_valid_(is_valid), parsed_(parsed) {
69 InitializeFromCanonicalSpec();
70 }
71
72 template <typename T, typename CharT>
InitCanonical(T input_spec,bool trim_path_end)73 void GURL::InitCanonical(T input_spec, bool trim_path_end) {
74 url::StdStringCanonOutput output(&spec_);
75 is_valid_ = url::Canonicalize(
76 input_spec.data(), static_cast<int>(input_spec.length()), trim_path_end,
77 NULL, &output, &parsed_);
78
79 output.Complete(); // Must be done before using string.
80 if (is_valid_ && SchemeIsFileSystem()) {
81 inner_url_ = std::make_unique<GURL>(spec_.data(), parsed_.Length(),
82 *parsed_.inner_parsed(), true);
83 }
84 // Valid URLs always have non-empty specs.
85 DCHECK(!is_valid_ || !spec_.empty());
86 }
87
InitializeFromCanonicalSpec()88 void GURL::InitializeFromCanonicalSpec() {
89 if (is_valid_ && SchemeIsFileSystem()) {
90 inner_url_ = std::make_unique<GURL>(spec_.data(), parsed_.Length(),
91 *parsed_.inner_parsed(), true);
92 }
93
94 #ifndef NDEBUG
95 // For testing purposes, check that the parsed canonical URL is identical to
96 // what we would have produced. Skip checking for invalid URLs have no meaning
97 // and we can't always canonicalize then reproducibly.
98 if (is_valid_) {
99 DCHECK(!spec_.empty());
100 url::Component scheme;
101 // We can't do this check on the inner_url of a filesystem URL, as
102 // canonical_spec actually points to the start of the outer URL, so we'd
103 // end up with infinite recursion in this constructor.
104 if (!url::FindAndCompareScheme(spec_.data(), spec_.length(),
105 url::kFileSystemScheme, &scheme) ||
106 scheme.begin == parsed_.scheme.begin) {
107 // We need to retain trailing whitespace on path URLs, as the |parsed_|
108 // spec we originally received may legitimately contain trailing white-
109 // space on the path or components e.g. if the #ref has been
110 // removed from a "foo:hello #ref" URL (see http://crbug.com/291747).
111 GURL test_url(spec_, RETAIN_TRAILING_PATH_WHITEPACE);
112
113 DCHECK_EQ(test_url.is_valid_, is_valid_);
114 DCHECK_EQ(test_url.spec_, spec_);
115
116 DCHECK_EQ(test_url.parsed_.scheme, parsed_.scheme);
117 DCHECK_EQ(test_url.parsed_.username, parsed_.username);
118 DCHECK_EQ(test_url.parsed_.password, parsed_.password);
119 DCHECK_EQ(test_url.parsed_.host, parsed_.host);
120 DCHECK_EQ(test_url.parsed_.port, parsed_.port);
121 DCHECK_EQ(test_url.parsed_.path, parsed_.path);
122 DCHECK_EQ(test_url.parsed_.query, parsed_.query);
123 DCHECK_EQ(test_url.parsed_.ref, parsed_.ref);
124 }
125 }
126 #endif
127 }
128
129 GURL::~GURL() = default;
130
operator =(const GURL & other)131 GURL& GURL::operator=(const GURL& other) {
132 spec_ = other.spec_;
133 is_valid_ = other.is_valid_;
134 parsed_ = other.parsed_;
135
136 if (!other.inner_url_)
137 inner_url_.reset();
138 else if (inner_url_)
139 *inner_url_ = *other.inner_url_;
140 else
141 inner_url_ = std::make_unique<GURL>(*other.inner_url_);
142
143 return *this;
144 }
145
operator =(GURL && other)146 GURL& GURL::operator=(GURL&& other) noexcept {
147 spec_ = std::move(other.spec_);
148 is_valid_ = other.is_valid_;
149 parsed_ = other.parsed_;
150 inner_url_ = std::move(other.inner_url_);
151
152 other.is_valid_ = false;
153 other.parsed_ = url::Parsed();
154 return *this;
155 }
156
spec() const157 const std::string& GURL::spec() const {
158 if (is_valid_ || spec_.empty())
159 return spec_;
160
161 DCHECK(false) << "Trying to get the spec of an invalid URL!";
162 return base::EmptyString();
163 }
164
operator <(const GURL & other) const165 bool GURL::operator<(const GURL& other) const {
166 return spec_ < other.spec_;
167 }
168
operator >(const GURL & other) const169 bool GURL::operator>(const GURL& other) const {
170 return spec_ > other.spec_;
171 }
172
173 // Note: code duplicated below (it's inconvenient to use a template here).
Resolve(base::StringPiece relative) const174 GURL GURL::Resolve(base::StringPiece relative) const {
175 // Not allowed for invalid URLs.
176 if (!is_valid_)
177 return GURL();
178
179 GURL result;
180 url::StdStringCanonOutput output(&result.spec_);
181 if (!url::ResolveRelative(spec_.data(), static_cast<int>(spec_.length()),
182 parsed_, relative.data(),
183 static_cast<int>(relative.length()),
184 nullptr, &output, &result.parsed_)) {
185 // Error resolving, return an empty URL.
186 return GURL();
187 }
188
189 output.Complete();
190 result.is_valid_ = true;
191 if (result.SchemeIsFileSystem()) {
192 result.inner_url_ =
193 std::make_unique<GURL>(result.spec_.data(), result.parsed_.Length(),
194 *result.parsed_.inner_parsed(), true);
195 }
196 return result;
197 }
198
199 // Note: code duplicated above (it's inconvenient to use a template here).
Resolve(base::StringPiece16 relative) const200 GURL GURL::Resolve(base::StringPiece16 relative) const {
201 // Not allowed for invalid URLs.
202 if (!is_valid_)
203 return GURL();
204
205 GURL result;
206 url::StdStringCanonOutput output(&result.spec_);
207 if (!url::ResolveRelative(spec_.data(), static_cast<int>(spec_.length()),
208 parsed_, relative.data(),
209 static_cast<int>(relative.length()),
210 nullptr, &output, &result.parsed_)) {
211 // Error resolving, return an empty URL.
212 return GURL();
213 }
214
215 output.Complete();
216 result.is_valid_ = true;
217 if (result.SchemeIsFileSystem()) {
218 result.inner_url_ =
219 std::make_unique<GURL>(result.spec_.data(), result.parsed_.Length(),
220 *result.parsed_.inner_parsed(), true);
221 }
222 return result;
223 }
224
225 // Note: code duplicated below (it's inconvenient to use a template here).
ReplaceComponents(const Replacements & replacements) const226 GURL GURL::ReplaceComponents(const Replacements& replacements) const {
227 GURL result;
228
229 // Not allowed for invalid URLs.
230 if (!is_valid_)
231 return GURL();
232
233 url::StdStringCanonOutput output(&result.spec_);
234 result.is_valid_ = url::ReplaceComponents(
235 spec_.data(), static_cast<int>(spec_.length()), parsed_, replacements,
236 NULL, &output, &result.parsed_);
237
238 output.Complete();
239
240 result.ProcessFileSystemURLAfterReplaceComponents();
241 return result;
242 }
243
244 // Note: code duplicated above (it's inconvenient to use a template here).
ReplaceComponents(const ReplacementsW & replacements) const245 GURL GURL::ReplaceComponents(const ReplacementsW& replacements) const {
246 GURL result;
247
248 // Not allowed for invalid URLs.
249 if (!is_valid_)
250 return GURL();
251
252 url::StdStringCanonOutput output(&result.spec_);
253 result.is_valid_ = url::ReplaceComponents(
254 spec_.data(), static_cast<int>(spec_.length()), parsed_, replacements,
255 NULL, &output, &result.parsed_);
256
257 output.Complete();
258
259 result.ProcessFileSystemURLAfterReplaceComponents();
260
261 return result;
262 }
263
ProcessFileSystemURLAfterReplaceComponents()264 void GURL::ProcessFileSystemURLAfterReplaceComponents() {
265 if (!is_valid_)
266 return;
267 if (SchemeIsFileSystem()) {
268 inner_url_ = std::make_unique<GURL>(spec_.data(), parsed_.Length(),
269 *parsed_.inner_parsed(), true);
270 }
271 }
272
DeprecatedGetOriginAsURL() const273 GURL GURL::DeprecatedGetOriginAsURL() const {
274 // This doesn't make sense for invalid or nonstandard URLs, so return
275 // the empty URL.
276 if (!is_valid_ || !IsStandard())
277 return GURL();
278
279 if (SchemeIsFileSystem())
280 return inner_url_->DeprecatedGetOriginAsURL();
281
282 Replacements replacements;
283 replacements.ClearUsername();
284 replacements.ClearPassword();
285 replacements.ClearPath();
286 replacements.ClearQuery();
287 replacements.ClearRef();
288
289 return ReplaceComponents(replacements);
290 }
291
GetAsReferrer() const292 GURL GURL::GetAsReferrer() const {
293 if (!is_valid() || !IsReferrerScheme(spec_.data(), parsed_.scheme))
294 return GURL();
295
296 if (!has_ref() && !has_username() && !has_password())
297 return GURL(*this);
298
299 Replacements replacements;
300 replacements.ClearRef();
301 replacements.ClearUsername();
302 replacements.ClearPassword();
303 return ReplaceComponents(replacements);
304 }
305
GetWithEmptyPath() const306 GURL GURL::GetWithEmptyPath() const {
307 // This doesn't make sense for invalid or nonstandard URLs, so return
308 // the empty URL.
309 if (!is_valid_ || !IsStandard())
310 return GURL();
311
312 // We could optimize this since we know that the URL is canonical, and we are
313 // appending a canonical path, so avoiding re-parsing.
314 GURL other(*this);
315 if (parsed_.path.len == 0)
316 return other;
317
318 // Clear everything after the path.
319 other.parsed_.query.reset();
320 other.parsed_.ref.reset();
321
322 // Set the path, since the path is longer than one, we can just set the
323 // first character and resize.
324 other.spec_[other.parsed_.path.begin] = '/';
325 other.parsed_.path.len = 1;
326 other.spec_.resize(other.parsed_.path.begin + 1);
327 return other;
328 }
329
GetWithoutFilename() const330 GURL GURL::GetWithoutFilename() const {
331 return Resolve(".");
332 }
333
GetWithoutRef() const334 GURL GURL::GetWithoutRef() const {
335 if (!has_ref())
336 return GURL(*this);
337
338 Replacements replacements;
339 replacements.ClearRef();
340 return ReplaceComponents(replacements);
341 }
342
IsStandard() const343 bool GURL::IsStandard() const {
344 return url::IsStandard(spec_.data(), parsed_.scheme);
345 }
346
IsAboutBlank() const347 bool GURL::IsAboutBlank() const {
348 return IsAboutUrl(url::kAboutBlankPath);
349 }
350
IsAboutSrcdoc() const351 bool GURL::IsAboutSrcdoc() const {
352 return IsAboutUrl(url::kAboutSrcdocPath);
353 }
354
SchemeIs(base::StringPiece lower_ascii_scheme) const355 bool GURL::SchemeIs(base::StringPiece lower_ascii_scheme) const {
356 DCHECK(base::IsStringASCII(lower_ascii_scheme));
357 DCHECK(base::ToLowerASCII(lower_ascii_scheme) == lower_ascii_scheme);
358
359 if (!has_scheme())
360 return lower_ascii_scheme.empty();
361 return scheme_piece() == lower_ascii_scheme;
362 }
363
SchemeIsHTTPOrHTTPS() const364 bool GURL::SchemeIsHTTPOrHTTPS() const {
365 return SchemeIs(url::kHttpsScheme) || SchemeIs(url::kHttpScheme);
366 }
367
SchemeIsWSOrWSS() const368 bool GURL::SchemeIsWSOrWSS() const {
369 return SchemeIs(url::kWsScheme) || SchemeIs(url::kWssScheme);
370 }
371
SchemeIsCryptographic() const372 bool GURL::SchemeIsCryptographic() const {
373 if (!has_scheme())
374 return false;
375 return SchemeIsCryptographic(scheme_piece());
376 }
377
SchemeIsCryptographic(base::StringPiece lower_ascii_scheme)378 bool GURL::SchemeIsCryptographic(base::StringPiece lower_ascii_scheme) {
379 DCHECK(base::IsStringASCII(lower_ascii_scheme));
380 DCHECK(base::ToLowerASCII(lower_ascii_scheme) == lower_ascii_scheme);
381
382 return lower_ascii_scheme == url::kHttpsScheme ||
383 lower_ascii_scheme == url::kWssScheme;
384 }
385
SchemeIsLocal() const386 bool GURL::SchemeIsLocal() const {
387 // The `filesystem:` scheme is not in the Fetch spec, but Chromium still
388 // supports it in large part. It should be treated as a local scheme too.
389 return SchemeIs(url::kAboutScheme) || SchemeIs(url::kBlobScheme) ||
390 SchemeIs(url::kDataScheme) || SchemeIs(url::kFileSystemScheme);
391 }
392
IntPort() const393 int GURL::IntPort() const {
394 if (parsed_.port.is_nonempty())
395 return url::ParsePort(spec_.data(), parsed_.port);
396 return url::PORT_UNSPECIFIED;
397 }
398
EffectiveIntPort() const399 int GURL::EffectiveIntPort() const {
400 int int_port = IntPort();
401 if (int_port == url::PORT_UNSPECIFIED && IsStandard())
402 return url::DefaultPortForScheme(spec_.data() + parsed_.scheme.begin,
403 parsed_.scheme.len);
404 return int_port;
405 }
406
ExtractFileName() const407 std::string GURL::ExtractFileName() const {
408 url::Component file_component;
409 url::ExtractFileName(spec_.data(), parsed_.path, &file_component);
410 return ComponentString(file_component);
411 }
412
PathForRequestPiece() const413 base::StringPiece GURL::PathForRequestPiece() const {
414 DCHECK(parsed_.path.is_nonempty())
415 << "Canonical path for requests should be non-empty";
416 if (parsed_.ref.is_valid()) {
417 // Clip off the reference when it exists. The reference starts after the
418 // #-sign, so we have to subtract one to also remove it.
419 return base::StringPiece(spec_).substr(
420 parsed_.path.begin, parsed_.ref.begin - parsed_.path.begin - 1);
421 }
422 // Compute the actual path length, rather than depending on the spec's
423 // terminator. If we're an inner_url, our spec continues on into our outer
424 // URL's path/query/ref.
425 int path_len = parsed_.path.len;
426 if (parsed_.query.is_valid())
427 path_len = parsed_.query.end() - parsed_.path.begin;
428
429 return base::StringPiece(spec_).substr(parsed_.path.begin, path_len);
430 }
431
PathForRequest() const432 std::string GURL::PathForRequest() const {
433 return std::string(PathForRequestPiece());
434 }
435
HostNoBrackets() const436 std::string GURL::HostNoBrackets() const {
437 return std::string(HostNoBracketsPiece());
438 }
439
HostNoBracketsPiece() const440 base::StringPiece GURL::HostNoBracketsPiece() const {
441 // If host looks like an IPv6 literal, strip the square brackets.
442 url::Component h(parsed_.host);
443 if (h.len >= 2 && spec_[h.begin] == '[' && spec_[h.end() - 1] == ']') {
444 h.begin++;
445 h.len -= 2;
446 }
447 return ComponentStringPiece(h);
448 }
449
GetContent() const450 std::string GURL::GetContent() const {
451 return std::string(GetContentPiece());
452 }
453
GetContentPiece() const454 base::StringPiece GURL::GetContentPiece() const {
455 if (!is_valid_)
456 return base::StringPiece();
457 url::Component content_component = parsed_.GetContent();
458 if (!SchemeIs(url::kJavaScriptScheme) && parsed_.ref.is_valid())
459 content_component.len -= parsed_.ref.len + 1;
460 return ComponentStringPiece(content_component);
461 }
462
HostIsIPAddress() const463 bool GURL::HostIsIPAddress() const {
464 return is_valid_ && url::HostIsIPAddress(host_piece());
465 }
466
EmptyGURL()467 const GURL& GURL::EmptyGURL() {
468 static base::NoDestructor<GURL> empty_gurl;
469 return *empty_gurl;
470 }
471
DomainIs(base::StringPiece canonical_domain) const472 bool GURL::DomainIs(base::StringPiece canonical_domain) const {
473 if (!is_valid_)
474 return false;
475
476 // FileSystem URLs have empty host_piece, so check this first.
477 if (inner_url_ && SchemeIsFileSystem())
478 return inner_url_->DomainIs(canonical_domain);
479 return url::DomainIs(host_piece(), canonical_domain);
480 }
481
EqualsIgnoringRef(const GURL & other) const482 bool GURL::EqualsIgnoringRef(const GURL& other) const {
483 int ref_position = parsed_.CountCharactersBefore(url::Parsed::REF, true);
484 int ref_position_other =
485 other.parsed_.CountCharactersBefore(url::Parsed::REF, true);
486 return base::StringPiece(spec_).substr(0, ref_position) ==
487 base::StringPiece(other.spec_).substr(0, ref_position_other);
488 }
489
Swap(GURL * other)490 void GURL::Swap(GURL* other) {
491 spec_.swap(other->spec_);
492 std::swap(is_valid_, other->is_valid_);
493 std::swap(parsed_, other->parsed_);
494 inner_url_.swap(other->inner_url_);
495 }
496
EstimateMemoryUsage() const497 size_t GURL::EstimateMemoryUsage() const {
498 return base::trace_event::EstimateMemoryUsage(spec_) +
499 base::trace_event::EstimateMemoryUsage(inner_url_) +
500 (parsed_.inner_parsed() ? sizeof(url::Parsed) : 0);
501 }
502
IsAboutUrl(base::StringPiece allowed_path) const503 bool GURL::IsAboutUrl(base::StringPiece allowed_path) const {
504 if (!SchemeIs(url::kAboutScheme))
505 return false;
506
507 if (has_host() || has_username() || has_password() || has_port())
508 return false;
509
510 return IsAboutPath(path_piece(), allowed_path);
511 }
512
513 // static
IsAboutPath(base::StringPiece actual_path,base::StringPiece allowed_path)514 bool GURL::IsAboutPath(base::StringPiece actual_path,
515 base::StringPiece allowed_path) {
516 if (!base::StartsWith(actual_path, allowed_path))
517 return false;
518
519 if (actual_path.size() == allowed_path.size()) {
520 DCHECK_EQ(actual_path, allowed_path);
521 return true;
522 }
523
524 if ((actual_path.size() == allowed_path.size() + 1) &&
525 actual_path.back() == '/') {
526 DCHECK_EQ(actual_path, std::string(allowed_path) + '/');
527 return true;
528 }
529
530 return false;
531 }
532
WriteIntoTrace(perfetto::TracedValue context) const533 void GURL::WriteIntoTrace(perfetto::TracedValue context) const {
534 std::move(context).WriteString(possibly_invalid_spec());
535 }
536
operator <<(std::ostream & out,const GURL & url)537 std::ostream& operator<<(std::ostream& out, const GURL& url) {
538 return out << url.possibly_invalid_spec();
539 }
540
operator ==(const GURL & x,const GURL & y)541 bool operator==(const GURL& x, const GURL& y) {
542 return x.possibly_invalid_spec() == y.possibly_invalid_spec();
543 }
544
operator !=(const GURL & x,const GURL & y)545 bool operator!=(const GURL& x, const GURL& y) {
546 return !(x == y);
547 }
548
operator ==(const GURL & x,const base::StringPiece & spec)549 bool operator==(const GURL& x, const base::StringPiece& spec) {
550 DCHECK_EQ(GURL(spec).possibly_invalid_spec(), spec)
551 << "Comparisons of GURLs and strings must ensure as a precondition that "
552 "the string is fully canonicalized.";
553 return x.possibly_invalid_spec() == spec;
554 }
555
operator ==(const base::StringPiece & spec,const GURL & x)556 bool operator==(const base::StringPiece& spec, const GURL& x) {
557 return x == spec;
558 }
559
operator !=(const GURL & x,const base::StringPiece & spec)560 bool operator!=(const GURL& x, const base::StringPiece& spec) {
561 return !(x == spec);
562 }
563
operator !=(const base::StringPiece & spec,const GURL & x)564 bool operator!=(const base::StringPiece& spec, const GURL& x) {
565 return !(x == spec);
566 }
567
568 namespace url::debug {
569
ScopedUrlCrashKey(base::debug::CrashKeyString * crash_key,const GURL & url)570 ScopedUrlCrashKey::ScopedUrlCrashKey(base::debug::CrashKeyString* crash_key,
571 const GURL& url)
572 : scoped_string_value_(
573 crash_key,
574 url.is_empty() ? "<empty url>" : url.possibly_invalid_spec()) {}
575
576 ScopedUrlCrashKey::~ScopedUrlCrashKey() = default;
577
578 } // namespace url::debug
579