WordPress.org

Make WordPress Core


Ignore:
Timestamp:
07/27/2016 03:31:48 PM (5 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.

File:
1 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     */
Note: See TracChangeset for help on using the changeset viewer.