File: proxy_list.cc

package info (click to toggle)
chromium 145.0.7632.159-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,976,224 kB
  • sloc: cpp: 36,198,469; ansic: 7,634,080; javascript: 3,564,060; python: 1,649,622; xml: 838,470; asm: 717,087; pascal: 185,708; sh: 88,786; perl: 88,718; objc: 79,984; sql: 59,811; cs: 42,452; fortran: 24,101; makefile: 21,144; tcl: 15,277; php: 14,022; yacc: 9,066; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,328; ada: 727; jsp: 228; sed: 36
file content (227 lines) | stat: -rw-r--r-- 6,785 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/proxy_resolution/proxy_list.h"

#include <algorithm>

#include "base/check.h"
#include "base/functional/callback.h"
#include "base/notreached.h"
#include "base/strings/string_tokenizer.h"
#include "base/time/time.h"
#include "base/values.h"
#include "net/base/proxy_chain.h"
#include "net/base/proxy_server.h"
#include "net/base/proxy_string_util.h"
#include "net/log/net_log.h"
#include "net/log/net_log_event_type.h"
#include "net/log/net_log_with_source.h"

using base::TimeTicks;

namespace net {

ProxyList::ProxyList() = default;

ProxyList::ProxyList(const ProxyList& other) = default;

ProxyList::ProxyList(ProxyList&& other) = default;

ProxyList& ProxyList::operator=(const ProxyList& other) = default;

ProxyList& ProxyList::operator=(ProxyList&& other) = default;

ProxyList::~ProxyList() = default;

void ProxyList::Set(const std::string& proxy_uri_list) {
  Clear();
  base::StringTokenizer str_tok(proxy_uri_list, ";");
  while (str_tok.GetNext()) {
    ProxyChain chain =
        ProxyUriToProxyChain(str_tok.token_piece(), ProxyServer::SCHEME_HTTP);
    AddProxyChain(std::move(chain));
  }
}

void ProxyList::SetSingleProxyChain(ProxyChain proxy_chain) {
  Clear();
  AddProxyChain(std::move(proxy_chain));
}

void ProxyList::SetSingleProxyServer(ProxyServer proxy_server) {
  Clear();
  AddProxyServer(std::move(proxy_server));
}

void ProxyList::AddProxyChain(ProxyChain proxy_chain) {
  // Silently discard malformed inputs.
  if (proxy_chain.IsValid()) {
    proxy_chains_.emplace_back(std::move(proxy_chain));
  }
}

void ProxyList::AddProxyServer(ProxyServer proxy_server) {
  AddProxyChain(ProxyChain(std::move(proxy_server)));
}

void ProxyList::DeprioritizeBadProxyChains(
    const ProxyRetryInfoMap& proxy_retry_info,
    bool remove_bad_proxy_chains) {
  // Partition the proxy list in two:
  //   (1) the known bad proxy chains
  //   (2) everything else
  std::vector<ProxyChain> good_chains;
  std::vector<ProxyChain> bad_chains_to_try;

  for (const auto& proxy_chain : proxy_chains_) {
    if (auto bad_info = proxy_retry_info.find(proxy_chain);
        bad_info != proxy_retry_info.end() &&
        bad_info->second.bad_until >= TimeTicks::Now()) {
      // This proxy is bad and it's not time to retry.
      bad_chains_to_try.push_back(proxy_chain);
      continue;
    }
    good_chains.push_back(proxy_chain);
  }

  // "proxy_chains_ = good_chains + bad_proxies"
  proxy_chains_.swap(good_chains);
  if (!remove_bad_proxy_chains) {
    proxy_chains_.insert(proxy_chains_.end(), bad_chains_to_try.begin(),
                         bad_chains_to_try.end());
  }
}

void ProxyList::RemoveProxiesWithoutScheme(int scheme_bit_field) {
  std::erase_if(proxy_chains_, [&](const ProxyChain& chain) {
    auto& proxy_servers = chain.proxy_servers();
    // Remove the chain if any of the component servers does not match
    // at least one scheme in `scheme_bit_field`.
    return std::any_of(proxy_servers.begin(), proxy_servers.end(),
                       [&](const ProxyServer& server) {
                         return !(scheme_bit_field & server.scheme());
                       });
  });
}

void ProxyList::Clear() {
  proxy_chains_.clear();
}

bool ProxyList::IsEmpty() const {
  return proxy_chains_.empty();
}

size_t ProxyList::size() const {
  return proxy_chains_.size();
}

// Returns true if |*this| lists the same proxy chains as |other|.
bool ProxyList::Equals(const ProxyList& other) const {
  if (size() != other.size())
    return false;
  return proxy_chains_ == other.proxy_chains_;
}

const ProxyChain& ProxyList::First() const {
  CHECK(!proxy_chains_.empty());
  return proxy_chains_[0];
}

const std::vector<ProxyChain>& ProxyList::AllChains() const {
  return proxy_chains_;
}

void ProxyList::SetFromPacString(const std::string& pac_string) {
  Clear();
  base::StringTokenizer entry_tok(pac_string, ";");
  while (entry_tok.GetNext()) {
    ProxyChain proxy_chain =
        PacResultElementToProxyChain(entry_tok.token_piece());
    if (proxy_chain.IsValid()) {
      proxy_chains_.emplace_back(proxy_chain);
    }
  }

  // If we failed to parse anything from the PAC results list, fallback to
  // DIRECT (this basically means an error in the PAC script).
  if (proxy_chains_.empty()) {
    proxy_chains_.push_back(ProxyChain::Direct());
  }
}

std::string ProxyList::ToPacString() const {
  std::string proxy_list;
  for (const ProxyChain& proxy_chain : proxy_chains_) {
    if (!proxy_list.empty()) {
      proxy_list += ";";
    }
    CHECK(!proxy_chain.is_multi_proxy());
    proxy_list += proxy_chain.is_direct()
                      ? "DIRECT"
                      : ProxyServerToPacResultElement(proxy_chain.First());
  }
  return proxy_list;
}

std::string ProxyList::ToDebugString() const {
  std::string proxy_list;

  for (const ProxyChain& proxy_chain : proxy_chains_) {
    if (!proxy_list.empty()) {
      proxy_list += ";";
    }
    if (proxy_chain.is_multi_proxy()) {
      proxy_list += proxy_chain.ToDebugString();
    } else {
      proxy_list += proxy_chain.is_direct()
                        ? "DIRECT"
                        : ProxyServerToPacResultElement(proxy_chain.First());
    }
  }
  return proxy_list;
}

base::Value ProxyList::ToValue() const {
  base::Value::List list;
  for (const auto& proxy_chain : proxy_chains_) {
    if (proxy_chain.is_direct()) {
      list.Append("direct://");
    } else {
      list.Append(proxy_chain.ToDebugString());
    }
  }
  return base::Value(std::move(list));
}

bool ProxyList::Fallback(ProxyRetryInfoMap* proxy_retry_info,
                         int net_error,
                         const NetLogWithSource& net_log) {
  CHECK(!proxy_chains_.empty());

  auto first_chain = std::move(proxy_chains_.front());
  proxy_chains_.erase(proxy_chains_.begin());

  if (!first_chain.is_direct()) {
    net_log.AddEventWithStringParams(NetLogEventType::PROXY_LIST_FALLBACK,
                                     "bad_proxy_chain",
                                     first_chain.ToDebugString());

    // By default, proxy chains are not retried for 5 minutes.
    static constexpr base::TimeDelta kDefaultRetryDelay = base::Minutes(5);
    TimeTicks bad_until = TimeTicks::Now() + kDefaultRetryDelay;
    auto iter = proxy_retry_info->find(first_chain);
    if (iter == proxy_retry_info->end() || bad_until > iter->second.bad_until) {
      ProxyRetryInfo retry_info;
      retry_info.bad_until = bad_until;
      retry_info.net_error = net_error;
      (*proxy_retry_info)[std::move(first_chain)] = std::move(retry_info);
    }
  }

  return !proxy_chains_.empty();
}

}  // namespace net