/*
 * 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/compiler_specific.h"
#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 "base/strings/string_view_util.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/strcat.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_to_number.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 IsAsciiPrintable(c) && c != ' ' && !IsAsciiUpper(c);
  }));
}
#endif

// Note: You must ensure that |spec| is a valid canonicalized URL before calling
// this function.
std::string_view AsUrlChar8Subtle(const StringView& spec) {
  DCHECK(spec.Is8Bit());
  // Span8() 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 is safe as long as
  // you don't look at the fragment component.
  return base::as_string_view(spec.Span8());
}

// Returns a string_view on the given string, or a string_view on 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.
std::string_view CharactersOrEmpty(const StringUtf8Adaptor& string) {
  static const char kZero = 0;
  // NOLINTNEXTLINE(bugprone-string-constructor)
  return string.data() ? string.AsStringView() : std::string_view(&kZero, 0u);
}

bool IsSchemeFirstChar(char c) {
  return IsAsciiAlpha(c);
}

bool IsSchemeChar(char c) {
  return IsSchemeFirstChar(c) || IsAsciiDigit(c) || c == '.' || c == '-' ||
         c == '+';
}

bool IsUnicodeEncoding(const 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 TextEncoding* encoding)
      : encoding_(encoding) {}

  void ConvertFromUtf16(std::u16string_view input,
                        url::CanonOutput* output) override {
    std::string encoded = encoding_->Encode(
        String(input), UnencodableHandling::kUrlEncodedCharRef);
    output->Append(encoded);
  }

 private:
  raw_ptr<const TextEncoding> encoding_;
};

}  // namespace

bool IsValidProtocol(const StringView& protocol) {
  // RFC3986: ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
  if (protocol.empty())
    return false;
  if (!IsSchemeFirstChar(UNSAFE_BUFFERS(protocol[0]))) {
    return false;
  }
  unsigned protocol_length = protocol.length();
  for (unsigned i = 1; i < protocol_length; i++) {
    if (!IsSchemeChar(UNSAFE_BUFFERS(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 StringView& 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 String& string = string_;
  if (string.length() <= 1024) {
    return string;
  }
  StringView left_part(string, 0, 511);
  StringView right_part(string, string.length() - 510, 510);
  return StrCat({left_part, "...", right_part});
}

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 StringView& 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 StringView& 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 StringView& relative,
           const 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(string_.View16(), 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(string_.View16(), 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 StringView& 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 ':'.
  StringView new_protocol = protocol.substr(0, protocol.find(':'));
  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;
  }

  if (SchemeRegistry::IsSpecialScheme(Protocol())) {
    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(
        base::span(canon_protocol.view()).first(protocol_length));

    // 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.SetSchemeStr(CharactersOrEmpty(new_protocol_utf8));
  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 {

StringView ParsePortFromString(const StringView& value) {
  // "008080junk" needs to be treated as port "8080" and "000" as "0".
  wtf_size_t num_leading_digits = 0;
  while (num_leading_digits < value.length() &&
         IsAsciiDigit(UNSAFE_BUFFERS(value[num_leading_digits]))) {
    ++num_leading_digits;
  }
  wtf_size_t num_leading_zeros = 0;
  while (num_leading_zeros < num_leading_digits &&
         UNSAFE_BUFFERS(value[num_leading_zeros]) == '0') {
    ++num_leading_zeros;
  }
  // If all digits are zeros, consider the last one significant.
  if (num_leading_zeros == num_leading_digits && num_leading_zeros) {
    --num_leading_zeros;
  }
  return value.substr(num_leading_zeros,
                      num_leading_digits - num_leading_zeros);
}

// 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 == '\\';
}

StringView FindHostPart(const StringView& host, bool is_special) {
  wtf_size_t end = host.Find(is_special ? IsEndOfHostSpecial : IsEndOfHost);
  return host.substr(0, end);
}

}  // namespace

void KURL::SetHost(const String& input) {
  String host = RemoveUrlWhitespace(input);
  StringView truncated_host = FindHostPart(host, IsStandard());
  StringUtf8Adaptor host_utf8(truncated_host);
  url::Replacements<char> replacements;
  replacements.SetHostStr(CharactersOrEmpty(host_utf8));
  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);
  StringView host_and_port = FindHostPart(orig_host_and_port, IsStandard());

  // 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.rfind(']');
  if (ipv6_terminator == kNotFound) {
    ipv6_terminator =
        host_and_port.starts_with('[') ? 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;

  StringView host;
  StringView port;
  if (colon == kNotFound) {
    host = host_and_port;
  } else {
    host = host_and_port.substr(0, colon);
    port = ParsePortFromString(host_and_port.substr(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.SetHostStr(CharactersOrEmpty(host_utf8));
    ReplaceComponents(replacements);
  }

  // Replace port next.
  if (is_valid_ && !port.empty()) {
    url::Replacements<char> replacements;
    StringUtf8Adaptor port_utf8(port);
    replacements.SetPortStr(CharactersOrEmpty(port_utf8));
    ReplaceComponents(replacements, /*preserve_validity=*/true);
  }
}

void KURL::RemovePort() {
  if (!HasPort())
    return;
  url::Replacements<char> replacements;
  replacements.ClearPort();
  ReplaceComponents(replacements);
}

bool KURL::SetPort(const String& input) {
  String port = RemoveUrlWhitespace(input);
  StringView parsed_port = ParsePortFromString(port);
  if (parsed_port.empty()) {
    return false;
  }
  auto port_value = StringToUintLoose(parsed_port);
  if (!port_value || *port_value > UINT16_MAX) {
    return false;
  }
  SetPort(*port_value);
  return true;
}

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.SetPortStr(base::as_string_view(port_string.Span8()));
  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.SetUsernameStr(CharactersOrEmpty(user_utf8));
  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.SetPasswordStr(CharactersOrEmpty(pass_utf8));
  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.SetRefStr(CharactersOrEmpty(fragment_utf8));
  }
  ReplaceComponents(replacements);
}

void KURL::RemoveFragmentIdentifier() {
  if (!HasFragmentIdentifier()) {
    return;
  }
  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] == '?') {
    // Blink expects the query string to begin with a question mark, but
    // GoogleURL doesn't. So we trim off the question mark when setting.
    replacements.SetQueryStr(CharactersOrEmpty(query_utf8).substr(1u));
  } else {
    // When set with the empty string or something that doesn't begin with
    // a question mark, kurl.cc 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.cc will leave a '?' with nothing following it in the
    // URL, whereas we'll clear it.
    // FIXME We should eliminate this difference.
    replacements.SetQueryStr(CharactersOrEmpty(query_utf8));
  }
  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.SetPathStr(CharactersOrEmpty(path_utf8));
  ReplaceComponents(replacements);
}

String DecodeUrlEscapeSequences(const StringView& string, DecodeUrlMode mode) {
  StringUtf8Adaptor string_utf8(string);
  url::UrlEscapeDecoder unescaped(string_utf8.AsStringView(), mode);
  return StringImpl::Create8BitIfPossible(unescaped.view());
}

String EncodeWithUrlEscapeSequences(const StringView& not_encoded_string) {
  std::string utf8 =
      Utf8Encoding().Encode(not_encoded_string, UnencodableHandling::kNone);
  String escaped(base::span(url::UriComponentEncoder(utf8).view()));
  // Unescape '/'; it's safe and much prettier.
  escaped.Replace("%2F", "/");
  return escaped;
}

bool HasInvalidUrlEscapeSequences(const StringView& 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 {
  return IsHierarchical() && !IncludesCredentials() && !HasPort();
}

bool KURL::IsHierarchical() const {
  return IsStandard() || (IsValid() && !HasOpaquePath());
}

bool KURL::IsStandard() const {
  if (string_.IsNull() || parsed_.scheme.is_empty())
    return false;
  return string_.Is8Bit() ? url::IsStandard(AsUrlChar8Subtle(string_).substr(
                                parsed_.scheme.begin, parsed_.scheme.len))
                          : url::IsStandard(string_.View16().substr(
                                parsed_.scheme.begin, parsed_.scheme.len));
}

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(string_.View16(), parsed_.path, &filename);
  }
  return filename.begin;
}

bool ProtocolIs(const StringView& url, const char* protocol) {
#if DCHECK_IS_ON()
  AssertProtocolIsGood(protocol);
#endif
  if (url.IsNull())
    return false;
  if (url.Is8Bit()) {
    return url::FindAndCompareScheme(AsUrlChar8Subtle(url), protocol, nullptr);
  }
  return url::FindAndCompareScheme(std::u16string_view(url.Span16()), protocol,
                                   nullptr);
}

void KURL::Init(const KURL& base,
                const StringView& relative,
                const 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.AsStringView(), base.parsed_,
                                     relative_utf8.AsStringView(),
                                     charset_converter, &output, &parsed_);
  } else {
    is_valid_ = url::ResolveRelative(
        base_utf8.AsStringView(), base.parsed_,
        relative.IsNull() ? std::u16string_view()
                          : std::u16string_view(relative.Span16()),
        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() && relative.SharedImpl() &&
      StringView(output_url_span) == relative) {
    string_ = AtomicString(relative.SharedImpl());
  } else {
    string_ = AtomicString(output_url_span);
  }

  InitProtocolMetadata();
  InitInnerUrl();
  AssertStringSpecIsAscii();
}

void KURL::InitInnerUrl() {
  if (!is_valid_) {
    inner_url_.reset();
    return;
  }
  if (url::Parsed* inner_parsed = parsed_.inner_parsed()) {
    auto scheme_begin = inner_parsed->scheme.begin;
    inner_url_ = std::make_unique<KURL>(string_.GetString().substr(
        scheme_begin, inner_parsed->Length() - 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 == g_https_atom) {
    protocol_ = g_https_atom;
  } else if (protocol == g_http_atom) {
    protocol_ = g_http_atom;
  } else {
    protocol_ = protocol.ToAtomicString();
    protocol_is_in_http_family_ = false;
  }
  DCHECK(protocol_.ContainsNoAsciiUpper());
}

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.AsStringView(), 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.AsStringView(), 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();
}

std::ostream& operator<<(std::ostream& os, const KURL& url) {
  return os << url.GetString();
}

}  // namespace blink
