1 // Copyright 2021 The Chromium Authors 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_BASE_PROXY_STRING_UTIL_H_ 6 #define NET_BASE_PROXY_STRING_UTIL_H_ 7 8 #include <string> 9 10 #include "base/strings/string_piece.h" 11 #include "build/build_config.h" 12 #include "net/base/net_export.h" 13 #include "net/base/proxy_server.h" 14 15 #if BUILDFLAG(IS_APPLE) 16 #include <CoreFoundation/CoreFoundation.h> 17 #endif // BUILDFLAG(IS_APPLE) 18 19 namespace net { 20 21 // Converts a PAC result element (commonly called a PAC string) to/from a 22 // ProxyServer. Note that this only deals with a single proxy server element 23 // separated out from the complete semicolon-delimited PAC result string. 24 // 25 // PAC result elements have the format: 26 // <scheme>" "<host>[":"<port>] 27 // 28 // Where <scheme> may be one of (case-insensitive): 29 // "DIRECT" 30 // "PROXY" 31 // "HTTPS" 32 // "SOCKS4" 33 // "SOCKS5" 34 // "SOCKS" (canonicalizes to "SOCKS4") 35 // "QUIC" 36 // 37 // If <port> is omitted, it will be assumed as the default port for the 38 // chosen scheme (via ProxyServer::GetDefaultPortForScheme()). 39 // 40 // If parsing fails the returned proxy will have scheme 41 // `ProxyServer::SCHEME_INVALID`. 42 // 43 // Examples: 44 // "PROXY foopy:19" {scheme=HTTP, host="foopy", port=19} 45 // "DIRECT" {scheme=DIRECT} 46 // "SOCKS5 foopy" {scheme=SOCKS5, host="foopy", port=1080} 47 // "HTTPS foopy:123" {scheme=HTTPS, host="foopy", port=123} 48 // "QUIC foopy:123" {scheme=QUIC, host="foopy", port=123} 49 // "BLAH xxx:xx" INVALID 50 NET_EXPORT ProxyServer 51 PacResultElementToProxyServer(base::StringPiece pac_result_element); 52 NET_EXPORT std::string ProxyServerToPacResultElement( 53 const ProxyServer& proxy_server); 54 55 // Converts a non-standard URI string to/from a ProxyServer. 56 // 57 // The non-standard URI strings have the format: 58 // [<scheme>"://"]<server>[":"<port>] 59 // 60 // Where <scheme> may be one of: 61 // "http" 62 // "socks4" 63 // "socks5 64 // "socks" (equivalent to "socks5") 65 // "direct" 66 // "https" 67 // "quic" 68 // 69 // Both <scheme> and <port> are optional. If <scheme> is omitted, it will be 70 // assumed as |default_scheme|. If <port> is omitted, it will be assumed as 71 // the default port for the chosen scheme (via 72 // ProxyServer::GetDefaultPortForScheme()). 73 // 74 // If parsing fails the returned proxy will have scheme 75 // `ProxyServer::SCHEME_INVALID`. 76 // 77 // Examples (for `default_pac_scheme` = `kHttp` ): 78 // "foopy" {scheme=HTTP, host="foopy", port=80} 79 // "socks://foopy" {scheme=SOCKS5, host="foopy", port=1080} 80 // "socks4://foopy" {scheme=SOCKS4, host="foopy", port=1080} 81 // "socks5://foopy" {scheme=SOCKS5, host="foopy", port=1080} 82 // "http://foopy:17" {scheme=HTTP, host="foopy", port=17} 83 // "https://foopy:17" {scheme=HTTPS, host="foopy", port=17} 84 // "quic://foopy:17" {scheme=QUIC, host="foopy", port=17} 85 // "direct://" {scheme=DIRECT} 86 // "foopy:X" INVALID -- bad port. 87 NET_EXPORT ProxyServer 88 ProxyUriToProxyServer(base::StringPiece uri, 89 ProxyServer::Scheme default_scheme); 90 NET_EXPORT std::string ProxyServerToProxyUri(const ProxyServer& proxy_server); 91 92 // Parses the proxy scheme from the non-standard URI scheme string 93 // representation used in `ProxyUriToProxyServer()` and 94 // `ProxyServerToProxyUri()`. If no type could be matched, returns 95 // SCHEME_INVALID. 96 NET_EXPORT ProxyServer::Scheme GetSchemeFromUriScheme(base::StringPiece scheme); 97 98 #if BUILDFLAG(IS_APPLE) 99 // Utility function to pull out a host/port pair from a dictionary and return 100 // it as a ProxyServer object. Pass in a dictionary that has a value for the 101 // host key and optionally a value for the port key. In the error condition 102 // where the host value is especially malformed, returns an invalid 103 // ProxyServer. 104 // 105 // TODO(ericorth@chromium.org): Dictionary isn't really a string representation, 106 // so this doesn't really belong in this file. Consider moving this logic to 107 // somewhere alongside the Apple-specific proxy-resolution code. 108 ProxyServer ProxyDictionaryToProxyServer(ProxyServer::Scheme scheme, 109 CFDictionaryRef dict, 110 CFStringRef host_key, 111 CFStringRef port_key); 112 #endif // BUILDFLAG(IS_APPLE) 113 114 } // namespace net 115 116 #endif // NET_BASE_PROXY_STRING_UTIL_H_ 117