WP_HTTP_Proxy

Core class used to implement HTTP API proxy support.

Defined (1)

The class is defined in the following location(s).

/wp-includes/class-wp-http-proxy.php  
  1. class WP_HTTP_Proxy { 
  2.  
  3. /** 
  4. * Whether proxy connection should be used. 
  5. * @since 2.8.0 
  6. * @use WP_PROXY_HOST 
  7. * @use WP_PROXY_PORT 
  8. * @return bool 
  9. */ 
  10. public function is_enabled() { 
  11. return defined('WP_PROXY_HOST') && defined('WP_PROXY_PORT'); 
  12.  
  13. /** 
  14. * Whether authentication should be used. 
  15. * @since 2.8.0 
  16. * @use WP_PROXY_USERNAME 
  17. * @use WP_PROXY_PASSWORD 
  18. * @return bool 
  19. */ 
  20. public function use_authentication() { 
  21. return defined('WP_PROXY_USERNAME') && defined('WP_PROXY_PASSWORD'); 
  22.  
  23. /** 
  24. * Retrieve the host for the proxy server. 
  25. * @since 2.8.0 
  26. * @return string 
  27. */ 
  28. public function host() { 
  29. if ( defined('WP_PROXY_HOST') ) 
  30. return WP_PROXY_HOST; 
  31.  
  32. return ''; 
  33.  
  34. /** 
  35. * Retrieve the port for the proxy server. 
  36. * @since 2.8.0 
  37. * @return string 
  38. */ 
  39. public function port() { 
  40. if ( defined('WP_PROXY_PORT') ) 
  41. return WP_PROXY_PORT; 
  42.  
  43. return ''; 
  44.  
  45. /** 
  46. * Retrieve the username for proxy authentication. 
  47. * @since 2.8.0 
  48. * @return string 
  49. */ 
  50. public function username() { 
  51. if ( defined('WP_PROXY_USERNAME') ) 
  52. return WP_PROXY_USERNAME; 
  53.  
  54. return ''; 
  55.  
  56. /** 
  57. * Retrieve the password for proxy authentication. 
  58. * @since 2.8.0 
  59. * @return string 
  60. */ 
  61. public function password() { 
  62. if ( defined('WP_PROXY_PASSWORD') ) 
  63. return WP_PROXY_PASSWORD; 
  64.  
  65. return ''; 
  66.  
  67. /** 
  68. * Retrieve authentication string for proxy authentication. 
  69. * @since 2.8.0 
  70. * @return string 
  71. */ 
  72. public function authentication() { 
  73. return $this->username() . ':' . $this->password(); 
  74.  
  75. /** 
  76. * Retrieve header string for proxy authentication. 
  77. * @since 2.8.0 
  78. * @return string 
  79. */ 
  80. public function authentication_header() { 
  81. return 'Proxy-Authorization: Basic ' . base64_encode( $this->authentication() ); 
  82.  
  83. /** 
  84. * Whether URL should be sent through the proxy server. 
  85. * We want to keep localhost and the site URL from being sent through the proxy server, because 
  86. * some proxies can not handle this. We also have the constant available for defining other 
  87. * hosts that won't be sent through the proxy. 
  88. * @since 2.8.0 
  89. * @staticvar array|null $bypass_hosts 
  90. * @staticvar array $wildcard_regex 
  91. * @param string $uri URI to check. 
  92. * @return bool True, to send through the proxy and false if, the proxy should not be used. 
  93. */ 
  94. public function send_through_proxy( $uri ) { 
  95. /** 
  96. * parse_url() only handles http, https type URLs, and will emit E_WARNING on failure. 
  97. * This will be displayed on sites, which is not reasonable. 
  98. */ 
  99. $check = @parse_url($uri); 
  100.  
  101. // Malformed URL, can not process, but this could mean ssl, so let through anyway. 
  102. if ( $check === false ) 
  103. return true; 
  104.  
  105. $home = parse_url( get_option('siteurl') ); 
  106.  
  107. /** 
  108. * Filters whether to preempt sending the request through the proxy server. 
  109. * Returning false will bypass the proxy; returning true will send 
  110. * the request through the proxy. Returning null bypasses the filter. 
  111. * @since 3.5.0 
  112. * @param null $override Whether to override the request result. Default null. 
  113. * @param string $uri URL to check. 
  114. * @param array $check Associative array result of parsing the URI. 
  115. * @param array $home Associative array result of parsing the site URL. 
  116. */ 
  117. $result = apply_filters( 'pre_http_send_through_proxy', null, $uri, $check, $home ); 
  118. if ( ! is_null( $result ) ) 
  119. return $result; 
  120.  
  121. if ( 'localhost' == $check['host'] || ( isset( $home['host'] ) && $home['host'] == $check['host'] ) ) 
  122. return false; 
  123.  
  124. if ( !defined('WP_PROXY_BYPASS_HOSTS') ) 
  125. return true; 
  126.  
  127. static $bypass_hosts = null; 
  128. static $wildcard_regex = array(); 
  129. if ( null === $bypass_hosts ) { 
  130. $bypass_hosts = preg_split('|, \s*|', WP_PROXY_BYPASS_HOSTS); 
  131.  
  132. if ( false !== strpos(WP_PROXY_BYPASS_HOSTS, '*') ) { 
  133. $wildcard_regex = array(); 
  134. foreach ( $bypass_hosts as $host ) 
  135. $wildcard_regex[] = str_replace( '\*', '.+', preg_quote( $host, '/' ) ); 
  136. $wildcard_regex = '/^(' . implode('|', $wildcard_regex) . ')$/i'; 
  137.  
  138. if ( !empty($wildcard_regex) ) 
  139. return !preg_match($wildcard_regex, $check['host']); 
  140. else 
  141. return !in_array( $check['host'], $bypass_hosts );