OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/proxy/proxy_server.h" | 5 #include "net/proxy/proxy_server.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "net/base/net_util.h" | 10 #include "net/base/net_util.h" |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 // Remove brackets from an RFC 2732-style IPv6 literal address. | 68 // Remove brackets from an RFC 2732-style IPv6 literal address. |
69 const std::string::size_type len = host.size(); | 69 const std::string::size_type len = host.size(); |
70 if (len >= 2 && host[0] == '[' && host[len - 1] == ']') | 70 if (len >= 2 && host[0] == '[' && host[len - 1] == ']') |
71 return host.substr(1, len - 2); | 71 return host.substr(1, len - 2); |
72 return host; | 72 return host; |
73 } | 73 } |
74 | 74 |
75 } // namespace | 75 } // namespace |
76 | 76 |
77 ProxyServer::ProxyServer(Scheme scheme, const HostPortPair& host_port_pair) | 77 ProxyServer::ProxyServer(Scheme scheme, const HostPortPair& host_port_pair) |
78 : scheme_(scheme), host_port_pair_(host_port_pair) { | 78 : scheme_(scheme), host_port_pair_(host_port_pair) { |
79 if (scheme_ == SCHEME_DIRECT || scheme_ == SCHEME_INVALID) { | 79 if (scheme_ == SCHEME_DIRECT || scheme_ == SCHEME_INVALID) { |
80 // |host_port_pair| isn't relevant for these special schemes, so none should | 80 // |host_port_pair| isn't relevant for these special schemes, so none should |
81 // have been specified. It is important for this to be consistent since we | 81 // have been specified. It is important for this to be consistent since we |
82 // do raw field comparisons in the equality and comparison functions. | 82 // do raw field comparisons in the equality and comparison functions. |
83 DCHECK(host_port_pair.Equals(HostPortPair())); | 83 DCHECK(host_port_pair.Equals(HostPortPair())); |
84 host_port_pair_ = HostPortPair(); | 84 host_port_pair_ = HostPortPair(); |
85 } | 85 } |
86 } | 86 } |
87 | 87 |
88 const HostPortPair& ProxyServer::host_port_pair() const { | 88 const HostPortPair& ProxyServer::host_port_pair() const { |
(...skipping 14 matching lines...) Expand all Loading... |
103 std::string::const_iterator end, | 103 std::string::const_iterator end, |
104 Scheme default_scheme) { | 104 Scheme default_scheme) { |
105 // We will default to |default_scheme| if no scheme specifier was given. | 105 // We will default to |default_scheme| if no scheme specifier was given. |
106 Scheme scheme = default_scheme; | 106 Scheme scheme = default_scheme; |
107 | 107 |
108 // Trim the leading/trailing whitespace. | 108 // Trim the leading/trailing whitespace. |
109 HttpUtil::TrimLWS(&begin, &end); | 109 HttpUtil::TrimLWS(&begin, &end); |
110 | 110 |
111 // Check for [<scheme> "://"] | 111 // Check for [<scheme> "://"] |
112 std::string::const_iterator colon = std::find(begin, end, ':'); | 112 std::string::const_iterator colon = std::find(begin, end, ':'); |
113 if (colon != end && | 113 if (colon != end && (end - colon) >= 3 && *(colon + 1) == '/' && |
114 (end - colon) >= 3 && | |
115 *(colon + 1) == '/' && | |
116 *(colon + 2) == '/') { | 114 *(colon + 2) == '/') { |
117 scheme = GetSchemeFromURIInternal(begin, colon); | 115 scheme = GetSchemeFromURIInternal(begin, colon); |
118 begin = colon + 3; // Skip past the "://" | 116 begin = colon + 3; // Skip past the "://" |
119 } | 117 } |
120 | 118 |
121 // Now parse the <host>[":"<port>]. | 119 // Now parse the <host>[":"<port>]. |
122 return FromSchemeHostAndPort(scheme, begin, end); | 120 return FromSchemeHostAndPort(scheme, begin, end); |
123 } | 121 } |
124 | 122 |
125 std::string ProxyServer::ToURI() const { | 123 std::string ProxyServer::ToURI() const { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 | 166 |
169 // Everything to the left of the space is the scheme. | 167 // Everything to the left of the space is the scheme. |
170 Scheme scheme = GetSchemeFromPacTypeInternal(begin, space); | 168 Scheme scheme = GetSchemeFromPacTypeInternal(begin, space); |
171 | 169 |
172 // And everything to the right of the space is the | 170 // And everything to the right of the space is the |
173 // <host>[":" <port>]. | 171 // <host>[":" <port>]. |
174 return FromSchemeHostAndPort(scheme, space, end); | 172 return FromSchemeHostAndPort(scheme, space, end); |
175 } | 173 } |
176 | 174 |
177 std::string ProxyServer::ToPacString() const { | 175 std::string ProxyServer::ToPacString() const { |
178 switch (scheme_) { | 176 switch (scheme_) { |
179 case SCHEME_DIRECT: | 177 case SCHEME_DIRECT: |
180 return "DIRECT"; | 178 return "DIRECT"; |
181 case SCHEME_HTTP: | 179 case SCHEME_HTTP: |
182 return std::string("PROXY ") + host_port_pair().ToString(); | 180 return std::string("PROXY ") + host_port_pair().ToString(); |
183 case SCHEME_SOCKS4: | 181 case SCHEME_SOCKS4: |
184 // For compatibility send SOCKS instead of SOCKS4. | 182 // For compatibility send SOCKS instead of SOCKS4. |
185 return std::string("SOCKS ") + host_port_pair().ToString(); | 183 return std::string("SOCKS ") + host_port_pair().ToString(); |
186 case SCHEME_SOCKS5: | 184 case SCHEME_SOCKS5: |
187 return std::string("SOCKS5 ") + host_port_pair().ToString(); | 185 return std::string("SOCKS5 ") + host_port_pair().ToString(); |
188 case SCHEME_HTTPS: | 186 case SCHEME_HTTPS: |
(...skipping 26 matching lines...) Expand all Loading... |
215 } | 213 } |
216 | 214 |
217 // static | 215 // static |
218 ProxyServer::Scheme ProxyServer::GetSchemeFromURI(const std::string& scheme) { | 216 ProxyServer::Scheme ProxyServer::GetSchemeFromURI(const std::string& scheme) { |
219 return GetSchemeFromURIInternal(scheme.begin(), scheme.end()); | 217 return GetSchemeFromURIInternal(scheme.begin(), scheme.end()); |
220 } | 218 } |
221 | 219 |
222 // TODO(bengr): Use |scheme_| to indicate that this is the data reduction proxy. | 220 // TODO(bengr): Use |scheme_| to indicate that this is the data reduction proxy. |
223 #if defined(SPDY_PROXY_AUTH_ORIGIN) | 221 #if defined(SPDY_PROXY_AUTH_ORIGIN) |
224 bool ProxyServer::isDataReductionProxy() const { | 222 bool ProxyServer::isDataReductionProxy() const { |
225 bool dev_host = false; | 223 bool dev_host = false; |
226 #if defined (DATA_REDUCTION_DEV_HOST) | 224 #if defined(DATA_REDUCTION_DEV_HOST) |
227 dev_host = host_port_pair_.Equals( | 225 dev_host = host_port_pair_.Equals( |
228 HostPortPair::FromURL(GURL(DATA_REDUCTION_DEV_HOST))); | 226 HostPortPair::FromURL(GURL(DATA_REDUCTION_DEV_HOST))); |
229 #endif | 227 #endif |
230 return dev_host || host_port_pair_.Equals( | 228 return dev_host || |
231 HostPortPair::FromURL(GURL(SPDY_PROXY_AUTH_ORIGIN))); | 229 host_port_pair_.Equals( |
| 230 HostPortPair::FromURL(GURL(SPDY_PROXY_AUTH_ORIGIN))); |
232 } | 231 } |
233 | 232 |
234 bool ProxyServer::isDataReductionProxyFallback() const { | 233 bool ProxyServer::isDataReductionProxyFallback() const { |
235 #if defined(DATA_REDUCTION_FALLBACK_HOST) | 234 #if defined(DATA_REDUCTION_FALLBACK_HOST) |
236 return host_port_pair_.Equals( | 235 return host_port_pair_.Equals( |
237 HostPortPair::FromURL(GURL(DATA_REDUCTION_FALLBACK_HOST))); | 236 HostPortPair::FromURL(GURL(DATA_REDUCTION_FALLBACK_HOST))); |
238 #endif // defined(DATA_REDUCTION_FALLBACK_HOST) | 237 #endif // defined(DATA_REDUCTION_FALLBACK_HOST) |
239 return false; | 238 return false; |
240 } | 239 } |
241 #endif // defined(SPDY_PROXY_AUTH_ORIGIN) | 240 #endif // defined(SPDY_PROXY_AUTH_ORIGIN) |
242 | 241 |
243 // static | 242 // static |
244 ProxyServer ProxyServer::FromSchemeHostAndPort( | 243 ProxyServer ProxyServer::FromSchemeHostAndPort( |
245 Scheme scheme, | 244 Scheme scheme, |
246 std::string::const_iterator begin, | 245 std::string::const_iterator begin, |
247 std::string::const_iterator end) { | 246 std::string::const_iterator end) { |
248 | |
249 // Trim leading/trailing space. | 247 // Trim leading/trailing space. |
250 HttpUtil::TrimLWS(&begin, &end); | 248 HttpUtil::TrimLWS(&begin, &end); |
251 | 249 |
252 if (scheme == SCHEME_DIRECT && begin != end) | 250 if (scheme == SCHEME_DIRECT && begin != end) |
253 return ProxyServer(); // Invalid -- DIRECT cannot have a host/port. | 251 return ProxyServer(); // Invalid -- DIRECT cannot have a host/port. |
254 | 252 |
255 HostPortPair host_port_pair; | 253 HostPortPair host_port_pair; |
256 | 254 |
257 if (scheme != SCHEME_INVALID && scheme != SCHEME_DIRECT) { | 255 if (scheme != SCHEME_INVALID && scheme != SCHEME_DIRECT) { |
258 std::string host; | 256 std::string host; |
259 int port = -1; | 257 int port = -1; |
260 // If the scheme has a host/port, parse it. | 258 // If the scheme has a host/port, parse it. |
261 bool ok = net::ParseHostAndPort(begin, end, &host, &port); | 259 bool ok = net::ParseHostAndPort(begin, end, &host, &port); |
262 if (!ok) | 260 if (!ok) |
263 return ProxyServer(); // Invalid -- failed parsing <host>[":"<port>] | 261 return ProxyServer(); // Invalid -- failed parsing <host>[":"<port>] |
264 | 262 |
265 // Choose a default port number if none was given. | 263 // Choose a default port number if none was given. |
266 if (port == -1) | 264 if (port == -1) |
267 port = GetDefaultPortForScheme(scheme); | 265 port = GetDefaultPortForScheme(scheme); |
268 | 266 |
269 host_port_pair = HostPortPair(HostNoBrackets(host), port); | 267 host_port_pair = HostPortPair(HostNoBrackets(host), port); |
270 } | 268 } |
271 | 269 |
272 return ProxyServer(scheme, host_port_pair); | 270 return ProxyServer(scheme, host_port_pair); |
273 } | 271 } |
274 | 272 |
275 } // namespace net | 273 } // namespace net |
OLD | NEW |