WP_Http_Streams

Core class used to integrate PHP Streams as an HTTP transport.

Defined (1)

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

/wp-includes/class-wp-http-streams.php  
  1. class WP_Http_Streams { 
  2. /** 
  3. * Send a HTTP request to a URI using PHP Streams. 
  4. * @see WP_Http::request For default options descriptions. 
  5. * @since 2.7.0 
  6. * @since 3.7.0 Combined with the fsockopen transport and switched to stream_socket_client(). 
  7. * @access public 
  8. * @param string $url The request URL. 
  9. * @param string|array $args Optional. Override the defaults. 
  10. * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error 
  11. */ 
  12. public function request($url, $args = array()) { 
  13. $defaults = array( 
  14. 'method' => 'GET', 'timeout' => 5,  
  15. 'redirection' => 5, 'httpversion' => '1.0',  
  16. 'blocking' => true,  
  17. 'headers' => array(), 'body' => null, 'cookies' => array() 
  18. ); 
  19.  
  20. $r = wp_parse_args( $args, $defaults ); 
  21.  
  22. if ( isset( $r['headers']['User-Agent'] ) ) { 
  23. $r['user-agent'] = $r['headers']['User-Agent']; 
  24. unset( $r['headers']['User-Agent'] ); 
  25. } elseif ( isset( $r['headers']['user-agent'] ) ) { 
  26. $r['user-agent'] = $r['headers']['user-agent']; 
  27. unset( $r['headers']['user-agent'] ); 
  28.  
  29. // Construct Cookie: header if any cookies are set. 
  30. WP_Http::buildCookieHeader( $r ); 
  31.  
  32. $arrURL = parse_url($url); 
  33.  
  34. $connect_host = $arrURL['host']; 
  35.  
  36. $secure_transport = ( $arrURL['scheme'] == 'ssl' || $arrURL['scheme'] == 'https' ); 
  37. if ( ! isset( $arrURL['port'] ) ) { 
  38. if ( $arrURL['scheme'] == 'ssl' || $arrURL['scheme'] == 'https' ) { 
  39. $arrURL['port'] = 443; 
  40. $secure_transport = true; 
  41. } else { 
  42. $arrURL['port'] = 80; 
  43.  
  44. // Always pass a Path, defaulting to the root in cases such as http://example.com 
  45. if ( ! isset( $arrURL['path'] ) ) { 
  46. $arrURL['path'] = '/'; 
  47.  
  48. if ( isset( $r['headers']['Host'] ) || isset( $r['headers']['host'] ) ) { 
  49. if ( isset( $r['headers']['Host'] ) ) 
  50. $arrURL['host'] = $r['headers']['Host']; 
  51. else 
  52. $arrURL['host'] = $r['headers']['host']; 
  53. unset( $r['headers']['Host'], $r['headers']['host'] ); 
  54.  
  55. /** 
  56. * Certain versions of PHP have issues with 'localhost' and IPv6, It attempts to connect 
  57. * to ::1, which fails when the server is not set up for it. For compatibility, always 
  58. * connect to the IPv4 address. 
  59. */ 
  60. if ( 'localhost' == strtolower( $connect_host ) ) 
  61. $connect_host = '127.0.0.1'; 
  62.  
  63. $connect_host = $secure_transport ? 'ssl://' . $connect_host : 'tcp://' . $connect_host; 
  64.  
  65. $is_local = isset( $r['local'] ) && $r['local']; 
  66. $ssl_verify = isset( $r['sslverify'] ) && $r['sslverify']; 
  67. if ( $is_local ) { 
  68. /** 
  69. * Filters whether SSL should be verified for local requests. 
  70. * @since 2.8.0 
  71. * @param bool $ssl_verify Whether to verify the SSL connection. Default true. 
  72. */ 
  73. $ssl_verify = apply_filters( 'https_local_ssl_verify', $ssl_verify ); 
  74. } elseif ( ! $is_local ) { 
  75. /** 
  76. * Filters whether SSL should be verified for non-local requests. 
  77. * @since 2.8.0 
  78. * @param bool $ssl_verify Whether to verify the SSL connection. Default true. 
  79. */ 
  80. $ssl_verify = apply_filters( 'https_ssl_verify', $ssl_verify ); 
  81.  
  82. $proxy = new WP_HTTP_Proxy(); 
  83.  
  84. $context = stream_context_create( array( 
  85. 'ssl' => array( 
  86. 'verify_peer' => $ssl_verify,  
  87. //'CN_match' => $arrURL['host'], // This is handled by self::verify_ssl_certificate() 
  88. 'capture_peer_cert' => $ssl_verify,  
  89. 'SNI_enabled' => true,  
  90. 'cafile' => $r['sslcertificates'],  
  91. 'allow_self_signed' => ! $ssl_verify,  
  92. ) ); 
  93.  
  94. $timeout = (int) floor( $r['timeout'] ); 
  95. $utimeout = $timeout == $r['timeout'] ? 0 : 1000000 * $r['timeout'] % 1000000; 
  96. $connect_timeout = max( $timeout, 1 ); 
  97.  
  98. // Store error number. 
  99. $connection_error = null; 
  100.  
  101. // Store error string. 
  102. $connection_error_str = null; 
  103.  
  104. if ( !WP_DEBUG ) { 
  105. // In the event that the SSL connection fails, silence the many PHP Warnings. 
  106. if ( $secure_transport ) 
  107. $error_reporting = error_reporting(0); 
  108.  
  109. if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) 
  110. $handle = @stream_socket_client( 'tcp://' . $proxy->host() . ':' . $proxy->port(), $connection_error, $connection_error_str, $connect_timeout, STREAM_CLIENT_CONNECT, $context ); 
  111. else 
  112. $handle = @stream_socket_client( $connect_host . ':' . $arrURL['port'], $connection_error, $connection_error_str, $connect_timeout, STREAM_CLIENT_CONNECT, $context ); 
  113.  
  114. if ( $secure_transport ) 
  115. error_reporting( $error_reporting ); 
  116.  
  117. } else { 
  118. if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) 
  119. $handle = stream_socket_client( 'tcp://' . $proxy->host() . ':' . $proxy->port(), $connection_error, $connection_error_str, $connect_timeout, STREAM_CLIENT_CONNECT, $context ); 
  120. else 
  121. $handle = stream_socket_client( $connect_host . ':' . $arrURL['port'], $connection_error, $connection_error_str, $connect_timeout, STREAM_CLIENT_CONNECT, $context ); 
  122.  
  123. if ( false === $handle ) { 
  124. // SSL connection failed due to expired/invalid cert, or, OpenSSL configuration is broken. 
  125. if ( $secure_transport && 0 === $connection_error && '' === $connection_error_str ) 
  126. return new WP_Error( 'http_request_failed', __( 'The SSL certificate for the host could not be verified.' ) ); 
  127.  
  128. return new WP_Error('http_request_failed', $connection_error . ': ' . $connection_error_str ); 
  129.  
  130. // Verify that the SSL certificate is valid for this request. 
  131. if ( $secure_transport && $ssl_verify && ! $proxy->is_enabled() ) { 
  132. if ( ! self::verify_ssl_certificate( $handle, $arrURL['host'] ) ) 
  133. return new WP_Error( 'http_request_failed', __( 'The SSL certificate for the host could not be verified.' ) ); 
  134.  
  135. stream_set_timeout( $handle, $timeout, $utimeout ); 
  136.  
  137. if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) //Some proxies require full URL in this field. 
  138. $requestPath = $url; 
  139. else 
  140. $requestPath = $arrURL['path'] . ( isset($arrURL['query']) ? '?' . $arrURL['query'] : '' ); 
  141.  
  142. $strHeaders = strtoupper($r['method']) . ' ' . $requestPath . ' HTTP/' . $r['httpversion'] . "\r\n"; 
  143.  
  144. $include_port_in_host_header = ( 
  145. ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) || 
  146. ( 'http' == $arrURL['scheme'] && 80 != $arrURL['port'] ) || 
  147. ( 'https' == $arrURL['scheme'] && 443 != $arrURL['port'] ) 
  148. ); 
  149.  
  150. if ( $include_port_in_host_header ) { 
  151. $strHeaders .= 'Host: ' . $arrURL['host'] . ':' . $arrURL['port'] . "\r\n"; 
  152. } else { 
  153. $strHeaders .= 'Host: ' . $arrURL['host'] . "\r\n"; 
  154.  
  155. if ( isset($r['user-agent']) ) 
  156. $strHeaders .= 'User-agent: ' . $r['user-agent'] . "\r\n"; 
  157.  
  158. if ( is_array($r['headers']) ) { 
  159. foreach ( (array) $r['headers'] as $header => $headerValue ) 
  160. $strHeaders .= $header . ': ' . $headerValue . "\r\n"; 
  161. } else { 
  162. $strHeaders .= $r['headers']; 
  163.  
  164. if ( $proxy->use_authentication() ) 
  165. $strHeaders .= $proxy->authentication_header() . "\r\n"; 
  166.  
  167. $strHeaders .= "\r\n"; 
  168.  
  169. if ( ! is_null($r['body']) ) 
  170. $strHeaders .= $r['body']; 
  171.  
  172. fwrite($handle, $strHeaders); 
  173.  
  174. if ( ! $r['blocking'] ) { 
  175. stream_set_blocking( $handle, 0 ); 
  176. fclose( $handle ); 
  177. return array( 'headers' => array(), 'body' => '', 'response' => array('code' => false, 'message' => false), 'cookies' => array() ); 
  178.  
  179. $strResponse = ''; 
  180. $bodyStarted = false; 
  181. $keep_reading = true; 
  182. $block_size = 4096; 
  183. if ( isset( $r['limit_response_size'] ) ) 
  184. $block_size = min( $block_size, $r['limit_response_size'] ); 
  185.  
  186. // If streaming to a file setup the file handle. 
  187. if ( $r['stream'] ) { 
  188. if ( ! WP_DEBUG ) 
  189. $stream_handle = @fopen( $r['filename'], 'w+' ); 
  190. else 
  191. $stream_handle = fopen( $r['filename'], 'w+' ); 
  192. if ( ! $stream_handle ) 
  193. return new WP_Error( 'http_request_failed', sprintf( __( 'Could not open handle for fopen() to %s' ), $r['filename'] ) ); 
  194.  
  195. $bytes_written = 0; 
  196. while ( ! feof($handle) && $keep_reading ) { 
  197. $block = fread( $handle, $block_size ); 
  198. if ( ! $bodyStarted ) { 
  199. $strResponse .= $block; 
  200. if ( strpos( $strResponse, "\r\n\r\n" ) ) { 
  201. $process = WP_Http::processResponse( $strResponse ); 
  202. $bodyStarted = true; 
  203. $block = $process['body']; 
  204. unset( $strResponse ); 
  205. $process['body'] = ''; 
  206.  
  207. $this_block_size = strlen( $block ); 
  208.  
  209. if ( isset( $r['limit_response_size'] ) && ( $bytes_written + $this_block_size ) > $r['limit_response_size'] ) { 
  210. $this_block_size = ( $r['limit_response_size'] - $bytes_written ); 
  211. $block = substr( $block, 0, $this_block_size ); 
  212.  
  213. $bytes_written_to_file = fwrite( $stream_handle, $block ); 
  214.  
  215. if ( $bytes_written_to_file != $this_block_size ) { 
  216. fclose( $handle ); 
  217. fclose( $stream_handle ); 
  218. return new WP_Error( 'http_request_failed', __( 'Failed to write request to temporary file.' ) ); 
  219.  
  220. $bytes_written += $bytes_written_to_file; 
  221.  
  222. $keep_reading = !isset( $r['limit_response_size'] ) || $bytes_written < $r['limit_response_size']; 
  223.  
  224. fclose( $stream_handle ); 
  225.  
  226. } else { 
  227. $header_length = 0; 
  228. while ( ! feof( $handle ) && $keep_reading ) { 
  229. $block = fread( $handle, $block_size ); 
  230. $strResponse .= $block; 
  231. if ( ! $bodyStarted && strpos( $strResponse, "\r\n\r\n" ) ) { 
  232. $header_length = strpos( $strResponse, "\r\n\r\n" ) + 4; 
  233. $bodyStarted = true; 
  234. $keep_reading = ( ! $bodyStarted || !isset( $r['limit_response_size'] ) || strlen( $strResponse ) < ( $header_length + $r['limit_response_size'] ) ); 
  235.  
  236. $process = WP_Http::processResponse( $strResponse ); 
  237. unset( $strResponse ); 
  238.  
  239.  
  240. fclose( $handle ); 
  241.  
  242. $arrHeaders = WP_Http::processHeaders( $process['headers'], $url ); 
  243.  
  244. $response = array( 
  245. 'headers' => $arrHeaders['headers'],  
  246. // Not yet processed. 
  247. 'body' => null,  
  248. 'response' => $arrHeaders['response'],  
  249. 'cookies' => $arrHeaders['cookies'],  
  250. 'filename' => $r['filename'] 
  251. ); 
  252.  
  253. // Handle redirects. 
  254. if ( false !== ( $redirect_response = WP_Http::handle_redirects( $url, $r, $response ) ) ) 
  255. return $redirect_response; 
  256.  
  257. // If the body was chunk encoded, then decode it. 
  258. if ( ! empty( $process['body'] ) && isset( $arrHeaders['headers']['transfer-encoding'] ) && 'chunked' == $arrHeaders['headers']['transfer-encoding'] ) 
  259. $process['body'] = WP_Http::chunkTransferDecode($process['body']); 
  260.  
  261. if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($arrHeaders['headers']) ) 
  262. $process['body'] = WP_Http_Encoding::decompress( $process['body'] ); 
  263.  
  264. if ( isset( $r['limit_response_size'] ) && strlen( $process['body'] ) > $r['limit_response_size'] ) 
  265. $process['body'] = substr( $process['body'], 0, $r['limit_response_size'] ); 
  266.  
  267. $response['body'] = $process['body']; 
  268.  
  269. return $response; 
  270.  
  271. /** 
  272. * Verifies the received SSL certificate against its Common Names and subjectAltName fields. 
  273. * PHP's SSL verifications only verify that it's a valid Certificate, it doesn't verify if 
  274. * the certificate is valid for the hostname which was requested. 
  275. * This function verifies the requested hostname against certificate's subjectAltName field,  
  276. * if that is empty, or contains no DNS entries, a fallback to the Common Name field is used. 
  277. * IP Address support is included if the request is being made to an IP address. 
  278. * @since 3.7.0 
  279. * @static 
  280. * @param stream $stream The PHP Stream which the SSL request is being made over 
  281. * @param string $host The hostname being requested 
  282. * @return bool If the cerficiate presented in $stream is valid for $host 
  283. */ 
  284. public static function verify_ssl_certificate( $stream, $host ) { 
  285. $context_options = stream_context_get_options( $stream ); 
  286.  
  287. if ( empty( $context_options['ssl']['peer_certificate'] ) ) 
  288. return false; 
  289.  
  290. $cert = openssl_x509_parse( $context_options['ssl']['peer_certificate'] ); 
  291. if ( ! $cert ) 
  292. return false; 
  293.  
  294. /** 
  295. * If the request is being made to an IP address, we'll validate against IP fields 
  296. * in the cert (if they exist) 
  297. */ 
  298. $host_type = ( WP_Http::is_ip_address( $host ) ? 'ip' : 'dns' ); 
  299.  
  300. $certificate_hostnames = array(); 
  301. if ( ! empty( $cert['extensions']['subjectAltName'] ) ) { 
  302. $match_against = preg_split( '/, \s*/', $cert['extensions']['subjectAltName'] ); 
  303. foreach ( $match_against as $match ) { 
  304. list( $match_type, $match_host ) = explode( ':', $match ); 
  305. if ( $host_type == strtolower( trim( $match_type ) ) ) // IP: or DNS: 
  306. $certificate_hostnames[] = strtolower( trim( $match_host ) ); 
  307. } elseif ( !empty( $cert['subject']['CN'] ) ) { 
  308. // Only use the CN when the certificate includes no subjectAltName extension. 
  309. $certificate_hostnames[] = strtolower( $cert['subject']['CN'] ); 
  310.  
  311. // Exact hostname/IP matches. 
  312. if ( in_array( strtolower( $host ), $certificate_hostnames ) ) 
  313. return true; 
  314.  
  315. // IP's can't be wildcards, Stop processing. 
  316. if ( 'ip' == $host_type ) 
  317. return false; 
  318.  
  319. // Test to see if the domain is at least 2 deep for wildcard support. 
  320. if ( substr_count( $host, '.' ) < 2 ) 
  321. return false; 
  322.  
  323. // Wildcard subdomains certs (*.example.com) are valid for a.example.com but not a.b.example.com. 
  324. $wildcard_host = preg_replace( '/^[^.]+\./', '*.', $host ); 
  325.  
  326. return in_array( strtolower( $wildcard_host ), $certificate_hostnames ); 
  327.  
  328. /** 
  329. * Determines whether this class can be used for retrieving a URL. 
  330. * @static 
  331. * @access public 
  332. * @since 2.7.0 
  333. * @since 3.7.0 Combined with the fsockopen transport and switched to stream_socket_client(). 
  334. * @param array $args Optional. Array of request arguments. Default empty array. 
  335. * @return bool False means this class can not be used, true means it can. 
  336. */ 
  337. public static function test( $args = array() ) { 
  338. if ( ! function_exists( 'stream_socket_client' ) ) 
  339. return false; 
  340.  
  341. $is_ssl = isset( $args['ssl'] ) && $args['ssl']; 
  342.  
  343. if ( $is_ssl ) { 
  344. if ( ! extension_loaded( 'openssl' ) ) 
  345. return false; 
  346. if ( ! function_exists( 'openssl_x509_parse' ) ) 
  347. return false; 
  348.  
  349. /** 
  350. * Filters whether streams can be used as a transport for retrieving a URL. 
  351. * @since 2.7.0 
  352. * @param bool $use_class Whether the class can be used. Default true. 
  353. * @param array $args Request arguments. 
  354. */ 
  355. return apply_filters( 'use_streams_transport', true, $args );