Make WordPress Core

Opened 2 years ago

Closed 22 months ago

#20222 closed defect (bug) (fixed)

No way to force local HTTP requests through proxy

Reported by: johnbillion Owned by: dd32
Milestone: 3.5 Priority: normal
Severity: normal Version:
Component: HTTP API Keywords: has-patch
Focuses: Cc:


The HTTP API will send requests through a proxy server if the WP_PROXY_HOST and WP_PROXY_POST constants are defined.

If the host of an HTTP request is the same as the current host (as is the case when a WP-cron request is spawned) then the request won't be sent through the proxy and there is no way to override this (see trunk/wp-includes/class-http.php@20171#L1346).

I use a proxy for logging and debugging HTTP requests and it's not possible for me to send local HTTP requests through the proxy.

Attachments (3)

20222.patch (1.0 KB) - added by johnbillion 2 years ago.
20222-2.patch (644 bytes) - added by kurtpayne 2 years ago.
Alternate patch
20222.diff (506 bytes) - added by dd32 22 months ago.

Download all attachments as: .zip

Change History (13)

johnbillion2 years ago

comment:1 johnbillion2 years ago

  • Keywords commit has-patch added

Patch. The HTTP API can now be told to use the proxy (if one's defined) for local requests by using the following code in your wp-config.php file:

define( 'WP_PROXY_LOCAL_HOSTS', true );

comment:2 johnbillion2 years ago

  • Keywords commit removed

Sorry, didn't mean to add commit tag.

comment:3 dd322 years ago

I think I'd prefer a filter on the function rather than more constants here

comment:4 johnbillion2 years ago

I chose to use another constant just so it was inline with the WP_PROXY_HOST and WP_PROXY_PORT constants. It's the sort of setting which will be done on a per-site basis and it's easier just to drop that constant into wp-config.php rather than using a filter.

comment:5 markjaquith2 years ago

Constants box us in. I wish we'd use fewer of them. You can't, for instance, proxy some local requests, but not all, just using a constant.

kurtpayne2 years ago

Alternate patch

comment:6 kurtpayne2 years ago

  • Cc kpayne@… added

20222-2.patch uses a callback.

You can determine per URL like so:

add_filter( 'proxy_local_urls_callback', function() {
	return 'check_url';
function check_url( $url ) {
	return true;  // your logic here

Or you can enable all proxying for all local URLs

add_filter( 'proxy_local_urls_callback', '__return_true' );

Default behavior is backwards compatible.

comment:7 follow-up: dd3222 months ago

@kurtpayne: Is there any reason you've re-invented the wheel so to say here?

Does that approach have any advatages over this patch? (which I'm about to add)

dd3222 months ago

comment:8 in reply to: ↑ 7 kurtpayne22 months ago

Replying to dd32:

@kurtpayne: Is there any reason you've re-invented the wheel so to say here?

No sir. Just lack of practice. :-)

Does that approach have any advatages over this patch? (which I'm about to add)

Nope, your patch looks good. Thanks.

comment:9 dd3222 months ago

  • Milestone changed from Awaiting Review to 3.5

No sir. Just lack of practice. :-)

Thanks for confirming, I thought that was the case, but was thrown by your use of apply_filters()

comment:10 dd3222 months ago

  • Owner set to dd32
  • Resolution set to fixed
  • Status changed from new to closed

In [21225]:

WP_HTTP: Add a filter to WP_HTTP_Proxy::send_through_proxy() to allow for plugins to modify which requests are sent to a proxy dynamically. Fixes #20222

Note: See TracTickets for help on using tickets.