Ticket #4779: 4779.r8529.diff

File 4779.r8529.diff, 6.1 KB (added by jacobsantos, 5 years ago)

Cycle through available transports trying each one. Based off of r8529

Line 
1Index: http.php
2===================================================================
3--- http.php    (revision 8529)
4+++ http.php    (working copy)
5@@ -88,16 +88,16 @@
6                static $working_transport;
7 
8                if ( is_null($working_transport) ) {
9-                       if ( true === WP_Http_ExtHttp::test() )
10-                               $working_transport = new WP_Http_ExtHttp();
11-                       else if ( true === WP_Http_Curl::test() )
12-                               $working_transport = new WP_Http_Curl();
13-                       else if ( true === WP_Http_Streams::test() )
14-                               $working_transport = new WP_Http_Streams();
15-                       else if ( true === WP_Http_Fopen::test() )
16-                               $working_transport = new WP_Http_Fopen();
17-                       else if ( true === WP_Http_Fsockopen::test() )
18-                               $working_transport = new WP_Http_Fsockopen();
19+                       if ( true === WP_Http_ExtHttp::test() && apply_filters('use_http_extension_transport', true) )
20+                               $working_transport[] = new WP_Http_ExtHttp();
21+                       else if ( true === WP_Http_Curl::test() && apply_filters('use_curl_transport', true) )
22+                               $working_transport[] = new WP_Http_Curl();
23+                       else if ( true === WP_Http_Streams::test() && apply_filters('use_streams_transport', true) )
24+                               $working_transport[] = new WP_Http_Streams();
25+                       else if ( true === WP_Http_Fopen::test() && apply_filters('use_fopen_transport', true) )
26+                               $working_transport[] = new WP_Http_Fopen();
27+                       else if ( true === WP_Http_Fsockopen::test() && apply_filters('use_fsockopen_transport', true) )
28+                               $working_transport[] = new WP_Http_Fsockopen();
29                }
30 
31                return $working_transport;
32@@ -121,12 +121,12 @@
33                static $working_transport;
34 
35                if ( is_null($working_transport) ) {
36-                       if ( true === WP_Http_ExtHttp::test() )
37-                               $working_transport = new WP_Http_ExtHttp();
38-                       else if ( true === WP_Http_Streams::test() )
39-                               $working_transport = new WP_Http_Streams();
40-                       else if ( true === WP_Http_Fsockopen::test() )
41-                               $working_transport = new WP_Http_Fsockopen();
42+                       if ( true === WP_Http_ExtHttp::test() && apply_filters('use_http_extension_transport', true) )
43+                               $working_transport[] = new WP_Http_ExtHttp();
44+                       else if ( true === WP_Http_Streams::test() && apply_filters('use_streams_transport', true) )
45+                               $working_transport[] = new WP_Http_Streams();
46+                       else if ( true === WP_Http_Fsockopen::test() && apply_filters('use_fsockopen_transport', true) )
47+                               $working_transport[] = new WP_Http_Fsockopen();
48                }
49 
50                return $working_transport;
51@@ -170,14 +170,14 @@
52         * @param string $url URI resource.
53         * @param str|array $args Optional. Override the defaults.
54         * @param string|array $headers Optional. Either the header string or array of Header name and value pairs. Expects sanitized.
55-        * @param string $body Optional. The body that should be sent. Expected to be already processed.
56+        * @param string $body Optional. The body that should be sent. Will be automatically escaped and processed.
57         * @return boolean
58         */
59        function request($url, $args = array(), $headers = null, $body = null) {
60                global $wp_version;
61 
62                $defaults = array(
63-                       'method' => 'GET', 'timeout' => 3,
64+                       'method' => 'GET', 'timeout' => apply_filters('http_request_timeout', 3),
65                        'redirection' => 5, 'httpversion' => '1.0',
66                        'user-agent' => apply_filters('http_headers_useragent', 'WordPress/' . $wp_version ),
67                        'blocking' => true
68@@ -195,12 +195,23 @@
69                if ( ! isset($headers['user-agent']) || ! isset($headers['User-Agent']) )
70                        $headers['user-agent'] = $r['user-agent'];
71 
72-               if ( is_null($body) )
73-                       $transport = WP_Http::_getTransport();
74-               else
75-                       $transport = WP_Http::_postTransport();
76+               if ( is_null($body) ) {
77+                       if ( ! is_string($body) )
78+                               $body = http_build_query($body);
79 
80-               return $transport->request($url, $r, $headers, $body);
81+                       $transports = WP_Http::_getTransport();
82+               } else
83+                       $transports = WP_Http::_postTransport();
84+
85+               $response = array( 'headers' => array(), 'body' => '', 'response' => array('code', 'message') );
86+               foreach( (array) $transports as $transport ) {
87+                       $response = $transport->request($url, $r, $headers, $body);
88+
89+                       if( !is_wp_error($response) )
90+                               break;
91+               }
92+
93+               return $response;
94        }
95 
96        /**
97@@ -414,7 +425,7 @@
98                if ( true === $secure_transport )
99                        $error_reporting = error_reporting(0);
100 
101-               $handle = fsockopen($arrURL['host'], $arrURL['port'], $iError, $strError, apply_filters('http_request_timeout', absint($r['timeout']) ) );
102+               $handle = fsockopen($arrURL['host'], $arrURL['port'], $iError, $strError, $r['timeout'] );
103 
104                if ( false === $handle )
105                        return new WP_Error('http_request_failed', $iError . ': ' . $strError);
106@@ -538,7 +549,7 @@
107                        return new WP_Error('http_request_failed', sprintf(__('Could not open handle for fopen() to %s'), $url));
108 
109                if ( function_exists('stream_set_timeout') )
110-                       stream_set_timeout($handle, apply_filters('http_request_timeout', $r['timeout']) );
111+                       stream_set_timeout($handle, $r['timeout'] );
112 
113                if ( ! $r['blocking'] ) {
114                        fclose($handle);
115@@ -612,6 +623,16 @@
116 
117                $r = wp_parse_args( $args, $defaults );
118 
119+               if ( isset($headers['User-Agent']) ) {
120+                       $r['user-agent'] = $headers['User-Agent'];
121+                       unset($headers['User-Agent']);
122+               } else if( isset($headers['user-agent']) ) {
123+                       $r['user-agent'] = $headers['user-agent'];
124+                       unset($headers['user-agent']);
125+               } else {
126+                       $r['user-agent'] = apply_filters('http_headers_useragent', 'WordPress/' . $wp_version );
127+               }
128+
129                $arrURL = parse_url($url);
130 
131                if ( 'http' != $arrURL['scheme'] || 'https' != $arrURL['scheme'] )
132@@ -620,7 +641,7 @@
133                $arrContext = array('http' =>
134                        array(
135                                'method' => strtoupper($r['method']),
136-                               'user-agent' => $headers['User-Agent'],
137+                               'user-agent' => $r['user-agent'],
138                                'max_redirects' => $r['redirection'],
139                                'protocol_version' => (float) $r['httpversion'],
140                                'header' => $headers,
141@@ -628,6 +649,7 @@
142                        )
143                );
144 
145+               var_dump($arrContext);
146                if ( ! is_null($body) )
147                        $arrContext['http']['content'] = $body;
148 
149@@ -638,7 +660,7 @@
150                if ( ! $handle)
151                        return new WP_Error('http_request_failed', sprintf(__('Could not open handle for fopen() to %s'), $url));
152 
153-               stream_set_timeout($handle, apply_filters('http_request_stream_timeout', $r['timeout']) );
154+               stream_set_timeout($handle, $r['timeout'] );
155 
156                if ( ! $r['blocking'] ) {
157                        fclose($handle);