[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
[email protected] | f6fb2de | 2009-02-19 08:11:42 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 5 | #include "net/base/proxy_server.h" |
[email protected] | f6fb2de | 2009-02-19 08:11:42 | [diff] [blame] | 6 | |
Eric Orth | 597399c | 2021-09-23 17:34:37 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
| 9 | #include <ostream> |
| 10 | #include <string> |
| 11 | |
| 12 | #include "base/check_op.h" |
| 13 | #include "base/numerics/safe_conversions.h" |
| 14 | #include "base/strings/strcat.h" |
| 15 | #include "base/strings/string_number_conversions.h" |
| 16 | #include "base/strings/string_piece.h" |
| 17 | #include "net/base/proxy_string_util.h" |
| 18 | #include "third_party/abseil-cpp/absl/types/optional.h" |
| 19 | #include "url/third_party/mozilla/url_parse.h" |
| 20 | #include "url/url_canon.h" |
| 21 | #include "url/url_canon_stdstring.h" |
[email protected] | f6fb2de | 2009-02-19 08:11:42 | [diff] [blame] | 22 | |
| 23 | namespace net { |
| 24 | |
[email protected] | fae7669f | 2010-08-02 21:49:40 | [diff] [blame] | 25 | ProxyServer::ProxyServer(Scheme scheme, const HostPortPair& host_port_pair) |
| 26 | : scheme_(scheme), host_port_pair_(host_port_pair) { |
[email protected] | 31e68d7 | 2010-08-25 06:36:58 | [diff] [blame] | 27 | if (scheme_ == SCHEME_DIRECT || scheme_ == SCHEME_INVALID) { |
| 28 | // |host_port_pair| isn't relevant for these special schemes, so none should |
| 29 | // have been specified. It is important for this to be consistent since we |
| 30 | // do raw field comparisons in the equality and comparison functions. |
| 31 | DCHECK(host_port_pair.Equals(HostPortPair())); |
| 32 | host_port_pair_ = HostPortPair(); |
| 33 | } |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 34 | } |
| 35 | |
Eric Orth | 597399c | 2021-09-23 17:34:37 | [diff] [blame] | 36 | // static |
| 37 | ProxyServer ProxyServer::FromSchemeHostAndPort(Scheme scheme, |
| 38 | base::StringPiece host, |
| 39 | base::StringPiece port_str) { |
| 40 | // Create INVALID proxies directly using `ProxyServer()`. |
| 41 | DCHECK_NE(scheme, SCHEME_INVALID); |
| 42 | |
| 43 | // Create DIRECT proxies directly using `Direct()`. |
| 44 | DCHECK_NE(scheme, SCHEME_DIRECT); |
| 45 | |
| 46 | int port_number = |
| 47 | url::ParsePort(port_str.data(), url::Component(0, port_str.size())); |
| 48 | if (port_number == url::PORT_UNSPECIFIED) |
| 49 | return FromSchemeHostAndPort(scheme, host, absl::nullopt); |
| 50 | if (port_number == url::PORT_INVALID) |
| 51 | return ProxyServer(); |
| 52 | |
| 53 | DCHECK(base::IsValueInRangeForNumericType<uint16_t>(port_number)); |
| 54 | |
| 55 | return FromSchemeHostAndPort(scheme, host, |
| 56 | static_cast<uint16_t>(port_number)); |
| 57 | } |
| 58 | |
| 59 | // static |
| 60 | ProxyServer ProxyServer::FromSchemeHostAndPort(Scheme scheme, |
| 61 | base::StringPiece host, |
| 62 | absl::optional<uint16_t> port) { |
| 63 | // Create INVALID proxies directly using `ProxyServer()`. |
| 64 | DCHECK_NE(scheme, SCHEME_INVALID); |
| 65 | |
| 66 | // Create DIRECT proxies directly using `Direct()`. |
| 67 | DCHECK_NE(scheme, SCHEME_DIRECT); |
| 68 | |
| 69 | // Add brackets to IPv6 literals if missing, as required by url |
| 70 | // canonicalization. |
| 71 | std::string bracketed_host; |
| 72 | if (!host.empty() && host.front() != '[' && |
| 73 | host.find(":") != base::StringPiece::npos) { |
| 74 | bracketed_host = base::StrCat({"[", host, "]"}); |
| 75 | host = bracketed_host; |
| 76 | } |
| 77 | |
| 78 | std::string canonicalized_host; |
| 79 | url::StdStringCanonOutput canonicalized_output(&canonicalized_host); |
| 80 | url::Component component_output; |
| 81 | |
| 82 | if (!url::CanonicalizeHost(host.data(), url::Component(0, host.size()), |
| 83 | &canonicalized_output, &component_output)) { |
| 84 | return ProxyServer(); |
| 85 | } |
| 86 | if (!component_output.is_nonempty()) |
| 87 | return ProxyServer(); |
| 88 | |
| 89 | canonicalized_output.Complete(); |
| 90 | |
| 91 | // Remove IPv6 literal bracketing, as required by HostPortPair. |
| 92 | base::StringPiece unbracketed_host = canonicalized_host; |
| 93 | if (canonicalized_host.front() == '[' && canonicalized_host.back() == ']') |
| 94 | unbracketed_host = unbracketed_host.substr(1, unbracketed_host.size() - 2); |
| 95 | |
| 96 | // A uint16_t port is always valid and canonicalized. |
| 97 | uint16_t fixed_port = port.value_or(GetDefaultPortForScheme(scheme)); |
| 98 | |
| 99 | return ProxyServer(scheme, HostPortPair(unbracketed_host, fixed_port)); |
| 100 | } |
| 101 | |
| 102 | std::string ProxyServer::GetHost() const { |
| 103 | return host_port_pair().HostForURL(); |
| 104 | } |
| 105 | |
| 106 | uint16_t ProxyServer::GetPort() const { |
| 107 | return host_port_pair().port(); |
| 108 | } |
| 109 | |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 110 | const HostPortPair& ProxyServer::host_port_pair() const { |
[email protected] | f6fb2de | 2009-02-19 08:11:42 | [diff] [blame] | 111 | // Doesn't make sense to call this if the URI scheme doesn't |
| 112 | // have concept of a host. |
kundaji | 88ca411 | 2014-09-23 01:35:21 | [diff] [blame] | 113 | DCHECK(is_valid()); |
| 114 | DCHECK(!is_direct()); |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 115 | return host_port_pair_; |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 116 | } |
| 117 | |
[email protected] | f6fb2de | 2009-02-19 08:11:42 | [diff] [blame] | 118 | // static |
[email protected] | 06031390 | 2009-02-19 22:20:22 | [diff] [blame] | 119 | int ProxyServer::GetDefaultPortForScheme(Scheme scheme) { |
| 120 | switch (scheme) { |
| 121 | case SCHEME_HTTP: |
| 122 | return 80; |
| 123 | case SCHEME_SOCKS4: |
| 124 | case SCHEME_SOCKS5: |
| 125 | return 1080; |
[email protected] | 224c0ad | 2010-07-20 17:55:53 | [diff] [blame] | 126 | case SCHEME_HTTPS: |
[email protected] | a3c5c07dd | 2014-02-05 07:38:01 | [diff] [blame] | 127 | case SCHEME_QUIC: |
[email protected] | 224c0ad | 2010-07-20 17:55:53 | [diff] [blame] | 128 | return 443; |
[email protected] | a3c5c07dd | 2014-02-05 07:38:01 | [diff] [blame] | 129 | case SCHEME_INVALID: |
| 130 | case SCHEME_DIRECT: |
| 131 | break; |
[email protected] | 06031390 | 2009-02-19 22:20:22 | [diff] [blame] | 132 | } |
[email protected] | a3c5c07dd | 2014-02-05 07:38:01 | [diff] [blame] | 133 | return -1; |
[email protected] | 06031390 | 2009-02-19 22:20:22 | [diff] [blame] | 134 | } |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 135 | |
Eric Orth | 597399c | 2021-09-23 17:34:37 | [diff] [blame] | 136 | std::ostream& operator<<(std::ostream& os, const ProxyServer& proxy_server) { |
| 137 | return os << ProxyServerToPacResultElement(proxy_server); |
| 138 | } |
| 139 | |
[email protected] | f6fb2de | 2009-02-19 08:11:42 | [diff] [blame] | 140 | } // namespace net |