 Chromium Code Reviews
 Chromium Code Reviews Issue 1272113002:
  Allow url::SchemeHostPort to hold non-file scheme without port  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 1272113002:
  Allow url::SchemeHostPort to hold non-file scheme without port  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| OLD | NEW | 
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include "url/scheme_host_port.h" | 5 #include "url/scheme_host_port.h" | 
| 6 | 6 | 
| 7 #include <string.h> | 7 #include <string.h> | 
| 8 | 8 | 
| 9 #include "base/logging.h" | 9 #include "base/logging.h" | 
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" | 
| 11 #include "url/gurl.h" | 11 #include "url/gurl.h" | 
| 12 #include "url/url_canon.h" | 12 #include "url/url_canon.h" | 
| 13 #include "url/url_canon_stdstring.h" | 13 #include "url/url_canon_stdstring.h" | 
| 14 #include "url/url_constants.h" | 14 #include "url/url_constants.h" | 
| 15 #include "url/url_util.h" | 15 #include "url/url_util.h" | 
| 16 | 16 | 
| 17 namespace url { | 17 namespace url { | 
| 18 | 18 | 
| 19 SchemeHostPort::SchemeHostPort() : port_(0) { | 19 namespace { | 
| 20 } | |
| 21 | 20 | 
| 22 SchemeHostPort::SchemeHostPort(base::StringPiece scheme, | 21 bool IsCanonicalHost(const base::StringPiece& host) { | 
| 23 base::StringPiece host, | 22 std::string canon_host; | 
| 24 uint16 port) | 23 | 
| 25 : scheme_(scheme.data(), scheme.length()), | |
| 26 host_(host.data(), host.length()), | |
| 27 port_(port) { | |
| 28 // Try to canonicalize the host (copy/pasted from net/base. :( ). | 24 // Try to canonicalize the host (copy/pasted from net/base. :( ). | 
| 29 const url::Component raw_host_component(0, static_cast<int>(host.length())); | 25 const Component raw_host_component(0, static_cast<int>(host.length())); | 
| 
Ryan Sleevi
2015/08/13 22:32:12
Same DANGER remark applies below.
 
tyoshino (SeeGerritForStatus)
2015/08/14 06:29:01
Done.
 | |
| 30 std::string canon_host; | 26 StdStringCanonOutput canon_host_output(&canon_host); | 
| 31 url::StdStringCanonOutput canon_host_output(&canon_host); | 27 CanonHostInfo host_info; | 
| 32 url::CanonHostInfo host_info; | 28 CanonicalizeHostVerbose(host.data(), raw_host_component, | 
| 33 url::CanonicalizeHostVerbose(host.data(), raw_host_component, | |
| 34 &canon_host_output, &host_info); | 29 &canon_host_output, &host_info); | 
| 35 | 30 | 
| 36 if (host_info.out_host.is_nonempty() && | 31 if (host_info.out_host.is_nonempty() && | 
| 37 host_info.family != url::CanonHostInfo::BROKEN) { | 32 host_info.family != CanonHostInfo::BROKEN) { | 
| 38 // Success! Assert that there's no extra garbage. | 33 // Success! Assert that there's no extra garbage. | 
| 39 canon_host_output.Complete(); | 34 canon_host_output.Complete(); | 
| 40 DCHECK_EQ(host_info.out_host.len, static_cast<int>(canon_host.length())); | 35 DCHECK_EQ(host_info.out_host.len, static_cast<int>(canon_host.length())); | 
| 41 } else { | 36 } else { | 
| 42 // Empty host, or canonicalization failed. | 37 // Empty host, or canonicalization failed. | 
| 43 canon_host.clear(); | 38 canon_host.clear(); | 
| 44 } | 39 } | 
| 45 | 40 | 
| 46 // Return an invalid SchemeHostPort object if any of the following conditions | 41 return host == canon_host; | 
| 47 // hold: | |
| 48 // | |
| 49 // 1. The provided scheme is non-standard, 'blob:', or 'filesystem:'. | |
| 50 // 2. The provided host is non-canonical. | |
| 51 // 3. The scheme is 'file' and the port is non-zero. | |
| 52 // 4. The scheme is not 'file', and the port is zero or the host is empty. | |
| 53 bool isUnsupportedScheme = | |
| 54 !url::IsStandard(scheme.data(), | |
| 55 url::Component(0, static_cast<int>(scheme.length()))) || | |
| 56 scheme == kFileSystemScheme || scheme == kBlobScheme; | |
| 57 bool isNoncanonicalHost = host != canon_host; | |
| 58 bool isFileSchemeWithPort = scheme == kFileScheme && port != 0; | |
| 59 bool isNonFileSchemeWithoutPortOrHost = | |
| 60 scheme != kFileScheme && (port == 0 || host.empty()); | |
| 61 if (isUnsupportedScheme || isNoncanonicalHost || isFileSchemeWithPort || | |
| 62 isNonFileSchemeWithoutPortOrHost) { | |
| 63 scheme_.clear(); | |
| 64 host_.clear(); | |
| 65 port_ = 0; | |
| 66 } | |
| 67 } | 42 } | 
| 68 | 43 | 
| 69 SchemeHostPort::SchemeHostPort(const GURL& url) : port_(0) { | 44 bool IsValidInput(const base::StringPiece& scheme, | 
| 70 if (!url.is_valid() || !url.IsStandard()) | 45 const base::StringPiece& host, | 
| 71 return; | 46 uint16 port) { | 
| 47 SchemeType scheme_type = SCHEME_WITH_PORT; | |
| 48 bool is_standard = GetStandardSchemeType( | |
| 49 scheme.data(), | |
| 50 Component(0, static_cast<int>(scheme.length())), | |
| 
Ryan Sleevi
2015/08/13 22:32:12
DANGER: I missed this in the original review, but
 
tyoshino (SeeGerritForStatus)
2015/08/14 06:29:01
Agreed. Changed to checked_cast.
 | |
| 51 &scheme_type); | |
| 52 if (!is_standard) | |
| 53 return false; | |
| 72 | 54 | 
| 73 // These schemes do not follow the generic URL syntax, so we treat them as | 55 // These schemes do not follow the generic URL syntax, so we treat them as | 
| 74 // invalid (scheme, host, port) tuples (even though such URLs' _Origin_ might | 56 // invalid (scheme, host, port) tuples (even though such URLs' _Origin_ might | 
| 75 // have a (scheme, host, port) tuple, they themselves do not). | 57 // have a (scheme, host, port) tuple, they themselves do not). | 
| 76 if (url.SchemeIsBlob() || url.SchemeIsFileSystem()) | 58 if (scheme == kFileSystemScheme || scheme == kBlobScheme) | 
| 59 return false; | |
| 60 | |
| 61 switch (scheme_type) { | |
| 62 case SCHEME_WITH_PORT: | |
| 63 // A URL with |scheme| is required to have the host and port (may be | |
| 64 // omitted in a serialization if it's the same as the default value). | |
| 65 // Return an invalid instance if either of them is not given. | |
| 66 if (host.empty() || port == 0) | |
| 67 return false; | |
| 68 | |
| 69 if (!IsCanonicalHost(host)) | |
| 70 return false; | |
| 71 | |
| 72 return true; | |
| 73 | |
| 74 case SCHEME_WITHOUT_PORT: | |
| 75 if (port != 0) { | |
| 76 // Return an invalid object if the scheme is 'file' and the port is | |
| 
Ryan Sleevi
2015/08/13 22:32:12
This comment seems out of date/incorrect; that is,
 
tyoshino (SeeGerritForStatus)
2015/08/14 06:29:01
Updated
 | |
| 77 // non-zero. | |
| 78 return false; | |
| 79 } | |
| 80 | |
| 81 if (!IsCanonicalHost(host)) | |
| 82 return false; | |
| 83 | |
| 84 return true; | |
| 85 | |
| 86 case SCHEME_WITHOUT_AUTHORITY: | |
| 87 return false; | |
| 88 | |
| 89 default: | |
| 90 NOTREACHED(); | |
| 91 return false; | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 } // namespace | |
| 96 | |
| 97 SchemeHostPort::SchemeHostPort() : port_(0) { | |
| 98 } | |
| 99 | |
| 100 SchemeHostPort::SchemeHostPort(base::StringPiece scheme, | |
| 101 base::StringPiece host, | |
| 102 uint16 port) | |
| 103 : port_(0) { | |
| 104 if (!IsValidInput(scheme, host, port)) | |
| 77 return; | 105 return; | 
| 78 | 106 | 
| 79 scheme_ = url.scheme(); | 107 scheme.CopyToString(&scheme_); | 
| 80 host_ = url.host(); | 108 host.CopyToString(&host_); | 
| 81 port_ = url.EffectiveIntPort() == url::PORT_UNSPECIFIED | 109 port_ = port; | 
| 82 ? 0 | 110 } | 
| 83 : url.EffectiveIntPort(); | 111 | 
| 112 SchemeHostPort::SchemeHostPort(const GURL& url) : port_(0) { | |
| 113 if (!url.is_valid()) | |
| 114 return; | |
| 115 | |
| 116 const std::string& scheme = url.scheme(); | |
| 117 const std::string& host = url.host(); | |
| 118 | |
| 119 // A valid GURL never returns PORT_INVALID. | |
| 120 int port = url.EffectiveIntPort(); | |
| 121 if (port == PORT_UNSPECIFIED) | |
| 122 port = 0; | |
| 123 | |
| 124 if (!IsValidInput(scheme, host, port)) | |
| 125 return; | |
| 126 | |
| 127 scheme_ = scheme; | |
| 128 host_ = host; | |
| 129 port_ = port; | |
| 84 } | 130 } | 
| 85 | 131 | 
| 86 SchemeHostPort::~SchemeHostPort() { | 132 SchemeHostPort::~SchemeHostPort() { | 
| 87 } | 133 } | 
| 88 | 134 | 
| 89 bool SchemeHostPort::IsInvalid() const { | 135 bool SchemeHostPort::IsInvalid() const { | 
| 90 return scheme_.empty() && host_.empty() && !port_; | 136 return scheme_.empty() && host_.empty() && !port_; | 
| 91 } | 137 } | 
| 92 | 138 | 
| 93 std::string SchemeHostPort::Serialize() const { | 139 std::string SchemeHostPort::Serialize() const { | 
| 94 std::string result; | 140 std::string result; | 
| 95 if (IsInvalid()) | 141 if (IsInvalid()) | 
| 96 return result; | 142 return result; | 
| 97 | 143 | 
| 98 bool is_default_port = | |
| 99 port_ == url::DefaultPortForScheme(scheme_.data(), | |
| 100 static_cast<int>(scheme_.length())); | |
| 101 | |
| 102 result.append(scheme_); | 144 result.append(scheme_); | 
| 103 result.append(kStandardSchemeSeparator); | 145 result.append(kStandardSchemeSeparator); | 
| 104 result.append(host_); | 146 result.append(host_); | 
| 105 | 147 | 
| 106 if (scheme_ != kFileScheme && !is_default_port) { | 148 if (port_ == 0) | 
| 149 return result; | |
| 150 | |
| 151 // Omit the port component if the port matches with the default port | |
| 152 // defined for the scheme, if any. | |
| 153 int default_port = DefaultPortForScheme(scheme_.data(), | |
| 154 static_cast<int>(scheme_.length())); | |
| 155 if (default_port == PORT_UNSPECIFIED) | |
| 156 return result; | |
| 157 if (port_ != default_port) { | |
| 107 result.push_back(':'); | 158 result.push_back(':'); | 
| 108 result.append(base::IntToString(port_)); | 159 result.append(base::IntToString(port_)); | 
| 109 } | 160 } | 
| 110 | 161 | 
| 111 return result; | 162 return result; | 
| 112 } | 163 } | 
| 113 | 164 | 
| 114 bool SchemeHostPort::Equals(const SchemeHostPort& other) const { | 165 bool SchemeHostPort::Equals(const SchemeHostPort& other) const { | 
| 115 return port_ == other.port() && scheme_ == other.scheme() && | 166 return port_ == other.port() && scheme_ == other.scheme() && | 
| 116 host_ == other.host(); | 167 host_ == other.host(); | 
| 117 } | 168 } | 
| 118 | 169 | 
| 119 bool SchemeHostPort::operator<(const SchemeHostPort& other) const { | 170 bool SchemeHostPort::operator<(const SchemeHostPort& other) const { | 
| 120 if (port_ != other.port_) | 171 if (port_ != other.port_) | 
| 121 return port_ < other.port_; | 172 return port_ < other.port_; | 
| 122 if (scheme_ != other.scheme_) | 173 if (scheme_ != other.scheme_) | 
| 123 return scheme_ < other.scheme_; | 174 return scheme_ < other.scheme_; | 
| 124 if (host_ != other.host_) | 175 if (host_ != other.host_) | 
| 125 return host_ < other.host_; | 176 return host_ < other.host_; | 
| 126 return false; | 177 return false; | 
| 127 } | 178 } | 
| 128 | 179 | 
| 129 } // namespace url | 180 } // namespace url | 
| OLD | NEW |