Plugin Directory

Changeset 1451930


Ignore:
Timestamp:
07/09/2016 08:32:10 PM (10 years ago)
Author:
bobbywalters
Message:

Updates for WordPress 4.6 which changes the HTTP API and request handling (ie Requests library in core).

Location:
beam/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • beam/trunk/beam.php

    r1414289 r1451930  
    66 * Author: Bobby Walters
    77 * Author URI: https://github.com/bobbywalters
    8  * Version: 1.0.0
     8 * Version: 2.0.0
    99 * Text Domain: beam
    1010 * Domain Path: /languages
     
    5151 * * The OpenSSL extension is needed for SSL/TLS (HTTPS) support.
    5252 * * PHP streams must be able to create socket connections.
    53  * * The name of this class had to be prefixed with "WP_HTTP_" in order
    54  * for the WordPress HTTP API to register it as a valid transport.
    5553 */
    56 class WP_HTTP_Beam {
     54class Beam {
    5755    /**
    5856     * Read a single line from the supplied PHP stream.
     
    8078     * @return array An associative array matching `WP_Http::processHeaders`.
    8179     * @see WP_Http::processHeaders
    82      * @uses WP_HTTP_Beam::get_line
     80     * @uses Beam::get_line
    8381     */
    8482    protected static function get_meta_data( $stream, $url ) {
     
    143141
    144142    /**
    145      * Registers Beam with the WordPress HTTP API when hooked by the
    146      * `http_api_transports` filter.
    147      *
    148      * @param array  $transports Array of HTTP transports to check.
    149      * Default array contains 'curl', and 'streams', in that order.
    150      * @param array  $args       HTTP request arguments.
    151      * @param string $url        The URL to request.
    152      * @return array An array with a single element 'Beam'.
     143     * Preempt the WordPress core HTTP request handling to use Beam.
     144     *
     145     * @param false|array|WP_Error $preempt Whether to preempt an HTTP
     146     * request's return value. Default false.
     147     * @param array                $r       HTTP request arguments.
     148     * @param string               $url     The request URL.
     149     * @return `false` if the request will not be processed by Beam
     150     * otherwise an `array` with server response data or a `WP_Error`
     151     * indicating the request was handled but an issue occurred.
     152     * @uses Beam::request
     153     * @since 2.0.0
    153154     */
    154     static function register( $transports, $args, $url ) {
    155         return array( 'Beam' );
     155    static function pre_http_request( $preempt, $r, $url ) {
     156        if ( $r['reject_unsafe_urls'] && ! wp_http_validate_url( $url ) ) {
     157            return new WP_Error( 'http_request_failed', __( 'Rejected unsafe URL through HTTP.' ) );
     158        }
     159
     160        $http = new WP_Http;
     161        if ( $http->block_request( $url ) ) {
     162            return new WP_Error( 'http_request_failed', __( 'User has blocked requests through HTTP.' ) );
     163        }
     164        unset( $http );
     165
     166        if ( isset( $r['headers'] ) ) {
     167            if ( is_string( $r['headers'] ) ) {
     168                $stream = fopen( 'php://memory', 'rb+' );
     169                fwrite( $stream, "HTTP/1.1 200 OK\r\n" );
     170                fwrite( $stream, $r['headers'] );
     171                fwrite( $stream, "\r\n\r\n" );
     172                rewind( $stream );
     173
     174                $r['headers'] = self::get_meta_data( $stream, $url )['headers'];
     175
     176                fclose( $stream );
     177                unset( $stream );
     178            }
     179
     180            // WP_Http::buildCookieHeader allowed strings and WP_Http_Cookie.
     181            if ( false === empty( $r['cookies'] ) ) {
     182                $h = '';
     183                foreach ( $r['cookies'] as $k => $c ) {
     184                    if ( $c instanceof WP_Http_Cookie ) {
     185                        $h .= '; ' . $c->getHeaderValue();
     186                    } else {
     187                        $h .= '; ' . $k . '=' . $c;
     188                    }
     189                }
     190
     191                if ( '' !== $h ) {
     192                    if ( isset( $r['headers']['cookie']) ) {
     193                        $r['headers']['cookie'] .= $h;
     194                    } elseif ( isset( $r['headers']['Cookie'] ) ) {
     195                        $r['headers']['cookie'] = $r['headers']['Cookie'] . $h;
     196                        unset( $r['headers']['Cookie'] );
     197                    } else {
     198                        $r['headers']['cookie'] = substr( $h, 2 );
     199                    }
     200                }
     201
     202                unset( $c, $h, $k );
     203            }
     204
     205            if ( isset( $r['headers']['user-agent'] ) ) {
     206                $r['user-agent'] = $r['headers']['user-agent'];
     207            } elseif ( isset( $r['headers']['User-Agent'] ) ) {
     208                $r['user-agent'] = $r['headers']['User-Agent'];
     209            }
     210            unset( $r['headers']['user-agent'], $r['headers']['User-Agent'] );
     211
     212            unset( $r['headers']['connection'], $r['headers']['Connection'] );
     213        } else {
     214            $r['headers'] = array();
     215        }
     216
     217        $r['method'] = strtoupper( $r['method'] );
     218
     219        if ( 'POST' === $r['method']
     220            || 'PUT' === $r['method']
     221            || ( isset( $r['body'] ) && '' !== $r['body'] ) ) {
     222
     223            if ( is_array( $r['body'] ) || is_object( $r['body'] ) ) {
     224                $r['body'] = http_build_query( $r['body'], null, '&' );
     225                if ( false === isset( $r['headers']['content-type'] ) && false === isset( $r['headers']['Content-Type'] ) ) {
     226                    $r['headers']['content-type'] = 'application/x-www-form-urlencoded; charset=' . get_option( 'blog_charset' );
     227                }
     228            }
     229
     230            if ( '' === $r['body'] ) {
     231                $r['body'] = null;
     232            }
     233
     234            if ( false === isset( $r['headers']['content-length'] ) && false === isset( $r['headers']['Content-Length'] ) ) {
     235                $r['headers']['content-length'] = strlen( $r['body'] );
     236            }
     237        }
     238
     239        return Beam::request( $url, $r );
    156240    }
    157241
     
    172256     * @see WP_Http::request
    173257     */
    174     function request( $url, $r ) {
     258    static function request( $url, $r ) {
    175259        $parsed_url = parse_url( $url );
    176260
     
    199283        unset( $r['headers']['Host'], $r['headers']['host'] );
    200284
    201         /*
    202          * Certain versions of PHP have issues with 'localhost' and IPv6, It
    203          * attempts to connect to ::1, which fails when the server is not
    204          * set up for it. For compatibility, always connect to the IPv4 address.
    205          */
    206         if ( 'localhost' === strtolower( $host ) ) {
    207             $host = '127.0.0.1';
     285        switch ( strtolower( $host ) ) {
     286            case 'localhost':
     287                // Avoid issues with IPv6 vs IPv4, DNS, and PHP.
     288                $host = '127.0.0.1';
     289            case strtolower( parse_url( home_url(), PHP_URL_HOST ) ):
     290                $local = true;
     291                break;
     292            default:
     293                $local = false;
    208294        }
    209295
     
    226312         */
    227313        $ssl_verify = apply_filters(
    228             isset( $r['local'] ) && $r['local'] ? 'https_local_ssl_verify' : 'https_ssl_verify',
     314            $local ? 'https_local_ssl_verify' : 'https_ssl_verify',
    229315            isset( $r['sslverify'] ) && $r['sslverify']
    230316        );
     
    378464        }
    379465
    380         fwrite( $stream, strtoupper( $r['method'] ) );
     466        fwrite( $stream, $r['method'] );
    381467        fwrite( $stream, ' ' );
    382468
     
    429515        }
    430516
     517        // Always close the connection.
     518        fwrite( $stream, "connection: close\r\n" );
     519
     520        // Marks end of headers.
    431521        fwrite( $stream, "\r\n" );
    432522
     
    485575                    }
    486576
    487                     return $this->request( $url, $r );
     577                    return self::request( $url, $r );
    488578            }
    489579        }
     
    505595
    506596        if ( $r['stream'] ) {
     597            if ( false === isset( $r['filename'] ) ) {
     598                $r['filename'] = tempnam( get_temp_dir(), 'beam' );
     599            }
     600
    507601            if ( $file = fopen( $r['filename'], 'wb' ) ) {
    508602                if ( null !== $limit ) {
     
    567661        return '1.1';
    568662    }
    569 
    570     /**
    571      * Determines whether this class can be used for retrieving a URL.
    572      *
    573      * @param array  $args Request arguments.
    574      * @param string $url  URL to request.
    575      * @return bool False means this class can not be used, true
    576      * means it can. Default true.
    577      */
    578     static function test( $args, $url ) {
    579         return true;
    580     }
    581663}
    582664
    583 add_filter( 'http_api_transports', 'WP_HTTP_Beam::register', 99, 3 );
    584 add_filter( 'http_request_version', 'WP_HTTP_Beam::request_version' );
     665add_filter( 'http_request_version', 'Beam::request_version' );
     666add_filter( 'pre_http_request', 'Beam::pre_http_request', 99, 3 );
  • beam/trunk/readme.txt

    r1414289 r1451930  
    33Tags: http, proxy, remote API, socket, stream, tunnel
    44Requires at least: 3.7.0
    5 Tested up to: 4.5
     5Tested up to: 4.6
    66Stable tag: trunk
    77License: GPLv2
     
    6161== Changelog ==
    6262
     63= 2.0.0 =
     64
     652016-07-09
     66
     67This release was focused on getting Beam to work on WordPress 4.6.
     68
     69* FIX: WordPress 4.6 now uses the `Requests` library to handle HTTP requests and the swap prevented Beam from being used.
     70* NEW: Beam is now triggered via the `pre_http_request` filter to handle requests.
     71* NEW: `WP_HTTP_Beam` was renamed to `Beam` since Beam is no longer loaded as a WP HTTP API transport.
     72
    6373= 1.0.0 =
     74
    6475Initial release.
Note: See TracChangeset for help on using the changeset viewer.