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/string_tokenizer.h"
10 #include "base/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 (LowerCaseEqualsASCII(begin
, end
, "proxy"))
25 return ProxyServer::SCHEME_HTTP
;
26 if (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 (LowerCaseEqualsASCII(begin
, end
, "socks4"))
33 return ProxyServer::SCHEME_SOCKS4
;
34 if (LowerCaseEqualsASCII(begin
, end
, "socks5"))
35 return ProxyServer::SCHEME_SOCKS5
;
36 if (LowerCaseEqualsASCII(begin
, end
, "direct"))
37 return ProxyServer::SCHEME_DIRECT
;
38 if (LowerCaseEqualsASCII(begin
, end
, "https"))
39 return ProxyServer::SCHEME_HTTPS
;
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(std::string::const_iterator begin
,
48 std::string::const_iterator end
) {
49 if (LowerCaseEqualsASCII(begin
, end
, "http"))
50 return ProxyServer::SCHEME_HTTP
;
51 if (LowerCaseEqualsASCII(begin
, end
, "socks4"))
52 return ProxyServer::SCHEME_SOCKS4
;
53 if (LowerCaseEqualsASCII(begin
, end
, "socks"))
54 return ProxyServer::SCHEME_SOCKS5
;
55 if (LowerCaseEqualsASCII(begin
, end
, "socks5"))
56 return ProxyServer::SCHEME_SOCKS5
;
57 if (LowerCaseEqualsASCII(begin
, end
, "direct"))
58 return ProxyServer::SCHEME_DIRECT
;
59 if (LowerCaseEqualsASCII(begin
, end
, "https"))
60 return ProxyServer::SCHEME_HTTPS
;
61 return ProxyServer::SCHEME_INVALID
;
64 std::string
HostNoBrackets(const std::string
& host
) {
65 // Remove brackets from an RFC 2732-style IPv6 literal address.
66 const std::string::size_type len
= host
.size();
67 if (len
>= 2 && host
[0] == '[' && host
[len
- 1] == ']')
68 return host
.substr(1, len
- 2);
74 ProxyServer::ProxyServer(Scheme scheme
, const HostPortPair
& host_port_pair
)
75 : scheme_(scheme
), host_port_pair_(host_port_pair
) {
76 if (scheme_
== SCHEME_DIRECT
|| scheme_
== SCHEME_INVALID
) {
77 // |host_port_pair| isn't relevant for these special schemes, so none should
78 // have been specified. It is important for this to be consistent since we
79 // do raw field comparisons in the equality and comparison functions.
80 DCHECK(host_port_pair
.Equals(HostPortPair()));
81 host_port_pair_
= HostPortPair();
85 const HostPortPair
& ProxyServer::host_port_pair() const {
86 // Doesn't make sense to call this if the URI scheme doesn't
87 // have concept of a host.
88 DCHECK(is_valid() && !is_direct());
89 return host_port_pair_
;
93 ProxyServer
ProxyServer::FromURI(const std::string
& uri
,
94 Scheme default_scheme
) {
95 return FromURI(uri
.begin(), uri
.end(), default_scheme
);
99 ProxyServer
ProxyServer::FromURI(std::string::const_iterator begin
,
100 std::string::const_iterator end
,
101 Scheme default_scheme
) {
102 // We will default to |default_scheme| if no scheme specifier was given.
103 Scheme scheme
= default_scheme
;
105 // Trim the leading/trailing whitespace.
106 HttpUtil::TrimLWS(&begin
, &end
);
108 // Check for [<scheme> "://"]
109 std::string::const_iterator colon
= std::find(begin
, end
, ':');
111 (end
- colon
) >= 3 &&
112 *(colon
+ 1) == '/' &&
113 *(colon
+ 2) == '/') {
114 scheme
= GetSchemeFromURIInternal(begin
, colon
);
115 begin
= colon
+ 3; // Skip past the "://"
118 // Now parse the <host>[":"<port>].
119 return FromSchemeHostAndPort(scheme
, begin
, end
);
122 std::string
ProxyServer::ToURI() const {
127 // Leave off "http://" since it is our default scheme.
128 return host_port_pair().ToString();
130 return std::string("socks4://") + host_port_pair().ToString();
132 return std::string("socks5://") + host_port_pair().ToString();
134 return std::string("https://") + host_port_pair().ToString();
136 // Got called with an invalid scheme.
138 return std::string();
143 ProxyServer
ProxyServer::FromPacString(const std::string
& pac_string
) {
144 return FromPacString(pac_string
.begin(), pac_string
.end());
148 ProxyServer
ProxyServer::FromPacString(std::string::const_iterator begin
,
149 std::string::const_iterator end
) {
150 // Trim the leading/trailing whitespace.
151 HttpUtil::TrimLWS(&begin
, &end
);
153 // Input should match:
154 // "DIRECT" | ( <type> 1*(LWS) <host-and-port> )
156 // Start by finding the first space (if any).
157 std::string::const_iterator space
;
158 for (space
= begin
; space
!= end
; ++space
) {
159 if (HttpUtil::IsLWS(*space
)) {
164 // Everything to the left of the space is the scheme.
165 Scheme scheme
= GetSchemeFromPacTypeInternal(begin
, space
);
167 // And everything to the right of the space is the
168 // <host>[":" <port>].
169 return FromSchemeHostAndPort(scheme
, space
, end
);
172 std::string
ProxyServer::ToPacString() const {
177 return std::string("PROXY ") + host_port_pair().ToString();
179 // For compatibility send SOCKS instead of SOCKS4.
180 return std::string("SOCKS ") + host_port_pair().ToString();
182 return std::string("SOCKS5 ") + host_port_pair().ToString();
184 return std::string("HTTPS ") + host_port_pair().ToString();
186 // Got called with an invalid scheme.
188 return std::string();
193 int ProxyServer::GetDefaultPortForScheme(Scheme scheme
) {
208 ProxyServer::Scheme
ProxyServer::GetSchemeFromURI(const std::string
& scheme
) {
209 return GetSchemeFromURIInternal(scheme
.begin(), scheme
.end());
213 ProxyServer
ProxyServer::FromSchemeHostAndPort(
215 std::string::const_iterator begin
,
216 std::string::const_iterator end
) {
218 // Trim leading/trailing space.
219 HttpUtil::TrimLWS(&begin
, &end
);
221 if (scheme
== SCHEME_DIRECT
&& begin
!= end
)
222 return ProxyServer(); // Invalid -- DIRECT cannot have a host/port.
224 HostPortPair host_port_pair
;
226 if (scheme
!= SCHEME_INVALID
&& scheme
!= SCHEME_DIRECT
) {
229 // If the scheme has a host/port, parse it.
230 bool ok
= net::ParseHostAndPort(begin
, end
, &host
, &port
);
232 return ProxyServer(); // Invalid -- failed parsing <host>[":"<port>]
234 // Choose a default port number if none was given.
236 port
= GetDefaultPortForScheme(scheme
);
238 host_port_pair
= HostPortPair(HostNoBrackets(host
), port
);
241 return ProxyServer(scheme
, host_port_pair
);