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 3 // found in the LICENSE file. 4 5 #ifndef NET_PROXY_PROXY_CONFIG_H_ 6 #define NET_PROXY_PROXY_CONFIG_H_ 7 #pragma once 8 9 #include <string> 10 11 #include "googleurl/src/gurl.h" 12 #include "net/proxy/proxy_bypass_rules.h" 13 #include "net/proxy/proxy_server.h" 14 15 class Value; 16 17 namespace net { 18 19 class ProxyInfo; 20 21 // ProxyConfig describes a user's proxy settings. 22 // 23 // There are two categories of proxy settings: 24 // (1) Automatic (indicates the methods to obtain a PAC script) 25 // (2) Manual (simple set of proxy servers per scheme, and bypass patterns) 26 // 27 // When both automatic and manual settings are specified, the Automatic ones 28 // take precedence over the manual ones. 29 // 30 // For more details see: 31 // http://www.chromium.org/developers/design-documents/proxy-settings-fallback 32 class ProxyConfig { 33 public: 34 // ProxyRules describes the "manual" proxy settings. 35 // TODO(eroman): Turn this into a class. 36 struct ProxyRules { 37 enum Type { 38 TYPE_NO_RULES, 39 TYPE_SINGLE_PROXY, 40 TYPE_PROXY_PER_SCHEME, 41 }; 42 43 // Note that the default of TYPE_NO_RULES results in direct connections 44 // being made when using this ProxyConfig. 45 ProxyRules(); 46 ~ProxyRules(); 47 48 bool empty() const { 49 return type == TYPE_NO_RULES; 50 } 51 52 // Sets |result| with the proxy to use for |url| based on the current rules. 53 void Apply(const GURL& url, ProxyInfo* result); 54 55 // Parses the rules from a string, indicating which proxies to use. 56 // 57 // proxy-uri = [<proxy-scheme>"://"]<proxy-host>[":"<proxy-port>] 58 // 59 // If the proxy to use depends on the scheme of the URL, can instead specify 60 // a semicolon separated list of: 61 // 62 // <url-scheme>"="<proxy-uri> 63 // 64 // For example: 65 // "http=foopy:80;ftp=foopy2" -- use HTTP proxy "foopy:80" for http:// 66 // URLs, and HTTP proxy "foopy2:80" for 67 // ftp:// URLs. 68 // "foopy:80" -- use HTTP proxy "foopy:80" for all URLs. 69 // "socks4://foopy" -- use SOCKS v4 proxy "foopy:1080" for all 70 // URLs. 71 void ParseFromString(const std::string& proxy_rules); 72 73 // Returns one of {&proxy_for_http, &proxy_for_https, &proxy_for_ftp, 74 // &fallback_proxy}, or NULL if there is no proxy to use. 75 // Should only call this if the type is TYPE_PROXY_PER_SCHEME. 76 const ProxyServer* MapUrlSchemeToProxy(const std::string& url_scheme) const; 77 78 // Returns true if |*this| describes the same configuration as |other|. 79 bool Equals(const ProxyRules& other) const; 80 81 // Exceptions for when not to use a proxy. 82 ProxyBypassRules bypass_rules; 83 84 // Reverse the meaning of |bypass_rules|. 85 bool reverse_bypass; 86 87 Type type; 88 89 // Set if |type| is TYPE_SINGLE_PROXY. 90 ProxyServer single_proxy; 91 92 // Set if |type| is TYPE_PROXY_PER_SCHEME. 93 ProxyServer proxy_for_http; 94 ProxyServer proxy_for_https; 95 ProxyServer proxy_for_ftp; 96 97 // Used when there isn't a more specific per-scheme proxy server. 98 ProxyServer fallback_proxy; 99 100 private: 101 // Returns one of {&proxy_for_http, &proxy_for_https, &proxy_for_ftp} 102 // or NULL if it is a scheme that we don't have a mapping 103 // for. Should only call this if the type is TYPE_PROXY_PER_SCHEME. 104 ProxyServer* MapUrlSchemeToProxyNoFallback(const std::string& scheme); 105 }; 106 107 typedef int ID; 108 109 // Indicates an invalid proxy config. 110 enum { INVALID_ID = 0 }; 111 112 ProxyConfig(); 113 ProxyConfig(const ProxyConfig& config); 114 ~ProxyConfig(); 115 ProxyConfig& operator=(const ProxyConfig& config); 116 117 // Used to numerically identify this configuration. 118 ID id() const { return id_; } 119 void set_id(int id) { id_ = id; } 120 bool is_valid() const { return id_ != INVALID_ID; } 121 122 // Returns true if the given config is equivalent to this config. 123 bool Equals(const ProxyConfig& other) const; 124 125 // Returns true if this config contains any "automatic" settings. See the 126 // class description for what that means. 127 bool HasAutomaticSettings() const; 128 129 void ClearAutomaticSettings(); 130 131 // Creates a Value dump of this configuration. The caller is responsible for 132 // deleting the returned value. 133 Value* ToValue() const; 134 135 ProxyRules& proxy_rules() { 136 return proxy_rules_; 137 } 138 139 const ProxyRules& proxy_rules() const { 140 return proxy_rules_; 141 } 142 143 void set_pac_url(const GURL& url) { 144 pac_url_ = url; 145 } 146 147 const GURL& pac_url() const { 148 return pac_url_; 149 } 150 151 bool has_pac_url() const { 152 return pac_url_.is_valid(); 153 } 154 155 void set_auto_detect(bool enable_auto_detect) { 156 auto_detect_ = enable_auto_detect; 157 } 158 159 bool auto_detect() const { 160 return auto_detect_; 161 } 162 163 // Helpers to construct some common proxy configurations. 164 165 static ProxyConfig CreateDirect() { 166 return ProxyConfig(); 167 } 168 169 static ProxyConfig CreateAutoDetect() { 170 ProxyConfig config; 171 config.set_auto_detect(true); 172 return config; 173 } 174 175 static ProxyConfig CreateFromCustomPacURL(const GURL& pac_url) { 176 ProxyConfig config; 177 config.set_pac_url(pac_url); 178 return config; 179 } 180 181 private: 182 // True if the proxy configuration should be auto-detected. 183 bool auto_detect_; 184 185 // If non-empty, indicates the URL of the proxy auto-config file to use. 186 GURL pac_url_; 187 188 // Manual proxy settings. 189 ProxyRules proxy_rules_; 190 191 int id_; 192 }; 193 194 } // namespace net 195 196 197 198 #endif // NET_PROXY_PROXY_CONFIG_H_ 199