diff options
author | msramek <msramek@chromium.org> | 2015-08-18 06:04:19 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-08-18 13:05:06 +0000 |
commit | 9b7972dd82ae09d1d1c38c6078bc384759eb18ee (patch) | |
tree | 2aaaec212da3f07d6e37340e03f384d66b172530 /url/scheme_host_port.cc | |
parent | ed9b2a0e8440ce2ff16b4ed043bad27a995a39c6 (diff) | |
download | chromium_src-9b7972dd82ae09d1d1c38c6078bc384759eb18ee.zip chromium_src-9b7972dd82ae09d1d1c38c6078bc384759eb18ee.tar.gz chromium_src-9b7972dd82ae09d1d1c38c6078bc384759eb18ee.tar.bz2 |
Revert of Allow url::SchemeHostPort to hold non-file scheme without port (patchset #16 id:320001 of https://codereview.chromium.org/1272113002/ )
Reason for revert:
Broken build.
http://build.chromium.org/p/chromium.mac/builders/iOS_Device_%28ninja%29/builds/17899/steps/compile/logs/stdio
Original issue's description:
> Allow url::SchemeHostPort to hold non-file scheme without port
>
> WebSockets use url::Origin to pass origin info between renderer and
> browser. Currently, it cannot hold an origin with non-file scheme and
> no port. Chrome extensions have been using such origins, so we need
> to keep the channel to convey origin info work for such origins.
>
> BUG=516971
> R=sleevi,brettw
>
> Committed: https://crrev.com/1ac9ec7bccd1b5178b18338b10149f36292f5fb6
> Cr-Commit-Position: refs/heads/master@{#343895}
TBR=rsleevi@chromium.org,mkwst@chromium.org,brettw@chromium.org,tyoshino@chromium.org
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=516971
Review URL: https://codereview.chromium.org/1301563003
Cr-Commit-Position: refs/heads/master@{#343898}
Diffstat (limited to 'url/scheme_host_port.cc')
-rw-r--r-- | url/scheme_host_port.cc | 153 |
1 files changed, 50 insertions, 103 deletions
diff --git a/url/scheme_host_port.cc b/url/scheme_host_port.cc index 9c12295..c2fe830 100644 --- a/url/scheme_host_port.cc +++ b/url/scheme_host_port.cc @@ -7,7 +7,6 @@ #include <string.h> #include "base/logging.h" -#include "base/numerics/safe_conversions.h" #include "base/strings/string_number_conversions.h" #include "url/gurl.h" #include "url/url_canon.h" @@ -17,21 +16,25 @@ namespace url { -namespace { - -bool IsCanonicalHost(const base::StringPiece& host) { - std::string canon_host; +SchemeHostPort::SchemeHostPort() : port_(0) { +} +SchemeHostPort::SchemeHostPort(base::StringPiece scheme, + base::StringPiece host, + uint16 port) + : scheme_(scheme.data(), scheme.length()), + host_(host.data(), host.length()), + port_(port) { // Try to canonicalize the host (copy/pasted from net/base. :( ). - const Component raw_host_component(0, - base::checked_cast<int>(host.length())); - StdStringCanonOutput canon_host_output(&canon_host); - CanonHostInfo host_info; - CanonicalizeHostVerbose(host.data(), raw_host_component, - &canon_host_output, &host_info); + const url::Component raw_host_component(0, static_cast<int>(host.length())); + std::string canon_host; + url::StdStringCanonOutput canon_host_output(&canon_host); + url::CanonHostInfo host_info; + url::CanonicalizeHostVerbose(host.data(), raw_host_component, + &canon_host_output, &host_info); if (host_info.out_host.is_nonempty() && - host_info.family != CanonHostInfo::BROKEN) { + host_info.family != url::CanonHostInfo::BROKEN) { // Success! Assert that there's no extra garbage. canon_host_output.Complete(); DCHECK_EQ(host_info.out_host.len, static_cast<int>(canon_host.length())); @@ -40,95 +43,44 @@ bool IsCanonicalHost(const base::StringPiece& host) { canon_host.clear(); } - return host == canon_host; -} - -bool IsValidInput(const base::StringPiece& scheme, - const base::StringPiece& host, - uint16 port) { - SchemeType scheme_type = SCHEME_WITH_PORT; - bool is_standard = GetStandardSchemeType( - scheme.data(), - Component(0, base::checked_cast<int>(scheme.length())), - &scheme_type); - if (!is_standard) - return false; - - // These schemes do not follow the generic URL syntax, so we treat them as - // invalid (scheme, host, port) tuples (even though such URLs' _Origin_ might - // have a (scheme, host, port) tuple, they themselves do not). - if (scheme == kFileSystemScheme || scheme == kBlobScheme) - return false; - - switch (scheme_type) { - case SCHEME_WITH_PORT: - // A URL with |scheme| is required to have the host and port (may be - // omitted in a serialization if it's the same as the default value). - // Return an invalid instance if either of them is not given. - if (host.empty() || port == 0) - return false; - - if (!IsCanonicalHost(host)) - return false; - - return true; - - case SCHEME_WITHOUT_PORT: - if (port != 0) { - // Return an invalid object if a URL with the scheme never represents - // the port data but the given |port| is non-zero. - return false; - } - - if (!IsCanonicalHost(host)) - return false; - - return true; - - case SCHEME_WITHOUT_AUTHORITY: - return false; - - default: - NOTREACHED(); - return false; + // Return an invalid SchemeHostPort object if any of the following conditions + // hold: + // + // 1. The provided scheme is non-standard, 'blob:', or 'filesystem:'. + // 2. The provided host is non-canonical. + // 3. The scheme is 'file' and the port is non-zero. + // 4. The scheme is not 'file', and the port is zero or the host is empty. + bool isUnsupportedScheme = + !url::IsStandard(scheme.data(), + url::Component(0, static_cast<int>(scheme.length()))) || + scheme == kFileSystemScheme || scheme == kBlobScheme; + bool isNoncanonicalHost = host != canon_host; + bool isFileSchemeWithPort = scheme == kFileScheme && port != 0; + bool isNonFileSchemeWithoutPortOrHost = + scheme != kFileScheme && (port == 0 || host.empty()); + if (isUnsupportedScheme || isNoncanonicalHost || isFileSchemeWithPort || + isNonFileSchemeWithoutPortOrHost) { + scheme_.clear(); + host_.clear(); + port_ = 0; } } -} // namespace - -SchemeHostPort::SchemeHostPort() : port_(0) { -} - -SchemeHostPort::SchemeHostPort(base::StringPiece scheme, - base::StringPiece host, - uint16 port) - : port_(0) { - if (!IsValidInput(scheme, host, port)) - return; - - scheme.CopyToString(&scheme_); - host.CopyToString(&host_); - port_ = port; -} - SchemeHostPort::SchemeHostPort(const GURL& url) : port_(0) { - if (!url.is_valid()) + if (!url.is_valid() || !url.IsStandard()) return; - const std::string& scheme = url.scheme(); - const std::string& host = url.host(); - - // A valid GURL never returns PORT_INVALID. - int port = url.EffectiveIntPort(); - if (port == PORT_UNSPECIFIED) - port = 0; - - if (!IsValidInput(scheme, host, port)) + // These schemes do not follow the generic URL syntax, so we treat them as + // invalid (scheme, host, port) tuples (even though such URLs' _Origin_ might + // have a (scheme, host, port) tuple, they themselves do not). + if (url.SchemeIsBlob() || url.SchemeIsFileSystem()) return; - scheme_ = scheme; - host_ = host; - port_ = port; + scheme_ = url.scheme(); + host_ = url.host(); + port_ = url.EffectiveIntPort() == url::PORT_UNSPECIFIED + ? 0 + : url.EffectiveIntPort(); } SchemeHostPort::~SchemeHostPort() { @@ -143,20 +95,15 @@ std::string SchemeHostPort::Serialize() const { if (IsInvalid()) return result; + bool is_default_port = + port_ == url::DefaultPortForScheme(scheme_.data(), + static_cast<int>(scheme_.length())); + result.append(scheme_); result.append(kStandardSchemeSeparator); result.append(host_); - if (port_ == 0) - return result; - - // Omit the port component if the port matches with the default port - // defined for the scheme, if any. - int default_port = DefaultPortForScheme(scheme_.data(), - static_cast<int>(scheme_.length())); - if (default_port == PORT_UNSPECIFIED) - return result; - if (port_ != default_port) { + if (scheme_ != kFileScheme && !is_default_port) { result.push_back(':'); result.append(base::IntToString(port_)); } |