[email protected] | 7dc52f2 | 2009-03-02 22:37:18 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 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 | #include "net/proxy/proxy_list.h" |
| 6 | |
| 7 | #include "base/logging.h" |
| 8 | #include "base/string_tokenizer.h" |
| 9 | #include "base/time.h" |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 10 | #include "net/proxy/proxy_server.h" |
[email protected] | 7dc52f2 | 2009-03-02 22:37:18 | [diff] [blame] | 11 | |
| 12 | using base::TimeDelta; |
| 13 | using base::TimeTicks; |
| 14 | |
| 15 | namespace net { |
| 16 | |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 17 | ProxyList::ProxyList() { |
| 18 | } |
| 19 | |
| 20 | ProxyList::~ProxyList() { |
| 21 | } |
| 22 | |
[email protected] | 7dc52f2 | 2009-03-02 22:37:18 | [diff] [blame] | 23 | void ProxyList::Set(const std::string& proxy_uri_list) { |
| 24 | proxies_.clear(); |
| 25 | StringTokenizer str_tok(proxy_uri_list, ";"); |
| 26 | while (str_tok.GetNext()) { |
| 27 | ProxyServer uri = ProxyServer::FromURI( |
[email protected] | 87a102b | 2009-07-14 05:23:30 | [diff] [blame] | 28 | str_tok.token_begin(), str_tok.token_end(), ProxyServer::SCHEME_HTTP); |
[email protected] | 7dc52f2 | 2009-03-02 22:37:18 | [diff] [blame] | 29 | // Silently discard malformed inputs. |
| 30 | if (uri.is_valid()) |
| 31 | proxies_.push_back(uri); |
| 32 | } |
| 33 | } |
| 34 | |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 35 | void ProxyList::SetSingleProxyServer(const ProxyServer& proxy_server) { |
| 36 | proxies_.clear(); |
| 37 | if (proxy_server.is_valid()) |
| 38 | proxies_.push_back(proxy_server); |
| 39 | } |
| 40 | |
[email protected] | 02cf5a4 | 2010-01-12 22:10:25 | [diff] [blame] | 41 | void ProxyList::DeprioritizeBadProxies( |
| 42 | const ProxyRetryInfoMap& proxy_retry_info) { |
| 43 | // Partition the proxy list in two: |
| 44 | // (1) the known bad proxies |
| 45 | // (2) everything else |
| 46 | std::vector<ProxyServer> good_proxies; |
| 47 | std::vector<ProxyServer> bad_proxies; |
| 48 | |
[email protected] | 7dc52f2 | 2009-03-02 22:37:18 | [diff] [blame] | 49 | std::vector<ProxyServer>::const_iterator iter = proxies_.begin(); |
| 50 | for (; iter != proxies_.end(); ++iter) { |
| 51 | ProxyRetryInfoMap::const_iterator bad_proxy = |
| 52 | proxy_retry_info.find(iter->ToURI()); |
| 53 | if (bad_proxy != proxy_retry_info.end()) { |
| 54 | // This proxy is bad. Check if it's time to retry. |
| 55 | if (bad_proxy->second.bad_until >= TimeTicks::Now()) { |
| 56 | // still invalid. |
[email protected] | 02cf5a4 | 2010-01-12 22:10:25 | [diff] [blame] | 57 | bad_proxies.push_back(*iter); |
[email protected] | 7dc52f2 | 2009-03-02 22:37:18 | [diff] [blame] | 58 | continue; |
| 59 | } |
| 60 | } |
[email protected] | 02cf5a4 | 2010-01-12 22:10:25 | [diff] [blame] | 61 | good_proxies.push_back(*iter); |
[email protected] | 7dc52f2 | 2009-03-02 22:37:18 | [diff] [blame] | 62 | } |
| 63 | |
[email protected] | 02cf5a4 | 2010-01-12 22:10:25 | [diff] [blame] | 64 | // "proxies_ = good_proxies + bad_proxies" |
| 65 | proxies_.swap(good_proxies); |
| 66 | proxies_.insert(proxies_.end(), bad_proxies.begin(), bad_proxies.end()); |
[email protected] | 7dc52f2 | 2009-03-02 22:37:18 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void ProxyList::RemoveProxiesWithoutScheme(int scheme_bit_field) { |
| 70 | for (std::vector<ProxyServer>::iterator it = proxies_.begin(); |
| 71 | it != proxies_.end(); ) { |
| 72 | if (!(scheme_bit_field & it->scheme())) { |
| 73 | it = proxies_.erase(it); |
| 74 | continue; |
| 75 | } |
| 76 | ++it; |
| 77 | } |
| 78 | } |
| 79 | |
[email protected] | 6971906 | 2010-01-05 20:09:21 | [diff] [blame] | 80 | bool ProxyList::IsEmpty() const { |
| 81 | return proxies_.empty(); |
| 82 | } |
| 83 | |
| 84 | const ProxyServer& ProxyList::Get() const { |
| 85 | DCHECK(!proxies_.empty()); |
| 86 | return proxies_[0]; |
[email protected] | 7dc52f2 | 2009-03-02 22:37:18 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | std::string ProxyList::ToPacString() const { |
| 90 | std::string proxy_list; |
| 91 | std::vector<ProxyServer>::const_iterator iter = proxies_.begin(); |
| 92 | for (; iter != proxies_.end(); ++iter) { |
| 93 | if (!proxy_list.empty()) |
| 94 | proxy_list += ";"; |
| 95 | proxy_list += iter->ToPacString(); |
| 96 | } |
[email protected] | 6971906 | 2010-01-05 20:09:21 | [diff] [blame] | 97 | return proxy_list.empty() ? std::string() : proxy_list; |
[email protected] | 7dc52f2 | 2009-03-02 22:37:18 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void ProxyList::SetFromPacString(const std::string& pac_string) { |
| 101 | StringTokenizer entry_tok(pac_string, ";"); |
| 102 | proxies_.clear(); |
| 103 | while (entry_tok.GetNext()) { |
| 104 | ProxyServer uri = ProxyServer::FromPacString( |
| 105 | entry_tok.token_begin(), entry_tok.token_end()); |
| 106 | // Silently discard malformed inputs. |
| 107 | if (uri.is_valid()) |
| 108 | proxies_.push_back(uri); |
| 109 | } |
[email protected] | 6971906 | 2010-01-05 20:09:21 | [diff] [blame] | 110 | |
| 111 | // If we failed to parse anything from the PAC results list, fallback to |
| 112 | // DIRECT (this basically means an error in the PAC script). |
| 113 | if (proxies_.empty()) { |
| 114 | proxies_.push_back(ProxyServer::Direct()); |
| 115 | } |
[email protected] | 7dc52f2 | 2009-03-02 22:37:18 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | bool ProxyList::Fallback(ProxyRetryInfoMap* proxy_retry_info) { |
| 119 | // Number of minutes to wait before retrying a bad proxy server. |
| 120 | const TimeDelta kProxyRetryDelay = TimeDelta::FromMinutes(5); |
| 121 | |
[email protected] | 6971906 | 2010-01-05 20:09:21 | [diff] [blame] | 122 | // TODO(eroman): It would be good if instead of removing failed proxies |
| 123 | // from the list, we simply annotated them with the error code they failed |
| 124 | // with. Of course, ProxyService::ReconsiderProxyAfterError() would need to |
| 125 | // be given this information by the network transaction. |
| 126 | // |
| 127 | // The advantage of this approach is when the network transaction |
| 128 | // fails, we could output the full list of proxies that were attempted, and |
| 129 | // why each one of those failed (as opposed to just the last failure). |
| 130 | // |
| 131 | // And also, before failing the transaction wholesale, we could go back and |
| 132 | // retry the "bad proxies" which we never tried to begin with. |
| 133 | // (RemoveBadProxies would annotate them as 'expected bad' rather then delete |
| 134 | // them from the list, so we would know what they were). |
| 135 | |
[email protected] | 7dc52f2 | 2009-03-02 22:37:18 | [diff] [blame] | 136 | if (proxies_.empty()) { |
| 137 | NOTREACHED(); |
| 138 | return false; |
| 139 | } |
| 140 | |
[email protected] | 6971906 | 2010-01-05 20:09:21 | [diff] [blame] | 141 | if (!proxies_[0].is_direct()) { |
| 142 | std::string key = proxies_[0].ToURI(); |
| 143 | // Mark this proxy as bad. |
| 144 | ProxyRetryInfoMap::iterator iter = proxy_retry_info->find(key); |
| 145 | if (iter != proxy_retry_info->end()) { |
| 146 | // TODO(nsylvain): This is not the first time we get this. We should |
| 147 | // double the retry time. Bug 997660. |
| 148 | iter->second.bad_until = TimeTicks::Now() + iter->second.current_delay; |
| 149 | } else { |
| 150 | ProxyRetryInfo retry_info; |
| 151 | retry_info.current_delay = kProxyRetryDelay; |
| 152 | retry_info.bad_until = TimeTicks().Now() + retry_info.current_delay; |
| 153 | (*proxy_retry_info)[key] = retry_info; |
| 154 | } |
[email protected] | 7dc52f2 | 2009-03-02 22:37:18 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | // Remove this proxy from our list. |
| 158 | proxies_.erase(proxies_.begin()); |
| 159 | |
| 160 | return !proxies_.empty(); |
| 161 | } |
| 162 | |
| 163 | } // namespace net |