diff -r be944660c56a -r 3d72ae0968f4 wp/wp-includes/class-wp-http-streams.php --- a/wp/wp-includes/class-wp-http-streams.php Wed Sep 21 18:19:35 2022 +0200 +++ b/wp/wp-includes/class-wp-http-streams.php Tue Sep 27 16:37:53 2022 +0200 @@ -51,30 +51,30 @@ // Construct Cookie: header if any cookies are set. WP_Http::buildCookieHeader( $parsed_args ); - $arrURL = parse_url( $url ); + $parsed_url = parse_url( $url ); - $connect_host = $arrURL['host']; + $connect_host = $parsed_url['host']; - $secure_transport = ( 'ssl' === $arrURL['scheme'] || 'https' === $arrURL['scheme'] ); - if ( ! isset( $arrURL['port'] ) ) { - if ( 'ssl' === $arrURL['scheme'] || 'https' === $arrURL['scheme'] ) { - $arrURL['port'] = 443; - $secure_transport = true; + $secure_transport = ( 'ssl' === $parsed_url['scheme'] || 'https' === $parsed_url['scheme'] ); + if ( ! isset( $parsed_url['port'] ) ) { + if ( 'ssl' === $parsed_url['scheme'] || 'https' === $parsed_url['scheme'] ) { + $parsed_url['port'] = 443; + $secure_transport = true; } else { - $arrURL['port'] = 80; + $parsed_url['port'] = 80; } } // Always pass a path, defaulting to the root in cases such as http://example.com. - if ( ! isset( $arrURL['path'] ) ) { - $arrURL['path'] = '/'; + if ( ! isset( $parsed_url['path'] ) ) { + $parsed_url['path'] = '/'; } if ( isset( $parsed_args['headers']['Host'] ) || isset( $parsed_args['headers']['host'] ) ) { if ( isset( $parsed_args['headers']['Host'] ) ) { - $arrURL['host'] = $parsed_args['headers']['Host']; + $parsed_url['host'] = $parsed_args['headers']['Host']; } else { - $arrURL['host'] = $parsed_args['headers']['host']; + $parsed_url['host'] = $parsed_args['headers']['host']; } unset( $parsed_args['headers']['Host'], $parsed_args['headers']['host'] ); } @@ -92,6 +92,7 @@ $is_local = isset( $parsed_args['local'] ) && $parsed_args['local']; $ssl_verify = isset( $parsed_args['sslverify'] ) && $parsed_args['sslverify']; + if ( $is_local ) { /** * Filters whether SSL should be verified for local HTTP API requests. @@ -104,7 +105,7 @@ */ $ssl_verify = apply_filters( 'https_local_ssl_verify', $ssl_verify, $url ); } elseif ( ! $is_local ) { - /** This filter is documented in wp-includes/class-http.php */ + /** This filter is documented in wp-includes/class-wp-http.php */ $ssl_verify = apply_filters( 'https_ssl_verify', $ssl_verify, $url ); } @@ -114,7 +115,7 @@ array( 'ssl' => array( 'verify_peer' => $ssl_verify, - // 'CN_match' => $arrURL['host'], // This is handled by self::verify_ssl_certificate(). + // 'CN_match' => $parsed_url['host'], // This is handled by self::verify_ssl_certificate(). 'capture_peer_cert' => $ssl_verify, 'SNI_enabled' => true, 'cafile' => $parsed_args['sslcertificates'], @@ -141,10 +142,24 @@ if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged - $handle = @stream_socket_client( 'tcp://' . $proxy->host() . ':' . $proxy->port(), $connection_error, $connection_error_str, $connect_timeout, STREAM_CLIENT_CONNECT, $context ); + $handle = @stream_socket_client( + 'tcp://' . $proxy->host() . ':' . $proxy->port(), + $connection_error, + $connection_error_str, + $connect_timeout, + STREAM_CLIENT_CONNECT, + $context + ); } else { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged - $handle = @stream_socket_client( $connect_host . ':' . $arrURL['port'], $connection_error, $connection_error_str, $connect_timeout, STREAM_CLIENT_CONNECT, $context ); + $handle = @stream_socket_client( + $connect_host . ':' . $parsed_url['port'], + $connection_error, + $connection_error_str, + $connect_timeout, + STREAM_CLIENT_CONNECT, + $context + ); } if ( $secure_transport ) { @@ -152,9 +167,23 @@ } } else { if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) { - $handle = stream_socket_client( 'tcp://' . $proxy->host() . ':' . $proxy->port(), $connection_error, $connection_error_str, $connect_timeout, STREAM_CLIENT_CONNECT, $context ); + $handle = stream_socket_client( + 'tcp://' . $proxy->host() . ':' . $proxy->port(), + $connection_error, + $connection_error_str, + $connect_timeout, + STREAM_CLIENT_CONNECT, + $context + ); } else { - $handle = stream_socket_client( $connect_host . ':' . $arrURL['port'], $connection_error, $connection_error_str, $connect_timeout, STREAM_CLIENT_CONNECT, $context ); + $handle = stream_socket_client( + $connect_host . ':' . $parsed_url['port'], + $connection_error, + $connection_error_str, + $connect_timeout, + STREAM_CLIENT_CONNECT, + $context + ); } } @@ -169,7 +198,7 @@ // Verify that the SSL certificate is valid for this request. if ( $secure_transport && $ssl_verify && ! $proxy->is_enabled() ) { - if ( ! self::verify_ssl_certificate( $handle, $arrURL['host'] ) ) { + if ( ! self::verify_ssl_certificate( $handle, $parsed_url['host'] ) ) { return new WP_Error( 'http_request_failed', __( 'The SSL certificate for the host could not be verified.' ) ); } } @@ -177,48 +206,48 @@ stream_set_timeout( $handle, $timeout, $utimeout ); if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) { // Some proxies require full URL in this field. - $requestPath = $url; + $request_path = $url; } else { - $requestPath = $arrURL['path'] . ( isset( $arrURL['query'] ) ? '?' . $arrURL['query'] : '' ); + $request_path = $parsed_url['path'] . ( isset( $parsed_url['query'] ) ? '?' . $parsed_url['query'] : '' ); } - $strHeaders = strtoupper( $parsed_args['method'] ) . ' ' . $requestPath . ' HTTP/' . $parsed_args['httpversion'] . "\r\n"; + $headers = strtoupper( $parsed_args['method'] ) . ' ' . $request_path . ' HTTP/' . $parsed_args['httpversion'] . "\r\n"; $include_port_in_host_header = ( - ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) || - ( 'http' === $arrURL['scheme'] && 80 != $arrURL['port'] ) || - ( 'https' === $arrURL['scheme'] && 443 != $arrURL['port'] ) + ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) + || ( 'http' === $parsed_url['scheme'] && 80 != $parsed_url['port'] ) + || ( 'https' === $parsed_url['scheme'] && 443 != $parsed_url['port'] ) ); if ( $include_port_in_host_header ) { - $strHeaders .= 'Host: ' . $arrURL['host'] . ':' . $arrURL['port'] . "\r\n"; + $headers .= 'Host: ' . $parsed_url['host'] . ':' . $parsed_url['port'] . "\r\n"; } else { - $strHeaders .= 'Host: ' . $arrURL['host'] . "\r\n"; + $headers .= 'Host: ' . $parsed_url['host'] . "\r\n"; } if ( isset( $parsed_args['user-agent'] ) ) { - $strHeaders .= 'User-agent: ' . $parsed_args['user-agent'] . "\r\n"; + $headers .= 'User-agent: ' . $parsed_args['user-agent'] . "\r\n"; } if ( is_array( $parsed_args['headers'] ) ) { - foreach ( (array) $parsed_args['headers'] as $header => $headerValue ) { - $strHeaders .= $header . ': ' . $headerValue . "\r\n"; + foreach ( (array) $parsed_args['headers'] as $header => $header_value ) { + $headers .= $header . ': ' . $header_value . "\r\n"; } } else { - $strHeaders .= $parsed_args['headers']; + $headers .= $parsed_args['headers']; } if ( $proxy->use_authentication() ) { - $strHeaders .= $proxy->authentication_header() . "\r\n"; + $headers .= $proxy->authentication_header() . "\r\n"; } - $strHeaders .= "\r\n"; + $headers .= "\r\n"; if ( ! is_null( $parsed_args['body'] ) ) { - $strHeaders .= $parsed_args['body']; + $headers .= $parsed_args['body']; } - fwrite( $handle, $strHeaders ); + fwrite( $handle, $headers ); if ( ! $parsed_args['blocking'] ) { stream_set_blocking( $handle, 0 ); @@ -234,10 +263,11 @@ ); } - $strResponse = ''; - $bodyStarted = false; + $response = ''; + $body_started = false; $keep_reading = true; $block_size = 4096; + if ( isset( $parsed_args['limit_response_size'] ) ) { $block_size = min( $block_size, $parsed_args['limit_response_size'] ); } @@ -249,6 +279,7 @@ } else { $stream_handle = fopen( $parsed_args['filename'], 'w+' ); } + if ( ! $stream_handle ) { return new WP_Error( 'http_request_failed', @@ -262,22 +293,25 @@ } $bytes_written = 0; + while ( ! feof( $handle ) && $keep_reading ) { $block = fread( $handle, $block_size ); - if ( ! $bodyStarted ) { - $strResponse .= $block; - if ( strpos( $strResponse, "\r\n\r\n" ) ) { - $process = WP_Http::processResponse( $strResponse ); - $bodyStarted = true; - $block = $process['body']; - unset( $strResponse ); - $process['body'] = ''; + if ( ! $body_started ) { + $response .= $block; + if ( strpos( $response, "\r\n\r\n" ) ) { + $processed_response = WP_Http::processResponse( $response ); + $body_started = true; + $block = $processed_response['body']; + unset( $response ); + $processed_response['body'] = ''; } } $this_block_size = strlen( $block ); - if ( isset( $parsed_args['limit_response_size'] ) && ( $bytes_written + $this_block_size ) > $parsed_args['limit_response_size'] ) { + if ( isset( $parsed_args['limit_response_size'] ) + && ( $bytes_written + $this_block_size ) > $parsed_args['limit_response_size'] + ) { $this_block_size = ( $parsed_args['limit_response_size'] - $bytes_written ); $block = substr( $block, 0, $this_block_size ); } @@ -292,38 +326,48 @@ $bytes_written += $bytes_written_to_file; - $keep_reading = ! isset( $parsed_args['limit_response_size'] ) || $bytes_written < $parsed_args['limit_response_size']; + $keep_reading = ( + ! isset( $parsed_args['limit_response_size'] ) + || $bytes_written < $parsed_args['limit_response_size'] + ); } fclose( $stream_handle ); } else { $header_length = 0; + while ( ! feof( $handle ) && $keep_reading ) { - $block = fread( $handle, $block_size ); - $strResponse .= $block; - if ( ! $bodyStarted && strpos( $strResponse, "\r\n\r\n" ) ) { - $header_length = strpos( $strResponse, "\r\n\r\n" ) + 4; - $bodyStarted = true; + $block = fread( $handle, $block_size ); + $response .= $block; + + if ( ! $body_started && strpos( $response, "\r\n\r\n" ) ) { + $header_length = strpos( $response, "\r\n\r\n" ) + 4; + $body_started = true; } - $keep_reading = ( ! $bodyStarted || ! isset( $parsed_args['limit_response_size'] ) || strlen( $strResponse ) < ( $header_length + $parsed_args['limit_response_size'] ) ); + + $keep_reading = ( + ! $body_started + || ! isset( $parsed_args['limit_response_size'] ) + || strlen( $response ) < ( $header_length + $parsed_args['limit_response_size'] ) + ); } - $process = WP_Http::processResponse( $strResponse ); - unset( $strResponse ); + $processed_response = WP_Http::processResponse( $response ); + unset( $response ); } fclose( $handle ); - $arrHeaders = WP_Http::processHeaders( $process['headers'], $url ); + $processed_headers = WP_Http::processHeaders( $processed_response['headers'], $url ); $response = array( - 'headers' => $arrHeaders['headers'], + 'headers' => $processed_headers['headers'], // Not yet processed. 'body' => null, - 'response' => $arrHeaders['response'], - 'cookies' => $arrHeaders['cookies'], + 'response' => $processed_headers['response'], + 'cookies' => $processed_headers['cookies'], 'filename' => $parsed_args['filename'], ); @@ -334,21 +378,26 @@ } // If the body was chunk encoded, then decode it. - if ( ! empty( $process['body'] ) && isset( $arrHeaders['headers']['transfer-encoding'] ) - && 'chunked' === $arrHeaders['headers']['transfer-encoding'] + if ( ! empty( $processed_response['body'] ) + && isset( $processed_headers['headers']['transfer-encoding'] ) + && 'chunked' === $processed_headers['headers']['transfer-encoding'] ) { - $process['body'] = WP_Http::chunkTransferDecode( $process['body'] ); + $processed_response['body'] = WP_Http::chunkTransferDecode( $processed_response['body'] ); } - if ( true === $parsed_args['decompress'] && true === WP_Http_Encoding::should_decode( $arrHeaders['headers'] ) ) { - $process['body'] = WP_Http_Encoding::decompress( $process['body'] ); + if ( true === $parsed_args['decompress'] + && true === WP_Http_Encoding::should_decode( $processed_headers['headers'] ) + ) { + $processed_response['body'] = WP_Http_Encoding::decompress( $processed_response['body'] ); } - if ( isset( $parsed_args['limit_response_size'] ) && strlen( $process['body'] ) > $parsed_args['limit_response_size'] ) { - $process['body'] = substr( $process['body'], 0, $parsed_args['limit_response_size'] ); + if ( isset( $parsed_args['limit_response_size'] ) + && strlen( $processed_response['body'] ) > $parsed_args['limit_response_size'] + ) { + $processed_response['body'] = substr( $processed_response['body'], 0, $parsed_args['limit_response_size'] ); } - $response['body'] = $process['body']; + $response['body'] = $processed_response['body']; return $response; } @@ -365,9 +414,9 @@ * * @since 3.7.0 * - * @param stream $stream The PHP Stream which the SSL request is being made over - * @param string $host The hostname being requested - * @return bool If the cerficiate presented in $stream is valid for $host + * @param resource $stream The PHP Stream which the SSL request is being made over + * @param string $host The hostname being requested + * @return bool If the certificate presented in $stream is valid for $host */ public static function verify_ssl_certificate( $stream, $host ) { $context_options = stream_context_get_options( $stream ); @@ -470,6 +519,6 @@ * @since 2.7.0 * @deprecated 3.7.0 Please use WP_HTTP::request() directly */ -class WP_HTTP_Fsockopen extends WP_HTTP_Streams { +class WP_HTTP_Fsockopen extends WP_Http_Streams { // For backward compatibility for users who are using the class directly. }