Fix nullptr crash in OnEmbed
[chromium-blink-merge.git] / net / proxy / proxy_server.cc
blobe451265cbb9700127d3826ce6254c84d163d01f5
1 // Copyright (c) 2010 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.
5 #include "net/proxy/proxy_server.h"
7 #include <algorithm>
9 #include "base/basictypes.h"
10 #include "base/strings/string_util.h"
11 #include "net/base/net_util.h"
12 #include "net/http/http_util.h"
14 namespace net {
16 namespace {
18 // Parses the proxy type from a PAC string, to a ProxyServer::Scheme.
19 // This mapping is case-insensitive. If no type could be matched
20 // returns SCHEME_INVALID.
21 ProxyServer::Scheme GetSchemeFromPacTypeInternal(base::StringPiece type) {
22 if (base::LowerCaseEqualsASCII(type, "proxy"))
23 return ProxyServer::SCHEME_HTTP;
24 if (base::LowerCaseEqualsASCII(type, "socks")) {
25 // Default to v4 for compatibility. This is because the SOCKS4 vs SOCKS5
26 // notation didn't originally exist, so if a client returns SOCKS they
27 // really meant SOCKS4.
28 return ProxyServer::SCHEME_SOCKS4;
30 if (base::LowerCaseEqualsASCII(type, "socks4"))
31 return ProxyServer::SCHEME_SOCKS4;
32 if (base::LowerCaseEqualsASCII(type, "socks5"))
33 return ProxyServer::SCHEME_SOCKS5;
34 if (base::LowerCaseEqualsASCII(type, "direct"))
35 return ProxyServer::SCHEME_DIRECT;
36 if (base::LowerCaseEqualsASCII(type, "https"))
37 return ProxyServer::SCHEME_HTTPS;
38 if (base::LowerCaseEqualsASCII(type, "quic"))
39 return ProxyServer::SCHEME_QUIC;
41 return ProxyServer::SCHEME_INVALID;
44 // Parses the proxy scheme from a URL-like representation, to a
45 // ProxyServer::Scheme. This corresponds with the values used in
46 // ProxyServer::ToURI(). If no type could be matched, returns SCHEME_INVALID.
47 ProxyServer::Scheme GetSchemeFromURIInternal(base::StringPiece type) {
48 if (base::LowerCaseEqualsASCII(type, "http"))
49 return ProxyServer::SCHEME_HTTP;
50 if (base::LowerCaseEqualsASCII(type, "socks4"))
51 return ProxyServer::SCHEME_SOCKS4;
52 if (base::LowerCaseEqualsASCII(type, "socks"))
53 return ProxyServer::SCHEME_SOCKS5;
54 if (base::LowerCaseEqualsASCII(type, "socks5"))
55 return ProxyServer::SCHEME_SOCKS5;
56 if (base::LowerCaseEqualsASCII(type, "direct"))
57 return ProxyServer::SCHEME_DIRECT;
58 if (base::LowerCaseEqualsASCII(type, "https"))
59 return ProxyServer::SCHEME_HTTPS;
60 if (base::LowerCaseEqualsASCII(type, "quic"))
61 return ProxyServer::SCHEME_QUIC;
62 return ProxyServer::SCHEME_INVALID;
65 } // namespace
67 ProxyServer::ProxyServer(Scheme scheme, const HostPortPair& host_port_pair)
68 : scheme_(scheme), host_port_pair_(host_port_pair) {
69 if (scheme_ == SCHEME_DIRECT || scheme_ == SCHEME_INVALID) {
70 // |host_port_pair| isn't relevant for these special schemes, so none should
71 // have been specified. It is important for this to be consistent since we
72 // do raw field comparisons in the equality and comparison functions.
73 DCHECK(host_port_pair.Equals(HostPortPair()));
74 host_port_pair_ = HostPortPair();
78 const HostPortPair& ProxyServer::host_port_pair() const {
79 // Doesn't make sense to call this if the URI scheme doesn't
80 // have concept of a host.
81 DCHECK(is_valid());
82 DCHECK(!is_direct());
83 return host_port_pair_;
86 // static
87 ProxyServer ProxyServer::FromURI(const std::string& uri,
88 Scheme default_scheme) {
89 return FromURI(uri.begin(), uri.end(), default_scheme);
92 // static
93 ProxyServer ProxyServer::FromURI(std::string::const_iterator begin,
94 std::string::const_iterator end,
95 Scheme default_scheme) {
96 // We will default to |default_scheme| if no scheme specifier was given.
97 Scheme scheme = default_scheme;
99 // Trim the leading/trailing whitespace.
100 HttpUtil::TrimLWS(&begin, &end);
102 // Check for [<scheme> "://"]
103 std::string::const_iterator colon = std::find(begin, end, ':');
104 if (colon != end &&
105 (end - colon) >= 3 &&
106 *(colon + 1) == '/' &&
107 *(colon + 2) == '/') {
108 scheme = GetSchemeFromURIInternal(base::StringPiece(begin, colon));
109 begin = colon + 3; // Skip past the "://"
112 // Now parse the <host>[":"<port>].
113 return FromSchemeHostAndPort(scheme, begin, end);
116 std::string ProxyServer::ToURI() const {
117 switch (scheme_) {
118 case SCHEME_DIRECT:
119 return "direct://";
120 case SCHEME_HTTP:
121 // Leave off "http://" since it is our default scheme.
122 return host_port_pair().ToString();
123 case SCHEME_SOCKS4:
124 return std::string("socks4://") + host_port_pair().ToString();
125 case SCHEME_SOCKS5:
126 return std::string("socks5://") + host_port_pair().ToString();
127 case SCHEME_HTTPS:
128 return std::string("https://") + host_port_pair().ToString();
129 case SCHEME_QUIC:
130 return std::string("quic://") + host_port_pair().ToString();
131 default:
132 // Got called with an invalid scheme.
133 NOTREACHED();
134 return std::string();
138 // static
139 ProxyServer ProxyServer::FromPacString(const std::string& pac_string) {
140 return FromPacString(pac_string.begin(), pac_string.end());
143 // static
144 ProxyServer ProxyServer::FromPacString(std::string::const_iterator begin,
145 std::string::const_iterator end) {
146 // Trim the leading/trailing whitespace.
147 HttpUtil::TrimLWS(&begin, &end);
149 // Input should match:
150 // "DIRECT" | ( <type> 1*(LWS) <host-and-port> )
152 // Start by finding the first space (if any).
153 std::string::const_iterator space;
154 for (space = begin; space != end; ++space) {
155 if (HttpUtil::IsLWS(*space)) {
156 break;
160 // Everything to the left of the space is the scheme.
161 Scheme scheme = GetSchemeFromPacTypeInternal(base::StringPiece(begin, space));
163 // And everything to the right of the space is the
164 // <host>[":" <port>].
165 return FromSchemeHostAndPort(scheme, space, end);
168 std::string ProxyServer::ToPacString() const {
169 switch (scheme_) {
170 case SCHEME_DIRECT:
171 return "DIRECT";
172 case SCHEME_HTTP:
173 return std::string("PROXY ") + host_port_pair().ToString();
174 case SCHEME_SOCKS4:
175 // For compatibility send SOCKS instead of SOCKS4.
176 return std::string("SOCKS ") + host_port_pair().ToString();
177 case SCHEME_SOCKS5:
178 return std::string("SOCKS5 ") + host_port_pair().ToString();
179 case SCHEME_HTTPS:
180 return std::string("HTTPS ") + host_port_pair().ToString();
181 case SCHEME_QUIC:
182 return std::string("QUIC ") + host_port_pair().ToString();
183 default:
184 // Got called with an invalid scheme.
185 NOTREACHED();
186 return std::string();
190 // static
191 int ProxyServer::GetDefaultPortForScheme(Scheme scheme) {
192 switch (scheme) {
193 case SCHEME_HTTP:
194 return 80;
195 case SCHEME_SOCKS4:
196 case SCHEME_SOCKS5:
197 return 1080;
198 case SCHEME_HTTPS:
199 case SCHEME_QUIC:
200 return 443;
201 case SCHEME_INVALID:
202 case SCHEME_DIRECT:
203 break;
205 return -1;
208 // static
209 ProxyServer::Scheme ProxyServer::GetSchemeFromURI(const std::string& scheme) {
210 return GetSchemeFromURIInternal(scheme);
213 // static
214 ProxyServer ProxyServer::FromSchemeHostAndPort(
215 Scheme scheme,
216 std::string::const_iterator begin,
217 std::string::const_iterator end) {
219 // Trim leading/trailing space.
220 HttpUtil::TrimLWS(&begin, &end);
222 if (scheme == SCHEME_DIRECT && begin != end)
223 return ProxyServer(); // Invalid -- DIRECT cannot have a host/port.
225 HostPortPair host_port_pair;
227 if (scheme != SCHEME_INVALID && scheme != SCHEME_DIRECT) {
228 std::string host;
229 int port = -1;
230 // If the scheme has a host/port, parse it.
231 bool ok = ParseHostAndPort(begin, end, &host, &port);
232 if (!ok)
233 return ProxyServer(); // Invalid -- failed parsing <host>[":"<port>]
235 // Choose a default port number if none was given.
236 if (port == -1)
237 port = GetDefaultPortForScheme(scheme);
239 host_port_pair = HostPortPair(host, static_cast<uint16>(port));
242 return ProxyServer(scheme, host_port_pair);
245 } // namespace net