Changeset 1451930
- Timestamp:
- 07/09/2016 08:32:10 PM (10 years ago)
- Location:
- beam/trunk
- Files:
-
- 2 edited
-
beam.php (modified) (12 diffs)
-
readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
beam/trunk/beam.php
r1414289 r1451930 6 6 * Author: Bobby Walters 7 7 * Author URI: https://github.com/bobbywalters 8 * Version: 1.0.08 * Version: 2.0.0 9 9 * Text Domain: beam 10 10 * Domain Path: /languages … … 51 51 * * The OpenSSL extension is needed for SSL/TLS (HTTPS) support. 52 52 * * PHP streams must be able to create socket connections. 53 * * The name of this class had to be prefixed with "WP_HTTP_" in order54 * for the WordPress HTTP API to register it as a valid transport.55 53 */ 56 class WP_HTTP_Beam {54 class Beam { 57 55 /** 58 56 * Read a single line from the supplied PHP stream. … … 80 78 * @return array An associative array matching `WP_Http::processHeaders`. 81 79 * @see WP_Http::processHeaders 82 * @uses WP_HTTP_Beam::get_line80 * @uses Beam::get_line 83 81 */ 84 82 protected static function get_meta_data( $stream, $url ) { … … 143 141 144 142 /** 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 153 154 */ 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 ); 156 240 } 157 241 … … 172 256 * @see WP_Http::request 173 257 */ 174 function request( $url, $r ) {258 static function request( $url, $r ) { 175 259 $parsed_url = parse_url( $url ); 176 260 … … 199 283 unset( $r['headers']['Host'], $r['headers']['host'] ); 200 284 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; 208 294 } 209 295 … … 226 312 */ 227 313 $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', 229 315 isset( $r['sslverify'] ) && $r['sslverify'] 230 316 ); … … 378 464 } 379 465 380 fwrite( $stream, strtoupper( $r['method'] ));466 fwrite( $stream, $r['method'] ); 381 467 fwrite( $stream, ' ' ); 382 468 … … 429 515 } 430 516 517 // Always close the connection. 518 fwrite( $stream, "connection: close\r\n" ); 519 520 // Marks end of headers. 431 521 fwrite( $stream, "\r\n" ); 432 522 … … 485 575 } 486 576 487 return $this->request( $url, $r );577 return self::request( $url, $r ); 488 578 } 489 579 } … … 505 595 506 596 if ( $r['stream'] ) { 597 if ( false === isset( $r['filename'] ) ) { 598 $r['filename'] = tempnam( get_temp_dir(), 'beam' ); 599 } 600 507 601 if ( $file = fopen( $r['filename'], 'wb' ) ) { 508 602 if ( null !== $limit ) { … … 567 661 return '1.1'; 568 662 } 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, true576 * means it can. Default true.577 */578 static function test( $args, $url ) {579 return true;580 }581 663 } 582 664 583 add_filter( 'http_ api_transports', 'WP_HTTP_Beam::register', 99, 3);584 add_filter( ' http_request_version', 'WP_HTTP_Beam::request_version');665 add_filter( 'http_request_version', 'Beam::request_version' ); 666 add_filter( 'pre_http_request', 'Beam::pre_http_request', 99, 3 ); -
beam/trunk/readme.txt
r1414289 r1451930 3 3 Tags: http, proxy, remote API, socket, stream, tunnel 4 4 Requires at least: 3.7.0 5 Tested up to: 4. 55 Tested up to: 4.6 6 6 Stable tag: trunk 7 7 License: GPLv2 … … 61 61 == Changelog == 62 62 63 = 2.0.0 = 64 65 2016-07-09 66 67 This 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 63 73 = 1.0.0 = 74 64 75 Initial release.
Note: See TracChangeset
for help on using the changeset viewer.