cURL.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. <?php
  2. /**
  3. * cURL HTTP transport
  4. *
  5. * @package Requests
  6. * @subpackage Transport
  7. */
  8. /**
  9. * cURL HTTP transport
  10. *
  11. * @package Requests
  12. * @subpackage Transport
  13. */
  14. class Requests_Transport_cURL implements Requests_Transport {
  15. const CURL_7_10_5 = 0x070A05;
  16. const CURL_7_16_2 = 0x071002;
  17. /**
  18. * Raw HTTP data
  19. *
  20. * @var string
  21. */
  22. public $headers = '';
  23. /**
  24. * Raw body data
  25. *
  26. * @var string
  27. */
  28. public $response_data = '';
  29. /**
  30. * Information on the current request
  31. *
  32. * @var array cURL information array, see {@see https://secure.php.net/curl_getinfo}
  33. */
  34. public $info;
  35. /**
  36. * Version string
  37. *
  38. * @var long
  39. */
  40. public $version;
  41. /**
  42. * cURL handle
  43. *
  44. * @var resource
  45. */
  46. protected $handle;
  47. /**
  48. * Hook dispatcher instance
  49. *
  50. * @var Requests_Hooks
  51. */
  52. protected $hooks;
  53. /**
  54. * Have we finished the headers yet?
  55. *
  56. * @var boolean
  57. */
  58. protected $done_headers = false;
  59. /**
  60. * If streaming to a file, keep the file pointer
  61. *
  62. * @var resource
  63. */
  64. protected $stream_handle;
  65. /**
  66. * How many bytes are in the response body?
  67. *
  68. * @var int
  69. */
  70. protected $response_bytes;
  71. /**
  72. * What's the maximum number of bytes we should keep?
  73. *
  74. * @var int|bool Byte count, or false if no limit.
  75. */
  76. protected $response_byte_limit;
  77. /**
  78. * Constructor
  79. */
  80. public function __construct() {
  81. $curl = curl_version();
  82. $this->version = $curl['version_number'];
  83. $this->handle = curl_init();
  84. curl_setopt($this->handle, CURLOPT_HEADER, false);
  85. curl_setopt($this->handle, CURLOPT_RETURNTRANSFER, 1);
  86. if ($this->version >= self::CURL_7_10_5) {
  87. curl_setopt($this->handle, CURLOPT_ENCODING, '');
  88. }
  89. if (defined('CURLOPT_PROTOCOLS')) {
  90. curl_setopt($this->handle, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
  91. }
  92. if (defined('CURLOPT_REDIR_PROTOCOLS')) {
  93. curl_setopt($this->handle, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
  94. }
  95. }
  96. /**
  97. * Destructor
  98. */
  99. public function __destruct() {
  100. if (is_resource($this->handle)) {
  101. curl_close($this->handle);
  102. }
  103. }
  104. /**
  105. * Perform a request
  106. *
  107. * @throws Requests_Exception On a cURL error (`curlerror`)
  108. *
  109. * @param string $url URL to request
  110. * @param array $headers Associative array of request headers
  111. * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD
  112. * @param array $options Request options, see {@see Requests::response()} for documentation
  113. * @return string Raw HTTP result
  114. */
  115. public function request($url, $headers = array(), $data = array(), $options = array()) {
  116. $this->hooks = $options['hooks'];
  117. $this->setup_handle($url, $headers, $data, $options);
  118. $options['hooks']->dispatch('curl.before_send', array(&$this->handle));
  119. if ($options['filename'] !== false) {
  120. $this->stream_handle = fopen($options['filename'], 'wb');
  121. }
  122. $this->response_data = '';
  123. $this->response_bytes = 0;
  124. $this->response_byte_limit = false;
  125. if ($options['max_bytes'] !== false) {
  126. $this->response_byte_limit = $options['max_bytes'];
  127. }
  128. if (isset($options['verify'])) {
  129. if ($options['verify'] === false) {
  130. curl_setopt($this->handle, CURLOPT_SSL_VERIFYHOST, 0);
  131. curl_setopt($this->handle, CURLOPT_SSL_VERIFYPEER, 0);
  132. }
  133. elseif (is_string($options['verify'])) {
  134. curl_setopt($this->handle, CURLOPT_CAINFO, $options['verify']);
  135. }
  136. }
  137. if (isset($options['verifyname']) && $options['verifyname'] === false) {
  138. curl_setopt($this->handle, CURLOPT_SSL_VERIFYHOST, 0);
  139. }
  140. curl_exec($this->handle);
  141. $response = $this->response_data;
  142. $options['hooks']->dispatch('curl.after_send', array());
  143. if (curl_errno($this->handle) === 23 || curl_errno($this->handle) === 61) {
  144. // Reset encoding and try again
  145. curl_setopt($this->handle, CURLOPT_ENCODING, 'none');
  146. $this->response_data = '';
  147. $this->response_bytes = 0;
  148. curl_exec($this->handle);
  149. $response = $this->response_data;
  150. }
  151. $this->process_response($response, $options);
  152. // Need to remove the $this reference from the curl handle.
  153. // Otherwise Requests_Transport_cURL wont be garbage collected and the curl_close() will never be called.
  154. curl_setopt($this->handle, CURLOPT_HEADERFUNCTION, null);
  155. curl_setopt($this->handle, CURLOPT_WRITEFUNCTION, null);
  156. return $this->headers;
  157. }
  158. /**
  159. * Send multiple requests simultaneously
  160. *
  161. * @param array $requests Request data
  162. * @param array $options Global options
  163. * @return array Array of Requests_Response objects (may contain Requests_Exception or string responses as well)
  164. */
  165. public function request_multiple($requests, $options) {
  166. // If you're not requesting, we can't get any responses ¯\_(ツ)_/¯
  167. if (empty($requests)) {
  168. return array();
  169. }
  170. $multihandle = curl_multi_init();
  171. $subrequests = array();
  172. $subhandles = array();
  173. $class = get_class($this);
  174. foreach ($requests as $id => $request) {
  175. $subrequests[$id] = new $class();
  176. $subhandles[$id] = $subrequests[$id]->get_subrequest_handle($request['url'], $request['headers'], $request['data'], $request['options']);
  177. $request['options']['hooks']->dispatch('curl.before_multi_add', array(&$subhandles[$id]));
  178. curl_multi_add_handle($multihandle, $subhandles[$id]);
  179. }
  180. $completed = 0;
  181. $responses = array();
  182. $request['options']['hooks']->dispatch('curl.before_multi_exec', array(&$multihandle));
  183. do {
  184. $active = false;
  185. do {
  186. $status = curl_multi_exec($multihandle, $active);
  187. }
  188. while ($status === CURLM_CALL_MULTI_PERFORM);
  189. $to_process = array();
  190. // Read the information as needed
  191. while ($done = curl_multi_info_read($multihandle)) {
  192. $key = array_search($done['handle'], $subhandles, true);
  193. if (!isset($to_process[$key])) {
  194. $to_process[$key] = $done;
  195. }
  196. }
  197. // Parse the finished requests before we start getting the new ones
  198. foreach ($to_process as $key => $done) {
  199. $options = $requests[$key]['options'];
  200. if (CURLE_OK !== $done['result']) {
  201. //get error string for handle.
  202. $reason = curl_error($done['handle']);
  203. $exception = new Requests_Exception_Transport_cURL(
  204. $reason,
  205. Requests_Exception_Transport_cURL::EASY,
  206. $done['handle'],
  207. $done['result']
  208. );
  209. $responses[$key] = $exception;
  210. $options['hooks']->dispatch('transport.internal.parse_error', array(&$responses[$key], $requests[$key]));
  211. }
  212. else {
  213. $responses[$key] = $subrequests[$key]->process_response($subrequests[$key]->response_data, $options);
  214. $options['hooks']->dispatch('transport.internal.parse_response', array(&$responses[$key], $requests[$key]));
  215. }
  216. curl_multi_remove_handle($multihandle, $done['handle']);
  217. curl_close($done['handle']);
  218. if (!is_string($responses[$key])) {
  219. $options['hooks']->dispatch('multiple.request.complete', array(&$responses[$key], $key));
  220. }
  221. $completed++;
  222. }
  223. }
  224. while ($active || $completed < count($subrequests));
  225. $request['options']['hooks']->dispatch('curl.after_multi_exec', array(&$multihandle));
  226. curl_multi_close($multihandle);
  227. return $responses;
  228. }
  229. /**
  230. * Get the cURL handle for use in a multi-request
  231. *
  232. * @param string $url URL to request
  233. * @param array $headers Associative array of request headers
  234. * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD
  235. * @param array $options Request options, see {@see Requests::response()} for documentation
  236. * @return resource Subrequest's cURL handle
  237. */
  238. public function &get_subrequest_handle($url, $headers, $data, $options) {
  239. $this->setup_handle($url, $headers, $data, $options);
  240. if ($options['filename'] !== false) {
  241. $this->stream_handle = fopen($options['filename'], 'wb');
  242. }
  243. $this->response_data = '';
  244. $this->response_bytes = 0;
  245. $this->response_byte_limit = false;
  246. if ($options['max_bytes'] !== false) {
  247. $this->response_byte_limit = $options['max_bytes'];
  248. }
  249. $this->hooks = $options['hooks'];
  250. return $this->handle;
  251. }
  252. /**
  253. * Setup the cURL handle for the given data
  254. *
  255. * @param string $url URL to request
  256. * @param array $headers Associative array of request headers
  257. * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD
  258. * @param array $options Request options, see {@see Requests::response()} for documentation
  259. */
  260. protected function setup_handle($url, $headers, $data, $options) {
  261. $options['hooks']->dispatch('curl.before_request', array(&$this->handle));
  262. // Force closing the connection for old versions of cURL (<7.22).
  263. if ( ! isset( $headers['Connection'] ) ) {
  264. $headers['Connection'] = 'close';
  265. }
  266. $headers = Requests::flatten($headers);
  267. if (!empty($data)) {
  268. $data_format = $options['data_format'];
  269. if ($data_format === 'query') {
  270. $url = self::format_get($url, $data);
  271. $data = '';
  272. }
  273. elseif (!is_string($data)) {
  274. $data = http_build_query($data, null, '&');
  275. }
  276. }
  277. switch ($options['type']) {
  278. case Requests::POST:
  279. curl_setopt($this->handle, CURLOPT_POST, true);
  280. curl_setopt($this->handle, CURLOPT_POSTFIELDS, $data);
  281. break;
  282. case Requests::HEAD:
  283. curl_setopt($this->handle, CURLOPT_CUSTOMREQUEST, $options['type']);
  284. curl_setopt($this->handle, CURLOPT_NOBODY, true);
  285. break;
  286. case Requests::TRACE:
  287. curl_setopt($this->handle, CURLOPT_CUSTOMREQUEST, $options['type']);
  288. break;
  289. case Requests::PATCH:
  290. case Requests::PUT:
  291. case Requests::DELETE:
  292. case Requests::OPTIONS:
  293. default:
  294. curl_setopt($this->handle, CURLOPT_CUSTOMREQUEST, $options['type']);
  295. if (!empty($data)) {
  296. curl_setopt($this->handle, CURLOPT_POSTFIELDS, $data);
  297. }
  298. }
  299. // cURL requires a minimum timeout of 1 second when using the system
  300. // DNS resolver, as it uses `alarm()`, which is second resolution only.
  301. // There's no way to detect which DNS resolver is being used from our
  302. // end, so we need to round up regardless of the supplied timeout.
  303. //
  304. // https://github.com/curl/curl/blob/4f45240bc84a9aa648c8f7243be7b79e9f9323a5/lib/hostip.c#L606-L609
  305. $timeout = max($options['timeout'], 1);
  306. if (is_int($timeout) || $this->version < self::CURL_7_16_2) {
  307. curl_setopt($this->handle, CURLOPT_TIMEOUT, ceil($timeout));
  308. }
  309. else {
  310. curl_setopt($this->handle, CURLOPT_TIMEOUT_MS, round($timeout * 1000));
  311. }
  312. if (is_int($options['connect_timeout']) || $this->version < self::CURL_7_16_2) {
  313. curl_setopt($this->handle, CURLOPT_CONNECTTIMEOUT, ceil($options['connect_timeout']));
  314. }
  315. else {
  316. curl_setopt($this->handle, CURLOPT_CONNECTTIMEOUT_MS, round($options['connect_timeout'] * 1000));
  317. }
  318. curl_setopt($this->handle, CURLOPT_URL, $url);
  319. curl_setopt($this->handle, CURLOPT_REFERER, $url);
  320. curl_setopt($this->handle, CURLOPT_USERAGENT, $options['useragent']);
  321. if (!empty($headers)) {
  322. curl_setopt($this->handle, CURLOPT_HTTPHEADER, $headers);
  323. }
  324. if ($options['protocol_version'] === 1.1) {
  325. curl_setopt($this->handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  326. }
  327. else {
  328. curl_setopt($this->handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  329. }
  330. if (true === $options['blocking']) {
  331. curl_setopt($this->handle, CURLOPT_HEADERFUNCTION, array(&$this, 'stream_headers'));
  332. curl_setopt($this->handle, CURLOPT_WRITEFUNCTION, array(&$this, 'stream_body'));
  333. curl_setopt($this->handle, CURLOPT_BUFFERSIZE, Requests::BUFFER_SIZE);
  334. }
  335. }
  336. /**
  337. * Process a response
  338. *
  339. * @param string $response Response data from the body
  340. * @param array $options Request options
  341. * @return string HTTP response data including headers
  342. */
  343. public function process_response($response, $options) {
  344. if ($options['blocking'] === false) {
  345. $fake_headers = '';
  346. $options['hooks']->dispatch('curl.after_request', array(&$fake_headers));
  347. return false;
  348. }
  349. if ($options['filename'] !== false) {
  350. fclose($this->stream_handle);
  351. $this->headers = trim($this->headers);
  352. }
  353. else {
  354. $this->headers .= $response;
  355. }
  356. if (curl_errno($this->handle)) {
  357. $error = sprintf(
  358. 'cURL error %s: %s',
  359. curl_errno($this->handle),
  360. curl_error($this->handle)
  361. );
  362. throw new Requests_Exception($error, 'curlerror', $this->handle);
  363. }
  364. $this->info = curl_getinfo($this->handle);
  365. $options['hooks']->dispatch('curl.after_request', array(&$this->headers, &$this->info));
  366. return $this->headers;
  367. }
  368. /**
  369. * Collect the headers as they are received
  370. *
  371. * @param resource $handle cURL resource
  372. * @param string $headers Header string
  373. * @return integer Length of provided header
  374. */
  375. public function stream_headers($handle, $headers) {
  376. // Why do we do this? cURL will send both the final response and any
  377. // interim responses, such as a 100 Continue. We don't need that.
  378. // (We may want to keep this somewhere just in case)
  379. if ($this->done_headers) {
  380. $this->headers = '';
  381. $this->done_headers = false;
  382. }
  383. $this->headers .= $headers;
  384. if ($headers === "\r\n") {
  385. $this->done_headers = true;
  386. }
  387. return strlen($headers);
  388. }
  389. /**
  390. * Collect data as it's received
  391. *
  392. * @since 1.6.1
  393. *
  394. * @param resource $handle cURL resource
  395. * @param string $data Body data
  396. * @return integer Length of provided data
  397. */
  398. public function stream_body($handle, $data) {
  399. $this->hooks->dispatch('request.progress', array($data, $this->response_bytes, $this->response_byte_limit));
  400. $data_length = strlen($data);
  401. // Are we limiting the response size?
  402. if ($this->response_byte_limit) {
  403. if ($this->response_bytes === $this->response_byte_limit) {
  404. // Already at maximum, move on
  405. return $data_length;
  406. }
  407. if (($this->response_bytes + $data_length) > $this->response_byte_limit) {
  408. // Limit the length
  409. $limited_length = ($this->response_byte_limit - $this->response_bytes);
  410. $data = substr($data, 0, $limited_length);
  411. }
  412. }
  413. if ($this->stream_handle) {
  414. fwrite($this->stream_handle, $data);
  415. }
  416. else {
  417. $this->response_data .= $data;
  418. }
  419. $this->response_bytes += strlen($data);
  420. return $data_length;
  421. }
  422. /**
  423. * Format a URL given GET data
  424. *
  425. * @param string $url
  426. * @param array|object $data Data to build query using, see {@see https://secure.php.net/http_build_query}
  427. * @return string URL with data
  428. */
  429. protected static function format_get($url, $data) {
  430. if (!empty($data)) {
  431. $url_parts = parse_url($url);
  432. if (empty($url_parts['query'])) {
  433. $query = $url_parts['query'] = '';
  434. }
  435. else {
  436. $query = $url_parts['query'];
  437. }
  438. $query .= '&' . http_build_query($data, null, '&');
  439. $query = trim($query, '&');
  440. if (empty($url_parts['query'])) {
  441. $url .= '?' . $query;
  442. }
  443. else {
  444. $url = str_replace($url_parts['query'], $query, $url);
  445. }
  446. }
  447. return $url;
  448. }
  449. /**
  450. * Whether this transport is valid
  451. *
  452. * @codeCoverageIgnore
  453. * @return boolean True if the transport is valid, false otherwise.
  454. */
  455. public static function test($capabilities = array()) {
  456. if (!function_exists('curl_init') || !function_exists('curl_exec')) {
  457. return false;
  458. }
  459. // If needed, check that our installed curl version supports SSL
  460. if (isset($capabilities['ssl']) && $capabilities['ssl']) {
  461. $curl_version = curl_version();
  462. if (!(CURL_VERSION_SSL & $curl_version['features'])) {
  463. return false;
  464. }
  465. }
  466. return true;
  467. }
  468. }