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