Ticket #17588: 17588.2.diff
File 17588.2.diff, 5.6 KB (added by , 11 years ago) |
---|
-
wp-includes/class-http.php
593 593 594 594 return $absolute_path . '/' . ltrim( $path, '/' ); 595 595 } 596 597 static function handle_redirects( $url, $args, $response ) { 598 // If no redirects are present, or, redirects were not requested, perform no action. 599 if ( ! isset( $response['headers']['location'] ) || 0 === $args['_redirection'] ) 600 return null; 601 602 // Only perform redirections on redirection http codes 603 if ( $response['response']['code'] > 399 || $response['response']['code'] < 300 ) 604 return null; 605 606 // Don't redirect if we've run out of redirects 607 if ( $args['redirection']-- <= 0 ) 608 return new WP_Error( 'http_request_failed', __('Too many redirects.') ); 609 610 $redirect_location = WP_HTTP::make_absolute_url( $response['headers']['location'], $url ); 611 612 // POST requests should not POST to a redirected location 613 if ( 'POST' == $args['method'] ) { 614 if ( in_array( $response['response']['code'], array( 302, 303 ) ) ) 615 $args['method'] = 'GET'; 616 } 617 618 return wp_remote_request( $redirect_location, $args ); 619 } 596 620 } 597 621 598 622 /** … … 804 828 805 829 $arrHeaders = WP_Http::processHeaders( $process['headers'] ); 806 830 807 // If location is found, then assume redirect and redirect to location. 808 if ( isset($arrHeaders['headers']['location']) && 0 !== $r['_redirection'] ) { 809 if ( $r['redirection']-- > 0 ) { 810 return wp_remote_request( WP_HTTP::make_absolute_url( $arrHeaders['headers']['location'], $url ), $r); 811 } else { 812 return new WP_Error('http_request_failed', __('Too many redirects.')); 813 } 814 } 831 $response = array( 832 'headers' => $arrHeaders['headers'], 833 'body' => null, // Not yet processed 834 'response' => $arrHeaders['response'], 835 'cookies' => $arrHeaders['cookies'], 836 'filename' => $r['filename'] 837 ); 815 838 839 // Handle redirects 840 if ( null !== ( $redirect_response = WP_HTTP::handle_redirects( $url, $r, $response ) ) ) 841 return $redirect_response; 842 816 843 // If the body was chunk encoded, then decode it. 817 844 if ( ! empty( $process['body'] ) && isset( $arrHeaders['headers']['transfer-encoding'] ) && 'chunked' == $arrHeaders['headers']['transfer-encoding'] ) 818 845 $process['body'] = WP_Http::chunkTransferDecode($process['body']); … … 823 850 if ( isset( $r['limit_response_size'] ) && strlen( $process['body'] ) > $r['limit_response_size'] ) 824 851 $process['body'] = substr( $process['body'], 0, $r['limit_response_size'] ); 825 852 826 return array( 'headers' => $arrHeaders['headers'], 'body' => $process['body'], 'response' => $arrHeaders['response'], 'cookies' => $arrHeaders['cookies'], 'filename' => $r['filename'] ); 853 $response['body'] = $process['body']; 854 855 return $response; 827 856 } 828 857 829 858 /** … … 995 1024 else 996 1025 $processedHeaders = WP_Http::processHeaders($meta['wrapper_data']); 997 1026 998 if ( ! empty( $processedHeaders['headers']['location'] ) && 0 !== $r['_redirection'] ) { // _redirection: The requested number of redirections999 if ( $r['redirection']-- > 0 ) {1000 return wp_remote_request( WP_HTTP::make_absolute_url( $processedHeaders['headers']['location'], $url ), $r );1001 } else {1002 return new WP_Error( 'http_request_failed', __( 'Too many redirects.' ) );1003 }1004 }1027 $response = array( 1028 'headers' => $processedHeaders['headers'], 1029 'body' => null, 1030 'response' => $processedHeaders['response'], 1031 'cookies' => $processedHeaders['cookies'], 1032 'filename' => $r['filename'] 1033 ); 1005 1034 1035 // Handle redirects 1036 if ( null !== ( $redirect_response = WP_HTTP::handle_redirects( $url, $r, $response ) ) ) 1037 return $redirect_response; 1038 1006 1039 if ( ! empty( $strResponse ) && isset( $processedHeaders['headers']['transfer-encoding'] ) && 'chunked' == $processedHeaders['headers']['transfer-encoding'] ) 1007 1040 $strResponse = WP_Http::chunkTransferDecode($strResponse); 1008 1041 1009 1042 if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($processedHeaders['headers']) ) 1010 1043 $strResponse = WP_Http_Encoding::decompress( $strResponse ); 1011 1044 1012 return array( 'headers' => $processedHeaders['headers'], 'body' => $strResponse, 'response' => $processedHeaders['response'], 'cookies' => $processedHeaders['cookies'], 'filename' => $r['filename'] ); 1045 $response['body'] = $strResponse; 1046 1047 return $response; 1013 1048 } 1014 1049 1015 1050 /** … … 1262 1297 if ( $r['stream'] ) 1263 1298 fclose( $this->stream_handle ); 1264 1299 1265 // See #11305 - When running under safe mode, redirection is disabled above. Handle it manually. 1266 if ( ! empty( $theHeaders['headers']['location'] ) && 0 !== $r['_redirection'] ) { // _redirection: The requested number of redirections 1267 if ( $r['redirection']-- > 0 ) { 1268 return wp_remote_request( WP_HTTP::make_absolute_url( $theHeaders['headers']['location'], $url ), $r ); 1269 } else { 1270 return new WP_Error( 'http_request_failed', __( 'Too many redirects.' ) ); 1271 } 1272 } 1300 $response = array( 1301 'headers' => $theHeaders['headers'], 1302 'body' => null, 1303 'response' => $response, 1304 'cookies' => $theHeaders['cookies'], 1305 'filename' => $r['filename'] 1306 ); 1273 1307 1308 // Handle redirects 1309 if ( null !== ( $redirect_response = WP_HTTP::handle_redirects( $url, $r, $response ) ) ) 1310 return $redirect_response; 1311 1274 1312 if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($theHeaders['headers']) ) 1275 1313 $theBody = WP_Http_Encoding::decompress( $theBody ); 1276 1314 1277 return array( 'headers' => $theHeaders['headers'], 'body' => $theBody, 'response' => $response, 'cookies' => $theHeaders['cookies'], 'filename' => $r['filename'] ); 1315 $response['body'] = $theBody; 1316 1317 return $response; 1278 1318 } 1279 1319 1280 1320 /**