WordPress.org

Make WordPress Core

Changeset 45135


Ignore:
Timestamp:
04/08/2019 05:31:35 AM (3 years ago)
Author:
pento
Message:

HTTP: Add support for the host-only flag to Wp_Http_Cookie.

Props soulseekah.
Fixes #43231.

Location:
trunk
Files:
4 edited

Legend:

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

    r44397 r45135  
    450450        foreach ( $cookies as $name => $value ) {
    451451            if ( $value instanceof WP_Http_Cookie ) {
    452                 $cookie_jar[ $value->name ] = new Requests_Cookie( $value->name, $value->value, $value->get_attributes() );
     452                $cookie_jar[ $value->name ] = new Requests_Cookie( $value->name, $value->value, $value->get_attributes(), array( 'host-only' => $value->host_only ) );
    453453            } elseif ( is_scalar( $value ) ) {
    454454                $cookie_jar[ $name ] = new Requests_Cookie( $name, $value );
  • trunk/src/wp-includes/class-wp-http-cookie.php

    r42343 r45135  
    6262
    6363    /**
     64     * host-only flag.
     65     *
     66     * @since 5.2.0
     67     * @var bool
     68     */
     69    public $host_only;
     70
     71    /**
    6472     * Sets up this cookie object.
    6573     *
     
    6876     *
    6977     * @since 2.8.0
     78     * @since 5.2.0 Added `host_only` to the `$data` parameter.
    7079     *
    7180     * @param string|array $data {
    7281     *     Raw cookie data as header string or data array.
    7382     *
    74      *     @type string     $name    Cookie name.
    75      *     @type mixed      $value   Value. Should NOT already be urlencoded.
    76      *     @type string|int $expires Optional. Unix timestamp or formatted date. Default null.
    77      *     @type string     $path    Optional. Path. Default '/'.
    78      *     @type string     $domain  Optional. Domain. Default host of parsed $requested_url.
    79      *     @type int        $port    Optional. Port. Default null.
     83     *     @type string     $name      Cookie name.
     84     *     @type mixed      $value     Value. Should NOT already be urlencoded.
     85     *     @type string|int $expires   Optional. Unix timestamp or formatted date. Default null.
     86     *     @type string     $path      Optional. Path. Default '/'.
     87     *     @type string     $domain    Optional. Domain. Default host of parsed $requested_url.
     88     *     @type int        $port      Optional. Port. Default null.
     89     *     @type bool       $host_only Optional. host-only storage flag. Default true.
    8090     * }
    8191     * @param string       $requested_url The URL which the cookie was set on, used for default $domain
     
    129139
    130140            // Set properties based directly on parameters.
    131             foreach ( array( 'name', 'value', 'path', 'domain', 'port' ) as $field ) {
     141            foreach ( array( 'name', 'value', 'path', 'domain', 'port', 'host_only' ) as $field ) {
    132142                if ( isset( $data[ $field ] ) ) {
    133143                    $this->$field = $data[ $field ];
  • trunk/src/wp-includes/class-wp-http-requests-response.php

    r42343 r45135  
    165165            $cookies[] = new WP_Http_Cookie(
    166166                array(
    167                     'name'    => $cookie->name,
    168                     'value'   => urldecode( $cookie->value ),
    169                     'expires' => isset( $cookie->attributes['expires'] ) ? $cookie->attributes['expires'] : null,
    170                     'path'    => isset( $cookie->attributes['path'] ) ? $cookie->attributes['path'] : null,
    171                     'domain'  => isset( $cookie->attributes['domain'] ) ? $cookie->attributes['domain'] : null,
     167                    'name'      => $cookie->name,
     168                    'value'     => urldecode( $cookie->value ),
     169                    'expires'   => isset( $cookie->attributes['expires'] ) ? $cookie->attributes['expires'] : null,
     170                    'path'      => isset( $cookie->attributes['path'] ) ? $cookie->attributes['path'] : null,
     171                    'domain'    => isset( $cookie->attributes['domain'] ) ? $cookie->attributes['domain'] : null,
     172                    'host_only' => isset( $cookie->flags['host-only'] ) ? $cookie->flags['host-only'] : null,
    172173                )
    173174            );
  • trunk/tests/phpunit/tests/http/functions.php

    r43571 r45135  
    200200        $this->assertSame( 'foo', $cookie->value );
    201201    }
     202
     203    /**
     204     * @ticket 43231
     205     */
     206    function test_get_cookie_host_only() {
     207        // emulate WP_Http::request() internals
     208        $requests_response = new Requests_Response();
     209
     210        $requests_response->cookies['test'] = Requests_Cookie::parse( 'test=foo; domain=.wordpress.org' );
     211
     212        $requests_response->cookies['test']->flags['host-only'] = false; // https://github.com/rmccue/Requests/issues/306
     213
     214        $http_response = new WP_HTTP_Requests_Response( $requests_response );
     215
     216        $response = $http_response->to_array();
     217
     218        // check the host_only flag in the resulting WP_Http_Cookie
     219        $cookie = wp_remote_retrieve_cookie( $response, 'test' );
     220        $this->assertEquals( $cookie->domain, 'wordpress.org' );
     221        $this->assertFalse( $cookie->host_only, 'host-only flag not set' );
     222
     223        // regurgitate (Requests_Cookie -> WP_Http_Cookie -> Requests_Cookie)
     224        $cookies = WP_Http::normalize_cookies( wp_remote_retrieve_cookies( $response ) );
     225        $this->assertFalse( $cookies['test']->flags['host-only'], 'host-only flag data lost' );
     226    }
    202227}
Note: See TracChangeset for help on using the changeset viewer.