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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
|
/*
* Copyright (C) 2004, 2007, 2008, 2011, 2012 Apple Inc. All rights reserved.
* Copyright (C) 2012 Research In Motion Limited. All rights reserved.
* Copyright (C) 2008, 2009, 2011 Google 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 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.
*/
#include "third_party/blink/renderer/platform/weborigin/kurl.h"
#include <algorithm>
#include <string_view>
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/numerics/checked_math.h"
#include "base/numerics/safe_conversions.h"
#include "third_party/blink/renderer/platform/weborigin/known_ports.h"
#include "third_party/blink/renderer/platform/weborigin/scheme_registry.h"
#include "third_party/blink/renderer/platform/wtf/math_extras.h"
#include "third_party/blink/renderer/platform/wtf/std_lib_extras.h"
#include "third_party/blink/renderer/platform/wtf/text/string_hash.h"
#include "third_party/blink/renderer/platform/wtf/text/string_statics.h"
#include "third_party/blink/renderer/platform/wtf/text/string_utf8_adaptor.h"
#include "third_party/blink/renderer/platform/wtf/text/text_encoding.h"
#include "third_party/perfetto/include/perfetto/tracing/traced_value.h"
#include "url/gurl.h"
#include "url/url_constants.h"
#include "url/url_features.h"
#include "url/url_util.h"
#ifndef NDEBUG
#include <stdio.h>
#endif
namespace blink {
namespace {
#if DCHECK_IS_ON()
void AssertProtocolIsGood(const StringView protocol) {
DCHECK(protocol != "");
DCHECK(std::ranges::all_of(protocol.Span8(), [](const LChar c) {
return c > ' ' && c < 0x7F && !(c >= 'A' && c <= 'Z');
}));
}
#endif
// Note: You must ensure that |spec| is a valid canonicalized URL before calling
// this function.
const char* AsURLChar8Subtle(const String& spec) {
DCHECK(spec.Is8Bit());
// characters8 really return characters in Latin-1, but because we
// canonicalize URL strings, we know that everything before the fragment
// identifier will actually be ASCII, which means this cast is safe as long as
// you don't look at the fragment component.
return base::as_chars(spec.Span8()).data();
}
// Returns the characters for the given string, or a pointer to a static empty
// string if the input string is null. This will always ensure we have a non-
// null character pointer since ReplaceComponents has special meaning for null.
const char* CharactersOrEmpty(const StringUTF8Adaptor& string) {
static const char kZero = 0;
return string.data() ? string.data() : &kZero;
}
bool IsSchemeFirstChar(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
bool IsSchemeChar(char c) {
return IsSchemeFirstChar(c) || (c >= '0' && c <= '9') || c == '.' ||
c == '-' || c == '+';
}
bool IsUnicodeEncoding(const WTF::TextEncoding* encoding) {
return encoding->EncodingForFormSubmission() == UTF8Encoding();
}
class KURLCharsetConverter final : public url::CharsetConverter {
DISALLOW_NEW();
public:
// The encoding parameter may be 0, but in this case the object must not be
// called.
explicit KURLCharsetConverter(const WTF::TextEncoding* encoding)
: encoding_(encoding) {}
void ConvertFromUTF16(std::u16string_view input,
url::CanonOutput* output) override {
std::string encoded = encoding_->Encode(
String(input), WTF::kURLEncodedEntitiesForUnencodables);
output->Append(encoded);
}
private:
raw_ptr<const WTF::TextEncoding> encoding_;
};
} // namespace
bool IsValidProtocol(const String& protocol) {
// RFC3986: ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
if (protocol.empty())
return false;
if (!IsSchemeFirstChar(protocol[0]))
return false;
unsigned protocol_length = protocol.length();
for (unsigned i = 1; i < protocol_length; i++) {
if (!IsSchemeChar(protocol[i]))
return false;
}
return true;
}
KURL KURL::UrlStrippedForUseAsReferrer() const {
if (!SchemeRegistry::ShouldTreatURLSchemeAsAllowedForReferrer(Protocol()))
return KURL();
KURL referrer(*this);
referrer.SetUser(String());
referrer.SetPass(String());
referrer.RemoveFragmentIdentifier();
return referrer;
}
String KURL::StrippedForUseAsReferrer() const {
return UrlStrippedForUseAsReferrer().GetString();
}
String KURL::StrippedForUseAsHref() const {
if (parsed_.username.is_nonempty() || parsed_.password.is_nonempty()) {
KURL href(*this);
href.SetUser(String());
href.SetPass(String());
return href.GetString();
}
return GetString();
}
bool KURL::IsLocalFile() const {
// Including feed here might be a bad idea since drag and drop uses this check
// and including feed would allow feeds to potentially let someone's blog
// read the contents of the clipboard on a drag, even without a drop.
// Likewise with using the FrameLoader::shouldTreatURLAsLocal() function.
return ProtocolIs(url::kFileScheme);
}
bool ProtocolIsJavaScript(const String& url) {
return ProtocolIs(url, url::kJavaScriptScheme);
}
const KURL& BlankURL() {
DEFINE_THREAD_SAFE_STATIC_LOCAL(KURL, blank_url,
(AtomicString(url::kAboutBlankURL)));
return blank_url;
}
const KURL& SrcdocURL() {
DEFINE_THREAD_SAFE_STATIC_LOCAL(KURL, srcdoc_url,
(AtomicString(url::kAboutSrcdocURL)));
return srcdoc_url;
}
bool KURL::IsAboutURL(const char* allowed_path) const {
if (!ProtocolIsAbout())
return false;
// Using `is_nonempty` for `host` and `is_valid` for `username` and `password`
// to replicate how GURL::IsAboutURL (and GURL::has_host vs
// GURL::has_username) works.
if (parsed_.host.is_nonempty() || parsed_.username.is_valid() ||
parsed_.password.is_valid() || HasPort()) {
return false;
}
StringView path = ComponentStringView(parsed_.path);
StringUTF8Adaptor path_utf8(path);
return GURL::IsAboutPath(path_utf8.AsStringView(), allowed_path);
}
bool KURL::IsAboutBlankURL() const {
return IsAboutURL(url::kAboutBlankPath);
}
bool KURL::IsAboutSrcdocURL() const {
return IsAboutURL(url::kAboutSrcdocPath);
}
const KURL& NullURL() {
DEFINE_THREAD_SAFE_STATIC_LOCAL(KURL, static_null_url, ());
return static_null_url;
}
String KURL::ElidedString() const {
const WTF::String& string = string_;
if (string.length() <= 1024) {
return string;
}
return string.Left(511) + "..." + string.Right(510);
}
KURL::KURL() : is_valid_(false), protocol_is_in_http_family_(false) {}
// Initializes with a string representing an absolute URL. No encoding
// information is specified. This generally happens when a KURL is converted
// to a string and then converted back. In this case, the URL is already
// canonical and in proper escaped form so needs no encoding. We treat it as
// UTF-8 just in case.
KURL::KURL(const String& url) {
if (!url.IsNull()) {
Init(NullURL(), url, nullptr);
AssertStringSpecIsASCII();
} else {
// WebCore expects us to preserve the nullness of strings when this
// constructor is used. In all other cases, it expects a non-null
// empty string, which is what Init() will create.
is_valid_ = false;
protocol_is_in_http_family_ = false;
}
}
// Initializes with a GURL. This is used to covert from a GURL to a KURL.
KURL::KURL(const GURL& gurl) {
Init(NullURL() /* base */, String(gurl.spec()) /* relative */,
nullptr /* query_encoding */);
AssertStringSpecIsASCII();
}
// Constructs a new URL given a base URL and a possibly relative input URL.
// This assumes UTF-8 encoding.
KURL::KURL(const KURL& base, const String& relative) {
Init(base, relative, nullptr);
AssertStringSpecIsASCII();
}
// Constructs a new URL given a base URL and a possibly relative input URL.
// Any query portion of the relative URL will be encoded in the given encoding.
KURL::KURL(const KURL& base,
const String& relative,
const WTF::TextEncoding& encoding) {
Init(base, relative, &encoding.EncodingForFormSubmission());
AssertStringSpecIsASCII();
}
KURL::KURL(const AtomicString& canonical_string,
const url::Parsed& parsed,
bool is_valid)
: is_valid_(is_valid),
protocol_is_in_http_family_(false),
parsed_(parsed),
string_(canonical_string) {
InitProtocolMetadata();
InitInnerURL();
// For URLs with non-ASCII hostnames canonical_string will be in punycode.
// We can't check has_idna2008_deviation_character_ without decoding punycode.
// here.
AssertStringSpecIsASCII();
}
KURL::KURL(const KURL& other)
: is_valid_(other.is_valid_),
protocol_is_in_http_family_(other.protocol_is_in_http_family_),
protocol_(other.protocol_),
parsed_(other.parsed_),
string_(other.string_) {
if (other.inner_url_.get())
inner_url_ = std::make_unique<KURL>(*other.inner_url_);
}
KURL::~KURL() = default;
KURL& KURL::operator=(const KURL& other) {
is_valid_ = other.is_valid_;
protocol_is_in_http_family_ = other.protocol_is_in_http_family_;
protocol_ = other.protocol_;
parsed_ = other.parsed_;
string_ = other.string_;
if (other.inner_url_)
inner_url_ = std::make_unique<KURL>(*other.inner_url_);
else
inner_url_.reset();
return *this;
}
bool KURL::IsNull() const {
return string_.IsNull();
}
bool KURL::IsEmpty() const {
return string_.empty();
}
bool KURL::IsValid() const {
return is_valid_;
}
bool KURL::HasPort() const {
return HostEnd() < PathStart();
}
bool KURL::ProtocolIsJavaScript() const {
return ComponentStringView(parsed_.scheme) == url::kJavaScriptScheme;
}
bool KURL::ProtocolIsInHTTPFamily() const {
return protocol_is_in_http_family_;
}
bool KURL::HasPath() const {
// Note that http://www.google.com/" has a path, the path is "/". This can
// return false only for invalid or nonstandard URLs.
return parsed_.path.is_valid();
}
StringView KURL::LastPathComponent() const {
if (!is_valid_) {
return StringViewForInvalidComponent();
}
DCHECK(!string_.IsNull());
// When the output ends in a slash, WebCore has different expectations than
// the GoogleURL library. For "/foo/bar/" the library will return the empty
// string, but WebCore wants "bar".
url::Component path = parsed_.path;
if (path.is_nonempty() && string_[path.end() - 1] == '/')
path.len--;
url::Component file;
if (string_.Is8Bit()) {
url::ExtractFileName(AsURLChar8Subtle(string_), path, &file);
} else {
url::ExtractFileName(UNSAFE_TODO(string_.Characters16()), path, &file);
}
// Bug: https://bugs.webkit.org/show_bug.cgi?id=21015 this function returns
// a null string when the path is empty, which we duplicate here.
if (file.is_empty()) {
return StringView();
}
return ComponentStringView(file);
}
String KURL::Protocol() const {
DCHECK_EQ(ComponentString(parsed_.scheme), protocol_);
return protocol_;
}
StringView KURL::Host() const {
return ComponentStringView(parsed_.host);
}
uint16_t KURL::Port() const {
if (!is_valid_ || parsed_.port.is_empty())
return 0;
DCHECK(!string_.IsNull());
int port =
string_.Is8Bit()
? url::ParsePort(AsURLChar8Subtle(string_), parsed_.port)
: url::ParsePort(UNSAFE_TODO(string_.Characters16()), parsed_.port);
DCHECK_NE(port, url::PORT_UNSPECIFIED); // Checked port.len <= 0 already.
DCHECK_NE(port, url::PORT_INVALID); // Checked is_valid_ already.
return static_cast<uint16_t>(port);
}
StringView KURL::Pass() const {
if (!parsed_.password.is_valid()) {
return StringView();
}
return ComponentStringView(parsed_.password);
}
StringView KURL::User() const {
if (!parsed_.username.is_valid()) {
return StringView();
}
return ComponentStringView(parsed_.username);
}
StringView KURL::FragmentIdentifier() const {
// Empty but present refs ("foo.com/bar#") should result in the empty
// string, which ComponentStringView will produce. Nonexistent refs
// should be the null string.
if (!parsed_.ref.is_valid()) {
return StringView();
}
return ComponentStringView(parsed_.ref);
}
StringView KURL::FragmentIdentifierWithLeadingNumberSign() const {
if (!parsed_.ref.is_valid()) {
return StringView();
}
if (!is_valid_ || parsed_.ref.is_empty()) {
return StringViewForInvalidComponent();
}
return StringView(GetString(), parsed_.ref.begin - 1, parsed_.ref.len + 1);
}
bool KURL::HasFragmentIdentifier() const {
return parsed_.ref.is_valid();
}
StringView KURL::BaseAsString() const {
return StringView(string_.GetString(), 0, PathAfterLastSlash());
}
StringView KURL::Query() const {
if (!parsed_.query.is_valid()) {
return StringView();
}
return ComponentStringView(parsed_.query);
}
StringView KURL::QueryWithLeadingQuestionMark() const {
if (!parsed_.query.is_valid()) {
return StringView();
}
if (!is_valid_ || parsed_.query.is_empty()) {
return StringViewForInvalidComponent();
}
return StringView(GetString(), parsed_.query.begin - 1,
parsed_.query.len + 1);
}
StringView KURL::GetPath() const {
return ComponentStringView(parsed_.path);
}
namespace {
bool IsASCIITabOrNewline(UChar ch) {
return ch == '\t' || ch == '\r' || ch == '\n';
}
// See https://url.spec.whatwg.org/#concept-basic-url-parser:
// 3. Remove all ASCII tab or newline from |input|.
//
// Matches url::RemoveURLWhitespace.
String RemoveURLWhitespace(const String& input) {
return input.RemoveCharacters(IsASCIITabOrNewline);
}
} // namespace
bool KURL::SetProtocol(const String& protocol) {
// We should remove whitespace from |protocol| according to spec, but Firefox
// and Safari don't do it.
// - https://url.spec.whatwg.org/#dom-url-protocol
// - https://github.com/whatwg/url/issues/609
// Firefox and IE remove everything after the first ':'.
wtf_size_t separator_position = protocol.find(':');
String new_protocol = protocol.Substring(0, separator_position);
StringUTF8Adaptor new_protocol_utf8(new_protocol);
// If KURL is given an invalid scheme, it returns failure without modifying
// the URL at all. This is in contrast to most other setters which modify
// the URL and set "m_isValid."
url::RawCanonOutputT<char> canon_protocol;
url::Component protocol_component;
if (!url::CanonicalizeScheme(new_protocol_utf8.AsStringView(),
&canon_protocol, &protocol_component) ||
protocol_component.is_empty()) {
return false;
}
DCHECK_EQ(protocol_component.begin, 0);
const wtf_size_t protocol_length =
base::checked_cast<wtf_size_t>(protocol_component.len);
const String new_protocol_canon =
String(base::span(canon_protocol.view()).first(protocol_length));
if (SchemeRegistry::IsSpecialScheme(Protocol())) {
// https://url.spec.whatwg.org/#scheme-state
// 2.1.1 If url’s scheme is a special scheme and buffer is not a special
// scheme, then return.
if (!SchemeRegistry::IsSpecialScheme(new_protocol_canon)) {
return true;
}
// The protocol is lower-cased during canonicalization.
const bool new_protocol_is_file = new_protocol_canon == url::kFileScheme;
const bool old_protocol_is_file = ProtocolIs(url::kFileScheme);
// https://url.spec.whatwg.org/#scheme-state
// 3. If url includes credentials or has a non-null port, and buffer is
// "file", then return.
if (new_protocol_is_file && !old_protocol_is_file &&
(HasPort() || parsed_.username.is_nonempty() ||
parsed_.password.is_nonempty())) {
// This fails silently, which is weird, but necessary to give the expected
// behaviour when setting location.protocol. See
// https://html.spec.whatwg.org/multipage/history.html#dom-location-protocol.
return true;
}
// 4. If url’s scheme is "file" and its host is an empty host, then return.
if (!new_protocol_is_file && old_protocol_is_file &&
parsed_.host.is_empty()) {
// This fails silently as above.
return true;
}
}
url::Replacements<char> replacements;
replacements.SetScheme(CharactersOrEmpty(new_protocol_utf8),
url::Component(0, new_protocol_utf8.size()));
ReplaceComponents(replacements);
// isValid could be false but we still return true here. This is because
// WebCore or JS scripts can build up a URL by setting individual
// components, and a JS exception is based on the return value of this
// function. We want to throw the exception and stop the script only when
// its trying to set a bad protocol, and not when it maybe just hasn't
// finished building up its final scheme.
return true;
}
namespace {
String ParsePortFromStringPosition(const String& value, unsigned port_start) {
// "008080junk" needs to be treated as port "8080" and "000" as "0".
size_t length = value.length();
unsigned port_end = port_start;
while (IsASCIIDigit(value[port_end]) && port_end < length)
++port_end;
while (value[port_start] == '0' && port_start < port_end - 1)
++port_start;
return value.Substring(port_start, port_end - port_start);
}
// Align with https://url.spec.whatwg.org/#host-state step 3, and also with the
// IsAuthorityTerminator() function in //url/third_party/mozilla/url_parse.cc.
bool IsEndOfHost(UChar ch) {
return ch == '/' || ch == '?' || ch == '#';
}
bool IsEndOfHostSpecial(UChar ch) {
return IsEndOfHost(ch) || ch == '\\';
}
wtf_size_t FindHostEnd(const String& host, bool is_special) {
wtf_size_t end = host.Find(is_special ? IsEndOfHostSpecial : IsEndOfHost);
if (end == kNotFound)
end = host.length();
return end;
}
} // namespace
void KURL::SetHost(const String& input) {
String host = RemoveURLWhitespace(input);
wtf_size_t value_end = FindHostEnd(host, IsStandard());
String truncated_host = host.Substring(0, value_end);
StringUTF8Adaptor host_utf8(truncated_host);
url::Replacements<char> replacements;
replacements.SetHost(CharactersOrEmpty(host_utf8),
url::Component(0, host_utf8.size()));
ReplaceComponents(replacements);
}
void KURL::SetHostAndPort(const String& input) {
// This method intentionally does very sloppy parsing for backwards
// compatibility. See https://url.spec.whatwg.org/#host-state for what we
// theoretically should be doing.
String orig_host_and_port = RemoveURLWhitespace(input);
wtf_size_t value_end = FindHostEnd(orig_host_and_port, IsStandard());
String host_and_port = orig_host_and_port.Substring(0, value_end);
// This logic for handling IPv6 addresses is adapted from ParseServerInfo in
// //url/third_party/mozilla/url_parse.cc. There's a slight behaviour
// difference for compatibility with the tests: the first colon after the
// address is considered to start the port, instead of the last.
wtf_size_t ipv6_terminator = host_and_port.ReverseFind(']');
if (ipv6_terminator == kNotFound) {
ipv6_terminator =
host_and_port.StartsWith('[') ? host_and_port.length() : 0;
}
wtf_size_t colon = host_and_port.find(':', ipv6_terminator);
// Legacy behavior: ignore input if host part is empty
if (colon == 0)
return;
String host;
String port;
if (colon == kNotFound) {
host = host_and_port;
} else {
host = host_and_port.Substring(0, colon);
port = ParsePortFromStringPosition(host_and_port, colon + 1);
}
// Replace host and port separately in order to maintain the original port if
// a valid host and invalid port are provided together.
// Replace host first.
{
url::Replacements<char> replacements;
StringUTF8Adaptor host_utf8(host);
replacements.SetHost(CharactersOrEmpty(host_utf8),
url::Component(0, host_utf8.size()));
ReplaceComponents(replacements);
}
// Replace port next.
if (is_valid_ && !port.empty()) {
url::Replacements<char> replacements;
StringUTF8Adaptor port_utf8(port);
replacements.SetPort(CharactersOrEmpty(port_utf8),
url::Component(0, port_utf8.size()));
ReplaceComponents(replacements, /*preserve_validity=*/true);
}
}
void KURL::RemovePort() {
if (!HasPort())
return;
url::Replacements<char> replacements;
replacements.ClearPort();
ReplaceComponents(replacements);
}
void KURL::SetPort(const String& input) {
String port = RemoveURLWhitespace(input);
String parsed_port = ParsePortFromStringPosition(port, 0);
if (parsed_port.empty()) {
return;
}
bool to_uint_ok;
unsigned port_value = parsed_port.ToUInt(&to_uint_ok);
if (port_value > UINT16_MAX || !to_uint_ok) {
return;
}
SetPort(port_value);
}
void KURL::SetPort(uint16_t port) {
if (IsDefaultPortForProtocol(port, Protocol())) {
RemovePort();
return;
}
String port_string = String::Number(port);
DCHECK(port_string.Is8Bit());
url::Replacements<char> replacements;
replacements.SetPort(base::as_chars(port_string.Span8()).data(),
url::Component(0, port_string.length()));
ReplaceComponents(replacements);
}
void KURL::SetUser(const String& user) {
// This function is commonly called to clear the username, which we
// normally don't have, so we optimize this case.
if (user.empty() && !parsed_.username.is_valid())
return;
// The canonicalizer will clear any usernames that are empty, so we
// don't have to explicitly call ClearUsername() here.
//
// Unlike other setters, we do not remove whitespace per spec:
// https://url.spec.whatwg.org/#dom-url-username
StringUTF8Adaptor user_utf8(user);
url::Replacements<char> replacements;
replacements.SetUsername(CharactersOrEmpty(user_utf8),
url::Component(0, user_utf8.size()));
ReplaceComponents(replacements);
}
void KURL::SetPass(const String& pass) {
// This function is commonly called to clear the password, which we
// normally don't have, so we optimize this case.
if (pass.empty() && !parsed_.password.is_valid())
return;
// The canonicalizer will clear any passwords that are empty, so we
// don't have to explicitly call ClearUsername() here.
//
// Unlike other setters, we do not remove whitespace per spec:
// https://url.spec.whatwg.org/#dom-url-password
StringUTF8Adaptor pass_utf8(pass);
url::Replacements<char> replacements;
replacements.SetPassword(CharactersOrEmpty(pass_utf8),
url::Component(0, pass_utf8.size()));
ReplaceComponents(replacements);
}
void KURL::SetFragmentIdentifier(const String& input) {
// This function is commonly called to clear the ref, which we
// normally don't have, so we optimize this case.
if (input.IsNull() && !parsed_.ref.is_valid())
return;
String fragment = RemoveURLWhitespace(input);
StringUTF8Adaptor fragment_utf8(fragment);
url::Replacements<char> replacements;
if (fragment.IsNull()) {
replacements.ClearRef();
} else {
replacements.SetRef(CharactersOrEmpty(fragment_utf8),
url::Component(0, fragment_utf8.size()));
}
ReplaceComponents(replacements);
}
void KURL::RemoveFragmentIdentifier() {
url::Replacements<char> replacements;
replacements.ClearRef();
ReplaceComponents(replacements);
}
void KURL::SetQuery(const String& input) {
String query = RemoveURLWhitespace(input);
StringUTF8Adaptor query_utf8(query);
url::Replacements<char> replacements;
if (query.IsNull()) {
// KURL.cpp sets to null to clear any query.
replacements.ClearQuery();
} else if (query.length() > 0 && query[0] == '?') {
// WebCore expects the query string to begin with a question mark, but
// GoogleURL doesn't. So we trim off the question mark when setting.
replacements.SetQuery(CharactersOrEmpty(query_utf8),
url::Component(1, query_utf8.size() - 1));
} else {
// When set with the empty string or something that doesn't begin with
// a question mark, KURL.cpp will add a question mark for you. The only
// way this isn't compatible is if you call this function with an empty
// string. KURL.cpp will leave a '?' with nothing following it in the
// URL, whereas we'll clear it.
// FIXME We should eliminate this difference.
replacements.SetQuery(CharactersOrEmpty(query_utf8),
url::Component(0, query_utf8.size()));
}
ReplaceComponents(replacements);
}
void KURL::SetPath(const String& input) {
// Empty paths will be canonicalized to "/", so we don't have to worry
// about calling ClearPath().
String path = RemoveURLWhitespace(input);
StringUTF8Adaptor path_utf8(path);
url::Replacements<char> replacements;
replacements.SetPath(CharactersOrEmpty(path_utf8),
url::Component(0, path_utf8.size()));
ReplaceComponents(replacements);
}
String DecodeURLEscapeSequences(const StringView& string, DecodeURLMode mode) {
StringUTF8Adaptor string_utf8(string);
url::RawCanonOutputT<char16_t> unescaped;
url::DecodeURLEscapeSequences(string_utf8.AsStringView(), mode, &unescaped);
return StringImpl::Create8BitIfPossible(unescaped.view());
}
String EncodeWithURLEscapeSequences(const StringView& not_encoded_string) {
std::string utf8 =
UTF8Encoding().Encode(not_encoded_string, WTF::kNoUnencodables);
url::RawCanonOutputT<char> buffer;
size_t input_length = utf8.length();
if (buffer.capacity() < input_length * 3)
buffer.Resize(input_length * 3);
url::EncodeURIComponent(utf8, &buffer);
String escaped(base::span(buffer.view()));
// Unescape '/'; it's safe and much prettier.
escaped.Replace("%2F", "/");
return escaped;
}
bool HasInvalidURLEscapeSequences(const String& string) {
StringUTF8Adaptor string_utf8(string);
return url::HasInvalidURLEscapeSequences(string_utf8.AsStringView());
}
bool KURL::CanSetHostOrPort() const {
return IsHierarchical();
}
bool KURL::CanSetPathname() const {
return IsHierarchical();
}
bool KURL::CanRemoveHost() const {
if (url::IsUsingStandardCompliantNonSpecialSchemeURLParsing()) {
return IsHierarchical() && !IncludesCredentials() && !HasPort();
}
return false;
}
bool KURL::IsHierarchical() const {
if (url::IsUsingStandardCompliantNonSpecialSchemeURLParsing()) {
return IsStandard() || (IsValid() && !HasOpaquePath());
}
return IsStandard();
}
bool KURL::IsStandard() const {
if (string_.IsNull() || parsed_.scheme.is_empty())
return false;
return string_.Is8Bit()
? url::IsStandard(AsURLChar8Subtle(string_), parsed_.scheme)
: url::IsStandard(UNSAFE_TODO(string_.Characters16()),
parsed_.scheme);
}
bool EqualIgnoringFragmentIdentifier(const KURL& a, const KURL& b) {
// Compute the length of each URL without its ref. Note that the reference
// begin (if it exists) points to the character *after* the '#', so we need
// to subtract one.
int a_length = a.string_.length();
if (a.parsed_.ref.is_valid())
a_length = a.parsed_.ref.begin - 1;
int b_length = b.string_.length();
if (b.parsed_.ref.is_valid())
b_length = b.parsed_.ref.begin - 1;
if (a_length != b_length)
return false;
const String& a_string = a.string_;
const String& b_string = b.string_;
// FIXME: Abstraction this into a function in WTFString.h.
for (int i = 0; i < a_length; ++i) {
if (a_string[i] != b_string[i])
return false;
}
return true;
}
unsigned KURL::HostStart() const {
return parsed_.CountCharactersBefore(url::Parsed::HOST, false);
}
unsigned KURL::HostEnd() const {
return parsed_.CountCharactersBefore(url::Parsed::PORT, true);
}
unsigned KURL::PathStart() const {
return parsed_.CountCharactersBefore(url::Parsed::PATH, false);
}
unsigned KURL::PathEnd() const {
return parsed_.CountCharactersBefore(url::Parsed::QUERY, true);
}
unsigned KURL::PathAfterLastSlash() const {
if (string_.IsNull())
return 0;
if (!is_valid_ || !parsed_.path.is_valid())
return parsed_.CountCharactersBefore(url::Parsed::PATH, false);
url::Component filename;
if (string_.Is8Bit()) {
url::ExtractFileName(AsURLChar8Subtle(string_), parsed_.path, &filename);
} else {
url::ExtractFileName(UNSAFE_TODO(string_.Characters16()), parsed_.path,
&filename);
}
return filename.begin;
}
bool ProtocolIs(const String& url, const char* protocol) {
#if DCHECK_IS_ON()
AssertProtocolIsGood(protocol);
#endif
if (url.IsNull())
return false;
if (url.Is8Bit()) {
return url::FindAndCompareScheme(AsURLChar8Subtle(url), url.length(),
protocol, nullptr);
}
return url::FindAndCompareScheme(UNSAFE_TODO(url.Characters16()),
url.length(), protocol, nullptr);
}
void KURL::Init(const KURL& base,
const String& relative,
const WTF::TextEncoding* query_encoding) {
// As a performance optimization, we do not use the charset converter
// if encoding is UTF-8 or other Unicode encodings. Note that this is
// per HTML5 2.5.3 (resolving URL). The URL canonicalizer will be more
// efficient with no charset converter object because it can do UTF-8
// internally with no extra copies.
StringUTF8Adaptor base_utf8(base.GetString());
// We feel free to make the charset converter object every time since it's
// just a wrapper around a reference.
KURLCharsetConverter charset_converter_object(query_encoding);
KURLCharsetConverter* charset_converter =
(!query_encoding || IsUnicodeEncoding(query_encoding))
? nullptr
: &charset_converter_object;
// Clamp to int max to avoid overflow.
url::RawCanonOutputT<char> output;
if (!relative.IsNull() && relative.Is8Bit()) {
StringUTF8Adaptor relative_utf8(relative);
is_valid_ = url::ResolveRelative(base_utf8.data(), base_utf8.size(),
base.parsed_, relative_utf8.data(),
ClampTo<int>(relative_utf8.size()),
charset_converter, &output, &parsed_);
} else {
is_valid_ = url::ResolveRelative(
base_utf8.data(), base_utf8.size(), base.parsed_,
UNSAFE_TODO(relative.Characters16()), ClampTo<int>(relative.length()),
charset_converter, &output, &parsed_);
}
// Constructing an AtomicString will re-hash the raw output and check the
// AtomicStringTable (addWithTranslator) for the string. This can be very
// expensive for large URLs. However, since many URLs are generated from
// existing AtomicStrings (which already have their hashes computed), the fast
// path can often avoid this work.
const auto output_url_span = base::as_byte_span(output.view());
if (!relative.IsNull() && StringView(output_url_span) == relative) {
string_ = AtomicString(relative.Impl());
} else {
string_ = AtomicString(output_url_span);
}
InitProtocolMetadata();
InitInnerURL();
AssertStringSpecIsASCII();
if (!url::IsUsingStandardCompliantNonSpecialSchemeURLParsing()) {
// This assertion implicitly assumes that "javascript:" scheme URL is always
// valid, but that is no longer true when
// kStandardCompliantNonSpecialSchemeURLParsing feature is enabled. e.g.
// "javascript://^", which is an invalid URL.
DCHECK(!::blink::ProtocolIsJavaScript(string_) || ProtocolIsJavaScript());
}
}
void KURL::InitInnerURL() {
if (!is_valid_) {
inner_url_.reset();
return;
}
if (url::Parsed* inner_parsed = parsed_.inner_parsed()) {
inner_url_ = std::make_unique<KURL>(string_.GetString().Substring(
inner_parsed->scheme.begin,
inner_parsed->Length() - inner_parsed->scheme.begin));
} else {
inner_url_.reset();
}
}
void KURL::InitProtocolMetadata() {
if (!is_valid_) {
protocol_is_in_http_family_ = false;
protocol_ = ComponentString(parsed_.scheme);
return;
}
DCHECK(!string_.IsNull());
StringView protocol = ComponentStringView(parsed_.scheme);
protocol_is_in_http_family_ = true;
if (protocol == WTF::g_https_atom) {
protocol_ = WTF::g_https_atom;
} else if (protocol == WTF::g_http_atom) {
protocol_ = WTF::g_http_atom;
} else {
protocol_ = protocol.ToAtomicString();
protocol_is_in_http_family_ = false;
}
DCHECK_EQ(protocol_, protocol_.DeprecatedLower());
}
void KURL::AssertStringSpecIsASCII() {
// //url canonicalizes to 7-bit ASCII, using punycode and percent-escapes.
// This means that even though KURL itself might sometimes contain 16-bit
// strings, it is still safe to reuse the `url::Parsed' object from the
// canonicalization step: the byte offsets in `url::Parsed` will still be
// valid for a 16-bit ASCII string, since there is a 1:1 mapping between the
// UTF-8 indices and UTF-16 indices.
DCHECK(string_.GetString().ContainsOnlyASCIIOrEmpty());
// It is not possible to check that `string_` is 8-bit here. There are some
// instances where `string_` reuses an already-canonicalized `AtomicString`
// which only contains ASCII characters but, for some reason or another, uses
// 16-bit characters.
}
bool KURL::ProtocolIs(const StringView protocol) const {
#if DCHECK_IS_ON()
AssertProtocolIsGood(protocol);
#endif
// JavaScript URLs are "valid" and should be executed even if KURL decides
// they are invalid. The free function protocolIsJavaScript() should be used
// instead.
// FIXME: Chromium code needs to be fixed for this assert to be enabled.
// DCHECK(strcmp(protocol, "javascript"));
return protocol_ == protocol;
}
StringView KURL::StringViewForInvalidComponent() const {
return string_.IsNull() ? StringView() : StringView(StringImpl::empty_);
}
StringView KURL::ComponentStringView(const url::Component& component) const {
if (!is_valid_ || component.is_empty())
return StringViewForInvalidComponent();
// begin and len are in terms of bytes which do not match
// if string() is UTF-16 and input contains non-ASCII characters.
// However, the only part in urlString that can contain non-ASCII
// characters is 'ref' at the end of the string. In that case,
// begin will always match the actual value and len (in terms of
// byte) will be longer than what's needed by 'mid'. However, mid
// truncates len to avoid go past the end of a string so that we can
// get away without doing anything here.
int max_length = GetString().length() - component.begin;
return StringView(GetString(), component.begin,
component.len > max_length ? max_length : component.len);
}
String KURL::ComponentString(const url::Component& component) const {
return ComponentStringView(component).ToString();
}
template <typename CHAR>
void KURL::ReplaceComponents(const url::Replacements<CHAR>& replacements,
bool preserve_validity) {
url::RawCanonOutputT<char> output;
url::Parsed new_parsed;
bool replacements_valid;
{
StringUTF8Adaptor utf8(string_);
replacements_valid =
url::ReplaceComponents(utf8.data(), utf8.size(), parsed_, replacements,
nullptr, &output, &new_parsed);
// `utf8` should be destructed before replacing `string_`.
}
if (replacements_valid || !preserve_validity) {
is_valid_ = replacements_valid;
parsed_ = new_parsed;
string_ = AtomicString(base::as_byte_span(output.view()));
InitProtocolMetadata();
AssertStringSpecIsASCII();
}
}
void KURL::WriteIntoTrace(perfetto::TracedValue context) const {
return perfetto::WriteIntoTracedValue(std::move(context), GetString());
}
KURL::operator GURL() const {
StringUTF8Adaptor utf8(string_);
return GURL(utf8.data(), utf8.size(), parsed_, is_valid_);
}
bool operator==(const KURL& a, const KURL& b) {
return a.GetString() == b.GetString();
}
bool operator==(const KURL& a, const String& b) {
return a.GetString() == b;
}
bool operator==(const String& a, const KURL& b) {
return a == b.GetString();
}
bool operator!=(const KURL& a, const KURL& b) {
return a.GetString() != b.GetString();
}
bool operator!=(const KURL& a, const String& b) {
return a.GetString() != b;
}
bool operator!=(const String& a, const KURL& b) {
return a != b.GetString();
}
std::ostream& operator<<(std::ostream& os, const KURL& url) {
return os << url.GetString();
}
} // namespace blink
|