1 // Copyright 2012 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_PROXY_RESOLUTION_PROXY_CONFIG_H_ 6 #define NET_PROXY_RESOLUTION_PROXY_CONFIG_H_ 7 8 #include <string> 9 10 #include "net/base/net_export.h" 11 #include "net/base/proxy_server.h" 12 #include "net/proxy_resolution/proxy_bypass_rules.h" 13 #include "net/proxy_resolution/proxy_list.h" 14 #include "url/gurl.h" 15 16 namespace base { 17 class Value; 18 } 19 20 namespace net { 21 22 class ProxyInfo; 23 24 // ProxyConfig describes a user's proxy settings. 25 // 26 // There are two categories of proxy settings: 27 // (1) Automatic (indicates the methods to obtain a PAC script) 28 // (2) Manual (simple set of proxy servers per scheme, and bypass patterns) 29 // 30 // When both automatic and manual settings are specified, the Automatic ones 31 // take precedence over the manual ones. 32 // 33 // For more details see: 34 // http://www.chromium.org/developers/design-documents/network-stack/proxy-settings-fallback 35 class NET_EXPORT ProxyConfig { 36 public: 37 // ProxyRules describes the "manual" proxy settings. 38 struct NET_EXPORT ProxyRules { 39 enum class Type { 40 EMPTY, 41 PROXY_LIST, 42 PROXY_LIST_PER_SCHEME, 43 }; 44 45 // Note that the default of Type::EMPTY results in direct connections 46 // being made when using this ProxyConfig. 47 ProxyRules(); 48 ProxyRules(const ProxyRules& other); 49 ~ProxyRules(); 50 emptyProxyRules51 bool empty() const { 52 return type == Type::EMPTY; 53 } 54 55 // Sets |result| with the proxies to use for |url| based on the current 56 // rules. 57 void Apply(const GURL& url, ProxyInfo* result) const; 58 59 // Parses the rules from a string, indicating which proxies to use. 60 // 61 // proxy-uri = [<proxy-scheme>"://"]<proxy-host>[":"<proxy-port>] 62 // 63 // proxy-uri-list = <proxy-uri>[","<proxy-uri-list>] 64 // 65 // url-scheme = "http" | "https" | "ftp" | "socks" 66 // 67 // scheme-proxies = [<url-scheme>"="]<proxy-uri-list> 68 // 69 // proxy-rules = scheme-proxies[";"<scheme-proxies>] 70 // 71 // Thus, the proxy-rules string should be a semicolon-separated list of 72 // ordered proxies that apply to a particular URL scheme. Unless specified, 73 // the proxy scheme for proxy-uris is assumed to be http. 74 // 75 // Some special cases: 76 // * If the scheme is omitted from the first proxy list, that list applies 77 // to all URL schemes and subsequent lists are ignored. 78 // * If a scheme is omitted from any proxy list after a list where a scheme 79 // has been provided, the list without a scheme is ignored. 80 // * If the url-scheme is set to 'socks', that sets a fallback list that 81 // to all otherwise unspecified url-schemes, however the default proxy- 82 // scheme for proxy urls in the 'socks' list is understood to be 83 // socks4:// if unspecified. 84 // 85 // For example: 86 // "http=foopy:80;ftp=foopy2" -- use HTTP proxy "foopy:80" for http:// 87 // URLs, and HTTP proxy "foopy2:80" for 88 // ftp:// URLs. 89 // "foopy:80" -- use HTTP proxy "foopy:80" for all URLs. 90 // "foopy:80,bar,direct://" -- use HTTP proxy "foopy:80" for all URLs, 91 // failing over to "bar" if "foopy:80" is 92 // unavailable, and after that using no 93 // proxy. 94 // "socks4://foopy" -- use SOCKS v4 proxy "foopy:1080" for all 95 // URLs. 96 // "http=foop,socks5://bar.com -- use HTTP proxy "foopy" for http URLs, 97 // and fail over to the SOCKS5 proxy 98 // "bar.com" if "foop" is unavailable. 99 // "http=foopy,direct:// -- use HTTP proxy "foopy" for http URLs, 100 // and use no proxy if "foopy" is 101 // unavailable. 102 // "http=foopy;socks=foopy2 -- use HTTP proxy "foopy" for http URLs, 103 // and use socks4://foopy2 for all other 104 // URLs. 105 void ParseFromString(const std::string& proxy_rules); 106 107 // Returns one of {&proxies_for_http, &proxies_for_https, &proxies_for_ftp, 108 // &fallback_proxies}, or NULL if there is no proxy to use. 109 // Should only call this if the type is Type::PROXY_LIST_PER_SCHEME. 110 const ProxyList* MapUrlSchemeToProxyList( 111 const std::string& url_scheme) const; 112 113 // Returns true if |*this| describes the same configuration as |other|. 114 bool Equals(const ProxyRules& other) const; 115 CreateForTestingProxyRules116 static ProxyRules CreateForTesting(const ProxyList& proxy_list) { 117 ProxyRules proxy_rules; 118 proxy_rules.type = Type::PROXY_LIST; 119 proxy_rules.single_proxies = proxy_list; 120 return proxy_rules; 121 } 122 123 // Exceptions for when not to use a proxy. 124 ProxyBypassRules bypass_rules; 125 126 // Reverse the meaning of |bypass_rules|. 127 bool reverse_bypass = false; 128 129 Type type = Type::EMPTY; 130 131 // Set if |type| is Type::PROXY_LIST. 132 ProxyList single_proxies; 133 134 // Set if |type| is Type::PROXY_LIST_PER_SCHEME. 135 ProxyList proxies_for_http; 136 ProxyList proxies_for_https; 137 ProxyList proxies_for_ftp; 138 139 // Used when a fallback has been defined and the url to be proxied doesn't 140 // match any of the standard schemes. 141 ProxyList fallback_proxies; 142 143 private: 144 // Returns one of {&proxies_for_http, &proxies_for_https, &proxies_for_ftp} 145 // or NULL if it is a scheme that we don't have a mapping for. Should only 146 // call this if the type is Type::PROXY_LIST_PER_SCHEME. Intentionally returns 147 // NULL for "ws" and "wss" as those are handled specially by 148 // GetProxyListForWebSocketScheme(). 149 ProxyList* MapUrlSchemeToProxyListNoFallback(const std::string& scheme); 150 151 // Returns the first of {&fallback_proxies, &proxies_for_https, 152 // &proxies_for_http} that is non-empty, or NULL. 153 const ProxyList* GetProxyListForWebSocketScheme() const; 154 }; 155 156 ProxyConfig(); 157 ProxyConfig(const ProxyConfig& config); 158 ~ProxyConfig(); 159 ProxyConfig& operator=(const ProxyConfig& config); 160 161 // Returns true if the given config is equivalent to this config. 162 bool Equals(const ProxyConfig& other) const; 163 164 // Returns true if this config contains any "automatic" settings. See the 165 // class description for what that means. 166 bool HasAutomaticSettings() const; 167 168 void ClearAutomaticSettings(); 169 170 // Creates a Value dump of this configuration. 171 base::Value ToValue() const; 172 proxy_rules()173 ProxyRules& proxy_rules() { 174 return proxy_rules_; 175 } 176 proxy_rules()177 const ProxyRules& proxy_rules() const { 178 return proxy_rules_; 179 } 180 set_pac_url(const GURL & url)181 void set_pac_url(const GURL& url) { 182 pac_url_ = url; 183 } 184 pac_url()185 const GURL& pac_url() const { 186 return pac_url_; 187 } 188 set_pac_mandatory(bool enable_pac_mandatory)189 void set_pac_mandatory(bool enable_pac_mandatory) { 190 pac_mandatory_ = enable_pac_mandatory; 191 } 192 pac_mandatory()193 bool pac_mandatory() const { 194 return pac_mandatory_; 195 } 196 has_pac_url()197 bool has_pac_url() const { 198 return pac_url_.is_valid(); 199 } 200 set_auto_detect(bool enable_auto_detect)201 void set_auto_detect(bool enable_auto_detect) { 202 auto_detect_ = enable_auto_detect; 203 } 204 auto_detect()205 bool auto_detect() const { 206 return auto_detect_; 207 } 208 set_from_system(bool from_system)209 void set_from_system(bool from_system) { from_system_ = from_system; } 210 from_system()211 bool from_system() const { return from_system_; } 212 213 // Helpers to construct some common proxy configurations. 214 CreateDirect()215 static ProxyConfig CreateDirect() { 216 return ProxyConfig(); 217 } 218 CreateAutoDetect()219 static ProxyConfig CreateAutoDetect() { 220 ProxyConfig config; 221 config.set_auto_detect(true); 222 return config; 223 } 224 CreateFromCustomPacURL(const GURL & pac_url)225 static ProxyConfig CreateFromCustomPacURL(const GURL& pac_url) { 226 ProxyConfig config; 227 config.set_pac_url(pac_url); 228 // By default fall back to direct connection in case PAC script fails. 229 config.set_pac_mandatory(false); 230 return config; 231 } 232 CreateForTesting(const ProxyList & proxy_list)233 static ProxyConfig CreateForTesting(const ProxyList& proxy_list) { 234 ProxyConfig config; 235 config.proxy_rules_ = ProxyRules::CreateForTesting(proxy_list); 236 return config; 237 } 238 239 private: 240 // True if the proxy configuration should be auto-detected. 241 bool auto_detect_ = false; 242 243 // True if the proxy configuration was created from system settings. 244 bool from_system_ = false; 245 246 // If non-empty, indicates the URL of the proxy auto-config file to use. 247 GURL pac_url_; 248 249 // If true, blocks all traffic in case fetching the PAC script from |pac_url_| 250 // fails. Only valid if |pac_url_| is non-empty. 251 bool pac_mandatory_ = false; 252 253 // Manual proxy settings. 254 ProxyRules proxy_rules_; 255 }; 256 257 } // namespace net 258 259 260 261 #endif // NET_PROXY_RESOLUTION_PROXY_CONFIG_H_ 262