Home | History | Annotate | Download | only in proxy
      1 // Copyright (c) 2012 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 #ifndef NET_PROXY_PROXY_LIST_H_
      6 #define NET_PROXY_PROXY_LIST_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "net/base/net_export.h"
     12 #include "net/base/net_log.h"
     13 #include "net/proxy/proxy_retry_info.h"
     14 
     15 namespace base {
     16 class ListValue;
     17 class TimeDelta;
     18 }
     19 
     20 namespace net {
     21 
     22 class ProxyServer;
     23 
     24 // This class is used to hold a list of proxies returned by GetProxyForUrl or
     25 // manually configured. It handles proxy fallback if multiple servers are
     26 // specified.
     27 class NET_EXPORT_PRIVATE ProxyList {
     28  public:
     29   ProxyList();
     30   ~ProxyList();
     31 
     32   // Initializes the proxy list to a string containing one or more proxy servers
     33   // delimited by a semicolon.
     34   void Set(const std::string& proxy_uri_list);
     35 
     36   // Set the proxy list to a single entry, |proxy_server|.
     37   void SetSingleProxyServer(const ProxyServer& proxy_server);
     38 
     39   // Append a single proxy server to the end of the proxy list.
     40   void AddProxyServer(const ProxyServer& proxy_server);
     41 
     42   // De-prioritizes the proxies that we have cached as not working, by moving
     43   // them to the end of the fallback list.
     44   void DeprioritizeBadProxies(const ProxyRetryInfoMap& proxy_retry_info);
     45 
     46   // Returns true if this proxy list contains at least one proxy that is
     47   // not currently present in |proxy_retry_info|.
     48   bool HasUntriedProxies(const ProxyRetryInfoMap& proxy_retry_info) const;
     49 
     50   // Delete any entry which doesn't have one of the specified proxy schemes.
     51   // |scheme_bit_field| is a bunch of ProxyServer::Scheme bitwise ORed together.
     52   void RemoveProxiesWithoutScheme(int scheme_bit_field);
     53 
     54   // Clear the proxy list.
     55   void Clear();
     56 
     57   // Returns true if there is nothing left in the ProxyList.
     58   bool IsEmpty() const;
     59 
     60   // Returns the number of proxy servers in this list.
     61   size_t size() const;
     62 
     63   // Returns true if |*this| lists the same proxies as |other|.
     64   bool Equals(const ProxyList& other) const;
     65 
     66   // Returns the first proxy server in the list. It is only valid to call
     67   // this if !IsEmpty().
     68   const ProxyServer& Get() const;
     69 
     70   // Sets the list by parsing the pac result |pac_string|.
     71   // Some examples for |pac_string|:
     72   //   "DIRECT"
     73   //   "PROXY foopy1"
     74   //   "PROXY foopy1; SOCKS4 foopy2:1188"
     75   // Does a best-effort parse, and silently discards any errors.
     76   void SetFromPacString(const std::string& pac_string);
     77 
     78   // Returns a PAC-style semicolon-separated list of valid proxy servers.
     79   // For example: "PROXY xxx.xxx.xxx.xxx:xx; SOCKS yyy.yyy.yyy:yy".
     80   std::string ToPacString() const;
     81 
     82   // Returns a serialized value for the list. The caller takes ownership of it.
     83   base::ListValue* ToValue() const;
     84 
     85   // Marks the current proxy server as bad and deletes it from the list.  The
     86   // list of known bad proxies is given by proxy_retry_info.  Returns true if
     87   // there is another server available in the list.
     88   bool Fallback(ProxyRetryInfoMap* proxy_retry_info,
     89                 const BoundNetLog& net_log);
     90 
     91   // Updates |proxy_retry_info| to indicate that the first proxy in the list
     92   // is bad. This is distinct from Fallback(), above, to allow updating proxy
     93   // retry information without modifying a given transction's proxy list. Will
     94   // retry after |retry_delay| if positive, and will use the default proxy retry
     95   // duration otherwise. Additionally updates |proxy_retry_info| with
     96   // |another_proxy_to_bypass| if non-empty.
     97   void UpdateRetryInfoOnFallback(
     98       ProxyRetryInfoMap* proxy_retry_info,
     99       base::TimeDelta retry_delay,
    100       const ProxyServer& another_proxy_to_bypass,
    101       const BoundNetLog& net_log) const;
    102 
    103  private:
    104   // Updates |proxy_retry_info| to indicate that the proxy in |proxies_| with
    105   // the URI of |proxy_key| is bad. The |proxy_key| must start with the scheme
    106   // (only if https) followed by the host and  then an explicit port. For
    107   // example, if the proxy origin is https://proxy.chromium.org:443/ the key is
    108   // https://proxy.chrome.org:443 whereas if the origin is
    109   // http://proxy.chrome.org/, the key is proxy.chrome.org:80.
    110   void AddProxyToRetryList(ProxyRetryInfoMap* proxy_retry_info,
    111                            base::TimeDelta retry_delay,
    112                            const std::string& proxy_key,
    113                            const BoundNetLog& net_log) const;
    114 
    115   // List of proxies.
    116   std::vector<ProxyServer> proxies_;
    117 };
    118 
    119 }  // namespace net
    120 
    121 #endif  // NET_PROXY_PROXY_LIST_H_
    122