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