1 // Copyright (c) 2009 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 16 namespace net { 17 18 // ProxyServer encodes the {type, host, port} of a proxy server. 19 // ProxyServer is immutable. 20 class ProxyServer { 21 public: 22 // The type of proxy. These are defined as bit flags so they can be ORed 23 // together to pass as the |scheme_bit_field| argument to 24 // ProxyService::RemoveProxiesWithoutScheme(). 25 enum Scheme { 26 SCHEME_INVALID = 1 << 0, 27 SCHEME_DIRECT = 1 << 1, 28 SCHEME_HTTP = 1 << 2, 29 SCHEME_SOCKS4 = 1 << 3, 30 SCHEME_SOCKS5 = 1 << 4, 31 }; 32 33 // Default copy-constructor and assignment operator are OK! 34 35 // Constructs an invalid ProxyServer. ProxyServer()36 ProxyServer() : scheme_(SCHEME_INVALID), port_(-1) {} 37 38 // If |host| is an IPv6 literal address, it must include the square 39 // brackets. ProxyServer(Scheme scheme,const std::string & host,int port)40 ProxyServer(Scheme scheme, const std::string& host, int port) 41 : scheme_(scheme), host_(host), port_(port) {} 42 is_valid()43 bool is_valid() const { return scheme_ != SCHEME_INVALID; } 44 45 // Gets the proxy's scheme (i.e. SOCKS4, SOCKS5, HTTP} scheme()46 Scheme scheme() const { return scheme_; } 47 48 // Returns true if this ProxyServer is actually just a DIRECT connection. is_direct()49 bool is_direct() const { return scheme_ == SCHEME_DIRECT; } 50 51 // Returns true if this ProxyServer is an HTTP proxy. is_http()52 bool is_http() const { return scheme_ == SCHEME_HTTP; } 53 54 // Returns true if this ProxyServer is a SOCKS proxy. is_socks()55 bool is_socks() const { 56 return scheme_ == SCHEME_SOCKS4 || scheme_ == SCHEME_SOCKS5; 57 } 58 59 // Gets the host portion of the proxy server. If the host portion is an 60 // IPv6 literal address, the return value does not include the square 61 // brackets ([]) used to separate it from the port portion. 62 std::string HostNoBrackets() const; 63 64 // Gets the port portion of the proxy server. 65 int port() const; 66 67 // Returns the <host>":"<port> string for the proxy server. 68 std::string host_and_port() const; 69 70 // Parse 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 // "socks4://foopy" {scheme=SOCKS4, host="foopy", port=1080} 82 // "socks5://foopy" {scheme=SOCKS5, host="foopy", port=1080} 83 // "http://foopy:17" {scheme=HTTP, host="foopy", port=17} 84 // "direct://" {scheme=DIRECT} 85 // "foopy:X" INVALID -- bad port. 86 static ProxyServer FromURI(const std::string& uri, Scheme default_scheme); 87 static ProxyServer FromURI(std::string::const_iterator uri_begin, 88 std::string::const_iterator uri_end, 89 Scheme default_scheme); 90 91 // Format as a URI string. This does the reverse of FromURI. 92 std::string ToURI() const; 93 94 // Parses from a PAC string result. 95 // 96 // If <port> is omitted, it will be assumed as the default port for the 97 // chosen scheme (80 for "http", 1080 for "socks"). 98 // 99 // If parsing fails the instance will be set to invalid. 100 // 101 // Examples: 102 // "PROXY foopy:19" {scheme=HTTP, host="foopy", port=19} 103 // "DIRECT" {scheme=DIRECT} 104 // "SOCKS5 foopy" {scheme=SOCKS5, host="foopy", port=1080} 105 // "BLAH xxx:xx" INVALID 106 static ProxyServer FromPacString(const std::string& pac_string); 107 static ProxyServer FromPacString(std::string::const_iterator pac_string_begin, 108 std::string::const_iterator pac_string_end); 109 110 // Returns a ProxyServer representing DIRECT connections. Direct()111 static ProxyServer Direct() { 112 return ProxyServer(SCHEME_DIRECT, std::string(), -1); 113 } 114 115 #if defined(OS_MACOSX) 116 // Utility function to pull out a host/port pair from a dictionary and return 117 // it as a ProxyServer object. Pass in a dictionary that has a value for the 118 // host key and optionally a value for the port key. In the error condition 119 // where the host value is especially malformed, returns an invalid 120 // ProxyServer. 121 static ProxyServer FromDictionary(Scheme scheme, 122 CFDictionaryRef dict, 123 CFStringRef host_key, 124 CFStringRef port_key); 125 #endif 126 127 128 // Format as a PAC result entry. This does the reverse of FromPacString(). 129 std::string ToPacString() const; 130 131 // Returns the default port number for a proxy server with the specified 132 // scheme. Returns -1 if unknown. 133 static int GetDefaultPortForScheme(Scheme scheme); 134 135 bool operator==(const ProxyServer& other) const { 136 return scheme_ == other.scheme_ && 137 host_ == other.host_ && 138 port_ == other.port_; 139 } 140 141 private: 142 // Create a ProxyServer given a scheme, and host/port string. If parsing the 143 // host/port string fails, the returned instance will be invalid. 144 static ProxyServer FromSchemeHostAndPort( 145 Scheme scheme, 146 std::string::const_iterator host_and_port_begin, 147 std::string::const_iterator host_and_port_end); 148 149 Scheme scheme_; 150 std::string host_; 151 int port_; 152 }; 153 154 } // namespace net 155 156 #endif // NET_PROXY_PROXY_SERVER_H_ 157