Home | History | Annotate | Download | only in proxy
      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 #ifndef NET_PROXY_PROXY_CONFIG_H_
      6 #define NET_PROXY_PROXY_CONFIG_H_
      7 
      8 #include <ostream>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "googleurl/src/gurl.h"
     13 #include "net/proxy/proxy_server.h"
     14 
     15 namespace net {
     16 
     17 // Proxy configuration used to by the ProxyService.
     18 class ProxyConfig {
     19  public:
     20   typedef int ID;
     21 
     22   // Indicates an invalid proxy config.
     23   enum { INVALID_ID = 0 };
     24 
     25   ProxyConfig();
     26   // Default copy-constructor and assignment operator are OK!
     27 
     28   // Used to numerically identify this configuration.
     29   ID id() const { return id_; }
     30   void set_id(int id) { id_ = id; }
     31   bool is_valid() { return id_ != INVALID_ID; }
     32 
     33   // True if the proxy configuration should be auto-detected.
     34   bool auto_detect;
     35 
     36   // If non-empty, indicates the URL of the proxy auto-config file to use.
     37   GURL pac_url;
     38 
     39   struct ProxyRules {
     40     enum Type {
     41       TYPE_NO_RULES,
     42       TYPE_SINGLE_PROXY,
     43       TYPE_PROXY_PER_SCHEME,
     44     };
     45 
     46     // Note that the default of TYPE_NO_RULES results in direct connections
     47     // being made when using this ProxyConfig.
     48     ProxyRules() : type(TYPE_NO_RULES) {}
     49 
     50     bool empty() const {
     51       return type == TYPE_NO_RULES;
     52     }
     53 
     54     // Parses the rules from a string, indicating which proxies to use.
     55     //
     56     //   proxy-uri = [<proxy-scheme>://]<proxy-host>[:"<proxy-port>]
     57     //
     58     // If the proxy to use depends on the scheme of the URL, can instead specify
     59     // a semicolon separated list of:
     60     //
     61     //   <url-scheme>"="<proxy-uri>
     62     //
     63     // For example:
     64     //   "http=foopy:80;ftp=foopy2"  -- use HTTP proxy "foopy:80" for http URLs,
     65     //                                  and HTTP proxy "foopy2:80" for ftp URLs.
     66     //   "foopy:80"                  -- use HTTP proxy "foopy:80" for all URLs.
     67     //   "socks4://foopy"            -- use SOCKS v4 proxy "foopy:1080" for all
     68     //                                  URLs.
     69     void ParseFromString(const std::string& proxy_rules);
     70 
     71     // Returns one of {&proxy_for_http, &proxy_for_https, &proxy_for_ftp,
     72     // &socks_proxy}, or NULL if it is a scheme that we don't have a mapping
     73     // for. If the scheme mapping is not present and socks_proxy is defined,
     74     // we fall back to using socks_proxy.
     75     // Should only call this if the type is TYPE_PROXY_PER_SCHEME.
     76     const ProxyServer* MapUrlSchemeToProxy(const std::string& url_scheme) const;
     77 
     78     bool operator==(const ProxyRules& other) const {
     79       return type == other.type &&
     80              single_proxy == other.single_proxy &&
     81              proxy_for_http == other.proxy_for_http &&
     82              proxy_for_https == other.proxy_for_https &&
     83              proxy_for_ftp == other.proxy_for_ftp &&
     84              socks_proxy == other.socks_proxy;
     85     }
     86 
     87     Type type;
     88 
     89     // Set if |type| is TYPE_SINGLE_PROXY.
     90     ProxyServer single_proxy;
     91 
     92     // Set if |type| is TYPE_PROXY_PER_SCHEME.
     93     ProxyServer proxy_for_http;
     94     ProxyServer proxy_for_https;
     95     ProxyServer proxy_for_ftp;
     96 
     97     // Set if configuration has SOCKS proxy.
     98     ProxyServer socks_proxy;
     99    private:
    100     // Returns one of {&proxy_for_http, &proxy_for_https, &proxy_for_ftp,
    101     // &socks_proxy}, or NULL if it is a scheme that we don't have a mapping
    102     // for. Should only call this if the type is TYPE_PROXY_PER_SCHEME.
    103     ProxyServer* MapSchemeToProxy(const std::string& scheme);
    104   };
    105 
    106   ProxyRules proxy_rules;
    107 
    108   // Parses entries from a comma-separated list of hosts for which proxy
    109   // configurations should be bypassed. Clears proxy_bypass and sets it to the
    110   // resulting list.
    111   void ParseNoProxyList(const std::string& no_proxy);
    112 
    113   // Indicates a list of hosts that should bypass any proxy configuration.  For
    114   // these hosts, a direct connection should always be used.
    115   // The form <host>:<port> is also supported, meaning that only
    116   // connections on the specified port should be direct.
    117   std::vector<std::string> proxy_bypass;
    118 
    119   // Indicates whether local names (no dots) bypass proxies.
    120   bool proxy_bypass_local_names;
    121 
    122   // Returns true if the given config is equivalent to this config.
    123   bool Equals(const ProxyConfig& other) const;
    124 
    125   // Returns true if this config could possibly require the proxy service to
    126   // use a PAC resolver.
    127   bool MayRequirePACResolver() const;
    128 
    129  private:
    130   int id_;
    131 };
    132 
    133 }  // namespace net
    134 
    135 // Dumps a human-readable string representation of the configuration to |out|;
    136 // used when logging the configuration changes.
    137 std::ostream& operator<<(std::ostream& out, const net::ProxyConfig& config);
    138 
    139 // Dumps a human-readable string representation of the |rules| to |out|;
    140 // used for logging and for better unittest failure output.
    141 std::ostream& operator<<(std::ostream& out,
    142                          const net::ProxyConfig::ProxyRules& rules);
    143 
    144 #endif  // NET_PROXY_PROXY_CONFIG_H_
    145