Make WordPress Core

Ticket #7770: 7770.2.diff

File 7770.2.diff, 5.7 KB (added by DD32, 17 years ago)
  • wp-includes/http.php

     
    530530                if ( (int) $arrHeaders['response']['code'] >= 400 && (int) $arrHeaders['response']['code'] < 500 )
    531531                        return new WP_Error('http_request_failed', $arrHeaders['response']['code'] . ': ' . $arrHeaders['response']['message']);
    532532
    533                 // If the body was chunk encoded, then decode it.
    534                 if ( ! empty( $process['body'] ) && isset( $arrHeaders['headers']['transfer-encoding'] ) && 'chunked' == $arrHeaders['headers']['transfer-encoding'] )
    535                         $process['body'] = WP_Http::chunkTransferDecode($process['body']);
    536 
    537533                // If location is found, then assume redirect and redirect to location.
    538534                if ( isset($arrHeaders['headers']['location']) ) {
    539535                        if ( $r['redirection']-- > 0 ) {
    540536                                return $this->request($arrHeaders['headers']['location'], $r);
     537                        } else {
     538                                return new WP_Error('http_request_failed', __('Too many redirects.'));
    541539                        }
    542                         else
    543                                 return new WP_Error('http_request_failed', __('Too many redirects.'));
    544540                }
    545541
     542                // If the body was chunk encoded, then decode it.
     543                if ( ! empty( $process['body'] ) && isset( $arrHeaders['headers']['transfer-encoding'] ) && 'chunked' == $arrHeaders['headers']['transfer-encoding'] )
     544                        $process['body'] = WP_Http::chunkTransferDecode($process['body']);
     545
    546546                return array('headers' => $arrHeaders['headers'], 'body' => $process['body'], 'response' => $arrHeaders['response']);
    547547        }
    548548
     
    646646
    647647                $processedHeaders = WP_Http::processHeaders($theHeaders);
    648648
     649                // If location is found, then assume redirect and redirect to location.
     650                if ( isset($processedHeaders['headers']['location']) ) {
     651                        if ( $r['redirection']-- > 0 ) {
     652                                return $this->request($processedHeaders['headers']['location'], $r);
     653                        } else {
     654                                return new WP_Error('http_request_failed', __('Too many redirects.'));
     655                        }
     656                }
     657
    649658                if ( ! empty( $strResponse ) && isset( $processedHeaders['headers']['transfer-encoding'] ) && 'chunked' == $processedHeaders['headers']['transfer-encoding'] )
    650659                        $strResponse = WP_Http::chunkTransferDecode($strResponse);
    651660
     
    760769                $meta = stream_get_meta_data($handle);
    761770                $processedHeaders = WP_Http::processHeaders($meta['wrapper_data']);
    762771
     772                // If location is found, then assume redirect and redirect to location.
     773                if ( isset($processedHeaders['headers']['location']) ) {
     774                        if ( $r['redirection']-- > 0 ) {
     775                                return $this->request($processedHeaders['headers']['location'], $r);
     776                        } else {
     777                                return new WP_Error('http_request_failed', __('Too many redirects.'));
     778                        }
     779                }
     780
    763781                if ( ! empty( $strResponse ) && isset( $processedHeaders['headers']['transfer-encoding'] ) && 'chunked' == $processedHeaders['headers']['transfer-encoding'] )
    764782                        $strResponse = WP_Http::chunkTransferDecode($strResponse);
    765783
     
    858876                        'headers' => $r['headers'],
    859877                );
    860878
    861                 $strResponse = http_request($r['method'], $url, $r['body'], $options, $info);
     879                if ( !defined('WP_DEBUG') || ( defined('WP_DEBUG') && false === WP_DEBUG ) ) //Emits warning level notices for max redirects and timeouts
     880                        $strResponse = @http_request($r['method'], $url, $r['body'], $options, $info);
     881                else
     882                        $strResponse = http_request($r['method'], $url, $r['body'], $options, $info); //Emits warning level notices for max redirects and timeouts
    862883
    863                 if ( false === $strResponse )
     884                if ( false === $strResponse || ! empty($info['error']) ) //Error may still be set, Response may return headers or partial document, and error contains a reason the request was aborted, eg, timeout expired or max-redirects reached
    864885                        return new WP_Error('http_request_failed', $info['response_code'] . ': ' . $info['error']);
    865886
    866887                if ( ! $r['blocking'] )
     
    869890                list($theHeaders, $theBody) = explode("\r\n\r\n", $strResponse, 2);
    870891                $theHeaders = WP_Http::processHeaders($theHeaders);
    871892
    872                 if ( ! empty( $theBody ) && isset( $theHeaders['headers']['transfer-encoding'] ) && 'chunked' == $theHeaders['headers']['transfer-encoding'] )
    873                         $theBody = http_chunked_decode($theBody);
     893                if ( ! empty( $theBody ) && isset( $theHeaders['headers']['transfer-encoding'] ) && 'chunked' == $theHeaders['headers']['transfer-encoding'] ) {
     894                        if ( !defined('WP_DEBUG') || ( defined('WP_DEBUG') && false === WP_DEBUG ) )
     895                                $theBody = @http_chunked_decode($theBody);
     896                        else
     897                                $theBody = http_chunked_decode($theBody);
     898                }
    874899
    875900                $theResponse = array();
    876901                $theResponse['code'] = $info['response_code'];
     
    970995
    971996                $theResponse = curl_exec( $handle );
    972997
    973                 list($theHeaders, $theBody) = explode("\r\n\r\n", $theResponse, 2);
    974                 $theHeaders = WP_Http::processHeaders($theHeaders);
    975 
    976                 if ( ! empty( $theBody ) && isset( $theHeaders['headers']['transfer-encoding'] ) && 'chunked' == $theHeaders['headers']['transfer-encoding'] )
    977                         $theBody = WP_Http::chunkTransferDecode($theBody);
    978 
     998                if ( $theResponse ) {
     999                        $headerLength = curl_getinfo($handle, CURLINFO_HEADER_SIZE);
     1000                        $theHeaders = trim( substr($theResponse, 0, $headerLength) );
     1001                        $theBody = substr( $theResponse, $headerLength );
     1002                        if ( false !== strrpos($theHeaders, "\r\n\r\n") ) {
     1003                                $headerParts = explode("\r\n\r\n", $theHeaders);
     1004                                $theHeaders = $headerParts[ count($headerParts) -1 ];
     1005                        }
     1006                        $theHeaders = WP_Http::processHeaders($theHeaders);
     1007                } else {
     1008                        if ( in_array( curl_getinfo( $handle, CURLINFO_HTTP_CODE ), array(301, 302) ) )
     1009                                return new WP_Error('http_request_failed', __('Too many redirects.'));
     1010                        $theHeaders = array( 'headers' => array() );
     1011                        $theBody = '';
     1012                }
    9791013                $response = array();
    9801014                $response['code'] = curl_getinfo( $handle, CURLINFO_HTTP_CODE );
    9811015                $response['message'] = get_status_header_desc($response['code']);