diff options
author | ericroman@google.com <ericroman@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-03-31 01:03:23 +0000 |
---|---|---|
committer | ericroman@google.com <ericroman@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-03-31 01:03:23 +0000 |
commit | 5b45aec04af2be235787c81369c77d627ebd639a (patch) | |
tree | af51655c9ca9374181722a96514f9b827cdceb34 /net/proxy/proxy_config.cc | |
parent | 6dd6c74b945802513e94b4be1012ccfbe240f023 (diff) | |
download | chromium_src-5b45aec04af2be235787c81369c77d627ebd639a.zip chromium_src-5b45aec04af2be235787c81369c77d627ebd639a.tar.gz chromium_src-5b45aec04af2be235787c81369c77d627ebd639a.tar.bz2 |
Extract the parsing of proxy rules to ProxyConfig::ProxyRules, and unit-test.
This avoids re-parsing the rules every time a proxy resolve is done, and also adds extra tolerance for white space.
The other motivation is to not have to fiddle around with strings as much in the various ProxyConfigServceXXXX implementations.
Review URL: http://codereview.chromium.org/57011
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@12829 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/proxy/proxy_config.cc')
-rw-r--r-- | net/proxy/proxy_config.cc | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/net/proxy/proxy_config.cc b/net/proxy/proxy_config.cc index fb30124..07c1d50 100644 --- a/net/proxy/proxy_config.cc +++ b/net/proxy/proxy_config.cc @@ -4,6 +4,9 @@ #include "net/proxy/proxy_config.h" +#include "base/string_tokenizer.h" +#include "base/string_util.h" + namespace net { // static @@ -25,4 +28,55 @@ bool ProxyConfig::Equals(const ProxyConfig& other) const { proxy_bypass_local_names == other.proxy_bypass_local_names; } +void ProxyConfig::ProxyRules::ParseFromString(const std::string& proxy_rules) { + // Reset. + type = TYPE_NO_RULES; + single_proxy = ProxyServer(); + proxy_for_http = ProxyServer(); + proxy_for_https = ProxyServer(); + proxy_for_ftp = ProxyServer(); + + StringTokenizer proxy_server_list(proxy_rules, ";"); + while (proxy_server_list.GetNext()) { + StringTokenizer proxy_server_for_scheme( + proxy_server_list.token_begin(), proxy_server_list.token_end(), "="); + + while (proxy_server_for_scheme.GetNext()) { + std::string url_scheme = proxy_server_for_scheme.token(); + + // If we fail to get the proxy server here, it means that + // this is a regular proxy server configuration, i.e. proxies + // are not configured per protocol. + if (!proxy_server_for_scheme.GetNext()) { + if (type == TYPE_PROXY_PER_SCHEME) + continue; // Unexpected. + single_proxy = ProxyServer::FromURI(url_scheme); + type = TYPE_SINGLE_PROXY; + return; + } + + // Trim whitespace off the url scheme. + TrimWhitespaceASCII(url_scheme, TRIM_ALL, &url_scheme); + + // Add it to the per-scheme mappings (if supported scheme). + type = TYPE_PROXY_PER_SCHEME; + if (const ProxyServer* entry = MapSchemeToProxy(url_scheme)) + *const_cast<ProxyServer*>(entry) = + ProxyServer::FromURI(proxy_server_for_scheme.token()); + } + } +} + +const ProxyServer* ProxyConfig::ProxyRules::MapSchemeToProxy( + const std::string& scheme) const { + DCHECK(type == TYPE_PROXY_PER_SCHEME); + if (scheme == "http") + return &proxy_for_http; + if (scheme == "https") + return &proxy_for_https; + if (scheme == "ftp") + return &proxy_for_ftp; + return NULL; // No mapping for this scheme. +} + } // namespace net |