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/string_tokenizer.h" | 9 #include "base/string_tokenizer.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "net/base/net_util.h" | 11 #include "net/base/net_util.h" |
12 #include "net/http/http_util.h" | 12 #include "net/http/http_util.h" |
13 | 13 |
14 namespace net { | 14 namespace net { |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 // Parse the proxy type from a PAC string, to a ProxyServer::Scheme. | 18 // Parse the proxy type from a PAC string, to a ProxyServer::Scheme. |
19 // This mapping is case-insensitive. If no type could be matched | 19 // This mapping is case-insensitive. If no type could be matched |
20 // returns SCHEME_INVALID. | 20 // returns SCHEME_INVALID. |
21 ProxyServer::Scheme GetSchemeFromPacType(std::string::const_iterator begin, | 21 ProxyServer::Scheme GetSchemeFromPacTypeInternal( |
22 std::string::const_iterator end) { | 22 std::string::const_iterator begin, |
| 23 std::string::const_iterator end) { |
23 if (LowerCaseEqualsASCII(begin, end, "proxy")) | 24 if (LowerCaseEqualsASCII(begin, end, "proxy")) |
24 return ProxyServer::SCHEME_HTTP; | 25 return ProxyServer::SCHEME_HTTP; |
25 if (LowerCaseEqualsASCII(begin, end, "socks")) { | 26 if (LowerCaseEqualsASCII(begin, end, "socks")) { |
26 // Default to v4 for compatibility. This is because the SOCKS4 vs SOCKS5 | 27 // 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 // notation didn't originally exist, so if a client returns SOCKS they |
28 // really meant SOCKS4. | 29 // really meant SOCKS4. |
29 return ProxyServer::SCHEME_SOCKS4; | 30 return ProxyServer::SCHEME_SOCKS4; |
30 } | 31 } |
31 if (LowerCaseEqualsASCII(begin, end, "socks4")) | 32 if (LowerCaseEqualsASCII(begin, end, "socks4")) |
32 return ProxyServer::SCHEME_SOCKS4; | 33 return ProxyServer::SCHEME_SOCKS4; |
33 if (LowerCaseEqualsASCII(begin, end, "socks5")) | 34 if (LowerCaseEqualsASCII(begin, end, "socks5")) |
34 return ProxyServer::SCHEME_SOCKS5; | 35 return ProxyServer::SCHEME_SOCKS5; |
35 if (LowerCaseEqualsASCII(begin, end, "direct")) | 36 if (LowerCaseEqualsASCII(begin, end, "direct")) |
36 return ProxyServer::SCHEME_DIRECT; | 37 return ProxyServer::SCHEME_DIRECT; |
37 if (LowerCaseEqualsASCII(begin, end, "https")) | 38 if (LowerCaseEqualsASCII(begin, end, "https")) |
38 return ProxyServer::SCHEME_HTTPS; | 39 return ProxyServer::SCHEME_HTTPS; |
39 | 40 |
40 return ProxyServer::SCHEME_INVALID; | 41 return ProxyServer::SCHEME_INVALID; |
41 } | 42 } |
42 | 43 |
43 // Parse the proxy scheme from a URL-like representation, to a | 44 // Parse the proxy scheme from a URL-like representation, to a |
44 // ProxyServer::Scheme. This corresponds with the values used in | 45 // ProxyServer::Scheme. This corresponds with the values used in |
45 // ProxyServer::ToURI(). If no type could be matched, returns SCHEME_INVALID. | 46 // ProxyServer::ToURI(). If no type could be matched, returns SCHEME_INVALID. |
46 ProxyServer::Scheme GetSchemeFromURI(std::string::const_iterator begin, | 47 ProxyServer::Scheme GetSchemeFromURIInternal(std::string::const_iterator begin, |
47 std::string::const_iterator end) { | 48 std::string::const_iterator end) { |
48 if (LowerCaseEqualsASCII(begin, end, "http")) | 49 if (LowerCaseEqualsASCII(begin, end, "http")) |
49 return ProxyServer::SCHEME_HTTP; | 50 return ProxyServer::SCHEME_HTTP; |
50 if (LowerCaseEqualsASCII(begin, end, "socks4")) | 51 if (LowerCaseEqualsASCII(begin, end, "socks4")) |
51 return ProxyServer::SCHEME_SOCKS4; | 52 return ProxyServer::SCHEME_SOCKS4; |
52 if (LowerCaseEqualsASCII(begin, end, "socks")) | 53 if (LowerCaseEqualsASCII(begin, end, "socks")) |
53 return ProxyServer::SCHEME_SOCKS5; | 54 return ProxyServer::SCHEME_SOCKS5; |
54 if (LowerCaseEqualsASCII(begin, end, "socks5")) | 55 if (LowerCaseEqualsASCII(begin, end, "socks5")) |
55 return ProxyServer::SCHEME_SOCKS5; | 56 return ProxyServer::SCHEME_SOCKS5; |
56 if (LowerCaseEqualsASCII(begin, end, "direct")) | 57 if (LowerCaseEqualsASCII(begin, end, "direct")) |
57 return ProxyServer::SCHEME_DIRECT; | 58 return ProxyServer::SCHEME_DIRECT; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 | 104 |
104 // Trim the leading/trailing whitespace. | 105 // Trim the leading/trailing whitespace. |
105 HttpUtil::TrimLWS(&begin, &end); | 106 HttpUtil::TrimLWS(&begin, &end); |
106 | 107 |
107 // Check for [<scheme> "://"] | 108 // Check for [<scheme> "://"] |
108 std::string::const_iterator colon = std::find(begin, end, ':'); | 109 std::string::const_iterator colon = std::find(begin, end, ':'); |
109 if (colon != end && | 110 if (colon != end && |
110 (end - colon) >= 3 && | 111 (end - colon) >= 3 && |
111 *(colon + 1) == '/' && | 112 *(colon + 1) == '/' && |
112 *(colon + 2) == '/') { | 113 *(colon + 2) == '/') { |
113 scheme = GetSchemeFromURI(begin, colon); | 114 scheme = GetSchemeFromURIInternal(begin, colon); |
114 begin = colon + 3; // Skip past the "://" | 115 begin = colon + 3; // Skip past the "://" |
115 } | 116 } |
116 | 117 |
117 // Now parse the <host>[":"<port>]. | 118 // Now parse the <host>[":"<port>]. |
118 return FromSchemeHostAndPort(scheme, begin, end); | 119 return FromSchemeHostAndPort(scheme, begin, end); |
119 } | 120 } |
120 | 121 |
121 std::string ProxyServer::ToURI() const { | 122 std::string ProxyServer::ToURI() const { |
122 switch (scheme_) { | 123 switch (scheme_) { |
123 case SCHEME_DIRECT: | 124 case SCHEME_DIRECT: |
(...skipping 30 matching lines...) Expand all Loading... |
154 | 155 |
155 // Start by finding the first space (if any). | 156 // Start by finding the first space (if any). |
156 std::string::const_iterator space; | 157 std::string::const_iterator space; |
157 for (space = begin; space != end; ++space) { | 158 for (space = begin; space != end; ++space) { |
158 if (HttpUtil::IsLWS(*space)) { | 159 if (HttpUtil::IsLWS(*space)) { |
159 break; | 160 break; |
160 } | 161 } |
161 } | 162 } |
162 | 163 |
163 // Everything to the left of the space is the scheme. | 164 // Everything to the left of the space is the scheme. |
164 Scheme scheme = GetSchemeFromPacType(begin, space); | 165 Scheme scheme = GetSchemeFromPacTypeInternal(begin, space); |
165 | 166 |
166 // And everything to the right of the space is the | 167 // And everything to the right of the space is the |
167 // <host>[":" <port>]. | 168 // <host>[":" <port>]. |
168 return FromSchemeHostAndPort(scheme, space, end); | 169 return FromSchemeHostAndPort(scheme, space, end); |
169 } | 170 } |
170 | 171 |
171 std::string ProxyServer::ToPacString() const { | 172 std::string ProxyServer::ToPacString() const { |
172 switch (scheme_) { | 173 switch (scheme_) { |
173 case SCHEME_DIRECT: | 174 case SCHEME_DIRECT: |
174 return "DIRECT"; | 175 return "DIRECT"; |
(...skipping 22 matching lines...) Expand all Loading... |
197 case SCHEME_SOCKS5: | 198 case SCHEME_SOCKS5: |
198 return 1080; | 199 return 1080; |
199 case SCHEME_HTTPS: | 200 case SCHEME_HTTPS: |
200 return 443; | 201 return 443; |
201 default: | 202 default: |
202 return -1; | 203 return -1; |
203 } | 204 } |
204 } | 205 } |
205 | 206 |
206 // static | 207 // static |
| 208 ProxyServer::Scheme ProxyServer::GetSchemeFromURI(const std::string& scheme) { |
| 209 return GetSchemeFromURIInternal(scheme.begin(), scheme.end()); |
| 210 } |
| 211 |
| 212 // static |
207 ProxyServer ProxyServer::FromSchemeHostAndPort( | 213 ProxyServer ProxyServer::FromSchemeHostAndPort( |
208 Scheme scheme, | 214 Scheme scheme, |
209 std::string::const_iterator begin, | 215 std::string::const_iterator begin, |
210 std::string::const_iterator end) { | 216 std::string::const_iterator end) { |
211 | 217 |
212 // Trim leading/trailing space. | 218 // Trim leading/trailing space. |
213 HttpUtil::TrimLWS(&begin, &end); | 219 HttpUtil::TrimLWS(&begin, &end); |
214 | 220 |
215 if (scheme == SCHEME_DIRECT && begin != end) | 221 if (scheme == SCHEME_DIRECT && begin != end) |
216 return ProxyServer(); // Invalid -- DIRECT cannot have a host/port. | 222 return ProxyServer(); // Invalid -- DIRECT cannot have a host/port. |
(...skipping 12 matching lines...) Expand all Loading... |
229 if (port == -1) | 235 if (port == -1) |
230 port = GetDefaultPortForScheme(scheme); | 236 port = GetDefaultPortForScheme(scheme); |
231 | 237 |
232 host_port_pair = HostPortPair(HostNoBrackets(host), port); | 238 host_port_pair = HostPortPair(HostNoBrackets(host), port); |
233 } | 239 } |
234 | 240 |
235 return ProxyServer(scheme, host_port_pair); | 241 return ProxyServer(scheme, host_port_pair); |
236 } | 242 } |
237 | 243 |
238 } // namespace net | 244 } // namespace net |
OLD | NEW |