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 116 // Exceptions for when not to use a proxy. 117 ProxyBypassRules bypass_rules; 118 119 // Reverse the meaning of |bypass_rules|. 120 bool reverse_bypass = false; 121 122 Type type = Type::EMPTY; 123 124 // Set if |type| is Type::PROXY_LIST. 125 ProxyList single_proxies; 126 127 // Set if |type| is Type::PROXY_LIST_PER_SCHEME. 128 ProxyList proxies_for_http; 129 ProxyList proxies_for_https; 130 ProxyList proxies_for_ftp; 131 132 // Used when a fallback has been defined and the url to be proxied doesn't 133 // match any of the standard schemes. 134 ProxyList fallback_proxies; 135 136 private: 137 // Returns one of {&proxies_for_http, &proxies_for_https, &proxies_for_ftp} 138 // or NULL if it is a scheme that we don't have a mapping for. Should only 139 // call this if the type is Type::PROXY_LIST_PER_SCHEME. Intentionally returns 140 // NULL for "ws" and "wss" as those are handled specially by 141 // GetProxyListForWebSocketScheme(). 142 ProxyList* MapUrlSchemeToProxyListNoFallback(const std::string& scheme); 143 144 // Returns the first of {&fallback_proxies, &proxies_for_https, 145 // &proxies_for_http} that is non-empty, or NULL. 146 const ProxyList* GetProxyListForWebSocketScheme() const; 147 }; 148 149 ProxyConfig(); 150 ProxyConfig(const ProxyConfig& config); 151 ~ProxyConfig(); 152 ProxyConfig& operator=(const ProxyConfig& config); 153 154 // Returns true if the given config is equivalent to this config. 155 bool Equals(const ProxyConfig& other) const; 156 157 // Returns true if this config contains any "automatic" settings. See the 158 // class description for what that means. 159 bool HasAutomaticSettings() const; 160 161 void ClearAutomaticSettings(); 162 163 // Creates a Value dump of this configuration. 164 base::Value ToValue() const; 165 proxy_rules()166 ProxyRules& proxy_rules() { 167 return proxy_rules_; 168 } 169 proxy_rules()170 const ProxyRules& proxy_rules() const { 171 return proxy_rules_; 172 } 173 set_pac_url(const GURL & url)174 void set_pac_url(const GURL& url) { 175 pac_url_ = url; 176 } 177 pac_url()178 const GURL& pac_url() const { 179 return pac_url_; 180 } 181 set_pac_mandatory(bool enable_pac_mandatory)182 void set_pac_mandatory(bool enable_pac_mandatory) { 183 pac_mandatory_ = enable_pac_mandatory; 184 } 185 pac_mandatory()186 bool pac_mandatory() const { 187 return pac_mandatory_; 188 } 189 has_pac_url()190 bool has_pac_url() const { 191 return pac_url_.is_valid(); 192 } 193 set_auto_detect(bool enable_auto_detect)194 void set_auto_detect(bool enable_auto_detect) { 195 auto_detect_ = enable_auto_detect; 196 } 197 auto_detect()198 bool auto_detect() const { 199 return auto_detect_; 200 } 201 set_from_system(bool from_system)202 void set_from_system(bool from_system) { from_system_ = from_system; } 203 from_system()204 bool from_system() const { return from_system_; } 205 206 // Helpers to construct some common proxy configurations. 207 CreateDirect()208 static ProxyConfig CreateDirect() { 209 return ProxyConfig(); 210 } 211 CreateAutoDetect()212 static ProxyConfig CreateAutoDetect() { 213 ProxyConfig config; 214 config.set_auto_detect(true); 215 return config; 216 } 217 CreateFromCustomPacURL(const GURL & pac_url)218 static ProxyConfig CreateFromCustomPacURL(const GURL& pac_url) { 219 ProxyConfig config; 220 config.set_pac_url(pac_url); 221 // By default fall back to direct connection in case PAC script fails. 222 config.set_pac_mandatory(false); 223 return config; 224 } 225 226 private: 227 // True if the proxy configuration should be auto-detected. 228 bool auto_detect_ = false; 229 230 // True if the proxy configuration was created from system settings. 231 bool from_system_ = false; 232 233 // If non-empty, indicates the URL of the proxy auto-config file to use. 234 GURL pac_url_; 235 236 // If true, blocks all traffic in case fetching the PAC script from |pac_url_| 237 // fails. Only valid if |pac_url_| is non-empty. 238 bool pac_mandatory_ = false; 239 240 // Manual proxy settings. 241 ProxyRules proxy_rules_; 242 }; 243 244 } // namespace net 245 246 247 248 #endif // NET_PROXY_RESOLUTION_PROXY_CONFIG_H_ 249