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"
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"
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
;
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.
83 return host_port_pair_
;
87 ProxyServer
ProxyServer::FromURI(const std::string
& uri
,
88 Scheme default_scheme
) {
89 return FromURI(uri
.begin(), uri
.end(), default_scheme
);
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
, ':');
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 {
121 // Leave off "http://" since it is our default scheme.
122 return host_port_pair().ToString();
124 return std::string("socks4://") + host_port_pair().ToString();
126 return std::string("socks5://") + host_port_pair().ToString();
128 return std::string("https://") + host_port_pair().ToString();
130 return std::string("quic://") + host_port_pair().ToString();
132 // Got called with an invalid scheme.
134 return std::string();
139 ProxyServer
ProxyServer::FromPacString(const std::string
& pac_string
) {
140 return FromPacString(pac_string
.begin(), pac_string
.end());
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
)) {
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 {
173 return std::string("PROXY ") + host_port_pair().ToString();
175 // For compatibility send SOCKS instead of SOCKS4.
176 return std::string("SOCKS ") + host_port_pair().ToString();
178 return std::string("SOCKS5 ") + host_port_pair().ToString();
180 return std::string("HTTPS ") + host_port_pair().ToString();
182 return std::string("QUIC ") + host_port_pair().ToString();
184 // Got called with an invalid scheme.
186 return std::string();
191 int ProxyServer::GetDefaultPortForScheme(Scheme scheme
) {
209 ProxyServer::Scheme
ProxyServer::GetSchemeFromURI(const std::string
& scheme
) {
210 return GetSchemeFromURIInternal(scheme
);
214 ProxyServer
ProxyServer::FromSchemeHostAndPort(
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
) {
230 // If the scheme has a host/port, parse it.
231 bool ok
= ParseHostAndPort(begin
, end
, &host
, &port
);
233 return ProxyServer(); // Invalid -- failed parsing <host>[":"<port>]
235 // Choose a default port number if none was given.
237 port
= GetDefaultPortForScheme(scheme
);
239 host_port_pair
= HostPortPair(host
, static_cast<uint16
>(port
));
242 return ProxyServer(scheme
, host_port_pair
);