1 // Copyright (c) 2010 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 #pragma once 8 9 #include "build/build_config.h" 10 11 #if defined(OS_MACOSX) 12 #include <CoreFoundation/CoreFoundation.h> 13 #endif 14 15 #include <string> 16 #include "net/base/host_port_pair.h" 17 18 namespace net { 19 20 // ProxyServer encodes the {type, host, port} of a proxy server. 21 // ProxyServer is immutable. 22 class 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 }; 35 36 // Default copy-constructor and assignment operator are OK! 37 38 // Constructs an invalid ProxyServer. ProxyServer()39 ProxyServer() : scheme_(SCHEME_INVALID) {} 40 41 ProxyServer(Scheme scheme, const HostPortPair& host_port_pair); 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 an HTTPS proxy. is_https()55 bool is_https() const { return scheme_ == SCHEME_HTTPS; } 56 57 // Returns true if this ProxyServer is a SOCKS proxy. is_socks()58 bool is_socks() const { 59 return scheme_ == SCHEME_SOCKS4 || scheme_ == SCHEME_SOCKS5; 60 } 61 62 const HostPortPair& host_port_pair() const; 63 64 // Parses from an input with format: 65 // [<scheme>"://"]<server>[":"<port>] 66 // 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"). 70 // 71 // If parsing fails the instance will be set to invalid. 72 // 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); 86 87 // Formats as a URI string. This does the reverse of FromURI. 88 std::string ToURI() const; 89 90 // Parses from a PAC string result. 91 // 92 // If <port> is omitted, it will be assumed as the default port for the 93 // chosen scheme (80 for "http", 1080 for "socks"). 94 // 95 // If parsing fails the instance will be set to invalid. 96 // 97 // Examples: 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); 106 107 // Returns a ProxyServer representing DIRECT connections. Direct()108 static ProxyServer Direct() { 109 return ProxyServer(SCHEME_DIRECT, HostPortPair()); 110 } 111 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 117 // ProxyServer. 118 static ProxyServer FromDictionary(Scheme scheme, 119 CFDictionaryRef dict, 120 CFStringRef host_key, 121 CFStringRef port_key); 122 #endif 123 124 // Formats as a PAC result entry. This does the reverse of FromPacString(). 125 std::string ToPacString() const; 126 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); 130 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); 136 137 bool operator==(const ProxyServer& other) const { 138 return scheme_ == other.scheme_ && 139 host_port_pair_.Equals(other.host_port_pair_); 140 } 141 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_; 147 } 148 149 private: 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( 153 Scheme scheme, 154 std::string::const_iterator host_and_port_begin, 155 std::string::const_iterator host_and_port_end); 156 157 Scheme scheme_; 158 HostPortPair host_port_pair_; 159 }; 160 161 typedef std::pair<HostPortPair, ProxyServer> HostPortProxyPair; 162 163 } // namespace net 164 165 #endif // NET_PROXY_PROXY_SERVER_H_ 166