Make WordPress Core

Changeset 38164


Ignore:
Timestamp:
07/27/2016 03:31:48 PM (10 years ago)
Author:
ocean90
Message:

HTTP API: Normalize cookies before passing them to Requests.

Requests has its own cookie object in form of Requests_Cookie. Therefore we have to convert WP_Http_Cookie objects to Requests_Cookie.
This introduces WP_Http_Cookie::get_attributes() to retrieve cookie attributes of a WP_Http_Cookie object and WP_Http::normalize_cookies() to convert the cookie objects.

Fixes #37437.

Location:
trunk
Files:
4 edited

Legend:

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

    r38054 r38164  
    326326                }
    327327
    328                 // If we've got cookies, use them
     328                // If we've got cookies, use and convert them to Requests_Cookie.
    329329                if ( ! empty( $r['cookies'] ) ) {
    330                         $options['cookies'] = $r['cookies'];
     330                        $options['cookies'] = WP_Http::normalize_cookies( $r['cookies'] );
    331331                }
    332332
    … …  
    415415
    416416        /**
     417         * Normalizes cookies for using in Requests.
     418         *
     419         * @since 4.6.0
     420         * @access public
     421         * @static
     422         *
     423         * @param array $cookies List of cookies to send with the request.
     424         * @return Requests_Cookie_Jar Cookie holder object.
     425         */
     426        public static function normalize_cookies( $cookies ) {
     427                $cookie_jar = new Requests_Cookie_Jar();
     428
     429                foreach ( $cookies as $name => $value ) {
     430                        if ( $value instanceof WP_Http_Cookie ) {
     431                                $cookie_jar[ $value->name ] = new Requests_Cookie( $value->name, $value->value, $value->get_attributes() );
     432                        } elseif ( is_string( $value ) ) {
     433                                $cookie_jar[ $name ] = new Requests_Cookie( $name, $value );
     434                        }
     435                }
     436
     437                return $cookie_jar;
     438        }
     439
     440        /**
    417441         * Match redirect behaviour to browser handling.
    418442         *
    … …  
    421445         * specification for compatibility purposes.
    422446         *
    423          * @param string $location URL to redirect to.
    424          * @param array $headers Headers for the redirect.
    425          * @param array $options Redirect request options.
     447         * @since 4.6.0
     448         * @access public
     449         * @static
     450         *
     451         * @param string            $location URL to redirect to.
     452         * @param array             $headers  Headers for the redirect.
     453         * @param array             $options  Redirect request options.
    426454         * @param Requests_Response $original Response object.
    427455         */
  • trunk/src/wp-includes/class-wp-http-cookie.php

    r37492 r38164  
    217217                return 'Cookie: ' . $this->getHeaderValue();
    218218        }
     219
     220        /**
     221         * Retrieves cookie attributes.
     222         *
     223         * @since 4.6.0
     224         * @access public
     225         *
     226         * @return array {
     227         *    List of attributes.
     228         *
     229         *    @type string $expires When the cookie expires.
     230         *    @type string $path    Cookie URL path.
     231         *    @type string $domain  Cookie domain.
     232         * }
     233         */
     234        public function get_attributes() {
     235                return array(
     236                        'expires' => $this->expires,
     237                        'path'    => $this->path,
     238                        'domain'  => $this->domain,
     239                );
     240        }
    219241}
  • trunk/src/wp-includes/class-wp-http-requests-response.php

    r38120 r38164  
    177177                                'name'    => $cookie->name,
    178178                                'value'   => urldecode( $cookie->value ),
    179                                 'expires' => $cookie->attributes['expires'],
    180                                 'path'    => $cookie->attributes['path'],
    181                                 'domain'  => $cookie->attributes['domain'],
     179                                'expires' => isset( $cookie->attributes['expires'] ) ? $cookie->attributes['expires'] : null,
     180                                'path'    => isset( $cookie->attributes['path'] ) ? $cookie->attributes['path'] : null,
     181                                'domain'  => isset( $cookie->attributes['domain'] ) ? $cookie->attributes['domain'] : null,
    182182                        ));
    183183                }
  • trunk/tests/phpunit/tests/http/functions.php

    r37989 r38164  
    108108        }
    109109
     110        /**
     111         * @ticket 37437
     112         */
     113        function test_get_response_cookies_with_wp_http_cookie_object() {
     114                $url = 'http://example.org';
     115
     116                $response = wp_remote_get( $url, array(
     117                        'cookies' => array(
     118                                new WP_Http_Cookie( array( 'name' => 'test', 'value' => 'foo' ) ),
     119                        ),
     120                ) );
     121                $cookies  = wp_remote_retrieve_cookies( $response );
     122
     123                $this->assertNotEmpty( $cookies );
     124
     125                $cookie = wp_remote_retrieve_cookie( $response, 'test' );
     126                $this->assertInstanceOf( 'WP_Http_Cookie', $cookie );
     127                $this->assertSame( 'test', $cookie->name );
     128                $this->assertSame( 'foo', $cookie->value );
     129        }
     130
     131        /**
     132         * @ticket 37437
     133         */
     134        function test_get_response_cookies_with_name_value_array() {
     135                $url = 'http://example.org';
     136
     137                $response = wp_remote_get( $url, array(
     138                        'cookies' => array(
     139                                'test' => 'foo',
     140                        ),
     141                ) );
     142                $cookies  = wp_remote_retrieve_cookies( $response );
     143
     144                $this->assertNotEmpty( $cookies );
     145
     146                $cookie = wp_remote_retrieve_cookie( $response, 'test' );
     147                $this->assertInstanceOf( 'WP_Http_Cookie', $cookie );
     148                $this->assertSame( 'test', $cookie->name );
     149                $this->assertSame( 'foo', $cookie->value );
     150        }
    110151}
Note: See TracChangeset for help on using the changeset viewer.