Make WordPress Core

Ticket #34538: 35553.patch

File 35553.patch, 14.3 KB (added by ka2, 9 years ago)

It is a patch that contains the unit test.

  • src/wp-includes/class-http.php

     
    140140                $args = wp_parse_args( $args );
    141141
    142142                // By default, Head requests do not cause redirections.
    143                 if ( isset($args['method']) && 'HEAD' == $args['method'] )
     143                if ( isset( $args['method'] ) && 'HEAD' == $args['method'] )
    144144                        $defaults['redirection'] = 0;
    145145
    146146                $r = wp_parse_args( $args, $defaults );
     
    192192                $arrURL = @parse_url( $url );
    193193
    194194                if ( empty( $url ) || empty( $arrURL['scheme'] ) )
    195                         return new WP_Error('http_request_failed', __('A valid URL was not provided.'));
     195                        return new WP_Error( 'http_request_failed', __( 'A valid URL was not provided.' ) );
    196196
    197197                if ( $this->block_request( $url ) )
    198198                        return new WP_Error( 'http_request_failed', __( 'User has blocked requests through HTTP.' ) );
     
    244244                        unset( $r['headers']['user-agent'] );
    245245                }
    246246
    247                 if ( '1.1' == $r['httpversion'] && !isset( $r['headers']['connection'] ) ) {
     247                if ( '1.1' == $r['httpversion'] && ! isset( $r['headers']['connection'] ) ) {
    248248                        $r['headers']['connection'] = 'close';
    249249                }
    250250
     
    327327                        $class = 'WP_Http_' . $transport;
    328328
    329329                        // Check to see if this transport is a possibility, calls the transport statically.
    330                         if ( !call_user_func( array( $class, 'test' ), $args, $url ) )
     330                        if ( ! call_user_func( array( $class, 'test' ), $args, $url ) )
    331331                                continue;
    332332
    333333                        return $class;
     
    357357                static $transports = array();
    358358
    359359                $class = $this->_get_first_available_transport( $args, $url );
    360                 if ( !$class )
     360                if ( ! $class )
    361361                        return new WP_Error( 'http_failure', __( 'There are no HTTP transports available which can complete the requested request.' ) );
    362362
    363363                // Transport claims to support request, instantiate it and give it a whirl.
     
    406406         * @param string|array $args Optional. Override the defaults.
    407407         * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error
    408408         */
    409         public function post($url, $args = array()) {
    410                 $defaults = array('method' => 'POST');
     409        public function post( $url, $args = array() ) {
     410                $defaults = array( 'method' => 'POST' );
    411411                $r = wp_parse_args( $args, $defaults );
    412                 return $this->request($url, $r);
     412                return $this->request( $url, $r );
    413413        }
    414414
    415415        /**
     
    424424         * @param string|array $args Optional. Override the defaults.
    425425         * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error
    426426         */
    427         public function get($url, $args = array()) {
    428                 $defaults = array('method' => 'GET');
     427        public function get( $url, $args = array() ) {
     428                $defaults = array( 'method' => 'GET' );
    429429                $r = wp_parse_args( $args, $defaults );
    430                 return $this->request($url, $r);
     430                return $this->request( $url, $r );
    431431        }
    432432
    433433        /**
     
    442442         * @param string|array $args Optional. Override the defaults.
    443443         * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error
    444444         */
    445         public function head($url, $args = array()) {
    446                 $defaults = array('method' => 'HEAD');
     445        public function head( $url, $args = array() ) {
     446                $defaults = array( 'method' => 'HEAD' );
    447447                $r = wp_parse_args( $args, $defaults );
    448                 return $this->request($url, $r);
     448                return $this->request( $url, $r );
    449449        }
    450450
    451451        /**
     
    458458         * @param string $strResponse The full response string
    459459         * @return array Array with 'headers' and 'body' keys.
    460460         */
    461         public static function processResponse($strResponse) {
    462                 $res = explode("\r\n\r\n", $strResponse, 2);
     461        public static function processResponse( $strResponse ) {
     462                $res = explode( "\r\n\r\n", $strResponse, 2 );
    463463
    464                 return array('headers' => $res[0], 'body' => isset($res[1]) ? $res[1] : '');
     464                return array( 'headers' => $res[0], 'body' => isset( $res[1] ) ? $res[1] : '' );
    465465        }
    466466
    467467        /**
     
    481481         */
    482482        public static function processHeaders( $headers, $url = '' ) {
    483483                // Split headers, one per array element.
    484                 if ( is_string($headers) ) {
     484                if ( is_string( $headers ) ) {
    485485                        // Tolerate line terminator: CRLF = LF (RFC 2616 19.3).
    486                         $headers = str_replace("\r\n", "\n", $headers);
     486                        $headers = str_replace( "\r\n", "\n", $headers );
    487487                        /*
    488488                         * Unfold folded header fields. LWS = [CRLF] 1*( SP | HT ) <US-ASCII SP, space (32)>,
    489489                         * <US-ASCII HT, horizontal-tab (9)> (RFC 2616 2.2).
    490490                         */
    491                         $headers = preg_replace('/\n[ \t]/', ' ', $headers);
     491                        $headers = preg_replace( '/\n[ \t]/', ' ', $headers );
    492492                        // Create the headers array.
    493                         $headers = explode("\n", $headers);
     493                        $headers = explode( "\n", $headers );
    494494                }
    495495
    496                 $response = array('code' => 0, 'message' => '');
     496                $response = array( 'code' => 0, 'message' => '' );
    497497
    498498                /*
    499499                 * If a redirection has taken place, The headers for each page request may have been passed.
    500500                 * In this case, determine the final HTTP header and parse from there.
    501501                 */
    502                 for ( $i = count($headers)-1; $i >= 0; $i-- ) {
    503                         if ( !empty($headers[$i]) && false === strpos($headers[$i], ':') ) {
    504                                 $headers = array_splice($headers, $i);
     502                for ( $i = count( $headers )-1; $i >= 0; $i-- ) {
     503                        if ( ! empty( $headers[$i] ) && false === strpos( $headers[$i], ':' ) ) {
     504                                $headers = array_splice( $headers, $i );
    505505                                break;
    506506                        }
    507507                }
     
    509509                $cookies = array();
    510510                $newheaders = array();
    511511                foreach ( (array) $headers as $tempheader ) {
    512                         if ( empty($tempheader) )
     512                        if ( empty( $tempheader ) )
    513513                                continue;
    514514
    515                         if ( false === strpos($tempheader, ':') ) {
    516                                 $stack = explode(' ', $tempheader, 3);
     515                        if ( false === strpos( $tempheader, ':' ) ) {
     516                                $stack = explode( ' ', $tempheader, 3 );
    517517                                $stack[] = '';
    518                                 list( , $response['code'], $response['message']) = $stack;
     518                                list( , $response['code'], $response['message'] ) = $stack;
    519519                                continue;
    520520                        }
    521521
    522                         list($key, $value) = explode(':', $tempheader, 2);
     522                        list( $key, $value ) = explode( ':', $tempheader, 2 );
    523523
    524524                        $key = strtolower( $key );
    525525                        $value = trim( $value );
     
    538538                // Cast the Response Code to an int
    539539                $response['code'] = intval( $response['code'] );
    540540
    541                 return array('response' => $response, 'headers' => $newheaders, 'cookies' => $cookies);
     541                return array( 'response' => $response, 'headers' => $newheaders, 'cookies' => $cookies );
    542542        }
    543543
    544544        /**
     
    555555         * @param array $r Full array of args passed into ::request()
    556556         */
    557557        public static function buildCookieHeader( &$r ) {
    558                 if ( ! empty($r['cookies']) ) {
     558                if ( ! empty( $r['cookies'] ) ) {
    559559                        // Upgrade any name => value cookie pairs to WP_HTTP_Cookie instances.
    560560                        foreach ( $r['cookies'] as $name => $value ) {
    561561                                if ( ! is_object( $value ) )
     
    638638         * @param string $uri URI of url.
    639639         * @return bool True to block, false to allow.
    640640         */
    641         public function block_request($uri) {
     641        public function block_request( $uri ) {
    642642                // We don't need to block requests, because nothing is blocked.
    643643                if ( ! defined( 'WP_HTTP_BLOCK_EXTERNAL' ) || ! WP_HTTP_BLOCK_EXTERNAL )
    644644                        return false;
    645645
    646                 $check = parse_url($uri);
     646                $check = parse_url( $uri );
    647647                if ( ! $check )
    648648                        return true;
    649649
    650                 $home = parse_url( get_option('siteurl') );
     650                $home = parse_url( get_option( 'siteurl' ) );
    651651
    652652                // Don't block requests back to ourselves by default.
    653653                if ( 'localhost' == $check['host'] || ( isset( $home['host'] ) && $home['host'] == $check['host'] ) ) {
     
    662662                        return apply_filters( 'block_local_requests', false );
    663663                }
    664664
    665                 if ( !defined('WP_ACCESSIBLE_HOSTS') )
     665                if ( ! defined( 'WP_ACCESSIBLE_HOSTS' ) )
    666666                        return true;
    667667
    668668                static $accessible_hosts = null;
    669669                static $wildcard_regex = array();
    670670                if ( null === $accessible_hosts ) {
    671                         $accessible_hosts = preg_split('|,\s*|', WP_ACCESSIBLE_HOSTS);
     671                        $accessible_hosts = preg_split( '|,\s*|', WP_ACCESSIBLE_HOSTS );
    672672
    673                         if ( false !== strpos(WP_ACCESSIBLE_HOSTS, '*') ) {
     673                        if ( false !== strpos( WP_ACCESSIBLE_HOSTS, '*' ) ) {
    674674                                $wildcard_regex = array();
    675675                                foreach ( $accessible_hosts as $host )
    676676                                        $wildcard_regex[] = str_replace( '\*', '.+', preg_quote( $host, '/' ) );
    677                                 $wildcard_regex = '/^(' . implode('|', $wildcard_regex) . ')$/i';
     677                                $wildcard_regex = '/^(' . implode( '|', $wildcard_regex ) . ')$/i';
    678678                        }
    679679                }
    680680
    681                 if ( !empty($wildcard_regex) )
    682                         return !preg_match($wildcard_regex, $check['host']);
     681                if ( ! empty( $wildcard_regex ) )
     682                        return !preg_match( $wildcard_regex, $check['host'] );
    683683                else
    684684                        return !in_array( $check['host'], $accessible_hosts ); //Inverse logic, If it's in the array, then we can't access it.
    685685
     
    799799
    800800                // Don't redirect if we've run out of redirects.
    801801                if ( $args['redirection']-- <= 0 )
    802                         return new WP_Error( 'http_request_failed', __('Too many redirects.') );
     802                        return new WP_Error( 'http_request_failed', __( 'Too many redirects.' ) );
    803803
    804804                $redirect_location = $response['headers']['location'];
    805805
     
    843843         * @return integer|bool Upon success, '4' or '6' to represent a IPv4 or IPv6 address, false upon failure
    844844         */
    845845        public static function is_ip_address( $maybe_ip ) {
    846                 if ( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $maybe_ip ) )
     846                if ( preg_match( '/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/', $maybe_ip ) )
    847847                        return 4;
    848848
    849849                if ( false !== strpos( $maybe_ip, ':' ) && preg_match( '/^(((?=.*(::))(?!.*\3.+\3))\3?|([\dA-F]{1,4}(\3|:\b|$)|\2))(?4){5}((?4){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4})$/i', trim( $maybe_ip, ' []' ) ) )
  • tests/phpunit/tests/http/http.php

     
    102102                  - ://example.com - assumed path in PHP >= 5.4.7, fails in <5.4.7
    103103                */
    104104        }
     105
     106        /**
     107         * @dataProvider ip_address_testcases
     108         */
     109        function test_is_ip_address( $maybe_ip, $expected ) {
     110                $actual = WP_Http::is_ip_address( $maybe_ip );
     111                $this->assertEquals( $expected, $actual );
     112        }
     113
     114        function ip_address_testcases() {
     115                // 0: The IP Address, 1: The expected resulting
     116                return array(
     117                        // Valid IPv4 address
     118                        // See: http://tools.ietf.org/html/rfc5737
     119                        array( '0.0.0.0', 4 ),
     120                        array( '127.0.0.1', 4 ), // The loopback address
     121                        array( '192.0.2.0', 4 ), // RFC 5737 - IPv4 Address Blocks Reserved for Documentation
     122                        array( '198.51.100.0', 4 ), // RFC 5737 - IPv4 Address Blocks Reserved for Documentation
     123                        array( '203.0.113.0', 4 ), // RFC 5737 - IPv4 Address Blocks Reserved for Documentation
     124                        array( '255.255.255.255', 4 ),
     125                       
     126                        // Invalid IPv4 address
     127                        array( '256.255.255.255', false ), // The first octet is out of range
     128                        array( '255.256.255.255', false ), // The second octet is out of range
     129                        array( '255.255.256.255', false ), // The third octet is out of range
     130                        array( '255.255.255.256', false ), // The fourth octet is out of range
     131                        array( '999.999.999.999', false ), // All octet is out of range
     132                        array( '2000.1.1.1', false ),
     133                        array( '1.2000.1.1', false ),
     134                        array( '1.1.2000.1', false ),
     135                        array( '1.1.1.2000', false ),
     136                        array( '2000.2000.2000.2000', false ),
     137                       
     138                        // Valid IPv6 address
     139                        // See: http://tools.ietf.org/html/rfc4291
     140                        array( '0:0:0:0:0:0:0:0', 6 ), // The unspecified address
     141                        array( '::', 6 ), // The unspecified address (in compressed form)
     142                        array( '0:0:0:0:0:0:0:1', 6 ), // The loopback address
     143                        array( '::1', 6 ), // The loopback address (in compressed form)
     144                        array( '2001:db8::', 6 ), // RFC 3849 - IPv6 Address Prefix Reserved for Documentation
     145                        array( '2001:0db8:0000:0000:0000:0000:dead:beaf', 6 ),
     146                        array( '2001:db8:0:0:0:0:dead:beaf', 6 ),
     147                        array( '2001:db8::dead:beaf', 6 ),
     148                        array( 'ABCD:EF01:2345:6789:ABCD:EF01:2345:6789', 6 ), // RFC 4291 - Example
     149                        array( '2001:DB8:0:0:8:800:200C:417A', 6 ), // RFC 4291 - Example of unicast address
     150                        array( '2001:DB8::8:800:200C:417A', 6 ), // RFC 4291 - Example of unicast address (in compressed form)
     151                        array( 'FF01:0:0:0:0:0:0:101', 6 ), // RFC 4291 - Example of multicast address
     152                        array( 'FF01::101', 6 ), // RFC 4291 - Example of multicast address (in compressed form)
     153                        array( '0:0:0:0:0:0:13.1.68.3', 6 ), // RFC 4291 - Example of an alternative form with a mixed environment of IPv4 and IPv6 nodes
     154                        array( '::13.1.68.3', 6 ), // RFC 4291 - Example of an alternative form with a mixed environment of IPv4 and IPv6 nodes (in compressed form)
     155                        array( '0:0:0:0:0:FFFF:129.144.52.38', 6 ), // RFC 4291 - Example of an alternative form with a mixed environment of IPv4 and IPv6 nodes
     156                        array( '::FFFF:129.144.52.38', 6 ), // RFC 4291 - Example of an alternative form with a mixed environment of IPv4 and IPv6 nodes (in compressed form)
     157                       
     158                        // Invalid IPv6 address
     159                        // See: https://github.com/richb-intermapper/IPv6-Regex
     160                        array( '2001:DB8:0:0:8:800:200C:417A:221', false ), // unicast full
     161                        array( 'FF01::101::2', false ), // multicast compressed
     162                        array( '02001:0000:1234:0000:0000:C1C0:ABCD:0876', false ), // extra 0 not allowed
     163                        array( '2001:0000:1234:0000:00001:C1C0:ABCD:0876', false ), // extra 0 not allowed
     164                        array( '2001:0000:1234:0000:0000:C1C0:ABCD:0876  0', false ), // junk after valid address
     165                        array( '2001:0000:1234: 0000:0000:C1C0:ABCD:0876', false ), // internal space
     166                        array( '3ffe:0b00:0000:0001:0000:0000:000a', false ), // Segments is less than 8
     167                        array( 'FF02:0000:0000:0000:0000:0000:0000:0000:0001', false ), // Segments is over 8
     168                        array( '3ffe:b00::1::a', false ), // Compressed double
     169                        array( '::1111:2222:3333:4444:5555:6666::', false ), // Compressed double
     170                        array( '1::5:400.2.3.4', false ), // Embedded ipv4 address is out of range
     171                        array( '1::5:192.168.0.256', false ), // Embedded ipv4 address is out of range
     172                        array( '2001:1:1:1:1:1:255Z255X255Y255', false ), // garbage instead of "." in IPv4
     173                        array( '::ffff:192x168.1.26', false ), // ditto
     174                        array( '::ffff:2.3.4', false ), // has ipv4 octet lost
     175                        array( '1.2.3.4:1111:2222:3333:4444::5555', false ), // Aeron
     176                        array( ':', false ),
     177                        array( ':::', false ),
     178                       
     179                        // other
     180                        array( '123', false ),
     181                        array( 'ldkfj', false ),
     182                        array( '.', false ),
     183                        array( '..', false ),
     184                        array( '...', false ),
     185                       
     186                );
     187        }
    105188}