mkwst | 28c7c11 | 2015-07-14 22:41:06 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "url/scheme_host_port.h" |
| 6 | |
avi | c0c6031 | 2015-12-21 21:03:50 | [diff] [blame] | 7 | #include <stdint.h> |
mkwst | 28c7c11 | 2015-07-14 22:41:06 | [diff] [blame] | 8 | #include <string.h> |
| 9 | |
jsbell | 938b025 | 2015-11-30 23:17:41 | [diff] [blame] | 10 | #include <tuple> |
| 11 | |
mkwst | 28c7c11 | 2015-07-14 22:41:06 | [diff] [blame] | 12 | #include "base/logging.h" |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 13 | #include "base/numerics/safe_conversions.h" |
mkwst | 28c7c11 | 2015-07-14 22:41:06 | [diff] [blame] | 14 | #include "base/strings/string_number_conversions.h" |
| 15 | #include "url/gurl.h" |
| 16 | #include "url/url_canon.h" |
| 17 | #include "url/url_canon_stdstring.h" |
| 18 | #include "url/url_constants.h" |
| 19 | #include "url/url_util.h" |
| 20 | |
| 21 | namespace url { |
| 22 | |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 23 | namespace { |
mkwst | 28c7c11 | 2015-07-14 22:41:06 | [diff] [blame] | 24 | |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 25 | bool IsCanonicalHost(const base::StringPiece& host) { |
msramek | 9b7972dd | 2015-08-18 13:04:19 | [diff] [blame] | 26 | std::string canon_host; |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 27 | |
| 28 | // Try to canonicalize the host (copy/pasted from net/base. :( ). |
| 29 | const Component raw_host_component(0, |
| 30 | base::checked_cast<int>(host.length())); |
| 31 | StdStringCanonOutput canon_host_output(&canon_host); |
| 32 | CanonHostInfo host_info; |
| 33 | CanonicalizeHostVerbose(host.data(), raw_host_component, |
| 34 | &canon_host_output, &host_info); |
mkwst | 28c7c11 | 2015-07-14 22:41:06 | [diff] [blame] | 35 | |
| 36 | if (host_info.out_host.is_nonempty() && |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 37 | host_info.family != CanonHostInfo::BROKEN) { |
mkwst | 28c7c11 | 2015-07-14 22:41:06 | [diff] [blame] | 38 | // Success! Assert that there's no extra garbage. |
| 39 | canon_host_output.Complete(); |
| 40 | DCHECK_EQ(host_info.out_host.len, static_cast<int>(canon_host.length())); |
| 41 | } else { |
| 42 | // Empty host, or canonicalization failed. |
| 43 | canon_host.clear(); |
| 44 | } |
mkwst | 28c7c11 | 2015-07-14 22:41:06 | [diff] [blame] | 45 | |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 46 | return host == canon_host; |
mkwst | 28c7c11 | 2015-07-14 22:41:06 | [diff] [blame] | 47 | } |
| 48 | |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 49 | bool IsValidInput(const base::StringPiece& scheme, |
| 50 | const base::StringPiece& host, |
avi | c0c6031 | 2015-12-21 21:03:50 | [diff] [blame] | 51 | uint16_t port) { |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 52 | SchemeType scheme_type = SCHEME_WITH_PORT; |
| 53 | bool is_standard = GetStandardSchemeType( |
| 54 | scheme.data(), |
| 55 | Component(0, base::checked_cast<int>(scheme.length())), |
| 56 | &scheme_type); |
| 57 | if (!is_standard) |
| 58 | return false; |
mkwst | 28c7c11 | 2015-07-14 22:41:06 | [diff] [blame] | 59 | |
| 60 | // These schemes do not follow the generic URL syntax, so we treat them as |
| 61 | // invalid (scheme, host, port) tuples (even though such URLs' _Origin_ might |
| 62 | // have a (scheme, host, port) tuple, they themselves do not). |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 63 | if (scheme == kFileSystemScheme || scheme == kBlobScheme) |
| 64 | return false; |
| 65 | |
| 66 | switch (scheme_type) { |
| 67 | case SCHEME_WITH_PORT: |
| 68 | // A URL with |scheme| is required to have the host and port (may be |
| 69 | // omitted in a serialization if it's the same as the default value). |
| 70 | // Return an invalid instance if either of them is not given. |
| 71 | if (host.empty() || port == 0) |
| 72 | return false; |
| 73 | |
| 74 | if (!IsCanonicalHost(host)) |
| 75 | return false; |
| 76 | |
| 77 | return true; |
| 78 | |
| 79 | case SCHEME_WITHOUT_PORT: |
| 80 | if (port != 0) { |
| 81 | // Return an invalid object if a URL with the scheme never represents |
| 82 | // the port data but the given |port| is non-zero. |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | if (!IsCanonicalHost(host)) |
| 87 | return false; |
| 88 | |
| 89 | return true; |
| 90 | |
| 91 | case SCHEME_WITHOUT_AUTHORITY: |
| 92 | return false; |
| 93 | |
| 94 | default: |
| 95 | NOTREACHED(); |
| 96 | return false; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | } // namespace |
| 101 | |
| 102 | SchemeHostPort::SchemeHostPort() : port_(0) { |
| 103 | } |
| 104 | |
| 105 | SchemeHostPort::SchemeHostPort(base::StringPiece scheme, |
| 106 | base::StringPiece host, |
avi | c0c6031 | 2015-12-21 21:03:50 | [diff] [blame] | 107 | uint16_t port) |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 108 | : port_(0) { |
| 109 | if (!IsValidInput(scheme, host, port)) |
mkwst | 28c7c11 | 2015-07-14 22:41:06 | [diff] [blame] | 110 | return; |
| 111 | |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 112 | scheme.CopyToString(&scheme_); |
| 113 | host.CopyToString(&host_); |
| 114 | port_ = port; |
| 115 | } |
| 116 | |
| 117 | SchemeHostPort::SchemeHostPort(const GURL& url) : port_(0) { |
| 118 | if (!url.is_valid()) |
| 119 | return; |
| 120 | |
brettw | adc84688 | 2015-09-25 01:16:22 | [diff] [blame] | 121 | base::StringPiece scheme = url.scheme_piece(); |
| 122 | base::StringPiece host = url.host_piece(); |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 123 | |
| 124 | // A valid GURL never returns PORT_INVALID. |
| 125 | int port = url.EffectiveIntPort(); |
| 126 | if (port == PORT_UNSPECIFIED) |
| 127 | port = 0; |
| 128 | |
| 129 | if (!IsValidInput(scheme, host, port)) |
| 130 | return; |
| 131 | |
brettw | adc84688 | 2015-09-25 01:16:22 | [diff] [blame] | 132 | scheme.CopyToString(&scheme_); |
| 133 | host.CopyToString(&host_); |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 134 | port_ = port; |
mkwst | 28c7c11 | 2015-07-14 22:41:06 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | SchemeHostPort::~SchemeHostPort() { |
| 138 | } |
| 139 | |
| 140 | bool SchemeHostPort::IsInvalid() const { |
| 141 | return scheme_.empty() && host_.empty() && !port_; |
| 142 | } |
| 143 | |
| 144 | std::string SchemeHostPort::Serialize() const { |
| 145 | std::string result; |
| 146 | if (IsInvalid()) |
| 147 | return result; |
| 148 | |
mkwst | 28c7c11 | 2015-07-14 22:41:06 | [diff] [blame] | 149 | result.append(scheme_); |
| 150 | result.append(kStandardSchemeSeparator); |
| 151 | result.append(host_); |
| 152 | |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 153 | if (port_ == 0) |
| 154 | return result; |
| 155 | |
| 156 | // Omit the port component if the port matches with the default port |
| 157 | // defined for the scheme, if any. |
| 158 | int default_port = DefaultPortForScheme(scheme_.data(), |
| 159 | static_cast<int>(scheme_.length())); |
| 160 | if (default_port == PORT_UNSPECIFIED) |
| 161 | return result; |
| 162 | if (port_ != default_port) { |
mkwst | 28c7c11 | 2015-07-14 22:41:06 | [diff] [blame] | 163 | result.push_back(':'); |
ricea | 460198b | 2015-09-18 22:42:28 | [diff] [blame] | 164 | result.append(base::UintToString(port_)); |
mkwst | 28c7c11 | 2015-07-14 22:41:06 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | return result; |
| 168 | } |
| 169 | |
| 170 | bool SchemeHostPort::Equals(const SchemeHostPort& other) const { |
| 171 | return port_ == other.port() && scheme_ == other.scheme() && |
| 172 | host_ == other.host(); |
| 173 | } |
| 174 | |
| 175 | bool SchemeHostPort::operator<(const SchemeHostPort& other) const { |
jsbell | 938b025 | 2015-11-30 23:17:41 | [diff] [blame] | 176 | return std::tie(port_, scheme_, host_) < |
| 177 | std::tie(other.port_, other.scheme_, other.host_); |
mkwst | 28c7c11 | 2015-07-14 22:41:06 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | } // namespace url |