| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <tuple> | 10 #include <tuple> |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 } else { | 42 } else { |
| 43 // Empty host, or canonicalization failed. | 43 // Empty host, or canonicalization failed. |
| 44 canon_host.clear(); | 44 canon_host.clear(); |
| 45 } | 45 } |
| 46 | 46 |
| 47 return host == canon_host; | 47 return host == canon_host; |
| 48 } | 48 } |
| 49 | 49 |
| 50 bool IsValidInput(const base::StringPiece& scheme, | 50 bool IsValidInput(const base::StringPiece& scheme, |
| 51 const base::StringPiece& host, | 51 const base::StringPiece& host, |
| 52 uint16_t port) { | 52 uint16_t port, |
| 53 SchemeHostPort::ConstructPolicy policy) { |
| 53 SchemeType scheme_type = SCHEME_WITH_PORT; | 54 SchemeType scheme_type = SCHEME_WITH_PORT; |
| 54 bool is_standard = GetStandardSchemeType( | 55 bool is_standard = GetStandardSchemeType( |
| 55 scheme.data(), | 56 scheme.data(), |
| 56 Component(0, base::checked_cast<int>(scheme.length())), | 57 Component(0, base::checked_cast<int>(scheme.length())), |
| 57 &scheme_type); | 58 &scheme_type); |
| 58 if (!is_standard) | 59 if (!is_standard) |
| 59 return false; | 60 return false; |
| 60 | 61 |
| 61 // These schemes do not follow the generic URL syntax, so we treat them as | 62 // These schemes do not follow the generic URL syntax, so we treat them as |
| 62 // invalid (scheme, host, port) tuples (even though such URLs' _Origin_ might | 63 // invalid (scheme, host, port) tuples (even though such URLs' _Origin_ might |
| 63 // have a (scheme, host, port) tuple, they themselves do not). | 64 // have a (scheme, host, port) tuple, they themselves do not). |
| 64 if (scheme == kFileSystemScheme || scheme == kBlobScheme) | 65 if (scheme == kFileSystemScheme || scheme == kBlobScheme) |
| 65 return false; | 66 return false; |
| 66 | 67 |
| 67 switch (scheme_type) { | 68 switch (scheme_type) { |
| 68 case SCHEME_WITH_PORT: | 69 case SCHEME_WITH_PORT: |
| 69 // A URL with |scheme| is required to have the host and port (may be | 70 // A URL with |scheme| is required to have the host and port (may be |
| 70 // omitted in a serialization if it's the same as the default value). | 71 // omitted in a serialization if it's the same as the default value). |
| 71 // Return an invalid instance if either of them is not given. | 72 // Return an invalid instance if either of them is not given. |
| 72 if (host.empty() || port == 0) | 73 if (host.empty() || port == 0) |
| 73 return false; | 74 return false; |
| 74 | 75 |
| 75 if (!IsCanonicalHost(host)) | 76 // Don't do an expensive canonicalization if the host is already |
| 77 // canonicalized. |
| 78 DCHECK(policy == SchemeHostPort::CHECK_CANONICALIZATION || |
| 79 IsCanonicalHost(host)); |
| 80 if (policy == SchemeHostPort::CHECK_CANONICALIZATION && |
| 81 !IsCanonicalHost(host)) { |
| 76 return false; | 82 return false; |
| 83 } |
| 77 | 84 |
| 78 return true; | 85 return true; |
| 79 | 86 |
| 80 case SCHEME_WITHOUT_PORT: | 87 case SCHEME_WITHOUT_PORT: |
| 81 if (port != 0) { | 88 if (port != 0) { |
| 82 // Return an invalid object if a URL with the scheme never represents | 89 // Return an invalid object if a URL with the scheme never represents |
| 83 // the port data but the given |port| is non-zero. | 90 // the port data but the given |port| is non-zero. |
| 84 return false; | 91 return false; |
| 85 } | 92 } |
| 86 | 93 |
| 87 if (!IsCanonicalHost(host)) | 94 // Don't do an expensive canonicalization if the host is already |
| 95 // canonicalized. |
| 96 DCHECK(policy == SchemeHostPort::CHECK_CANONICALIZATION || |
| 97 IsCanonicalHost(host)); |
| 98 if (policy == SchemeHostPort::CHECK_CANONICALIZATION && |
| 99 !IsCanonicalHost(host)) { |
| 88 return false; | 100 return false; |
| 101 } |
| 89 | 102 |
| 90 return true; | 103 return true; |
| 91 | 104 |
| 92 case SCHEME_WITHOUT_AUTHORITY: | 105 case SCHEME_WITHOUT_AUTHORITY: |
| 93 return false; | 106 return false; |
| 94 | 107 |
| 95 default: | 108 default: |
| 96 NOTREACHED(); | 109 NOTREACHED(); |
| 97 return false; | 110 return false; |
| 98 } | 111 } |
| 99 } | 112 } |
| 100 | 113 |
| 101 } // namespace | 114 } // namespace |
| 102 | 115 |
| 103 SchemeHostPort::SchemeHostPort() : port_(0) { | 116 SchemeHostPort::SchemeHostPort() : port_(0) { |
| 104 } | 117 } |
| 105 | 118 |
| 106 SchemeHostPort::SchemeHostPort(base::StringPiece scheme, | 119 SchemeHostPort::SchemeHostPort(base::StringPiece scheme, |
| 107 base::StringPiece host, | 120 base::StringPiece host, |
| 108 uint16_t port) | 121 uint16_t port, |
| 122 ConstructPolicy policy) |
| 109 : port_(0) { | 123 : port_(0) { |
| 110 if (!IsValidInput(scheme, host, port)) | 124 if (!IsValidInput(scheme, host, port, policy)) |
| 111 return; | 125 return; |
| 112 | 126 |
| 113 scheme.CopyToString(&scheme_); | 127 scheme.CopyToString(&scheme_); |
| 114 host.CopyToString(&host_); | 128 host.CopyToString(&host_); |
| 115 port_ = port; | 129 port_ = port; |
| 116 } | 130 } |
| 117 | 131 |
| 132 SchemeHostPort::SchemeHostPort(base::StringPiece scheme, |
| 133 base::StringPiece host, |
| 134 uint16_t port) |
| 135 : SchemeHostPort(scheme, |
| 136 host, |
| 137 port, |
| 138 ConstructPolicy::CHECK_CANONICALIZATION) {} |
| 139 |
| 118 SchemeHostPort::SchemeHostPort(const GURL& url) : port_(0) { | 140 SchemeHostPort::SchemeHostPort(const GURL& url) : port_(0) { |
| 119 if (!url.is_valid()) | 141 if (!url.is_valid()) |
| 120 return; | 142 return; |
| 121 | 143 |
| 122 base::StringPiece scheme = url.scheme_piece(); | 144 base::StringPiece scheme = url.scheme_piece(); |
| 123 base::StringPiece host = url.host_piece(); | 145 base::StringPiece host = url.host_piece(); |
| 124 | 146 |
| 125 // A valid GURL never returns PORT_INVALID. | 147 // A valid GURL never returns PORT_INVALID. |
| 126 int port = url.EffectiveIntPort(); | 148 int port = url.EffectiveIntPort(); |
| 127 if (port == PORT_UNSPECIFIED) | 149 if (port == PORT_UNSPECIFIED) |
| 128 port = 0; | 150 port = 0; |
| 129 | 151 |
| 130 if (!IsValidInput(scheme, host, port)) | 152 if (!IsValidInput(scheme, host, port, ALREADY_CANONICALIZED)) |
| 131 return; | 153 return; |
| 132 | 154 |
| 133 scheme.CopyToString(&scheme_); | 155 scheme.CopyToString(&scheme_); |
| 134 host.CopyToString(&host_); | 156 host.CopyToString(&host_); |
| 135 port_ = port; | 157 port_ = port; |
| 136 } | 158 } |
| 137 | 159 |
| 138 SchemeHostPort::~SchemeHostPort() { | 160 SchemeHostPort::~SchemeHostPort() { |
| 139 } | 161 } |
| 140 | 162 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 result.push_back(':'); | 227 result.push_back(':'); |
| 206 std::string port(base::UintToString(port_)); | 228 std::string port(base::UintToString(port_)); |
| 207 parsed->port = Component(result.length(), port.length()); | 229 parsed->port = Component(result.length(), port.length()); |
| 208 result.append(std::move(port)); | 230 result.append(std::move(port)); |
| 209 } | 231 } |
| 210 | 232 |
| 211 return result; | 233 return result; |
| 212 } | 234 } |
| 213 | 235 |
| 214 } // namespace url | 236 } // namespace url |
| OLD | NEW |