| 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> |
| 11 | 11 |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/numerics/safe_conversions.h" | 13 #include "base/numerics/safe_conversions.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "url/gurl.h" | 15 #include "url/gurl.h" |
| 16 #include "url/third_party/mozilla/url_parse.h" |
| 16 #include "url/url_canon.h" | 17 #include "url/url_canon.h" |
| 17 #include "url/url_canon_stdstring.h" | 18 #include "url/url_canon_stdstring.h" |
| 18 #include "url/url_constants.h" | 19 #include "url/url_constants.h" |
| 19 #include "url/url_util.h" | 20 #include "url/url_util.h" |
| 20 | 21 |
| 21 namespace url { | 22 namespace url { |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 bool IsCanonicalHost(const base::StringPiece& host) { | 26 bool IsCanonicalHost(const base::StringPiece& host) { |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 } | 136 } |
| 136 | 137 |
| 137 SchemeHostPort::~SchemeHostPort() { | 138 SchemeHostPort::~SchemeHostPort() { |
| 138 } | 139 } |
| 139 | 140 |
| 140 bool SchemeHostPort::IsInvalid() const { | 141 bool SchemeHostPort::IsInvalid() const { |
| 141 return scheme_.empty() && host_.empty() && !port_; | 142 return scheme_.empty() && host_.empty() && !port_; |
| 142 } | 143 } |
| 143 | 144 |
| 144 std::string SchemeHostPort::Serialize() const { | 145 std::string SchemeHostPort::Serialize() const { |
| 146 // Null checking for |parsed| in SerializeInternal is probably slower than |
| 147 // just filling it in and discarding it here. |
| 148 url::Parsed parsed; |
| 149 return SerializeInternal(&parsed); |
| 150 } |
| 151 |
| 152 GURL SchemeHostPort::GetURL() const { |
| 153 url::Parsed parsed; |
| 154 std::string serialized = SerializeInternal(&parsed); |
| 155 |
| 156 // If the serialized string is passed to GURL for parsing, it will append an |
| 157 // empty path /. Add that here. Note: per RFC 6454 we cannot do this for |
| 158 // normal Origin serialization. |
| 159 DCHECK(!parsed.path.is_valid()); |
| 160 parsed.path = Component(serialized.length(), 1); |
| 161 serialized.append("/"); |
| 162 return GURL(std::move(serialized), parsed, true); |
| 163 } |
| 164 |
| 165 bool SchemeHostPort::Equals(const SchemeHostPort& other) const { |
| 166 return port_ == other.port() && scheme_ == other.scheme() && |
| 167 host_ == other.host(); |
| 168 } |
| 169 |
| 170 bool SchemeHostPort::operator<(const SchemeHostPort& other) const { |
| 171 return std::tie(port_, scheme_, host_) < |
| 172 std::tie(other.port_, other.scheme_, other.host_); |
| 173 } |
| 174 |
| 175 std::string SchemeHostPort::SerializeInternal(url::Parsed* parsed) const { |
| 145 std::string result; | 176 std::string result; |
| 146 if (IsInvalid()) | 177 if (IsInvalid()) |
| 147 return result; | 178 return result; |
| 148 | 179 |
| 180 parsed->scheme = Component(0, scheme_.length()); |
| 149 result.append(scheme_); | 181 result.append(scheme_); |
| 182 |
| 150 result.append(kStandardSchemeSeparator); | 183 result.append(kStandardSchemeSeparator); |
| 184 |
| 185 parsed->host = Component(result.length(), host_.length()); |
| 151 result.append(host_); | 186 result.append(host_); |
| 152 | 187 |
| 153 if (port_ == 0) | 188 if (port_ == 0) |
| 154 return result; | 189 return result; |
| 155 | 190 |
| 156 // Omit the port component if the port matches with the default port | 191 // Omit the port component if the port matches with the default port |
| 157 // defined for the scheme, if any. | 192 // defined for the scheme, if any. |
| 158 int default_port = DefaultPortForScheme(scheme_.data(), | 193 int default_port = DefaultPortForScheme(scheme_.data(), |
| 159 static_cast<int>(scheme_.length())); | 194 static_cast<int>(scheme_.length())); |
| 160 if (default_port == PORT_UNSPECIFIED) | 195 if (default_port == PORT_UNSPECIFIED) |
| 161 return result; | 196 return result; |
| 162 if (port_ != default_port) { | 197 if (port_ != default_port) { |
| 163 result.push_back(':'); | 198 result.push_back(':'); |
| 164 result.append(base::UintToString(port_)); | 199 std::string port(base::UintToString(port_)); |
| 200 parsed->port = Component(result.length(), port.length()); |
| 201 result.append(std::move(port)); |
| 165 } | 202 } |
| 166 | 203 |
| 167 return result; | 204 return result; |
| 168 } | 205 } |
| 169 | 206 |
| 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 { | |
| 176 return std::tie(port_, scheme_, host_) < | |
| 177 std::tie(other.port_, other.scheme_, other.host_); | |
| 178 } | |
| 179 | |
| 180 } // namespace url | 207 } // namespace url |
| OLD | NEW |