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
3 // found in the LICENSE file.
5 #ifndef NET_PROXY_PROXY_CONFIG_H_
6 #define NET_PROXY_PROXY_CONFIG_H_
10 #include "net/base/net_export.h"
11 #include "net/proxy/proxy_bypass_rules.h"
12 #include "net/proxy/proxy_config_source.h"
13 #include "net/proxy/proxy_list.h"
14 #include "net/proxy/proxy_server.h"
25 // ProxyConfig describes a user's proxy settings.
27 // There are two categories of proxy settings:
28 // (1) Automatic (indicates the methods to obtain a PAC script)
29 // (2) Manual (simple set of proxy servers per scheme, and bypass patterns)
31 // When both automatic and manual settings are specified, the Automatic ones
32 // take precedence over the manual ones.
34 // For more details see:
35 // http://www.chromium.org/developers/design-documents/proxy-settings-fallback
36 class NET_EXPORT ProxyConfig
{
38 // ProxyRules describes the "manual" proxy settings.
39 // TODO(eroman): Turn this into a class.
40 // TODO(marq): Update the enum names; "TYPE_SINGLE_PROXY" really means
41 // the same set of proxies are used for all requests.
42 struct NET_EXPORT ProxyRules
{
46 TYPE_PROXY_PER_SCHEME
,
49 // Note that the default of TYPE_NO_RULES results in direct connections
50 // being made when using this ProxyConfig.
55 return type
== TYPE_NO_RULES
;
58 // Sets |result| with the proxies to use for |url| based on the current
60 void Apply(const GURL
& url
, ProxyInfo
* result
) const;
62 // Parses the rules from a string, indicating which proxies to use.
64 // proxy-uri = [<proxy-scheme>"://"]<proxy-host>[":"<proxy-port>]
66 // proxy-uri-list = <proxy-uri>[","<proxy-uri-list>]
68 // url-scheme = "http" | "https" | "ftp" | "socks"
70 // scheme-proxies = [<url-scheme>"="]<proxy-uri-list>
72 // proxy-rules = scheme-proxies[";"<scheme-proxies>]
74 // Thus, the proxy-rules string should be a semicolon-separated list of
75 // ordered proxies that apply to a particular URL scheme. Unless specified,
76 // the proxy scheme for proxy-uris is assumed to be http.
78 // Some special cases:
79 // * If the scheme is omitted from the first proxy list, that list applies
80 // to all URL schemes and subsequent lists are ignored.
81 // * If a scheme is omitted from any proxy list after a list where a scheme
82 // has been provided, the list without a scheme is ignored.
83 // * If the url-scheme is set to 'socks', that sets a fallback list that
84 // to all otherwise unspecified url-schemes, however the default proxy-
85 // scheme for proxy urls in the 'socks' list is understood to be
86 // socks4:// if unspecified.
89 // "http=foopy:80;ftp=foopy2" -- use HTTP proxy "foopy:80" for http://
90 // URLs, and HTTP proxy "foopy2:80" for
92 // "foopy:80" -- use HTTP proxy "foopy:80" for all URLs.
93 // "foopy:80,bar,direct://" -- use HTTP proxy "foopy:80" for all URLs,
94 // failing over to "bar" if "foopy:80" is
95 // unavailable, and after that using no
97 // "socks4://foopy" -- use SOCKS v4 proxy "foopy:1080" for all
99 // "http=foop,socks5://bar.com -- use HTTP proxy "foopy" for http URLs,
100 // and fail over to the SOCKS5 proxy
101 // "bar.com" if "foop" is unavailable.
102 // "http=foopy,direct:// -- use HTTP proxy "foopy" for http URLs,
103 // and use no proxy if "foopy" is
105 // "http=foopy;socks=foopy2 -- use HTTP proxy "foopy" for http URLs,
106 // and use socks4://foopy2 for all other
108 void ParseFromString(const std::string
& proxy_rules
);
110 // Returns one of {&proxies_for_http, &proxies_for_https, &proxies_for_ftp,
111 // &fallback_proxies}, or NULL if there is no proxy to use.
112 // Should only call this if the type is TYPE_PROXY_PER_SCHEME.
113 const ProxyList
* MapUrlSchemeToProxyList(
114 const std::string
& url_scheme
) const;
116 // Returns true if |*this| describes the same configuration as |other|.
117 bool Equals(const ProxyRules
& other
) const;
119 // Exceptions for when not to use a proxy.
120 ProxyBypassRules bypass_rules
;
122 // Reverse the meaning of |bypass_rules|.
127 // Set if |type| is TYPE_SINGLE_PROXY.
128 ProxyList single_proxies
;
130 // Set if |type| is TYPE_PROXY_PER_SCHEME.
131 ProxyList proxies_for_http
;
132 ProxyList proxies_for_https
;
133 ProxyList proxies_for_ftp
;
135 // Used when a fallback has been defined and the url to be proxied doesn't
136 // match any of the standard schemes.
137 ProxyList fallback_proxies
;
140 // Returns one of {&proxies_for_http, &proxies_for_https, &proxies_for_ftp}
141 // or NULL if it is a scheme that we don't have a mapping
142 // for. Should only call this if the type is TYPE_PROXY_PER_SCHEME.
143 ProxyList
* MapUrlSchemeToProxyListNoFallback(const std::string
& scheme
);
148 // Indicates an invalid proxy config.
149 static const ID kInvalidConfigID
= 0;
152 ProxyConfig(const ProxyConfig
& config
);
154 ProxyConfig
& operator=(const ProxyConfig
& config
);
156 // Used to numerically identify this configuration.
157 ID
id() const { return id_
; }
158 void set_id(ID id
) { id_
= id
; }
159 bool is_valid() const { return id_
!= kInvalidConfigID
; }
161 // Returns true if the given config is equivalent to this config. The
162 // comparison ignores differences in |id()| and |source()|.
163 bool Equals(const ProxyConfig
& other
) const;
165 // Returns true if this config contains any "automatic" settings. See the
166 // class description for what that means.
167 bool HasAutomaticSettings() const;
169 void ClearAutomaticSettings();
171 // Creates a Value dump of this configuration. The caller is responsible for
172 // deleting the returned value.
173 base::DictionaryValue
* ToValue() const;
175 ProxyRules
& proxy_rules() {
179 const ProxyRules
& proxy_rules() const {
183 void set_pac_url(const GURL
& url
) {
187 const GURL
& pac_url() const {
191 void set_pac_mandatory(bool enable_pac_mandatory
) {
192 pac_mandatory_
= enable_pac_mandatory
;
195 bool pac_mandatory() const {
196 return pac_mandatory_
;
199 bool has_pac_url() const {
200 return pac_url_
.is_valid();
203 void set_auto_detect(bool enable_auto_detect
) {
204 auto_detect_
= enable_auto_detect
;
207 bool auto_detect() const {
211 void set_source(ProxyConfigSource source
) {
215 ProxyConfigSource
source() const {
219 // Helpers to construct some common proxy configurations.
221 static ProxyConfig
CreateDirect() {
222 return ProxyConfig();
225 static ProxyConfig
CreateAutoDetect() {
227 config
.set_auto_detect(true);
231 static ProxyConfig
CreateFromCustomPacURL(const GURL
& pac_url
) {
233 config
.set_pac_url(pac_url
);
234 // By default fall back to direct connection in case PAC script fails.
235 config
.set_pac_mandatory(false);
240 // True if the proxy configuration should be auto-detected.
243 // If non-empty, indicates the URL of the proxy auto-config file to use.
246 // If true, blocks all traffic in case fetching the pac script from |pac_url_|
247 // fails. Only valid if |pac_url_| is non-empty.
250 // Manual proxy settings.
251 ProxyRules proxy_rules_
;
253 // Source of proxy settings.
254 ProxyConfigSource source_
;
263 #endif // NET_PROXY_PROXY_CONFIG_H_