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/macros.h" | |
10 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/string_util.h" | |
11 #include "url/gurl.h" | 13 #include "url/gurl.h" |
12 #include "url/url_canon.h" | 14 #include "url/url_canon.h" |
13 #include "url/url_canon_stdstring.h" | 15 #include "url/url_canon_stdstring.h" |
14 #include "url/url_constants.h" | 16 #include "url/url_constants.h" |
15 #include "url/url_util.h" | 17 #include "url/url_util.h" |
16 | 18 |
17 namespace url { | 19 namespace url { |
18 | 20 |
21 const char* kSchemesWithAuthorityOfHostPortPair[] = { | |
22 kHttpScheme, | |
23 kHttpsScheme, | |
24 kFtpScheme, | |
25 kFileScheme, | |
26 kGopherScheme, | |
27 kWsScheme, | |
28 kWssScheme, | |
29 }; | |
30 | |
31 static bool IsSchemeWithAuthorityOfHostPortPair(base::StringPiece scheme) { | |
32 for (size_t i = 0; i < arraysize(kSchemesWithAuthorityOfHostPortPair); ++i) { | |
33 if (base::LowerCaseEqualsASCII( | |
34 scheme, kSchemesWithAuthorityOfHostPortPair[i])) | |
35 return true; | |
36 } | |
37 return false; | |
38 } | |
Ryan Sleevi
2015/08/07 01:35:36
We already have such a registry. It's called a Sta
tyoshino (SeeGerritForStatus)
2015/08/07 02:47:54
url::IsStandard() which is used by GURL is no long
tyoshino (SeeGerritForStatus)
2015/08/07 06:10:23
This is not a problem in SchemeHostPort as it does
tyoshino (SeeGerritForStatus)
2015/08/07 07:57:39
To be clear,
GURL is using url::Canonicalize() to
| |
39 | |
19 SchemeHostPort::SchemeHostPort() : port_(0) { | 40 SchemeHostPort::SchemeHostPort() : port_(0) { |
20 } | 41 } |
21 | 42 |
22 SchemeHostPort::SchemeHostPort(base::StringPiece scheme, | 43 void SchemeHostPort::Clear() { |
23 base::StringPiece host, | 44 scheme_.clear(); |
24 uint16 port) | 45 host_.clear(); |
25 : scheme_(scheme.data(), scheme.length()), | 46 port_ = 0; |
26 host_(host.data(), host.length()), | 47 } |
27 port_(port) { | 48 |
49 void SchemeHostPort::CanonicalizeHost(base::StringPiece host, | |
Ryan Sleevi
2015/08/07 01:35:36
STYLE: Declaration order should match definition o
tyoshino (SeeGerritForStatus)
2015/08/07 06:10:23
Done.
| |
50 std::string* canon_host) { | |
28 // Try to canonicalize the host (copy/pasted from net/base. :( ). | 51 // Try to canonicalize the host (copy/pasted from net/base. :( ). |
29 const url::Component raw_host_component(0, static_cast<int>(host.length())); | 52 const url::Component raw_host_component(0, static_cast<int>(host.length())); |
30 std::string canon_host; | 53 url::StdStringCanonOutput canon_host_output(canon_host); |
31 url::StdStringCanonOutput canon_host_output(&canon_host); | |
32 url::CanonHostInfo host_info; | 54 url::CanonHostInfo host_info; |
33 url::CanonicalizeHostVerbose(host.data(), raw_host_component, | 55 url::CanonicalizeHostVerbose(host.data(), raw_host_component, |
34 &canon_host_output, &host_info); | 56 &canon_host_output, &host_info); |
35 | 57 |
36 if (host_info.out_host.is_nonempty() && | 58 if (host_info.out_host.is_nonempty() && |
37 host_info.family != url::CanonHostInfo::BROKEN) { | 59 host_info.family != url::CanonHostInfo::BROKEN) { |
38 // Success! Assert that there's no extra garbage. | 60 // Success! Assert that there's no extra garbage. |
39 canon_host_output.Complete(); | 61 canon_host_output.Complete(); |
40 DCHECK_EQ(host_info.out_host.len, static_cast<int>(canon_host.length())); | 62 DCHECK_EQ(host_info.out_host.len, static_cast<int>(canon_host->length())); |
41 } else { | 63 } else { |
42 // Empty host, or canonicalization failed. | 64 // Empty host, or canonicalization failed. |
43 canon_host.clear(); | 65 canon_host->clear(); |
44 } | |
45 | |
46 // Return an invalid SchemeHostPort object if any of the following conditions | |
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 = | |
Ryan Sleevi
2015/08/07 01:35:36
blah. How'd I miss these variable names during rev
| |
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 } | 66 } |
67 } | 67 } |
68 | 68 |
69 SchemeHostPort::SchemeHostPort(base::StringPiece scheme, | |
70 base::StringPiece host, | |
71 uint16 port) | |
72 : scheme_(scheme.data(), scheme.length()), | |
73 host_(host.data(), host.length()), | |
74 port_(port) { | |
75 if (!url::IsStandard(scheme.data(), | |
Ryan Sleevi
2015/08/07 01:35:36
You've removed all of the documentation that expla
tyoshino (SeeGerritForStatus)
2015/08/07 06:10:23
The comment in the .h file also explains almost th
| |
76 url::Component(0, static_cast<int>(scheme.length()))) || | |
77 scheme == kFileSystemScheme || scheme == kBlobScheme) { | |
78 Clear(); | |
79 return; | |
80 } | |
81 | |
82 if (!IsSchemeWithAuthorityOfHostPortPair(scheme)) | |
83 return; | |
84 | |
85 if (scheme == kFileScheme) { | |
86 if (port != 0) { | |
87 Clear(); | |
88 return; | |
89 } | |
90 } else if (port == 0 || host.empty()) { | |
91 Clear(); | |
92 return; | |
93 } | |
94 | |
95 std::string canon_host; | |
96 CanonicalizeHost(host, &canon_host); | |
97 if (host != canon_host) { | |
98 Clear(); | |
99 return; | |
100 } | |
101 } | |
102 | |
69 SchemeHostPort::SchemeHostPort(const GURL& url) : port_(0) { | 103 SchemeHostPort::SchemeHostPort(const GURL& url) : port_(0) { |
70 if (!url.is_valid() || !url.IsStandard()) | 104 if (!url.is_valid() || !url.IsStandard()) |
71 return; | 105 return; |
72 | 106 |
73 // These schemes do not follow the generic URL syntax, so we treat them as | 107 // 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 | 108 // invalid (scheme, host, port) tuples (even though such URLs' _Origin_ might |
75 // have a (scheme, host, port) tuple, they themselves do not). | 109 // have a (scheme, host, port) tuple, they themselves do not). |
76 if (url.SchemeIsBlob() || url.SchemeIsFileSystem()) | 110 if (url.SchemeIsBlob() || url.SchemeIsFileSystem()) |
77 return; | 111 return; |
78 | 112 |
(...skipping 17 matching lines...) Expand all Loading... | |
96 return result; | 130 return result; |
97 | 131 |
98 bool is_default_port = | 132 bool is_default_port = |
99 port_ == url::DefaultPortForScheme(scheme_.data(), | 133 port_ == url::DefaultPortForScheme(scheme_.data(), |
100 static_cast<int>(scheme_.length())); | 134 static_cast<int>(scheme_.length())); |
101 | 135 |
102 result.append(scheme_); | 136 result.append(scheme_); |
103 result.append(kStandardSchemeSeparator); | 137 result.append(kStandardSchemeSeparator); |
104 result.append(host_); | 138 result.append(host_); |
105 | 139 |
106 if (scheme_ != kFileScheme && !is_default_port) { | 140 if (IsSchemeWithAuthorityOfHostPortPair(scheme_) && |
141 scheme_ != kFileScheme && | |
142 !is_default_port) { | |
107 result.push_back(':'); | 143 result.push_back(':'); |
108 result.append(base::IntToString(port_)); | 144 result.append(base::IntToString(port_)); |
109 } | 145 } |
110 | 146 |
111 return result; | 147 return result; |
112 } | 148 } |
113 | 149 |
114 bool SchemeHostPort::Equals(const SchemeHostPort& other) const { | 150 bool SchemeHostPort::Equals(const SchemeHostPort& other) const { |
115 return port_ == other.port() && scheme_ == other.scheme() && | 151 return port_ == other.port() && scheme_ == other.scheme() && |
116 host_ == other.host(); | 152 host_ == other.host(); |
117 } | 153 } |
118 | 154 |
119 bool SchemeHostPort::operator<(const SchemeHostPort& other) const { | 155 bool SchemeHostPort::operator<(const SchemeHostPort& other) const { |
120 if (port_ != other.port_) | 156 if (port_ != other.port_) |
121 return port_ < other.port_; | 157 return port_ < other.port_; |
122 if (scheme_ != other.scheme_) | 158 if (scheme_ != other.scheme_) |
123 return scheme_ < other.scheme_; | 159 return scheme_ < other.scheme_; |
124 if (host_ != other.host_) | 160 if (host_ != other.host_) |
125 return host_ < other.host_; | 161 return host_ < other.host_; |
126 return false; | 162 return false; |
127 } | 163 } |
128 | 164 |
129 } // namespace url | 165 } // namespace url |
OLD | NEW |