OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_config.h" | 5 #include "net/proxy/proxy_config.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "base/strings/string_tokenizer.h" | 9 #include "base/strings/string_tokenizer.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 base::StringTokenizer proxy_uri_list(uri_list, ","); | 29 base::StringTokenizer proxy_uri_list(uri_list, ","); |
30 while (proxy_uri_list.GetNext()) { | 30 while (proxy_uri_list.GetNext()) { |
31 proxy_list->AddProxyServer( | 31 proxy_list->AddProxyServer( |
32 ProxyServer::FromURI(proxy_uri_list.token(), default_scheme)); | 32 ProxyServer::FromURI(proxy_uri_list.token(), default_scheme)); |
33 } | 33 } |
34 } | 34 } |
35 | 35 |
36 } // namespace | 36 } // namespace |
37 | 37 |
38 ProxyConfig::ProxyRules::ProxyRules() | 38 ProxyConfig::ProxyRules::ProxyRules() |
39 : reverse_bypass(false), | 39 : reverse_bypass(false), type(TYPE_NO_RULES) { |
40 type(TYPE_NO_RULES) { | |
41 } | 40 } |
42 | 41 |
43 ProxyConfig::ProxyRules::~ProxyRules() { | 42 ProxyConfig::ProxyRules::~ProxyRules() { |
44 } | 43 } |
45 | 44 |
46 void ProxyConfig::ProxyRules::Apply(const GURL& url, ProxyInfo* result) const { | 45 void ProxyConfig::ProxyRules::Apply(const GURL& url, ProxyInfo* result) const { |
47 if (empty()) { | 46 if (empty()) { |
48 result->UseDirect(); | 47 result->UseDirect(); |
49 return; | 48 return; |
50 } | 49 } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 | 96 |
98 while (proxy_server_for_scheme.GetNext()) { | 97 while (proxy_server_for_scheme.GetNext()) { |
99 std::string url_scheme = proxy_server_for_scheme.token(); | 98 std::string url_scheme = proxy_server_for_scheme.token(); |
100 | 99 |
101 // If we fail to get the proxy server here, it means that | 100 // If we fail to get the proxy server here, it means that |
102 // this is a regular proxy server configuration, i.e. proxies | 101 // this is a regular proxy server configuration, i.e. proxies |
103 // are not configured per protocol. | 102 // are not configured per protocol. |
104 if (!proxy_server_for_scheme.GetNext()) { | 103 if (!proxy_server_for_scheme.GetNext()) { |
105 if (type == TYPE_PROXY_PER_SCHEME) | 104 if (type == TYPE_PROXY_PER_SCHEME) |
106 continue; // Unexpected. | 105 continue; // Unexpected. |
107 AddProxyURIListToProxyList(url_scheme, | 106 AddProxyURIListToProxyList( |
108 &single_proxies, | 107 url_scheme, &single_proxies, ProxyServer::SCHEME_HTTP); |
109 ProxyServer::SCHEME_HTTP); | |
110 type = TYPE_SINGLE_PROXY; | 108 type = TYPE_SINGLE_PROXY; |
111 return; | 109 return; |
112 } | 110 } |
113 | 111 |
114 // Trim whitespace off the url scheme. | 112 // Trim whitespace off the url scheme. |
115 base::TrimWhitespaceASCII(url_scheme, base::TRIM_ALL, &url_scheme); | 113 base::TrimWhitespaceASCII(url_scheme, base::TRIM_ALL, &url_scheme); |
116 | 114 |
117 // Add it to the per-scheme mappings (if supported scheme). | 115 // Add it to the per-scheme mappings (if supported scheme). |
118 type = TYPE_PROXY_PER_SCHEME; | 116 type = TYPE_PROXY_PER_SCHEME; |
119 ProxyList* entry = MapUrlSchemeToProxyListNoFallback(url_scheme); | 117 ProxyList* entry = MapUrlSchemeToProxyListNoFallback(url_scheme); |
120 ProxyServer::Scheme default_scheme = ProxyServer::SCHEME_HTTP; | 118 ProxyServer::Scheme default_scheme = ProxyServer::SCHEME_HTTP; |
121 | 119 |
122 // socks=XXX is inconsistent with the other formats, since "socks" | 120 // socks=XXX is inconsistent with the other formats, since "socks" |
123 // is not a URL scheme. Rather this means "for everything else, send | 121 // is not a URL scheme. Rather this means "for everything else, send |
124 // it to the SOCKS proxy server XXX". | 122 // it to the SOCKS proxy server XXX". |
125 if (url_scheme == "socks") { | 123 if (url_scheme == "socks") { |
126 DCHECK(!entry); | 124 DCHECK(!entry); |
127 entry = &fallback_proxies; | 125 entry = &fallback_proxies; |
128 // Note that here 'socks' is understood to be SOCKS4, even though | 126 // Note that here 'socks' is understood to be SOCKS4, even though |
129 // 'socks' maps to SOCKS5 in ProxyServer::GetSchemeFromURIInternal. | 127 // 'socks' maps to SOCKS5 in ProxyServer::GetSchemeFromURIInternal. |
130 default_scheme = ProxyServer::SCHEME_SOCKS4; | 128 default_scheme = ProxyServer::SCHEME_SOCKS4; |
131 } | 129 } |
132 | 130 |
133 if (entry) { | 131 if (entry) { |
134 AddProxyURIListToProxyList(proxy_server_for_scheme.token(), | 132 AddProxyURIListToProxyList( |
135 entry, | 133 proxy_server_for_scheme.token(), entry, default_scheme); |
136 default_scheme); | |
137 } | 134 } |
138 } | 135 } |
139 } | 136 } |
140 } | 137 } |
141 | 138 |
142 const ProxyList* ProxyConfig::ProxyRules::MapUrlSchemeToProxyList( | 139 const ProxyList* ProxyConfig::ProxyRules::MapUrlSchemeToProxyList( |
143 const std::string& url_scheme) const { | 140 const std::string& url_scheme) const { |
144 const ProxyList* proxy_server_list = const_cast<ProxyRules*>(this)-> | 141 const ProxyList* proxy_server_list = |
145 MapUrlSchemeToProxyListNoFallback(url_scheme); | 142 const_cast<ProxyRules*>(this) |
| 143 ->MapUrlSchemeToProxyListNoFallback(url_scheme); |
146 if (proxy_server_list && !proxy_server_list->IsEmpty()) | 144 if (proxy_server_list && !proxy_server_list->IsEmpty()) |
147 return proxy_server_list; | 145 return proxy_server_list; |
148 if (!fallback_proxies.IsEmpty()) | 146 if (!fallback_proxies.IsEmpty()) |
149 return &fallback_proxies; | 147 return &fallback_proxies; |
150 return NULL; // No mapping for this scheme. Use direct. | 148 return NULL; // No mapping for this scheme. Use direct. |
151 } | 149 } |
152 | 150 |
153 bool ProxyConfig::ProxyRules::Equals(const ProxyRules& other) const { | 151 bool ProxyConfig::ProxyRules::Equals(const ProxyRules& other) const { |
154 return type == other.type && | 152 return type == other.type && single_proxies.Equals(other.single_proxies) && |
155 single_proxies.Equals(other.single_proxies) && | |
156 proxies_for_http.Equals(other.proxies_for_http) && | 153 proxies_for_http.Equals(other.proxies_for_http) && |
157 proxies_for_https.Equals(other.proxies_for_https) && | 154 proxies_for_https.Equals(other.proxies_for_https) && |
158 proxies_for_ftp.Equals(other.proxies_for_ftp) && | 155 proxies_for_ftp.Equals(other.proxies_for_ftp) && |
159 fallback_proxies.Equals(other.fallback_proxies) && | 156 fallback_proxies.Equals(other.fallback_proxies) && |
160 bypass_rules.Equals(other.bypass_rules) && | 157 bypass_rules.Equals(other.bypass_rules) && |
161 reverse_bypass == other.reverse_bypass; | 158 reverse_bypass == other.reverse_bypass; |
162 } | 159 } |
163 | 160 |
164 ProxyList* ProxyConfig::ProxyRules::MapUrlSchemeToProxyListNoFallback( | 161 ProxyList* ProxyConfig::ProxyRules::MapUrlSchemeToProxyListNoFallback( |
165 const std::string& scheme) { | 162 const std::string& scheme) { |
166 DCHECK_EQ(TYPE_PROXY_PER_SCHEME, type); | 163 DCHECK_EQ(TYPE_PROXY_PER_SCHEME, type); |
167 if (scheme == "http") | 164 if (scheme == "http") |
168 return &proxies_for_http; | 165 return &proxies_for_http; |
169 if (scheme == "https") | 166 if (scheme == "https") |
170 return &proxies_for_https; | 167 return &proxies_for_https; |
171 if (scheme == "ftp") | 168 if (scheme == "ftp") |
172 return &proxies_for_ftp; | 169 return &proxies_for_ftp; |
173 return NULL; // No mapping for this scheme. | 170 return NULL; // No mapping for this scheme. |
174 } | 171 } |
175 | 172 |
176 ProxyConfig::ProxyConfig() | 173 ProxyConfig::ProxyConfig() |
177 : auto_detect_(false), pac_mandatory_(false), | 174 : auto_detect_(false), |
178 source_(PROXY_CONFIG_SOURCE_UNKNOWN), id_(kInvalidConfigID) { | 175 pac_mandatory_(false), |
| 176 source_(PROXY_CONFIG_SOURCE_UNKNOWN), |
| 177 id_(kInvalidConfigID) { |
179 } | 178 } |
180 | 179 |
181 ProxyConfig::ProxyConfig(const ProxyConfig& config) | 180 ProxyConfig::ProxyConfig(const ProxyConfig& config) |
182 : auto_detect_(config.auto_detect_), | 181 : auto_detect_(config.auto_detect_), |
183 pac_url_(config.pac_url_), | 182 pac_url_(config.pac_url_), |
184 pac_mandatory_(config.pac_mandatory_), | 183 pac_mandatory_(config.pac_mandatory_), |
185 proxy_rules_(config.proxy_rules_), | 184 proxy_rules_(config.proxy_rules_), |
186 source_(config.source_), | 185 source_(config.source_), |
187 id_(config.id_) { | 186 id_(config.id_) { |
188 } | 187 } |
189 | 188 |
190 ProxyConfig::~ProxyConfig() { | 189 ProxyConfig::~ProxyConfig() { |
191 } | 190 } |
192 | 191 |
193 ProxyConfig& ProxyConfig::operator=(const ProxyConfig& config) { | 192 ProxyConfig& ProxyConfig::operator=(const ProxyConfig& config) { |
194 auto_detect_ = config.auto_detect_; | 193 auto_detect_ = config.auto_detect_; |
195 pac_url_ = config.pac_url_; | 194 pac_url_ = config.pac_url_; |
196 pac_mandatory_ = config.pac_mandatory_; | 195 pac_mandatory_ = config.pac_mandatory_; |
197 proxy_rules_ = config.proxy_rules_; | 196 proxy_rules_ = config.proxy_rules_; |
198 source_ = config.source_; | 197 source_ = config.source_; |
199 id_ = config.id_; | 198 id_ = config.id_; |
200 return *this; | 199 return *this; |
201 } | 200 } |
202 | 201 |
203 bool ProxyConfig::Equals(const ProxyConfig& other) const { | 202 bool ProxyConfig::Equals(const ProxyConfig& other) const { |
204 // The two configs can have different IDs and sources. We are just interested | 203 // The two configs can have different IDs and sources. We are just interested |
205 // in if they have the same settings. | 204 // in if they have the same settings. |
206 return auto_detect_ == other.auto_detect_ && | 205 return auto_detect_ == other.auto_detect_ && pac_url_ == other.pac_url_ && |
207 pac_url_ == other.pac_url_ && | |
208 pac_mandatory_ == other.pac_mandatory_ && | 206 pac_mandatory_ == other.pac_mandatory_ && |
209 proxy_rules_.Equals(other.proxy_rules()); | 207 proxy_rules_.Equals(other.proxy_rules()); |
210 } | 208 } |
211 | 209 |
212 bool ProxyConfig::HasAutomaticSettings() const { | 210 bool ProxyConfig::HasAutomaticSettings() const { |
213 return auto_detect_ || has_pac_url(); | 211 return auto_detect_ || has_pac_url(); |
214 } | 212 } |
215 | 213 |
216 void ProxyConfig::ClearAutomaticSettings() { | 214 void ProxyConfig::ClearAutomaticSettings() { |
217 auto_detect_ = false; | 215 auto_detect_ = false; |
218 pac_url_ = GURL(); | 216 pac_url_ = GURL(); |
219 } | 217 } |
220 | 218 |
221 base::DictionaryValue* ProxyConfig::ToValue() const { | 219 base::DictionaryValue* ProxyConfig::ToValue() const { |
222 base::DictionaryValue* dict = new base::DictionaryValue(); | 220 base::DictionaryValue* dict = new base::DictionaryValue(); |
223 | 221 |
224 // Output the automatic settings. | 222 // Output the automatic settings. |
225 if (auto_detect_) | 223 if (auto_detect_) |
226 dict->SetBoolean("auto_detect", auto_detect_); | 224 dict->SetBoolean("auto_detect", auto_detect_); |
227 if (has_pac_url()) { | 225 if (has_pac_url()) { |
228 dict->SetString("pac_url", pac_url_.possibly_invalid_spec()); | 226 dict->SetString("pac_url", pac_url_.possibly_invalid_spec()); |
229 if (pac_mandatory_) | 227 if (pac_mandatory_) |
230 dict->SetBoolean("pac_mandatory", pac_mandatory_); | 228 dict->SetBoolean("pac_mandatory", pac_mandatory_); |
231 } | 229 } |
232 | 230 |
233 // Output the manual settings. | 231 // Output the manual settings. |
234 if (proxy_rules_.type != ProxyRules::TYPE_NO_RULES) { | 232 if (proxy_rules_.type != ProxyRules::TYPE_NO_RULES) { |
235 switch (proxy_rules_.type) { | 233 switch (proxy_rules_.type) { |
236 case ProxyRules::TYPE_SINGLE_PROXY: | 234 case ProxyRules::TYPE_SINGLE_PROXY: |
237 AddProxyListToValue("single_proxy", | 235 AddProxyListToValue("single_proxy", proxy_rules_.single_proxies, dict); |
238 proxy_rules_.single_proxies, dict); | |
239 break; | 236 break; |
240 case ProxyRules::TYPE_PROXY_PER_SCHEME: { | 237 case ProxyRules::TYPE_PROXY_PER_SCHEME: { |
241 base::DictionaryValue* dict2 = new base::DictionaryValue(); | 238 base::DictionaryValue* dict2 = new base::DictionaryValue(); |
242 AddProxyListToValue("http", proxy_rules_.proxies_for_http, dict2); | 239 AddProxyListToValue("http", proxy_rules_.proxies_for_http, dict2); |
243 AddProxyListToValue("https", proxy_rules_.proxies_for_https, dict2); | 240 AddProxyListToValue("https", proxy_rules_.proxies_for_https, dict2); |
244 AddProxyListToValue("ftp", proxy_rules_.proxies_for_ftp, dict2); | 241 AddProxyListToValue("ftp", proxy_rules_.proxies_for_ftp, dict2); |
245 AddProxyListToValue("fallback", proxy_rules_.fallback_proxies, dict2); | 242 AddProxyListToValue("fallback", proxy_rules_.fallback_proxies, dict2); |
246 dict->Set("proxy_per_scheme", dict2); | 243 dict->Set("proxy_per_scheme", dict2); |
247 break; | 244 break; |
248 } | 245 } |
249 default: | 246 default: |
250 NOTREACHED(); | 247 NOTREACHED(); |
251 } | 248 } |
252 | 249 |
253 // Output the bypass rules. | 250 // Output the bypass rules. |
254 const ProxyBypassRules& bypass = proxy_rules_.bypass_rules; | 251 const ProxyBypassRules& bypass = proxy_rules_.bypass_rules; |
255 if (!bypass.rules().empty()) { | 252 if (!bypass.rules().empty()) { |
256 if (proxy_rules_.reverse_bypass) | 253 if (proxy_rules_.reverse_bypass) |
257 dict->SetBoolean("reverse_bypass", true); | 254 dict->SetBoolean("reverse_bypass", true); |
258 | 255 |
259 base::ListValue* list = new base::ListValue(); | 256 base::ListValue* list = new base::ListValue(); |
260 | 257 |
261 for (ProxyBypassRules::RuleList::const_iterator it = | 258 for (ProxyBypassRules::RuleList::const_iterator it = |
262 bypass.rules().begin(); | 259 bypass.rules().begin(); |
263 it != bypass.rules().end(); ++it) { | 260 it != bypass.rules().end(); |
| 261 ++it) { |
264 list->Append(new base::StringValue((*it)->ToString())); | 262 list->Append(new base::StringValue((*it)->ToString())); |
265 } | 263 } |
266 | 264 |
267 dict->Set("bypass_list", list); | 265 dict->Set("bypass_list", list); |
268 } | 266 } |
269 } | 267 } |
270 | 268 |
271 // Output the source. | 269 // Output the source. |
272 dict->SetString("source", ProxyConfigSourceToString(source_)); | 270 dict->SetString("source", ProxyConfigSourceToString(source_)); |
273 | 271 |
274 return dict; | 272 return dict; |
275 } | 273 } |
276 | 274 |
277 } // namespace net | 275 } // namespace net |
OLD | NEW |