Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(150)

Side by Side Diff: net/proxy/proxy_config.cc

Issue 12315019: Change ProxyRules to handle ProxyLists rather than just single ProxyServer instances. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/string_util.h" 8 #include "base/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"
11 #include "net/proxy/proxy_info.h" 11 #include "net/proxy/proxy_info.h"
12 12
13 namespace net { 13 namespace net {
14 14
15 namespace { 15 namespace {
16 16
17 // If |proxy| is valid, sets it in |dict| under the key |name|. 17 // If |proxy| is valid, sets it in |dict| under the key |name|.
eroman 2013/02/26 01:13:50 "is valid" needs to be updated.
marq_use_my_chromium_address 2013/02/27 23:08:19 Done.
18 void AddProxyToValue(const char* name, 18 void AddProxyToValue(const char* name,
eroman 2013/02/26 01:13:50 This should be renamed to AddProxyListToValue()
marq_use_my_chromium_address 2013/02/27 23:08:19 Done.
19 const ProxyServer& proxy, 19 const ProxyList& proxy,
20 base::DictionaryValue* dict) { 20 base::DictionaryValue* dict) {
21 if (proxy.is_valid()) 21 if (!proxy.IsEmpty())
22 dict->SetString(name, proxy.ToURI()); 22 dict->SetString(name, proxy.ToPacString());
23 } 23 }
24 24
25 } // namespace 25 } // namespace
26 26
27 ProxyConfig::ProxyRules::ProxyRules() 27 ProxyConfig::ProxyRules::ProxyRules()
28 : reverse_bypass(false), 28 : reverse_bypass(false),
29 type(TYPE_NO_RULES) { 29 type(TYPE_NO_RULES){
eroman 2013/02/26 01:13:50 Why remove the space? That is counter to the style
marq_use_my_chromium_address 2013/02/27 23:08:19 Done.
30 } 30 }
31 31
32 ProxyConfig::ProxyRules::~ProxyRules() { 32 ProxyConfig::ProxyRules::~ProxyRules() {
33 } 33 }
34 34
35 void ProxyConfig::ProxyRules::Apply(const GURL& url, ProxyInfo* result) const { 35 void ProxyConfig::ProxyRules::Apply(const GURL& url, ProxyInfo* result) const {
36 if (empty()) { 36 if (empty()) {
37 result->UseDirect(); 37 result->UseDirect();
38 return; 38 return;
39 } 39 }
40 40
41 bool bypass_proxy = bypass_rules.Matches(url); 41 bool bypass_proxy = bypass_rules.Matches(url);
42 if (reverse_bypass) 42 if (reverse_bypass)
43 bypass_proxy = !bypass_proxy; 43 bypass_proxy = !bypass_proxy;
44 if (bypass_proxy) { 44 if (bypass_proxy) {
45 result->UseDirectWithBypassedProxy(); 45 result->UseDirectWithBypassedProxy();
46 return; 46 return;
47 } 47 }
48 48
49 switch (type) { 49 switch (type) {
50 case ProxyRules::TYPE_SINGLE_PROXY: { 50 case ProxyRules::TYPE_SINGLE_PROXY: {
51 result->UseProxyServer(single_proxy); 51 result->UsePacString(single_proxy.ToPacString());
eroman 2013/02/26 01:13:50 Rather than round-tripping through the PAC string
marq_use_my_chromium_address 2013/02/27 23:08:19 Done.
52 return; 52 return;
53 } 53 }
54 case ProxyRules::TYPE_PROXY_PER_SCHEME: { 54 case ProxyRules::TYPE_PROXY_PER_SCHEME: {
55 const ProxyServer* entry = MapUrlSchemeToProxy(url.scheme()); 55 const ProxyList* entry = MapUrlSchemeToProxy(url.scheme());
56 if (entry) { 56 if (entry) {
57 result->UseProxyServer(*entry); 57 result->UsePacString(entry->ToPacString());
eroman 2013/02/26 01:13:50 See comment above.
marq_use_my_chromium_address 2013/02/27 23:08:19 Done.
58 } else { 58 } else {
59 // We failed to find a matching proxy server for the current URL 59 // We failed to find a matching proxy server for the current URL
60 // scheme. Default to direct. 60 // scheme. Default to direct.
61 result->UseDirect(); 61 result->UseDirect();
62 } 62 }
63 return; 63 return;
64 } 64 }
65 default: { 65 default: {
66 result->UseDirect(); 66 result->UseDirect();
67 NOTREACHED(); 67 NOTREACHED();
68 return; 68 return;
69 } 69 }
70 } 70 }
71 } 71 }
72 72
73 void ProxyConfig::ProxyRules::ParseFromString(const std::string& proxy_rules) { 73 void ProxyConfig::ProxyRules::ParseFromString(const std::string& proxy_rules) {
74 // Reset. 74 // Reset.
75 type = TYPE_NO_RULES; 75 type = TYPE_NO_RULES;
76 single_proxy = ProxyServer(); 76 single_proxy = ProxyList();
77 proxy_for_http = ProxyServer(); 77 proxy_for_http = ProxyList();
78 proxy_for_https = ProxyServer(); 78 proxy_for_https = ProxyList();
79 proxy_for_ftp = ProxyServer(); 79 proxy_for_ftp = ProxyList();
80 fallback_proxy = ProxyServer(); 80 fallback_proxy = ProxyList();
81 81
82 base::StringTokenizer proxy_server_list(proxy_rules, ";"); 82 base::StringTokenizer proxy_server_list(proxy_rules, ";");
83 while (proxy_server_list.GetNext()) { 83 while (proxy_server_list.GetNext()) {
84 base::StringTokenizer proxy_server_for_scheme( 84 base::StringTokenizer proxy_server_for_scheme(
85 proxy_server_list.token_begin(), proxy_server_list.token_end(), "="); 85 proxy_server_list.token_begin(), proxy_server_list.token_end(), "=");
86 86
87 while (proxy_server_for_scheme.GetNext()) { 87 while (proxy_server_for_scheme.GetNext()) {
88 std::string url_scheme = proxy_server_for_scheme.token(); 88 std::string url_scheme = proxy_server_for_scheme.token();
89 89
90 // If we fail to get the proxy server here, it means that 90 // If we fail to get the proxy server here, it means that
91 // this is a regular proxy server configuration, i.e. proxies 91 // this is a regular proxy server configuration, i.e. proxies
92 // are not configured per protocol. 92 // are not configured per protocol.
93 if (!proxy_server_for_scheme.GetNext()) { 93 if (!proxy_server_for_scheme.GetNext()) {
94 if (type == TYPE_PROXY_PER_SCHEME) 94 if (type == TYPE_PROXY_PER_SCHEME)
95 continue; // Unexpected. 95 continue; // Unexpected.
96 single_proxy = ProxyServer::FromURI(url_scheme, 96 single_proxy.SetSingleProxyServer(
97 ProxyServer::SCHEME_HTTP); 97 ProxyServer::FromURI(url_scheme, ProxyServer::SCHEME_HTTP));
98 type = TYPE_SINGLE_PROXY; 98 type = TYPE_SINGLE_PROXY;
99 return; 99 return;
100 } 100 }
101 101
102 // Trim whitespace off the url scheme. 102 // Trim whitespace off the url scheme.
103 TrimWhitespaceASCII(url_scheme, TRIM_ALL, &url_scheme); 103 TrimWhitespaceASCII(url_scheme, TRIM_ALL, &url_scheme);
104 104
105 // Add it to the per-scheme mappings (if supported scheme). 105 // Add it to the per-scheme mappings (if supported scheme).
106 type = TYPE_PROXY_PER_SCHEME; 106 type = TYPE_PROXY_PER_SCHEME;
107 ProxyServer* entry = MapUrlSchemeToProxyNoFallback(url_scheme); 107 ProxyList* entry = MapUrlSchemeToProxyNoFallback(url_scheme);
108 ProxyServer::Scheme default_scheme = ProxyServer::SCHEME_HTTP; 108 ProxyServer::Scheme default_scheme = ProxyServer::SCHEME_HTTP;
109 109
110 // socks=XXX is inconsistent with the other formats, since "socks" 110 // socks=XXX is inconsistent with the other formats, since "socks"
111 // is not a URL scheme. Rather this means "for everything else, send 111 // is not a URL scheme. Rather this means "for everything else, send
112 // it to the SOCKS proxy server XXX". 112 // it to the SOCKS proxy server XXX".
113 if (url_scheme == "socks") { 113 if (url_scheme == "socks") {
114 DCHECK(!entry); 114 DCHECK(!entry);
115 entry = &fallback_proxy; 115 entry = &fallback_proxy;
116 // Note that here 'socks' is understood to be SOCKS4, even though
117 // 'socks' maps to SOCKS5 in ProxyServer::GetSchemeFromURIInternal.
eroman 2013/02/26 01:13:50 Yeah, icky carry-over from the windows interpretat
marq_use_my_chromium_address 2013/02/27 23:08:19 Not my call; I just added the comment because it's
116 default_scheme = ProxyServer::SCHEME_SOCKS4; 118 default_scheme = ProxyServer::SCHEME_SOCKS4;
117 } 119 }
118 120
119 if (entry) { 121 if (entry) {
120 *entry = ProxyServer::FromURI(proxy_server_for_scheme.token(), 122 ProxyServer new_proxy = ProxyServer::FromURI(
121 default_scheme); 123 proxy_server_for_scheme.token(), default_scheme);
124 // Don't have DIRECT as the first proxy in a list.
eroman 2013/02/26 01:13:50 Why disallow this?
marq_use_my_chromium_address 2013/02/27 23:08:19 I had assumed that this would create a wasteful si
125 if (!new_proxy.is_direct() || !entry->IsEmpty()) {
126 entry->AddProxyServer(new_proxy);
127 }
122 } 128 }
123 } 129 }
124 } 130 }
125 } 131 }
126 132
127 const ProxyServer* ProxyConfig::ProxyRules::MapUrlSchemeToProxy( 133 const ProxyList* ProxyConfig::ProxyRules::MapUrlSchemeToProxy(
128 const std::string& url_scheme) const { 134 const std::string& url_scheme) const {
129 const ProxyServer* proxy_server = 135 const ProxyList* proxy_server_list =
130 const_cast<ProxyRules*>(this)->MapUrlSchemeToProxyNoFallback(url_scheme); 136 const_cast<ProxyRules*>(this)->MapUrlSchemeToProxyNoFallback(url_scheme);
131 if (proxy_server && proxy_server->is_valid()) 137 if (proxy_server_list && !proxy_server_list->IsEmpty())
132 return proxy_server; 138 return proxy_server_list;
133 if (fallback_proxy.is_valid()) 139 if (!fallback_proxy.IsEmpty())
134 return &fallback_proxy; 140 return &fallback_proxy;
135 return NULL; // No mapping for this scheme. Use direct. 141 return NULL; // No mapping for this scheme. Use direct.
136 } 142 }
137 143
138 bool ProxyConfig::ProxyRules::Equals(const ProxyRules& other) const { 144 bool ProxyConfig::ProxyRules::Equals(const ProxyRules& other) const {
139 return type == other.type && 145 return type == other.type &&
140 single_proxy == other.single_proxy && 146 single_proxy.Equals(other.single_proxy) &&
141 proxy_for_http == other.proxy_for_http && 147 proxy_for_http.Equals(other.proxy_for_http) &&
142 proxy_for_https == other.proxy_for_https && 148 proxy_for_https.Equals(other.proxy_for_https) &&
143 proxy_for_ftp == other.proxy_for_ftp && 149 proxy_for_ftp.Equals(other.proxy_for_ftp) &&
144 fallback_proxy == other.fallback_proxy &&
145 bypass_rules.Equals(other.bypass_rules) && 150 bypass_rules.Equals(other.bypass_rules) &&
146 reverse_bypass == other.reverse_bypass; 151 reverse_bypass == other.reverse_bypass;
147 } 152 }
148 153
149 ProxyServer* ProxyConfig::ProxyRules::MapUrlSchemeToProxyNoFallback( 154 ProxyList* ProxyConfig::ProxyRules::MapUrlSchemeToProxyNoFallback(
150 const std::string& scheme) { 155 const std::string& scheme) {
151 DCHECK_EQ(TYPE_PROXY_PER_SCHEME, type); 156 DCHECK_EQ(TYPE_PROXY_PER_SCHEME, type);
152 if (scheme == "http") 157 if (scheme == "http")
153 return &proxy_for_http; 158 return &proxy_for_http;
154 if (scheme == "https") 159 if (scheme == "https")
155 return &proxy_for_https; 160 return &proxy_for_https;
156 if (scheme == "ftp") 161 if (scheme == "ftp")
157 return &proxy_for_ftp; 162 return &proxy_for_ftp;
158 return NULL; // No mapping for this scheme. 163 return NULL; // No mapping for this scheme.
159 } 164 }
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
252 } 257 }
253 } 258 }
254 259
255 // Output the source. 260 // Output the source.
256 dict->SetString("source", ProxyConfigSourceToString(source_)); 261 dict->SetString("source", ProxyConfigSourceToString(source_));
257 262
258 return dict; 263 return dict;
259 } 264 }
260 265
261 } // namespace net 266 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698