Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(421)

Side by Side Diff: url/scheme_host_port.cc

Issue 1272113002: Allow url::SchemeHostPort to hold non-file scheme without port (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « url/scheme_host_port.h ('k') | url/url_util.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/numerics/safe_conversions.h"
10 #include "base/strings/string_number_conversions.h" 11 #include "base/strings/string_number_conversions.h"
11 #include "url/gurl.h" 12 #include "url/gurl.h"
12 #include "url/url_canon.h" 13 #include "url/url_canon.h"
13 #include "url/url_canon_stdstring.h" 14 #include "url/url_canon_stdstring.h"
14 #include "url/url_constants.h" 15 #include "url/url_constants.h"
15 #include "url/url_util.h" 16 #include "url/url_util.h"
16 17
17 namespace url { 18 namespace url {
18 19
19 SchemeHostPort::SchemeHostPort() : port_(0) { 20 namespace {
20 }
21 21
22 SchemeHostPort::SchemeHostPort(base::StringPiece scheme, 22 bool IsCanonicalHost(const base::StringPiece& host) {
23 base::StringPiece host, 23 std::string canon_host;
24 uint16 port) 24
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. :( ). 25 // Try to canonicalize the host (copy/pasted from net/base. :( ).
29 const url::Component raw_host_component(0, static_cast<int>(host.length())); 26 const Component raw_host_component(0,
30 std::string canon_host; 27 base::checked_cast<int>(host.length()));
31 url::StdStringCanonOutput canon_host_output(&canon_host); 28 StdStringCanonOutput canon_host_output(&canon_host);
32 url::CanonHostInfo host_info; 29 CanonHostInfo host_info;
33 url::CanonicalizeHostVerbose(host.data(), raw_host_component, 30 CanonicalizeHostVerbose(host.data(), raw_host_component,
34 &canon_host_output, &host_info); 31 &canon_host_output, &host_info);
35 32
36 if (host_info.out_host.is_nonempty() && 33 if (host_info.out_host.is_nonempty() &&
37 host_info.family != url::CanonHostInfo::BROKEN) { 34 host_info.family != CanonHostInfo::BROKEN) {
38 // Success! Assert that there's no extra garbage. 35 // Success! Assert that there's no extra garbage.
39 canon_host_output.Complete(); 36 canon_host_output.Complete();
40 DCHECK_EQ(host_info.out_host.len, static_cast<int>(canon_host.length())); 37 DCHECK_EQ(host_info.out_host.len, static_cast<int>(canon_host.length()));
41 } else { 38 } else {
42 // Empty host, or canonicalization failed. 39 // Empty host, or canonicalization failed.
43 canon_host.clear(); 40 canon_host.clear();
44 } 41 }
45 42
46 // Return an invalid SchemeHostPort object if any of the following conditions 43 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 } 44 }
68 45
69 SchemeHostPort::SchemeHostPort(const GURL& url) : port_(0) { 46 bool IsValidInput(const base::StringPiece& scheme,
70 if (!url.is_valid() || !url.IsStandard()) 47 const base::StringPiece& host,
71 return; 48 uint16 port) {
49 SchemeType scheme_type = SCHEME_WITH_PORT;
50 bool is_standard = GetStandardSchemeType(
51 scheme.data(),
52 Component(0, base::checked_cast<int>(scheme.length())),
53 &scheme_type);
54 if (!is_standard)
55 return false;
72 56
73 // These schemes do not follow the generic URL syntax, so we treat them as 57 // 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 58 // invalid (scheme, host, port) tuples (even though such URLs' _Origin_ might
75 // have a (scheme, host, port) tuple, they themselves do not). 59 // have a (scheme, host, port) tuple, they themselves do not).
76 if (url.SchemeIsBlob() || url.SchemeIsFileSystem()) 60 if (scheme == kFileSystemScheme || scheme == kBlobScheme)
61 return false;
62
63 switch (scheme_type) {
64 case SCHEME_WITH_PORT:
65 // A URL with |scheme| is required to have the host and port (may be
66 // omitted in a serialization if it's the same as the default value).
67 // Return an invalid instance if either of them is not given.
68 if (host.empty() || port == 0)
69 return false;
70
71 if (!IsCanonicalHost(host))
72 return false;
73
74 return true;
75
76 case SCHEME_WITHOUT_PORT:
77 if (port != 0) {
78 // Return an invalid object if a URL with the scheme never represents
79 // the port data but the given |port| is non-zero.
80 return false;
81 }
82
83 if (!IsCanonicalHost(host))
84 return false;
85
86 return true;
87
88 case SCHEME_WITHOUT_AUTHORITY:
89 return false;
90
91 default:
92 NOTREACHED();
93 return false;
94 }
95 }
96
97 } // namespace
98
99 SchemeHostPort::SchemeHostPort() : port_(0) {
100 }
101
102 SchemeHostPort::SchemeHostPort(base::StringPiece scheme,
103 base::StringPiece host,
104 uint16 port)
105 : port_(0) {
106 if (!IsValidInput(scheme, host, port))
77 return; 107 return;
78 108
79 scheme_ = url.scheme(); 109 scheme.CopyToString(&scheme_);
80 host_ = url.host(); 110 host.CopyToString(&host_);
81 port_ = url.EffectiveIntPort() == url::PORT_UNSPECIFIED 111 port_ = port;
82 ? 0 112 }
83 : url.EffectiveIntPort(); 113
114 SchemeHostPort::SchemeHostPort(const GURL& url) : port_(0) {
115 if (!url.is_valid())
116 return;
117
118 const std::string& scheme = url.scheme();
119 const std::string& host = url.host();
120
121 // A valid GURL never returns PORT_INVALID.
122 int port = url.EffectiveIntPort();
123 if (port == PORT_UNSPECIFIED)
124 port = 0;
125
126 if (!IsValidInput(scheme, host, port))
127 return;
128
129 scheme_ = scheme;
130 host_ = host;
131 port_ = port;
84 } 132 }
85 133
86 SchemeHostPort::~SchemeHostPort() { 134 SchemeHostPort::~SchemeHostPort() {
87 } 135 }
88 136
89 bool SchemeHostPort::IsInvalid() const { 137 bool SchemeHostPort::IsInvalid() const {
90 return scheme_.empty() && host_.empty() && !port_; 138 return scheme_.empty() && host_.empty() && !port_;
91 } 139 }
92 140
93 std::string SchemeHostPort::Serialize() const { 141 std::string SchemeHostPort::Serialize() const {
94 std::string result; 142 std::string result;
95 if (IsInvalid()) 143 if (IsInvalid())
96 return result; 144 return result;
97 145
98 bool is_default_port =
99 port_ == url::DefaultPortForScheme(scheme_.data(),
100 static_cast<int>(scheme_.length()));
101
102 result.append(scheme_); 146 result.append(scheme_);
103 result.append(kStandardSchemeSeparator); 147 result.append(kStandardSchemeSeparator);
104 result.append(host_); 148 result.append(host_);
105 149
106 if (scheme_ != kFileScheme && !is_default_port) { 150 if (port_ == 0)
151 return result;
152
153 // Omit the port component if the port matches with the default port
154 // defined for the scheme, if any.
155 int default_port = DefaultPortForScheme(scheme_.data(),
156 static_cast<int>(scheme_.length()));
157 if (default_port == PORT_UNSPECIFIED)
158 return result;
159 if (port_ != default_port) {
107 result.push_back(':'); 160 result.push_back(':');
108 result.append(base::IntToString(port_)); 161 result.append(base::IntToString(port_));
109 } 162 }
110 163
111 return result; 164 return result;
112 } 165 }
113 166
114 bool SchemeHostPort::Equals(const SchemeHostPort& other) const { 167 bool SchemeHostPort::Equals(const SchemeHostPort& other) const {
115 return port_ == other.port() && scheme_ == other.scheme() && 168 return port_ == other.port() && scheme_ == other.scheme() &&
116 host_ == other.host(); 169 host_ == other.host();
117 } 170 }
118 171
119 bool SchemeHostPort::operator<(const SchemeHostPort& other) const { 172 bool SchemeHostPort::operator<(const SchemeHostPort& other) const {
120 if (port_ != other.port_) 173 if (port_ != other.port_)
121 return port_ < other.port_; 174 return port_ < other.port_;
122 if (scheme_ != other.scheme_) 175 if (scheme_ != other.scheme_)
123 return scheme_ < other.scheme_; 176 return scheme_ < other.scheme_;
124 if (host_ != other.host_) 177 if (host_ != other.host_)
125 return host_ < other.host_; 178 return host_ < other.host_;
126 return false; 179 return false;
127 } 180 }
128 181
129 } // namespace url 182 } // namespace url
OLDNEW
« no previous file with comments | « url/scheme_host_port.h ('k') | url/url_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698