WordPress.org

Make WordPress Core

Changeset 25046


Ignore:
Timestamp:
08/17/2013 01:19:04 AM (8 years ago)
Author:
dd32
Message:

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

Location:
trunk
Files:
3 edited

Legend:

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

    r25044 r25046  
    196196        }
    197197
    198         return $this->_dispatch_request($url, $r);
     198        $response = $this->_dispatch_request($url, $r);
     199
     200        // Append cookies that were used in this request to the response
     201        if ( ! empty( $r['cookies'] ) ) {
     202            $cookies_set = wp_list_pluck( $response['cookies'], 'name' );
     203            foreach ( $r['cookies'] as $cookie ) {
     204                    if ( ! in_array( $cookie->name, $cookies_set ) && $cookie->test( $url ) ) {
     205                                $response['cookies'][] = $cookie;
     206                    }
     207            }
     208        }
     209
     210        return $response;
    199211    }
    200212
     
    638650            if ( in_array( $response['response']['code'], array( 302, 303 ) ) )
    639651                $args['method'] = 'GET';
     652        }
     653
     654        // Include valid cookies in the redirect process
     655        if ( ! empty( $response['cookies'] ) ) {
     656            foreach ( $response['cookies'] as $cookie ) {
     657                if ( $cookie->test( $redirect_location ) )
     658                        $args['cookies'][] = $cookie;
     659            }
    640660        }
    641661
  • trunk/tests/data/WPHTTP-testcase-redirection-script.php

    r25002 r25046  
    9797}
    9898
     99if ( isset( $_GET['cookie-test'] ) ) {
     100    if ( 'test-cookie' != $_GET['cookie-test'] ) {
     101        setcookie( 'api_test_cookie', 'value', time() + 365*24*60*60, '/core/tests/1.0/', 'api.wordpress.org' );
     102        setcookie( 'api_test_cookie_minimal', 'value'  );
     103        setcookie( 'api_test_cookie_wrong_host', 'value', time() + 365*24*60*60, '/', 'example.com' );
     104        setcookie( 'api_test_wildcard_domain', 'value', time() + 365*24*60*60, '/', '.wordpress.org' );
     105        setcookie( 'api_test_cookie_expired', 'value', time() - 365*24*60*60, '/', '.wordpress.org' );
     106        header( "Location: $url?cookie-test=test-cookie" );
     107        exit;
     108    }
     109
     110    if ( empty( $_COOKIE['api_test_cookie'] ) || 'value' != $_COOKIE['api_test_cookie'] )
     111        die( 'FAIL_NO_COOKIE' );
     112    if ( empty( $_COOKIE['api_test_cookie_minimal'] ) )
     113        die( 'FAIL_NO_MINIMAL' );
     114    if ( isset( $_COOKIE['api_test_cookie_wrong_host'] ) )
     115        die( 'FAIL_WRONG_HOST' );
     116    if ( empty( $_COOKIE['api_test_wildcard_domain'] ) )
     117        die( 'FAIL_NO_WILDCARD' );
     118    if ( isset( $_COOKIE['api_test_cookie_expired'] ) )
     119        die( 'FAIL_EXPIRED_COOKIE' );
     120
     121    echo 'PASS';
     122    exit;
     123}
     124
     125
    99126$rt = isset($_GET['rt']) ? $_GET['rt'] : 5;
    100127$r = isset($_GET['r']) ? $_GET['r'] : 0;
  • trunk/tests/tests/http/base.php

    r25002 r25046  
    260260    }
    261261
     262    /**
     263     * Test HTTP Cookie handling
     264     *
     265     * @ticket 21182
     266     */
     267    function test_cookie_handling() {
     268        $url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?cookie-test=1';
     269
     270        $res = wp_remote_get( $url );
     271        $this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) );
     272    }
    262273}
Note: See TracChangeset for help on using the changeset viewer.