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(
22 std::string::const_iterator begin
,
23 std::string::const_iterator end
) {
24 if (base::LowerCaseEqualsASCII(begin
, end
, "proxy"))
25 return ProxyServer::SCHEME_HTTP
;
26 if (base::LowerCaseEqualsASCII(begin
, end
, "socks")) {
27 // Default to v4 for compatibility. This is because the SOCKS4 vs SOCKS5
28 // notation didn't originally exist, so if a client returns SOCKS they
29 // really meant SOCKS4.
30 return ProxyServer::SCHEME_SOCKS4
;
32 if (base::LowerCaseEqualsASCII(begin
, end
, "socks4"))
33 return ProxyServer::SCHEME_SOCKS4
;
34 if (base::LowerCaseEqualsASCII(begin
, end
, "socks5"))
35 return ProxyServer::SCHEME_SOCKS5
;
36 if (base::LowerCaseEqualsASCII(begin
, end
, "direct"))
37 return ProxyServer::SCHEME_DIRECT
;
38 if (base::LowerCaseEqualsASCII(begin
, end
, "https"))
39 return ProxyServer::SCHEME_HTTPS
;
40 if (base::LowerCaseEqualsASCII(begin
, end
, "quic"))
41 return ProxyServer::SCHEME_QUIC
;
43 return ProxyServer::SCHEME_INVALID
;
46 // Parses the proxy scheme from a URL-like representation, to a
47 // ProxyServer::Scheme. This corresponds with the values used in
48 // ProxyServer::ToURI(). If no type could be matched, returns SCHEME_INVALID.
49 ProxyServer::Scheme
GetSchemeFromURIInternal(std::string::const_iterator begin
,
50 std::string::const_iterator end
) {
51 if (base::LowerCaseEqualsASCII(begin
, end
, "http"))
52 return ProxyServer::SCHEME_HTTP
;
53 if (base::LowerCaseEqualsASCII(begin
, end
, "socks4"))
54 return ProxyServer::SCHEME_SOCKS4
;
55 if (base::LowerCaseEqualsASCII(begin
, end
, "socks"))
56 return ProxyServer::SCHEME_SOCKS5
;
57 if (base::LowerCaseEqualsASCII(begin
, end
, "socks5"))
58 return ProxyServer::SCHEME_SOCKS5
;
59 if (base::LowerCaseEqualsASCII(begin
, end
, "direct"))
60 return ProxyServer::SCHEME_DIRECT
;
61 if (base::LowerCaseEqualsASCII(begin
, end
, "https"))
62 return ProxyServer::SCHEME_HTTPS
;
63 if (base::LowerCaseEqualsASCII(begin
, end
, "quic"))
64 return ProxyServer::SCHEME_QUIC
;
65 return ProxyServer::SCHEME_INVALID
;
70 ProxyServer::ProxyServer(Scheme scheme
, const HostPortPair
& host_port_pair
)
71 : scheme_(scheme
), host_port_pair_(host_port_pair
) {
72 if (scheme_
== SCHEME_DIRECT
|| scheme_
== SCHEME_INVALID
) {
73 // |host_port_pair| isn't relevant for these special schemes, so none should
74 // have been specified. It is important for this to be consistent since we
75 // do raw field comparisons in the equality and comparison functions.
76 DCHECK(host_port_pair
.Equals(HostPortPair()));
77 host_port_pair_
= HostPortPair();
81 const HostPortPair
& ProxyServer::host_port_pair() const {
82 // Doesn't make sense to call this if the URI scheme doesn't
83 // have concept of a host.
86 return host_port_pair_
;
90 ProxyServer
ProxyServer::FromURI(const std::string
& uri
,
91 Scheme default_scheme
) {
92 return FromURI(uri
.begin(), uri
.end(), default_scheme
);
96 ProxyServer
ProxyServer::FromURI(std::string::const_iterator begin
,
97 std::string::const_iterator end
,
98 Scheme default_scheme
) {
99 // We will default to |default_scheme| if no scheme specifier was given.
100 Scheme scheme
= default_scheme
;
102 // Trim the leading/trailing whitespace.
103 HttpUtil::TrimLWS(&begin
, &end
);
105 // Check for [<scheme> "://"]
106 std::string::const_iterator colon
= std::find(begin
, end
, ':');
108 (end
- colon
) >= 3 &&
109 *(colon
+ 1) == '/' &&
110 *(colon
+ 2) == '/') {
111 scheme
= GetSchemeFromURIInternal(begin
, colon
);
112 begin
= colon
+ 3; // Skip past the "://"
115 // Now parse the <host>[":"<port>].
116 return FromSchemeHostAndPort(scheme
, begin
, end
);
119 std::string
ProxyServer::ToURI() const {
124 // Leave off "http://" since it is our default scheme.
125 return host_port_pair().ToString();
127 return std::string("socks4://") + host_port_pair().ToString();
129 return std::string("socks5://") + host_port_pair().ToString();
131 return std::string("https://") + host_port_pair().ToString();
133 return std::string("quic://") + host_port_pair().ToString();
135 // Got called with an invalid scheme.
137 return std::string();
142 ProxyServer
ProxyServer::FromPacString(const std::string
& pac_string
) {
143 return FromPacString(pac_string
.begin(), pac_string
.end());
147 ProxyServer
ProxyServer::FromPacString(std::string::const_iterator begin
,
148 std::string::const_iterator end
) {
149 // Trim the leading/trailing whitespace.
150 HttpUtil::TrimLWS(&begin
, &end
);
152 // Input should match:
153 // "DIRECT" | ( <type> 1*(LWS) <host-and-port> )
155 // Start by finding the first space (if any).
156 std::string::const_iterator space
;
157 for (space
= begin
; space
!= end
; ++space
) {
158 if (HttpUtil::IsLWS(*space
)) {
163 // Everything to the left of the space is the scheme.
164 Scheme scheme
= GetSchemeFromPacTypeInternal(begin
, space
);
166 // And everything to the right of the space is the
167 // <host>[":" <port>].
168 return FromSchemeHostAndPort(scheme
, space
, end
);
171 std::string
ProxyServer::ToPacString() const {
176 return std::string("PROXY ") + host_port_pair().ToString();
178 // For compatibility send SOCKS instead of SOCKS4.
179 return std::string("SOCKS ") + host_port_pair().ToString();
181 return std::string("SOCKS5 ") + host_port_pair().ToString();
183 return std::string("HTTPS ") + host_port_pair().ToString();
185 return std::string("QUIC ") + host_port_pair().ToString();
187 // Got called with an invalid scheme.
189 return std::string();
194 int ProxyServer::GetDefaultPortForScheme(Scheme scheme
) {
212 ProxyServer::Scheme
ProxyServer::GetSchemeFromURI(const std::string
& scheme
) {
213 return GetSchemeFromURIInternal(scheme
.begin(), scheme
.end());
217 ProxyServer
ProxyServer::FromSchemeHostAndPort(
219 std::string::const_iterator begin
,
220 std::string::const_iterator end
) {
222 // Trim leading/trailing space.
223 HttpUtil::TrimLWS(&begin
, &end
);
225 if (scheme
== SCHEME_DIRECT
&& begin
!= end
)
226 return ProxyServer(); // Invalid -- DIRECT cannot have a host/port.
228 HostPortPair host_port_pair
;
230 if (scheme
!= SCHEME_INVALID
&& scheme
!= SCHEME_DIRECT
) {
233 // If the scheme has a host/port, parse it.
234 bool ok
= ParseHostAndPort(begin
, end
, &host
, &port
);
236 return ProxyServer(); // Invalid -- failed parsing <host>[":"<port>]
238 // Choose a default port number if none was given.
240 port
= GetDefaultPortForScheme(scheme
);
242 host_port_pair
= HostPortPair(host
, static_cast<uint16
>(port
));
245 return ProxyServer(scheme
, host_port_pair
);