proxy_string_util.h 4.3 KB

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