Make WordPress Core

Opened 8 years ago

Last modified 4 months ago

#40153 new enhancement

Filter for wp_safe_remote_get in download_url

Reported by: realloc's profile realloc Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.8
Component: General Keywords: has-patch
Focuses: Cc:

Description

I had to deal with images on a server that has basic authentication active. There is no filter in download_url that would give me the chance to add the header authentication that I needed in this case.

Attachments (2)

40153.diff (703 bytes) - added by realloc 8 years ago.
40153.2.diff (2.1 KB) - added by donmhico 5 years ago.

Download all attachments as: .zip

Change History (4)

@realloc
8 years ago

@donmhico
5 years ago

#1 @donmhico
5 years ago

  • Keywords has-patch added

I added a new patch for this issue.

Here are some key points about the new patch I attached 40153.2.diff.

  1. Added docs regarding the new filter named download_url_remote_get_args which was introduced.
  2. Unlike the first patch, I think it's best to not allow the new filter to override $args['timeout'], $args['stream'], and $args['filename'].

$args['timeout'] can be debatable. But I think it's enough that download_url() itself accepts the $timeout params.
$args['filename'] and $args['stream'] unless there's a good reason, these 2 should also be left untouched IMHO to prevent any unexpected issues to arise with how download_url() currently works.

#2 @threadi
4 months ago

I also stumbled across this today because I want to access an Auth Basic protected URL. My solution:

add_filter( 'http_request_args', array( $this, 'set_download_url_header' ) );
$file = download_url( $this->get_url() );
remove_filter( 'http_request_args', array( $this, 'set_download_url_header' ) );

In set_download_url_header() I add the Auth Basic header, which then works without any problems:

public function set_download_url_header( array $parsed_args ): array {
 return array_merge( $parsed_args, array( 'header' => array( 'Authorization' => 'Basic ...' ) ) );
}
Note: See TracTickets for help on using tickets.