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.
5 #ifndef NET_PROXY_PROXY_SERVER_H_
6 #define NET_PROXY_PROXY_SERVER_H_
8 #include "build/build_config.h"
10 #if defined(OS_MACOSX)
11 #include <CoreFoundation/CoreFoundation.h>
15 #include "net/base/host_port_pair.h"
16 #include "net/base/net_export.h"
20 // ProxyServer encodes the {type, host, port} of a proxy server.
21 // ProxyServer is immutable.
22 class NET_EXPORT ProxyServer
{
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().
28 SCHEME_INVALID
= 1 << 0,
29 SCHEME_DIRECT
= 1 << 1,
31 SCHEME_SOCKS4
= 1 << 3,
32 SCHEME_SOCKS5
= 1 << 4,
33 SCHEME_HTTPS
= 1 << 5,
36 // Default copy-constructor and assignment operator are OK!
38 // Constructs an invalid ProxyServer.
39 ProxyServer() : scheme_(SCHEME_INVALID
) {}
41 ProxyServer(Scheme scheme
, const HostPortPair
& host_port_pair
);
43 bool is_valid() const { return scheme_
!= SCHEME_INVALID
; }
45 // Gets the proxy's scheme (i.e. SOCKS4, SOCKS5, HTTP)
46 Scheme
scheme() const { return scheme_
; }
48 // Returns true if this ProxyServer is actually just a DIRECT connection.
49 bool is_direct() const { return scheme_
== SCHEME_DIRECT
; }
51 // Returns true if this ProxyServer is an HTTP proxy.
52 bool is_http() const { return scheme_
== SCHEME_HTTP
; }
54 // Returns true if this ProxyServer is an HTTPS proxy.
55 bool is_https() const { return scheme_
== SCHEME_HTTPS
; }
57 // Returns true if this ProxyServer is a SOCKS proxy.
58 bool is_socks() const {
59 return scheme_
== SCHEME_SOCKS4
|| scheme_
== SCHEME_SOCKS5
;
62 const HostPortPair
& host_port_pair() const;
64 // Parses from an input with format:
65 // [<scheme>"://"]<server>[":"<port>]
67 // Both <scheme> and <port> are optional. If <scheme> is omitted, it will be
68 // assumed as |default_scheme|. If <port> is omitted, it will be assumed as
69 // the default port for the chosen scheme (80 for "http", 1080 for "socks").
71 // If parsing fails the instance will be set to invalid.
73 // Examples (for |default_scheme| = SCHEME_HTTP ):
74 // "foopy" {scheme=HTTP, host="foopy", port=80}
75 // "socks://foopy" {scheme=SOCKS5, host="foopy", port=1080}
76 // "socks4://foopy" {scheme=SOCKS4, host="foopy", port=1080}
77 // "socks5://foopy" {scheme=SOCKS5, host="foopy", port=1080}
78 // "http://foopy:17" {scheme=HTTP, host="foopy", port=17}
79 // "https://foopy:17" {scheme=HTTPS, host="foopy", port=17}
80 // "direct://" {scheme=DIRECT}
81 // "foopy:X" INVALID -- bad port.
82 static ProxyServer
FromURI(const std::string
& uri
, Scheme default_scheme
);
83 static ProxyServer
FromURI(std::string::const_iterator uri_begin
,
84 std::string::const_iterator uri_end
,
85 Scheme default_scheme
);
87 // Formats as a URI string. This does the reverse of FromURI.
88 std::string
ToURI() const;
90 // Parses from a PAC string result.
92 // If <port> is omitted, it will be assumed as the default port for the
93 // chosen scheme (80 for "http", 1080 for "socks").
95 // If parsing fails the instance will be set to invalid.
98 // "PROXY foopy:19" {scheme=HTTP, host="foopy", port=19}
99 // "DIRECT" {scheme=DIRECT}
100 // "SOCKS5 foopy" {scheme=SOCKS5, host="foopy", port=1080}
101 // "HTTPS foopy:123" {scheme=HTTPS, host="foopy", port=123}
102 // "BLAH xxx:xx" INVALID
103 static ProxyServer
FromPacString(const std::string
& pac_string
);
104 static ProxyServer
FromPacString(std::string::const_iterator pac_string_begin
,
105 std::string::const_iterator pac_string_end
);
107 // Returns a ProxyServer representing DIRECT connections.
108 static ProxyServer
Direct() {
109 return ProxyServer(SCHEME_DIRECT
, HostPortPair());
112 #if defined(OS_MACOSX)
113 // Utility function to pull out a host/port pair from a dictionary and return
114 // it as a ProxyServer object. Pass in a dictionary that has a value for the
115 // host key and optionally a value for the port key. In the error condition
116 // where the host value is especially malformed, returns an invalid
118 static ProxyServer
FromDictionary(Scheme scheme
,
119 CFDictionaryRef dict
,
120 CFStringRef host_key
,
121 CFStringRef port_key
);
124 // Formats as a PAC result entry. This does the reverse of FromPacString().
125 std::string
ToPacString() const;
127 // Returns the default port number for a proxy server with the specified
128 // scheme. Returns -1 if unknown.
129 static int GetDefaultPortForScheme(Scheme scheme
);
131 // Parses the proxy scheme from a URL-like representation, to a
132 // ProxyServer::Scheme. This corresponds with the values used in
133 // ProxyServer::ToURI(). If no type could be matched, returns SCHEME_INVALID.
134 // |scheme| can be one of http, https, socks, socks4, socks5, direct.
135 static Scheme
GetSchemeFromURI(const std::string
& scheme
);
137 bool operator==(const ProxyServer
& other
) const {
138 return scheme_
== other
.scheme_
&&
139 host_port_pair_
.Equals(other
.host_port_pair_
);
142 // Comparator function so this can be placed in a std::map.
143 bool operator<(const ProxyServer
& other
) const {
144 if (scheme_
!= other
.scheme_
)
145 return scheme_
< other
.scheme_
;
146 return host_port_pair_
< other
.host_port_pair_
;
150 // Creates a ProxyServer given a scheme, and host/port string. If parsing the
151 // host/port string fails, the returned instance will be invalid.
152 static ProxyServer
FromSchemeHostAndPort(
154 std::string::const_iterator host_and_port_begin
,
155 std::string::const_iterator host_and_port_end
);
158 HostPortPair host_port_pair_
;
161 typedef std::pair
<HostPortPair
, ProxyServer
> HostPortProxyPair
;
165 #endif // NET_PROXY_PROXY_SERVER_H_