wppaste
WordPress

WP_HTTP_Proxy::send_through_proxy( string $uri ): bool

Since
2.8.0
Source
wp-includes/class-wp-http-proxy.php:171
Determines whether the request should be sent through a proxy.

Description

We want to keep localhost and the site URL from being sent through the proxy, because some proxies can not handle this. We also have the constant available for defining other hosts that won't be sent through the proxy.

Compatibility

WordPress
since 2.8.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$uristring
URL of the request.

Return value

bool
Whether to send the request through the proxy.

Performance profile

How much work a call to WP_HTTP_Proxy::send_through_proxy() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
8–63

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 80.

Plugin surface
1 hook

Third-party callbacks on 'pre_http_send_through_proxy' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • optionoption read or writeget_option()called directly
  • hookthird-party callbacksapply_filters()called directly
  • regexregular expression over the whole inputpreg_split()called directly
  • cacheobject cachewp_cache_get()one call below WP_HTTP_Proxy::send_through_proxy()
  • serializeserialisationmaybe_unserialize()one call below WP_HTTP_Proxy::send_through_proxy()

Further down the call graph this can also reach query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 3 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_HTTP_Proxy::send_through_proxy() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$check === false8parse_url()
$check !== false25–46parse_url(), get_option(), parse_url(), apply_filters()
$check !== false && $result === null && defined('WP_PROXY_BYPASS_HOSTS') && $bypass_hosts === null50–63parse_url(), get_option(), parse_url(), apply_filters(), preg_split()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev808–6311
8.5808–6311
8.4808–631115 fewer instructions than PHP 8.3
8.3958–7211
8.2958–7211
8.1958–7211
7.4958–7211

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while WP_HTTP_Proxy::send_through_proxy() runs, in this order:

  1. apply_filters( pre_http_send_through_proxy )filterline 194 (+23 into the body)

    Filters whether to preempt sending the request through the proxy.

Uses · 3

  • get_option()Retrieves an option value based on an option name.
  • apply_filters()Calls the callback functions that have been added to a filter hook.
  • str_contains()Polyfill for `str_contains()` function added in PHP 8.0.

Source code

	public function send_through_proxy( $uri ) {		$check = parse_url( $uri ); 		// Malformed URL, can not process, but this could mean ssl, so let through anyway.		if ( false === $check ) {			return true;		} 		$home = parse_url( get_option( 'siteurl' ) ); 		/**		 * Filters whether to preempt sending the request through the proxy.		 *		 * Returning false will bypass the proxy; returning true will send		 * the request through the proxy. Returning null bypasses the filter.		 *		 * @since 3.5.0		 *		 * @param bool|null $override Whether to send the request through the proxy. Default null.		 * @param string    $uri      URL of the request.		 * @param array     $check    Associative array result of parsing the request URL with `parse_url()`.		 * @param array     $home     Associative array result of parsing the site URL with `parse_url()`.		 */		$result = apply_filters( 'pre_http_send_through_proxy', null, $uri, $check, $home );		if ( ! is_null( $result ) ) {			return $result;		} 		if ( 'localhost' === $check['host'] || ( isset( $home['host'] ) && $home['host'] === $check['host'] ) ) {			return false;		} 		if ( ! defined( 'WP_PROXY_BYPASS_HOSTS' ) ) {			return true;		} 		static $bypass_hosts   = null;		static $wildcard_regex = array();		if ( null === $bypass_hosts ) {			$bypass_hosts = preg_split( '|,\s*|', WP_PROXY_BYPASS_HOSTS ); 			if ( str_contains( WP_PROXY_BYPASS_HOSTS, '*' ) ) {				$wildcard_regex = array();				foreach ( $bypass_hosts as $host ) {					$wildcard_regex[] = str_replace( '\*', '.+', preg_quote( $host, '/' ) );				}				$wildcard_regex = '/^(' . implode( '|', $wildcard_regex ) . ')$/i';			}		} 		if ( ! empty( $wildcard_regex ) ) {			return ! preg_match( $wildcard_regex, $check['host'] );		} else {			return ! in_array( $check['host'], $bypass_hosts, true );		}	}

Changelog

Introduced in 2.8.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 tag, from src/wp-includes/class-wp-http-proxy.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.