WP_HTTP: Cookies: When following redirects, include the request cookies in the redirected requests. Fixes #24987

git-svn-id: https://develop.svn.wordpress.org/trunk@25046 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Dion Hulse 2013-08-17 01:19:04 +00:00
parent 08eaf4e3dc
commit b5b118558e
3 changed files with 59 additions and 1 deletions

View File

@ -195,7 +195,19 @@ class WP_Http {
$r['headers']['Content-Length'] = strlen( $r['body'] );
}
return $this->_dispatch_request($url, $r);
$response = $this->_dispatch_request($url, $r);
// Append cookies that were used in this request to the response
if ( ! empty( $r['cookies'] ) ) {
$cookies_set = wp_list_pluck( $response['cookies'], 'name' );
foreach ( $r['cookies'] as $cookie ) {
if ( ! in_array( $cookie->name, $cookies_set ) && $cookie->test( $url ) ) {
$response['cookies'][] = $cookie;
}
}
}
return $response;
}
/**
@ -639,6 +651,14 @@ class WP_Http {
$args['method'] = 'GET';
}
// Include valid cookies in the redirect process
if ( ! empty( $response['cookies'] ) ) {
foreach ( $response['cookies'] as $cookie ) {
if ( $cookie->test( $redirect_location ) )
$args['cookies'][] = $cookie;
}
}
return wp_remote_request( $redirect_location, $args );
}
}

View File

@ -96,6 +96,33 @@ if ( isset( $_GET['multiple-location-headers'] ) ) {
exit;
}
if ( isset( $_GET['cookie-test'] ) ) {
if ( 'test-cookie' != $_GET['cookie-test'] ) {
setcookie( 'api_test_cookie', 'value', time() + 365*24*60*60, '/core/tests/1.0/', 'api.wordpress.org' );
setcookie( 'api_test_cookie_minimal', 'value' );
setcookie( 'api_test_cookie_wrong_host', 'value', time() + 365*24*60*60, '/', 'example.com' );
setcookie( 'api_test_wildcard_domain', 'value', time() + 365*24*60*60, '/', '.wordpress.org' );
setcookie( 'api_test_cookie_expired', 'value', time() - 365*24*60*60, '/', '.wordpress.org' );
header( "Location: $url?cookie-test=test-cookie" );
exit;
}
if ( empty( $_COOKIE['api_test_cookie'] ) || 'value' != $_COOKIE['api_test_cookie'] )
die( 'FAIL_NO_COOKIE' );
if ( empty( $_COOKIE['api_test_cookie_minimal'] ) )
die( 'FAIL_NO_MINIMAL' );
if ( isset( $_COOKIE['api_test_cookie_wrong_host'] ) )
die( 'FAIL_WRONG_HOST' );
if ( empty( $_COOKIE['api_test_wildcard_domain'] ) )
die( 'FAIL_NO_WILDCARD' );
if ( isset( $_COOKIE['api_test_cookie_expired'] ) )
die( 'FAIL_EXPIRED_COOKIE' );
echo 'PASS';
exit;
}
$rt = isset($_GET['rt']) ? $_GET['rt'] : 5;
$r = isset($_GET['r']) ? $_GET['r'] : 0;

View File

@ -259,4 +259,15 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase {
}
/**
* Test HTTP Cookie handling
*
* @ticket 21182
*/
function test_cookie_handling() {
$url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?cookie-test=1';
$res = wp_remote_get( $url );
$this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) );
}
}