blob: 49038801d662a5283ad138a428d460233c595aa9 [file] [log] [blame]
[email protected]f6fb2de2009-02-19 08:11:421// Copyright (c) 2009 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.
4
[email protected]f6fb2de2009-02-19 08:11:425#include "net/proxy/proxy_server.h"
6
[email protected]4b80a752009-02-19 08:51:457#include <algorithm>
8
[email protected]f6fb2de2009-02-19 08:11:429#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"
13
14namespace net {
15
16namespace {
17
[email protected]f6fb2de2009-02-19 08:11:4218// Parse 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.
21ProxyServer::Scheme GetSchemeFromPacType(std::string::const_iterator begin,
22 std::string::const_iterator end) {
23 if (LowerCaseEqualsASCII(begin, end, "proxy"))
24 return ProxyServer::SCHEME_HTTP;
25 if (LowerCaseEqualsASCII(begin, end, "socks")) {
26 // Default to v4 for compatibility. This is because the SOCKS4 vs SOCKS5
27 // notation didn't originally exist, so if a client returns SOCKS they
28 // really meant SOCKS4.
29 return ProxyServer::SCHEME_SOCKS4;
30 }
31 if (LowerCaseEqualsASCII(begin, end, "socks4"))
32 return ProxyServer::SCHEME_SOCKS4;
33 if (LowerCaseEqualsASCII(begin, end, "socks5"))
34 return ProxyServer::SCHEME_SOCKS5;
35 if (LowerCaseEqualsASCII(begin, end, "direct"))
36 return ProxyServer::SCHEME_DIRECT;
37
38 return ProxyServer::SCHEME_INVALID;
39}
40
41// Parse the proxy scheme from a URL-like representation, to a
42// ProxyServer::Scheme. This corresponds with the values used in
43// ProxyServer::ToURI(). If no type could be matched, returns SCHEME_INVALID.
44ProxyServer::Scheme GetSchemeFromURI(std::string::const_iterator begin,
45 std::string::const_iterator end) {
46 if (LowerCaseEqualsASCII(begin, end, "http"))
47 return ProxyServer::SCHEME_HTTP;
48 if (LowerCaseEqualsASCII(begin, end, "socks4"))
49 return ProxyServer::SCHEME_SOCKS4;
[email protected]3cd17242009-06-23 02:59:0250 if (LowerCaseEqualsASCII(begin, end, "socks"))
51 return ProxyServer::SCHEME_SOCKS4;
[email protected]f6fb2de2009-02-19 08:11:4252 if (LowerCaseEqualsASCII(begin, end, "socks5"))
53 return ProxyServer::SCHEME_SOCKS5;
54 if (LowerCaseEqualsASCII(begin, end, "direct"))
55 return ProxyServer::SCHEME_DIRECT;
56 return ProxyServer::SCHEME_INVALID;
57}
58
59} // namespace
60
[email protected]e57ec6f72009-05-14 23:17:1461std::string ProxyServer::HostNoBrackets() const {
[email protected]f6fb2de2009-02-19 08:11:4262 // Doesn't make sense to call this if the URI scheme doesn't
63 // have concept of a host.
64 DCHECK(is_valid() && !is_direct());
[email protected]e57ec6f72009-05-14 23:17:1465
66 // Remove brackets from an RFC 2732-style IPv6 literal address.
67 const std::string::size_type len = host_.size();
68 if (len != 0 && host_[0] == '[' && host_[len - 1] == ']')
69 return host_.substr(1, len - 2);
[email protected]f6fb2de2009-02-19 08:11:4270 return host_;
71}
72
73int ProxyServer::port() const {
74 // Doesn't make sense to call this if the URI scheme doesn't
75 // have concept of a port.
76 DCHECK(is_valid() && !is_direct());
77 return port_;
78}
79
80std::string ProxyServer::host_and_port() const {
81 // Doesn't make sense to call this if the URI scheme doesn't
82 // have concept of a host.
83 DCHECK(is_valid() && !is_direct());
84 return host_ + ":" + IntToString(port_);
85}
86
[email protected]2d731a32010-04-29 01:04:0687HostPortPair ProxyServer::host_port_pair() const {
88 // Doesn't make sense to call this if the URI scheme doesn't
89 // have concept of a host.
90 DCHECK(is_valid() && !is_direct());
91 return HostPortPair(host_, port_);
92}
93
[email protected]f6fb2de2009-02-19 08:11:4294// static
[email protected]87a102b2009-07-14 05:23:3095ProxyServer ProxyServer::FromURI(const std::string& uri,
96 Scheme default_scheme) {
97 return FromURI(uri.begin(), uri.end(), default_scheme);
[email protected]f6fb2de2009-02-19 08:11:4298}
99
100// static
101ProxyServer ProxyServer::FromURI(std::string::const_iterator begin,
[email protected]87a102b2009-07-14 05:23:30102 std::string::const_iterator end,
103 Scheme default_scheme) {
104 // We will default to |default_scheme| if no scheme specifier was given.
105 Scheme scheme = default_scheme;
[email protected]f6fb2de2009-02-19 08:11:42106
107 // Trim the leading/trailing whitespace.
108 HttpUtil::TrimLWS(&begin, &end);
109
110 // Check for [<scheme> "://"]
111 std::string::const_iterator colon = std::find(begin, end, ':');
112 if (colon != end &&
113 (end - colon) >= 3 &&
114 *(colon + 1) == '/' &&
115 *(colon + 2) == '/') {
116 scheme = GetSchemeFromURI(begin, colon);
117 begin = colon + 3; // Skip past the "://"
118 }
119
120 // Now parse the <host>[":"<port>].
121 return FromSchemeHostAndPort(scheme, begin, end);
122}
123
124std::string ProxyServer::ToURI() const {
125 switch (scheme_) {
126 case SCHEME_DIRECT:
127 return "direct://";
128 case SCHEME_HTTP:
129 // Leave off "http://" since it is our default scheme.
130 return host_and_port();
131 case SCHEME_SOCKS4:
132 return std::string("socks4://") + host_and_port();
133 case SCHEME_SOCKS5:
134 return std::string("socks5://") + host_and_port();
135 default:
136 // Got called with an invalid scheme.
137 NOTREACHED();
138 return std::string();
139 }
140}
141
142// static
143ProxyServer ProxyServer::FromPacString(const std::string& pac_string) {
144 return FromPacString(pac_string.begin(), pac_string.end());
145}
146
147ProxyServer ProxyServer::FromPacString(std::string::const_iterator begin,
148 std::string::const_iterator end) {
149 // Trim the leading/trailing whitespace.
150 HttpUtil::TrimLWS(&begin, &end);
151
152 // Input should match:
153 // "DIRECT" | ( <type> 1*(LWS) <host-and-port> )
154
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)) {
159 break;
160 }
161 }
162
163 // Everything to the left of the space is the scheme.
164 Scheme scheme = GetSchemeFromPacType(begin, space);
165
166 // And everything to the right of the space is the
167 // <host>[":" <port>].
168 return FromSchemeHostAndPort(scheme, space, end);
169}
170
171std::string ProxyServer::ToPacString() const {
172 switch (scheme_) {
173 case SCHEME_DIRECT:
174 return "DIRECT";
175 case SCHEME_HTTP:
176 return std::string("PROXY ") + host_and_port();
177 case SCHEME_SOCKS4:
178 // For compatibility send SOCKS instead of SOCKS4.
179 return std::string("SOCKS ") + host_and_port();
180 case SCHEME_SOCKS5:
181 return std::string("SOCKS5 ") + host_and_port();
182 default:
183 // Got called with an invalid scheme.
184 NOTREACHED();
185 return std::string();
186 }
187}
188
189// static
[email protected]060313902009-02-19 22:20:22190int ProxyServer::GetDefaultPortForScheme(Scheme scheme) {
191 switch (scheme) {
192 case SCHEME_HTTP:
193 return 80;
194 case SCHEME_SOCKS4:
195 case SCHEME_SOCKS5:
196 return 1080;
197 default:
198 return -1;
199 }
200}
[email protected]f0a51fb52009-03-05 12:46:38201
[email protected]060313902009-02-19 22:20:22202// static
[email protected]f6fb2de2009-02-19 08:11:42203ProxyServer ProxyServer::FromSchemeHostAndPort(
204 Scheme scheme,
205 std::string::const_iterator begin,
206 std::string::const_iterator end) {
207
208 // Trim leading/trailing space.
209 HttpUtil::TrimLWS(&begin, &end);
210
211 if (scheme == SCHEME_DIRECT && begin != end)
212 return ProxyServer(); // Invalid -- DIRECT cannot have a host/port.
213
214 std::string host;
215 int port = -1;
216
217 if (scheme != SCHEME_INVALID && scheme != SCHEME_DIRECT) {
218 // If the scheme has a host/port, parse it.
[email protected]71e4573a2009-05-21 22:03:00219 bool ok = net::ParseHostAndPort(begin, end, &host, &port);
[email protected]f6fb2de2009-02-19 08:11:42220 if (!ok)
221 return ProxyServer(); // Invalid -- failed parsing <host>[":"<port>]
222 }
223
224 // Choose a default port number if none was given.
225 if (port == -1)
226 port = GetDefaultPortForScheme(scheme);
227
228 return ProxyServer(scheme, host, port);
229}
230
231} // namespace net