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#include "net/proxy/proxy_server.h"
11
12using base::TimeDelta;
13using base::TimeTicks;
14
15namespace net {
16
17ProxyList::ProxyList() {
18}
19
20ProxyList::~ProxyList() {
21}
22
23void 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(
28        str_tok.token_begin(), str_tok.token_end(), ProxyServer::SCHEME_HTTP);
29    // Silently discard malformed inputs.
30    if (uri.is_valid())
31      proxies_.push_back(uri);
32  }
33}
34
35void ProxyList::SetSingleProxyServer(const ProxyServer& proxy_server) {
36  proxies_.clear();
37  if (proxy_server.is_valid())
38    proxies_.push_back(proxy_server);
39}
40
41void 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
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.
57        bad_proxies.push_back(*iter);
58        continue;
59      }
60    }
61    good_proxies.push_back(*iter);
62  }
63
64  // "proxies_ = good_proxies + bad_proxies"
65  proxies_.swap(good_proxies);
66  proxies_.insert(proxies_.end(), bad_proxies.begin(), bad_proxies.end());
67}
68
69void 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
80bool ProxyList::IsEmpty() const {
81  return proxies_.empty();
82}
83
84const ProxyServer& ProxyList::Get() const {
85  DCHECK(!proxies_.empty());
86  return proxies_[0];
87}
88
89void ProxyList::SetFromPacString(const std::string& pac_string) {
90  StringTokenizer entry_tok(pac_string, ";");
91  proxies_.clear();
92  while (entry_tok.GetNext()) {
93    ProxyServer uri = ProxyServer::FromPacString(
94        entry_tok.token_begin(), entry_tok.token_end());
95    // Silently discard malformed inputs.
96    if (uri.is_valid())
97      proxies_.push_back(uri);
98  }
99
100  // If we failed to parse anything from the PAC results list, fallback to
101  // DIRECT (this basically means an error in the PAC script).
102  if (proxies_.empty()) {
103    proxies_.push_back(ProxyServer::Direct());
104  }
105}
106
107std::string ProxyList::ToPacString() const {
108  std::string proxy_list;
109  std::vector<ProxyServer>::const_iterator iter = proxies_.begin();
110  for (; iter != proxies_.end(); ++iter) {
111    if (!proxy_list.empty())
112      proxy_list += ";";
113    proxy_list += iter->ToPacString();
114  }
115  return proxy_list.empty() ? std::string() : proxy_list;
116}
117
118bool 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
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
136  if (proxies_.empty()) {
137    NOTREACHED();
138    return false;
139  }
140
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    }
155  }
156
157  // Remove this proxy from our list.
158  proxies_.erase(proxies_.begin());
159
160  return !proxies_.empty();
161}
162
163}  // namespace net
164