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