← Back
Editing: class-guzzle-client-config.php
<?php namespace Smush\Core\Smush; /** * Builds the options for the Guzzle clients this plugin creates. * * The upload pool and the image download client both talk to the network, so the CA bundle, the * WordPress proxy constants and the TLS floor are resolved here rather than in each of them. * Guzzle_Client_Factory holds an instance of this and resolves the options for every request. */ class Guzzle_Client_Config { /** * How long a connection may sit idle and still be reused. Short enough to beat the idle * timeouts of common load balancers and NATs (60 seconds and up), long enough to keep reusing * connections between the requests of one image and between images in a bulk run. */ const CONNECTION_MAX_IDLE_SECONDS = 20; /** * Resolve the options for a Guzzle client. * * @param string $url URL the client will talk to. Used for proxy bypass checks and filters. * @param int $timeout Request timeout in seconds. * @param int $connect_timeout Connection timeout in seconds. * * @return array Guzzle options. */ public function get( $url, $timeout, $connect_timeout ) { $config = array( 'verify' => $this->get_ssl_verify( $url ), 'timeout' => $timeout, 'connect_timeout' => $connect_timeout, ); $minimum_tls_version = $this->get_minimum_tls_version(); if ( ! is_null( $minimum_tls_version ) ) { $config['crypto_method'] = $minimum_tls_version; } $curl_options = $this->get_curl_options(); if ( ! empty( $curl_options ) ) { $config['curl'] = $curl_options; } $proxy = $this->get_proxy( $url ); if ( '' !== $proxy ) { $config['proxy'] = $proxy; } /** * Filters the Guzzle options used for Smush API requests and image downloads. * * Lets support change `verify`, `proxy`, `timeout`, `connect_timeout` or `crypto_method` * for a site without patching files. * * @since 4.4.0 * * @param array $config Guzzle options. * @param string $url URL the client will talk to. */ return apply_filters( 'wp_smush_guzzle_client_config', $config, $url ); } /** * Resolve the Guzzle `verify` option the way WordPress core resolves it for HTTP requests. * * Mirrors WP_Http::request(): `http_request_args` may override `sslverify`/`sslcertificates`, * and `https_ssl_verify` has the final say. Verification is never disabled by default. * * @param string $url URL the client will talk to. * * @return bool|string Path to a CA bundle, true for the system store, or false when a filter * explicitly turned verification off. */ private function get_ssl_verify( $url ) { $defaults = array( 'sslverify' => true, 'sslcertificates' => $this->get_default_certificate_path(), ); $args = wp_parse_args( apply_filters( 'http_request_args', $defaults, $url ), $defaults ); $ssl_verify = $args['sslverify'] ? $args['sslcertificates'] : false; $ssl_verify = apply_filters( 'https_ssl_verify', $ssl_verify, $url ); if ( is_string( $ssl_verify ) ) { // Guzzle checks that a string `verify` exists and throws when it does not. return is_readable( $ssl_verify ) ? $ssl_verify : true; } return (bool) $ssl_verify; } /** * Path to the CA bundle WordPress ships, or true when it cannot be read. * * This is the bundle wp_remote_post() uses, which is why the sequential transport succeeds on * hosts whose system CA store is missing or stale. * * @return string|true */ private function get_default_certificate_path() { $ca_bundle = ABSPATH . WPINC . '/certificates/ca-bundle.crt'; return is_readable( $ca_bundle ) ? $ca_bundle : true; } /** * Lowest TLS version this host can negotiate, or null when that cannot be established. * * Guzzle throws at request time when `crypto_method` asks for TLS 1.2 on a stack that cannot * do it, so the floor is only raised where it is genuinely supported. Hosts too old to * negotiate TLS 1.2 keep their current behaviour rather than gaining a new failure. * * @return int|null STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT, or null to leave the choice to the stack. */ private function get_minimum_tls_version() { if ( ! defined( 'STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT' ) ) { return null; } $curl_version = function_exists( 'curl_version' ) ? curl_version() : null; if ( is_array( $curl_version ) ) { // The curl handler maps this option onto CURLOPT_SSLVERSION, which needs libcurl 7.34+. if ( ! defined( 'CURL_SSLVERSION_TLSv1_2' ) || version_compare( $curl_version['version'], '7.34.0', '<' ) ) { return null; } } return STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT; } /** * Raw cURL options for every client. * * Cached clients keep their connections between requests. A connection left idle can be * dropped silently by a load balancer or NAT on the way, and libcurl's default of 118 seconds * would reuse it anyway: the request then fails with "Connection died". Connections idle for * longer than CONNECTION_MAX_IDLE_SECONDS are replaced with fresh ones instead. * * Guzzle throws at request time when libcurl rejects an option, so it is only set where the * constant exists (PHP 8.2+) and the running libcurl supports it (7.65+). Other hosts keep * libcurl's default. * * @return array */ private function get_curl_options() { if ( ! defined( 'CURLOPT_MAXAGE_CONN' ) || ! function_exists( 'curl_version' ) ) { return array(); } $curl_version = curl_version(); if ( ! is_array( $curl_version ) || version_compare( $curl_version['version'], '7.65.0', '<' ) ) { return array(); } return array( CURLOPT_MAXAGE_CONN => self::CONNECTION_MAX_IDLE_SECONDS ); } /** * Resolve the Guzzle `proxy` option from the WordPress proxy constants. * * WordPress only ever configures an HTTP proxy (see WP_Http_Curl), so WP_HTTP_Proxy supplies * both the WP_PROXY_* handling and the WP_PROXY_BYPASS_HOSTS semantics. Guzzle expects the * credentials in the proxy URI, hence the encoding here. * * @param string $url URL the client will talk to. * * @return string Proxy URI, or an empty string when the request must not be proxied. */ private function get_proxy( $url ) { $proxy = new \WP_HTTP_Proxy(); if ( ! $proxy->is_enabled() ) { return ''; } // send_through_proxy() reads the request host, so skip it when the URL has none. if ( empty( wp_parse_url( $url, PHP_URL_HOST ) ) || ! $proxy->send_through_proxy( $url ) ) { return ''; } $credentials = ''; if ( $proxy->use_authentication() ) { $credentials = rawurlencode( $proxy->username() ) . ':' . rawurlencode( $proxy->password() ) . '@'; } return 'http://' . $credentials . $proxy->host() . ':' . $proxy->port(); } }
Save File
Cancel