Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / net / proxy / proxy_server.h
1 // Copyright (c) 2011 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_SERVER_H_
6 #define NET_PROXY_PROXY_SERVER_H_
7
8 #include "build/build_config.h"
9
10 #if defined(OS_MACOSX)
11 #include <CoreFoundation/CoreFoundation.h>
12 #endif
13
14 #include <string>
15 #include "net/base/host_port_pair.h"
16 #include "net/base/net_export.h"
17
18 namespace net {
19
20 // ProxyServer encodes the {type, host, port} of a proxy server.
21 // ProxyServer is immutable.
22 class NET_EXPORT ProxyServer {
23  public:
24   // The type of proxy. These are defined as bit flags so they can be ORed
25   // together to pass as the |scheme_bit_field| argument to
26   // ProxyService::RemoveProxiesWithoutScheme().
27   enum Scheme {
28     SCHEME_INVALID = 1 << 0,
29     SCHEME_DIRECT  = 1 << 1,
30     SCHEME_HTTP    = 1 << 2,
31     SCHEME_SOCKS4  = 1 << 3,
32     SCHEME_SOCKS5  = 1 << 4,
33     SCHEME_HTTPS   = 1 << 5,
34     // A QUIC proxy is an HTTP proxy in which QUIC is used as the transport,
35     // instead of TCP.
36     SCHEME_QUIC    = 1 << 6,
37   };
38
39   // Default copy-constructor and assignment operator are OK!
40
41   // Constructs an invalid ProxyServer.
42   ProxyServer() : scheme_(SCHEME_INVALID) {}
43
44   ProxyServer(Scheme scheme, const HostPortPair& host_port_pair);
45
46   bool is_valid() const { return scheme_ != SCHEME_INVALID; }
47
48   // Gets the proxy's scheme (i.e. SOCKS4, SOCKS5, HTTP)
49   Scheme scheme() const { return scheme_; }
50
51   // Returns true if this ProxyServer is actually just a DIRECT connection.
52   bool is_direct() const { return scheme_ == SCHEME_DIRECT; }
53
54   // Returns true if this ProxyServer is an HTTP proxy.
55   bool is_http() const { return scheme_ == SCHEME_HTTP; }
56
57   // Returns true if this ProxyServer is an HTTPS proxy.
58   bool is_https() const { return scheme_ == SCHEME_HTTPS; }
59
60   // Returns true if this ProxyServer is a SOCKS proxy.
61   bool is_socks() const {
62     return scheme_ == SCHEME_SOCKS4 || scheme_ == SCHEME_SOCKS5;
63   }
64
65   // Returns true if this ProxyServer is a QUIC proxy.
66   bool is_quic() const { return scheme_ == SCHEME_QUIC; }
67
68   const HostPortPair& host_port_pair() const;
69
70   // Parses from an input with format:
71   //   [<scheme>"://"]<server>[":"<port>]
72   //
73   // Both <scheme> and <port> are optional. If <scheme> is omitted, it will be
74   // assumed as |default_scheme|. If <port> is omitted, it will be assumed as
75   // the default port for the chosen scheme (80 for "http", 1080 for "socks").
76   //
77   // If parsing fails the instance will be set to invalid.
78   //
79   // Examples (for |default_scheme| = SCHEME_HTTP ):
80   //   "foopy"            {scheme=HTTP, host="foopy", port=80}
81   //   "socks://foopy"    {scheme=SOCKS5, host="foopy", port=1080}
82   //   "socks4://foopy"   {scheme=SOCKS4, host="foopy", port=1080}
83   //   "socks5://foopy"   {scheme=SOCKS5, host="foopy", port=1080}
84   //   "http://foopy:17"  {scheme=HTTP, host="foopy", port=17}
85   //   "https://foopy:17" {scheme=HTTPS, host="foopy", port=17}
86   //   "quic://foopy:17"  {scheme=QUIC, host="foopy", port=17}
87   //   "direct://"        {scheme=DIRECT}
88   //   "foopy:X"          INVALID -- bad port.
89   static ProxyServer FromURI(const std::string& uri, Scheme default_scheme);
90   static ProxyServer FromURI(std::string::const_iterator uri_begin,
91                              std::string::const_iterator uri_end,
92                              Scheme default_scheme);
93
94   // Formats as a URI string. This does the reverse of FromURI.
95   std::string ToURI() const;
96
97   // Parses from a PAC string result.
98   //
99   // If <port> is omitted, it will be assumed as the default port for the
100   // chosen scheme (80 for "http", 1080 for "socks").
101   //
102   // If parsing fails the instance will be set to invalid.
103   //
104   // Examples:
105   //   "PROXY foopy:19"   {scheme=HTTP, host="foopy", port=19}
106   //   "DIRECT"           {scheme=DIRECT}
107   //   "SOCKS5 foopy"     {scheme=SOCKS5, host="foopy", port=1080}
108   //   "HTTPS foopy:123"  {scheme=HTTPS, host="foopy", port=123}
109   //   "QUIC foopy:123"   {scheme=QUIC, host="foopy", port=123}
110   //   "BLAH xxx:xx"      INVALID
111   static ProxyServer FromPacString(const std::string& pac_string);
112   static ProxyServer FromPacString(std::string::const_iterator pac_string_begin,
113                                    std::string::const_iterator pac_string_end);
114
115   // Returns a ProxyServer representing DIRECT connections.
116   static ProxyServer Direct() {
117     return ProxyServer(SCHEME_DIRECT, HostPortPair());
118   }
119
120 #if defined(OS_MACOSX)
121   // Utility function to pull out a host/port pair from a dictionary and return
122   // it as a ProxyServer object. Pass in a dictionary that has a  value for the
123   // host key and optionally a value for the port key. In the error condition
124   // where the host value is especially malformed, returns an invalid
125   // ProxyServer.
126   static ProxyServer FromDictionary(Scheme scheme,
127                                     CFDictionaryRef dict,
128                                     CFStringRef host_key,
129                                     CFStringRef port_key);
130 #endif
131
132   // Formats as a PAC result entry. This does the reverse of FromPacString().
133   std::string ToPacString() const;
134
135   // Returns the default port number for a proxy server with the specified
136   // scheme. Returns -1 if unknown.
137   static int GetDefaultPortForScheme(Scheme scheme);
138
139   // Parses the proxy scheme from a URL-like representation, to a
140   // ProxyServer::Scheme. This corresponds with the values used in
141   // ProxyServer::ToURI(). If no type could be matched, returns SCHEME_INVALID.
142   // |scheme| can be one of http, https, socks, socks4, socks5, direct.
143   static Scheme GetSchemeFromURI(const std::string& scheme);
144
145   bool operator==(const ProxyServer& other) const {
146     return scheme_ == other.scheme_ &&
147            host_port_pair_.Equals(other.host_port_pair_);
148   }
149
150   // Comparator function so this can be placed in a std::map.
151   bool operator<(const ProxyServer& other) const {
152     if (scheme_ != other.scheme_)
153       return scheme_ < other.scheme_;
154     return host_port_pair_ < other.host_port_pair_;
155   }
156
157 #if defined(SPDY_PROXY_AUTH_ORIGIN)
158   // Returns true if this proxy server is the data reduction proxy or its
159   // fallback, respectively, as configured in gyp. These functions will return
160   // false for data reduction proxy servers specified on the command line.
161   bool isDataReductionProxy() const;
162   bool isDataReductionProxyFallback() const;
163 #endif  // defined(SPDY_PROXY_AUTH_ORIGIN)
164
165  private:
166   // Creates a ProxyServer given a scheme, and host/port string. If parsing the
167   // host/port string fails, the returned instance will be invalid.
168   static ProxyServer FromSchemeHostAndPort(
169       Scheme scheme,
170       std::string::const_iterator host_and_port_begin,
171       std::string::const_iterator host_and_port_end);
172
173   Scheme scheme_;
174   HostPortPair host_port_pair_;
175 };
176
177 typedef std::pair<HostPortPair, ProxyServer> HostPortProxyPair;
178
179 }  // namespace net
180
181 #endif  // NET_PROXY_PROXY_SERVER_H_