]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/Requests/Requests/Transport/cURL.php
Sync with recent Requests commits. Fixes #1796
[Github/YOURLS.git] / includes / Requests / Requests / Transport / cURL.php
1 <?php
2 /**
3  * cURL HTTP transport
4  *
5  * @package Requests
6  * @subpackage Transport
7  */
8
9 /**
10  * cURL HTTP transport
11  *
12  * @package Requests
13  * @subpackage Transport
14  */
15 class Requests_Transport_cURL implements Requests_Transport {
16         const CURL_7_10_5 = 0x070A05;
17         const CURL_7_16_2 = 0x071002;
18
19         /**
20          * Raw HTTP data
21          *
22          * @var string
23          */
24         public $headers = '';
25
26         /**
27          * Information on the current request
28          *
29          * @var array cURL information array, see {@see http://php.net/curl_getinfo}
30          */
31         public $info;
32
33         /**
34          * Version string
35          *
36          * @var long
37          */
38         public $version;
39
40         /**
41          * cURL handle
42          *
43          * @var resource
44          */
45         protected $fp;
46
47         /**
48          * Have we finished the headers yet?
49          *
50          * @var boolean
51          */
52         protected $done_headers = false;
53
54         /**
55          * If streaming to a file, keep the file pointer
56          *
57          * @var resource
58          */
59         protected $stream_handle;
60
61         /**
62          * Constructor
63          */
64         public function __construct() {
65                 $curl = curl_version();
66                 $this->version = $curl['version_number'];
67                 $this->fp = curl_init();
68
69                 curl_setopt($this->fp, CURLOPT_HEADER, false);
70                 curl_setopt($this->fp, CURLOPT_RETURNTRANSFER, 1);
71                 if ($this->version >= self::CURL_7_10_5) {
72                         curl_setopt($this->fp, CURLOPT_ENCODING, '');
73                 }
74                 if (defined('CURLOPT_PROTOCOLS')) {
75                         curl_setopt($this->fp, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
76                 }
77                 if (defined('CURLOPT_REDIR_PROTOCOLS')) {
78                         curl_setopt($this->fp, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
79                 }
80         }
81
82         /**
83          * Perform a request
84          *
85          * @throws Requests_Exception On a cURL error (`curlerror`)
86          *
87          * @param string $url URL to request
88          * @param array $headers Associative array of request headers
89          * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD
90          * @param array $options Request options, see {@see Requests::response()} for documentation
91          * @return string Raw HTTP result
92          */
93         public function request($url, $headers = array(), $data = array(), $options = array()) {
94                 $this->setup_handle($url, $headers, $data, $options);
95
96                 $options['hooks']->dispatch('curl.before_send', array(&$this->fp));
97
98                 if ($options['filename'] !== false) {
99                         $this->stream_handle = fopen($options['filename'], 'wb');
100                         curl_setopt($this->fp, CURLOPT_FILE, $this->stream_handle);
101                 }
102
103                 if (isset($options['verify'])) {
104                         if ($options['verify'] === false) {
105                                 curl_setopt($this->fp, CURLOPT_SSL_VERIFYHOST, 0);
106                                 curl_setopt($this->fp, CURLOPT_SSL_VERIFYPEER, 0);
107
108                         } elseif (is_string($options['verify'])) {
109                                 curl_setopt($this->fp, CURLOPT_CAINFO, $options['verify']);
110                         }
111                 }
112
113                 if (isset($options['verifyname']) && $options['verifyname'] === false) {
114                         curl_setopt($this->fp, CURLOPT_SSL_VERIFYHOST, 0);
115                 }
116
117                 $response = curl_exec($this->fp);
118
119                 $options['hooks']->dispatch('curl.after_send', array(&$fake_headers));
120
121                 if (curl_errno($this->fp) === 23 || curl_errno($this->fp) === 61) {
122                         curl_setopt($this->fp, CURLOPT_ENCODING, 'none');
123                         $response = curl_exec($this->fp);
124                 }
125
126                 $this->process_response($response, $options);
127                 curl_close($this->fp);
128                 return $this->headers;
129         }
130
131         /**
132          * Send multiple requests simultaneously
133          *
134          * @param array $requests Request data
135          * @param array $options Global options
136          * @return array Array of Requests_Response objects (may contain Requests_Exception or string responses as well)
137          */
138         public function request_multiple($requests, $options) {
139                 $multihandle = curl_multi_init();
140                 $subrequests = array();
141                 $subhandles = array();
142
143                 $class = get_class($this);
144                 foreach ($requests as $id => $request) {
145                         $subrequests[$id] = new $class();
146                         $subhandles[$id] = $subrequests[$id]->get_subrequest_handle($request['url'], $request['headers'], $request['data'], $request['options']);
147                         $request['options']['hooks']->dispatch('curl.before_multi_add', array(&$subhandles[$id]));
148                         curl_multi_add_handle($multihandle, $subhandles[$id]);
149                 }
150
151                 $completed = 0;
152                 $responses = array();
153
154                 $request['options']['hooks']->dispatch('curl.before_multi_exec', array(&$multihandle));
155
156                 do {
157                         $active = false;
158
159                         do {
160                                 $status = curl_multi_exec($multihandle, $active);
161                         }
162                         while ($status === CURLM_CALL_MULTI_PERFORM);
163
164                         $to_process = array();
165
166                         // Read the information as needed
167                         while ($done = curl_multi_info_read($multihandle)) {
168                                 $key = array_search($done['handle'], $subhandles, true);
169                                 if (!isset($to_process[$key])) {
170                                         $to_process[$key] = $done;
171                                 }
172                         }
173
174                         // Parse the finished requests before we start getting the new ones
175                         foreach ($to_process as $key => $done) {
176                                 $options = $requests[$key]['options'];
177                                 $responses[$key] = $subrequests[$key]->process_response(curl_multi_getcontent($done['handle']), $options);
178
179                                 $options['hooks']->dispatch('transport.internal.parse_response', array(&$responses[$key], $requests[$key]));
180
181                                 curl_multi_remove_handle($multihandle, $done['handle']);
182                                 curl_close($done['handle']);
183
184                                 if (!is_string($responses[$key])) {
185                                         $options['hooks']->dispatch('multiple.request.complete', array(&$responses[$key], $key));
186                                 }
187                                 $completed++;
188                         }
189                 }
190                 while ($active || $completed < count($subrequests));
191
192                 $request['options']['hooks']->dispatch('curl.after_multi_exec', array(&$multihandle));
193
194                 curl_multi_close($multihandle);
195
196                 return $responses;
197         }
198
199         /**
200          * Get the cURL handle for use in a multi-request
201          *
202          * @param string $url URL to request
203          * @param array $headers Associative array of request headers
204          * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD
205          * @param array $options Request options, see {@see Requests::response()} for documentation
206          * @return resource Subrequest's cURL handle
207          */
208         public function &get_subrequest_handle($url, $headers, $data, $options) {
209                 $this->setup_handle($url, $headers, $data, $options);
210
211                 if ($options['filename'] !== false) {
212                         $this->stream_handle = fopen($options['filename'], 'wb');
213                         curl_setopt($this->fp, CURLOPT_FILE, $this->stream_handle);
214                 }
215
216                 return $this->fp;
217         }
218
219         /**
220          * Setup the cURL handle for the given data
221          *
222          * @param string $url URL to request
223          * @param array $headers Associative array of request headers
224          * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD
225          * @param array $options Request options, see {@see Requests::response()} for documentation
226          */
227         protected function setup_handle($url, $headers, $data, $options) {
228                 $options['hooks']->dispatch('curl.before_request', array(&$this->fp));
229
230                 $headers = Requests::flatten($headers);
231                 if (in_array($options['type'], array(Requests::HEAD, Requests::GET, Requests::DELETE)) & !empty($data)) {
232                         $url = self::format_get($url, $data);
233                 }
234                 elseif (!empty($data) && !is_string($data)) {
235                         $data = http_build_query($data, null, '&');
236                 }
237
238                 switch ($options['type']) {
239                         case Requests::POST:
240                                 curl_setopt($this->fp, CURLOPT_POST, true);
241                                 curl_setopt($this->fp, CURLOPT_POSTFIELDS, $data);
242                                 break;
243                         case Requests::PATCH:
244                         case Requests::PUT:
245                                 curl_setopt($this->fp, CURLOPT_CUSTOMREQUEST, $options['type']);
246                                 curl_setopt($this->fp, CURLOPT_POSTFIELDS, $data);
247                                 break;
248                         case Requests::DELETE:
249                                 curl_setopt($this->fp, CURLOPT_CUSTOMREQUEST, 'DELETE');
250                                 break;
251                         case Requests::HEAD:
252                                 curl_setopt($this->fp, CURLOPT_NOBODY, true);
253                                 break;
254                 }
255
256                 if( is_int($options['timeout']) or $this->version < self::CURL_7_16_2 ) {
257                         curl_setopt($this->fp, CURLOPT_TIMEOUT, ceil($options['timeout']));
258                 } else {
259                         curl_setopt($this->fp, CURLOPT_TIMEOUT_MS, round($options['timeout'] * 1000) );
260                 }
261                 if( is_int($options['connect_timeout'])  or $this->version < self::CURL_7_16_2 ) {
262                         curl_setopt($this->fp, CURLOPT_CONNECTTIMEOUT, ceil($options['connect_timeout']));
263                 } else {
264                         curl_setopt($this->fp, CURLOPT_CONNECTTIMEOUT_MS, round($options['connect_timeout'] * 1000));
265                 }
266                 curl_setopt($this->fp, CURLOPT_URL, $url);
267                 curl_setopt($this->fp, CURLOPT_REFERER, $url);
268                 curl_setopt($this->fp, CURLOPT_USERAGENT, $options['useragent']);
269                 curl_setopt($this->fp, CURLOPT_HTTPHEADER, $headers);
270
271                 if (true === $options['blocking']) {
272                         curl_setopt($this->fp, CURLOPT_HEADERFUNCTION, array(&$this, 'stream_headers'));
273                 }
274         }
275
276         public function process_response($response, $options) {
277                 if ($options['blocking'] === false) {
278                         $fake_headers = '';
279                         $options['hooks']->dispatch('curl.after_request', array(&$fake_headers));
280                         return false;
281                 }
282                 if ($options['filename'] !== false) {
283                         fclose($this->stream_handle);
284                         $this->headers = trim($this->headers);
285                 }
286                 else {
287                         $this->headers .= $response;
288                 }
289
290                 if (curl_errno($this->fp)) {
291                         throw new Requests_Exception('cURL error ' . curl_errno($this->fp) . ': ' . curl_error($this->fp), 'curlerror', $this->fp);
292                         return;
293                 }
294                 $this->info = curl_getinfo($this->fp);
295
296                 $options['hooks']->dispatch('curl.after_request', array(&$this->headers));
297                 return $this->headers;
298         }
299
300         /**
301          * Collect the headers as they are received
302          *
303          * @param resource $handle cURL resource
304          * @param string $headers Header string
305          * @return integer Length of provided header
306          */
307         protected function stream_headers($handle, $headers) {
308                 // Why do we do this? cURL will send both the final response and any
309                 // interim responses, such as a 100 Continue. We don't need that.
310                 // (We may want to keep this somewhere just in case)
311                 if ($this->done_headers) {
312                         $this->headers = '';
313                         $this->done_headers = false;
314                 }
315                 $this->headers .= $headers;
316
317                 if ($headers === "\r\n") {
318                         $this->done_headers = true;
319                 }
320                 return strlen($headers);
321         }
322
323         /**
324          * Format a URL given GET data
325          *
326          * @param string $url
327          * @param array|object $data Data to build query using, see {@see http://php.net/http_build_query}
328          * @return string URL with data
329          */
330         protected static function format_get($url, $data) {
331                 if (!empty($data)) {
332                         $url_parts = parse_url($url);
333                         if (empty($url_parts['query'])) {
334                                 $query = $url_parts['query'] = '';
335                         }
336                         else {
337                                 $query = $url_parts['query'];
338                         }
339
340                         $query .= '&' . http_build_query($data, null, '&');
341                         $query = trim($query, '&');
342
343                         if (empty($url_parts['query'])) {
344                                 $url .= '?' . $query;
345                         }
346                         else {
347                                 $url = str_replace($url_parts['query'], $query, $url);
348                         }
349                 }
350                 return $url;
351         }
352
353         /**
354          * Whether this transport is valid
355          *
356          * @codeCoverageIgnore
357          * @return boolean True if the transport is valid, false otherwise.
358          */
359         public static function test($capabilities = array()) {
360                 if (!function_exists('curl_init') && !function_exists('curl_exec'))
361                         return false;
362
363                 // If needed, check that our installed curl version supports SSL
364                 if (isset( $capabilities['ssl'] ) && $capabilities['ssl']) {
365                         $curl_version = curl_version();
366                         if (!(CURL_VERSION_SSL & $curl_version['features']))
367                                 return false;
368                 }
369
370                 return true;
371         }
372 }