| 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 bool is_canonicalized) { |
| 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(!is_canonicalized || IsCanonicalHost(host)); |
| 79 if (!is_canonicalized && !IsCanonicalHost(host)) |
| 76 return false; | 80 return false; |
| 77 | 81 |
| 78 return true; | 82 return true; |
| 79 | 83 |
| 80 case SCHEME_WITHOUT_PORT: | 84 case SCHEME_WITHOUT_PORT: |
| 81 if (port != 0) { | 85 if (port != 0) { |
| 82 // Return an invalid object if a URL with the scheme never represents | 86 // Return an invalid object if a URL with the scheme never represents |
| 83 // the port data but the given |port| is non-zero. | 87 // the port data but the given |port| is non-zero. |
| 84 return false; | 88 return false; |
| 85 } | 89 } |
| 86 | 90 |
| 87 if (!IsCanonicalHost(host)) | 91 // Don't do an expensive canonicalization if the host is already |
| 92 // canonicalized. |
| 93 DCHECK(!is_canonicalized || IsCanonicalHost(host)); |
| 94 if (!is_canonicalized && !IsCanonicalHost(host)) |
| 88 return false; | 95 return false; |
| 89 | 96 |
| 90 return true; | 97 return true; |
| 91 | 98 |
| 92 case SCHEME_WITHOUT_AUTHORITY: | 99 case SCHEME_WITHOUT_AUTHORITY: |
| 93 return false; | 100 return false; |
| 94 | 101 |
| 95 default: | 102 default: |
| 96 NOTREACHED(); | 103 NOTREACHED(); |
| 97 return false; | 104 return false; |
| 98 } | 105 } |
| 99 } | 106 } |
| 100 | 107 |
| 101 } // namespace | 108 } // namespace |
| 102 | 109 |
| 103 SchemeHostPort::SchemeHostPort() : port_(0) { | 110 SchemeHostPort::SchemeHostPort() : port_(0) { |
| 104 } | 111 } |
| 105 | 112 |
| 106 SchemeHostPort::SchemeHostPort(base::StringPiece scheme, | 113 SchemeHostPort::SchemeHostPort(base::StringPiece scheme, |
| 107 base::StringPiece host, | 114 base::StringPiece host, |
| 108 uint16_t port) | 115 uint16_t port, |
| 116 bool is_canonicalized) |
| 109 : port_(0) { | 117 : port_(0) { |
| 110 if (!IsValidInput(scheme, host, port)) | 118 if (!IsValidInput(scheme, host, port, is_canonicalized)) |
| 111 return; | 119 return; |
| 112 | 120 |
| 113 scheme.CopyToString(&scheme_); | 121 scheme.CopyToString(&scheme_); |
| 114 host.CopyToString(&host_); | 122 host.CopyToString(&host_); |
| 115 port_ = port; | 123 port_ = port; |
| 116 } | 124 } |
| 117 | 125 |
| 126 SchemeHostPort::SchemeHostPort(base::StringPiece scheme, |
| 127 base::StringPiece host, |
| 128 uint16_t port) |
| 129 : SchemeHostPort(scheme, host, port, false /* is_canonicalized */) {} |
| 130 |
| 118 SchemeHostPort::SchemeHostPort(const GURL& url) : port_(0) { | 131 SchemeHostPort::SchemeHostPort(const GURL& url) : port_(0) { |
| 119 if (!url.is_valid()) | 132 if (!url.is_valid()) |
| 120 return; | 133 return; |
| 121 | 134 |
| 122 base::StringPiece scheme = url.scheme_piece(); | 135 base::StringPiece scheme = url.scheme_piece(); |
| 123 base::StringPiece host = url.host_piece(); | 136 base::StringPiece host = url.host_piece(); |
| 124 | 137 |
| 125 // A valid GURL never returns PORT_INVALID. | 138 // A valid GURL never returns PORT_INVALID. |
| 126 int port = url.EffectiveIntPort(); | 139 int port = url.EffectiveIntPort(); |
| 127 if (port == PORT_UNSPECIFIED) | 140 if (port == PORT_UNSPECIFIED) |
| 128 port = 0; | 141 port = 0; |
| 129 | 142 |
| 130 if (!IsValidInput(scheme, host, port)) | 143 if (!IsValidInput(scheme, host, port, true /* is_canonicalized */)) |
| 131 return; | 144 return; |
| 132 | 145 |
| 133 scheme.CopyToString(&scheme_); | 146 scheme.CopyToString(&scheme_); |
| 134 host.CopyToString(&host_); | 147 host.CopyToString(&host_); |
| 135 port_ = port; | 148 port_ = port; |
| 136 } | 149 } |
| 137 | 150 |
| 138 SchemeHostPort::~SchemeHostPort() { | 151 SchemeHostPort::~SchemeHostPort() { |
| 139 } | 152 } |
| 140 | 153 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 result.push_back(':'); | 211 result.push_back(':'); |
| 199 std::string port(base::UintToString(port_)); | 212 std::string port(base::UintToString(port_)); |
| 200 parsed->port = Component(result.length(), port.length()); | 213 parsed->port = Component(result.length(), port.length()); |
| 201 result.append(std::move(port)); | 214 result.append(std::move(port)); |
| 202 } | 215 } |
| 203 | 216 |
| 204 return result; | 217 return result; |
| 205 } | 218 } |
| 206 | 219 |
| 207 } // namespace url | 220 } // namespace url |
| OLD | NEW |