Make WordPress Core

Changeset 17551


Ignore:
Timestamp:
03/24/2011 05:18:34 AM (13 years ago)
Author:
dd32
Message:

Allow for array('redirection' => 0) to bypass WP_Error on redirects being encountered; Allows HEAD requests WITH 'redirection' > 0 specified at call time to follow redirections; Standardises on return values from all transports to act the same based on the Unit Tests. Fixes #16855

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/class-http.php

    r17550 r17551  
    101101        );
    102102
     103       
     104        // Pre-parse for the HEAD checks.
     105        $args = wp_parse_args( $args );
     106
     107        // By default, Head requests do not cause redirections.
     108        if ( isset($args['method']) && 'HEAD' == $args['method'] )
     109            $defaults['redirection'] = 0;
     110
    103111        $r = wp_parse_args( $args, $defaults );
    104112        $r = apply_filters( 'http_request_args', $r, $url );
     113
     114        // Certain classes decrement this, store a copy of the original value for loop purposes.
     115        $r['_redirection'] = $r['redirection'];
    105116
    106117        // Allow plugins to short-circuit the request
     
    665676
    666677        // If location is found, then assume redirect and redirect to location.
    667         if ( 'HEAD' != $r['method'] && isset($arrHeaders['headers']['location']) ) {
     678        if ( isset($arrHeaders['headers']['location']) && 0 !== $r['_redirection'] ) {
    668679            if ( $r['redirection']-- > 0 ) {
    669680                return $this->request($arrHeaders['headers']['location'], $r);
     
    801812        }
    802813
    803         if ( 'HEAD' == $r['method'] ) // Disable redirects for HEAD requests
    804             $arrContext['http']['max_redirects'] = 1;
    805 
    806814        if ( ! empty($r['body'] ) )
    807815            $arrContext['http']['content'] = $r['body'];
     
    837845        else
    838846            $processedHeaders = WP_Http::processHeaders($meta['wrapper_data']);
     847
     848        // Streams does not provide an error code which we can use to see why the request stream stoped.
     849        // We can however test to see if a location header is present and return based on that.
     850        if ( isset($processedHeaders['headers']['location']) && 0 !== $args['_redirection'] )
     851            return new WP_Error('http_request_failed', __('Too many redirects.'));
    839852
    840853        if ( ! empty( $strResponse ) && isset( $processedHeaders['headers']['transfer-encoding'] ) && 'chunked' == $processedHeaders['headers']['transfer-encoding'] )
     
    950963        );
    951964
    952         if ( HTTP_METH_HEAD == $r['method'] )
    953             $options['redirect'] = 0; // Assumption: Docs seem to suggest that this means do not follow. Untested.
    954 
    955965        // The HTTP extensions offers really easy proxy support.
    956966        $proxy = new WP_HTTP_Proxy();
     
    11171127
    11181128        // The option doesn't work with safe mode or when open_basedir is set.
    1119         // Disable HEAD when making HEAD requests.
    1120         if ( !ini_get('safe_mode') && !ini_get('open_basedir') && 'HEAD' != $r['method'] )
     1129        if ( !ini_get('safe_mode') && !ini_get('open_basedir') && 0 !== $r['_redirection'] )
    11211130            curl_setopt( $handle, CURLOPT_FOLLOWLOCATION, true );
    11221131
     
    11771186
    11781187        // See #11305 - When running under safe mode, redirection is disabled above. Handle it manually.
    1179         if ( !empty($theHeaders['headers']['location']) && (ini_get('safe_mode') || ini_get('open_basedir')) ) {
     1188        if ( !empty($theHeaders['headers']['location']) && (ini_get('safe_mode') || ini_get('open_basedir')) && 0 !== $r['_redirection'] ) {
    11801189            if ( $r['redirection']-- > 0 ) {
    11811190                return $this->request($theHeaders['headers']['location'], $r);
Note: See TracChangeset for help on using the changeset viewer.