截流自动化的商城平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cURL.php 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. * cURL version number
  37. *
  38. * @var int
  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. // phpcs:ignore PHPCompatibility.Constants.NewConstants.curlopt_protocolsFound
  91. curl_setopt($this->handle, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
  92. }
  93. if (defined('CURLOPT_REDIR_PROTOCOLS')) {
  94. // phpcs:ignore PHPCompatibility.Constants.NewConstants.curlopt_redir_protocolsFound
  95. curl_setopt($this->handle, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
  96. }
  97. }
  98. /**
  99. * Destructor
  100. */
  101. public function __destruct() {
  102. if (is_resource($this->handle)) {
  103. curl_close($this->handle);
  104. }
  105. }
  106. /**
  107. * Perform a request
  108. *
  109. * @throws Requests_Exception On a cURL error (`curlerror`)
  110. *
  111. * @param string $url URL to request
  112. * @param array $headers Associative array of request headers
  113. * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD
  114. * @param array $options Request options, see {@see Requests::response()} for documentation
  115. * @return string Raw HTTP result
  116. */
  117. public function request($url, $headers = array(), $data = array(), $options = array()) {
  118. $this->hooks = $options['hooks'];
  119. $this->setup_handle($url, $headers, $data, $options);
  120. $options['hooks']->dispatch('curl.before_send', array(&$this->handle));
  121. if ($options['filename'] !== false) {
  122. $this->stream_handle = fopen($options['filename'], 'wb');
  123. }
  124. $this->response_data = '';
  125. $this->response_bytes = 0;
  126. $this->response_byte_limit = false;
  127. if ($options['max_bytes'] !== false) {
  128. $this->response_byte_limit = $options['max_bytes'];
  129. }
  130. if (isset($options['verify'])) {
  131. if ($options['verify'] === false) {
  132. curl_setopt($this->handle, CURLOPT_SSL_VERIFYHOST, 0);
  133. curl_setopt($this->handle, CURLOPT_SSL_VERIFYPEER, 0);
  134. }
  135. elseif (is_string($options['verify'])) {
  136. curl_setopt($this->handle, CURLOPT_CAINFO, $options['verify']);
  137. }
  138. }
  139. if (isset($options['verifyname']) && $options['verifyname'] === false) {
  140. curl_setopt($this->handle, CURLOPT_SSL_VERIFYHOST, 0);
  141. }
  142. curl_exec($this->handle);
  143. $response = $this->response_data;
  144. $options['hooks']->dispatch('curl.after_send', array());
  145. if (curl_errno($this->handle) === 23 || curl_errno($this->handle) === 61) {
  146. // Reset encoding and try again
  147. curl_setopt($this->handle, CURLOPT_ENCODING, 'none');
  148. $this->response_data = '';
  149. $this->response_bytes = 0;
  150. curl_exec($this->handle);
  151. $response = $this->response_data;
  152. }
  153. $this->process_response($response, $options);
  154. // Need to remove the $this reference from the curl handle.
  155. // Otherwise Requests_Transport_cURL wont be garbage collected and the curl_close() will never be called.
  156. curl_setopt($this->handle, CURLOPT_HEADERFUNCTION, null);
  157. curl_setopt($this->handle, CURLOPT_WRITEFUNCTION, null);
  158. return $this->headers;
  159. }
  160. /**
  161. * Send multiple requests simultaneously
  162. *
  163. * @param array $requests Request data
  164. * @param array $options Global options
  165. * @return array Array of Requests_Response objects (may contain Requests_Exception or string responses as well)
  166. */
  167. public function request_multiple($requests, $options) {
  168. // If you're not requesting, we can't get any responses ¯\_(ツ)_/¯
  169. if (empty($requests)) {
  170. return array();
  171. }
  172. $multihandle = curl_multi_init();
  173. $subrequests = array();
  174. $subhandles = array();
  175. $class = get_class($this);
  176. foreach ($requests as $id => $request) {
  177. $subrequests[$id] = new $class();
  178. $subhandles[$id] = $subrequests[$id]->get_subrequest_handle($request['url'], $request['headers'], $request['data'], $request['options']);
  179. $request['options']['hooks']->dispatch('curl.before_multi_add', array(&$subhandles[$id]));
  180. curl_multi_add_handle($multihandle, $subhandles[$id]);
  181. }
  182. $completed = 0;
  183. $responses = array();
  184. $subrequestcount = count($subrequests);
  185. $request['options']['hooks']->dispatch('curl.before_multi_exec', array(&$multihandle));
  186. do {
  187. $active = 0;
  188. do {
  189. $status = curl_multi_exec($multihandle, $active);
  190. }
  191. while ($status === CURLM_CALL_MULTI_PERFORM);
  192. $to_process = array();
  193. // Read the information as needed
  194. while ($done = curl_multi_info_read($multihandle)) {
  195. $key = array_search($done['handle'], $subhandles, true);
  196. if (!isset($to_process[$key])) {
  197. $to_process[$key] = $done;
  198. }
  199. }
  200. // Parse the finished requests before we start getting the new ones
  201. foreach ($to_process as $key => $done) {
  202. $options = $requests[$key]['options'];
  203. if ($done['result'] !== CURLE_OK) {
  204. //get error string for handle.
  205. $reason = curl_error($done['handle']);
  206. $exception = new Requests_Exception_Transport_cURL(
  207. $reason,
  208. Requests_Exception_Transport_cURL::EASY,
  209. $done['handle'],
  210. $done['result']
  211. );
  212. $responses[$key] = $exception;
  213. $options['hooks']->dispatch('transport.internal.parse_error', array(&$responses[$key], $requests[$key]));
  214. }
  215. else {
  216. $responses[$key] = $subrequests[$key]->process_response($subrequests[$key]->response_data, $options);
  217. $options['hooks']->dispatch('transport.internal.parse_response', array(&$responses[$key], $requests[$key]));
  218. }
  219. curl_multi_remove_handle($multihandle, $done['handle']);
  220. curl_close($done['handle']);
  221. if (!is_string($responses[$key])) {
  222. $options['hooks']->dispatch('multiple.request.complete', array(&$responses[$key], $key));
  223. }
  224. $completed++;
  225. }
  226. }
  227. while ($active || $completed < $subrequestcount);
  228. $request['options']['hooks']->dispatch('curl.after_multi_exec', array(&$multihandle));
  229. curl_multi_close($multihandle);
  230. return $responses;
  231. }
  232. /**
  233. * Get the cURL handle for use in a multi-request
  234. *
  235. * @param string $url URL to request
  236. * @param array $headers Associative array of request headers
  237. * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD
  238. * @param array $options Request options, see {@see Requests::response()} for documentation
  239. * @return resource Subrequest's cURL handle
  240. */
  241. public function &get_subrequest_handle($url, $headers, $data, $options) {
  242. $this->setup_handle($url, $headers, $data, $options);
  243. if ($options['filename'] !== false) {
  244. $this->stream_handle = fopen($options['filename'], 'wb');
  245. }
  246. $this->response_data = '';
  247. $this->response_bytes = 0;
  248. $this->response_byte_limit = false;
  249. if ($options['max_bytes'] !== false) {
  250. $this->response_byte_limit = $options['max_bytes'];
  251. }
  252. $this->hooks = $options['hooks'];
  253. return $this->handle;
  254. }
  255. /**
  256. * Setup the cURL handle for the given data
  257. *
  258. * @param string $url URL to request
  259. * @param array $headers Associative array of request headers
  260. * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD
  261. * @param array $options Request options, see {@see Requests::response()} for documentation
  262. */
  263. protected function setup_handle($url, $headers, $data, $options) {
  264. $options['hooks']->dispatch('curl.before_request', array(&$this->handle));
  265. // Force closing the connection for old versions of cURL (<7.22).
  266. if (!isset($headers['Connection'])) {
  267. $headers['Connection'] = 'close';
  268. }
  269. /**
  270. * Add "Expect" header.
  271. *
  272. * By default, cURL adds a "Expect: 100-Continue" to most requests. This header can
  273. * add as much as a second to the time it takes for cURL to perform a request. To
  274. * prevent this, we need to set an empty "Expect" header. To match the behaviour of
  275. * Guzzle, we'll add the empty header to requests that are smaller than 1 MB and use
  276. * HTTP/1.1.
  277. *
  278. * https://curl.se/mail/lib-2017-07/0013.html
  279. */
  280. if (!isset($headers['Expect']) && $options['protocol_version'] === 1.1) {
  281. $headers['Expect'] = $this->get_expect_header($data);
  282. }
  283. $headers = Requests::flatten($headers);
  284. if (!empty($data)) {
  285. $data_format = $options['data_format'];
  286. if ($data_format === 'query') {
  287. $url = self::format_get($url, $data);
  288. $data = '';
  289. }
  290. elseif (!is_string($data)) {
  291. $data = http_build_query($data, null, '&');
  292. }
  293. }
  294. switch ($options['type']) {
  295. case Requests::POST:
  296. curl_setopt($this->handle, CURLOPT_POST, true);
  297. curl_setopt($this->handle, CURLOPT_POSTFIELDS, $data);
  298. break;
  299. case Requests::HEAD:
  300. curl_setopt($this->handle, CURLOPT_CUSTOMREQUEST, $options['type']);
  301. curl_setopt($this->handle, CURLOPT_NOBODY, true);
  302. break;
  303. case Requests::TRACE:
  304. curl_setopt($this->handle, CURLOPT_CUSTOMREQUEST, $options['type']);
  305. break;
  306. case Requests::PATCH:
  307. case Requests::PUT:
  308. case Requests::DELETE:
  309. case Requests::OPTIONS:
  310. default:
  311. curl_setopt($this->handle, CURLOPT_CUSTOMREQUEST, $options['type']);
  312. if (!empty($data)) {
  313. curl_setopt($this->handle, CURLOPT_POSTFIELDS, $data);
  314. }
  315. }
  316. // cURL requires a minimum timeout of 1 second when using the system
  317. // DNS resolver, as it uses `alarm()`, which is second resolution only.
  318. // There's no way to detect which DNS resolver is being used from our
  319. // end, so we need to round up regardless of the supplied timeout.
  320. //
  321. // https://github.com/curl/curl/blob/4f45240bc84a9aa648c8f7243be7b79e9f9323a5/lib/hostip.c#L606-L609
  322. $timeout = max($options['timeout'], 1);
  323. if (is_int($timeout) || $this->version < self::CURL_7_16_2) {
  324. curl_setopt($this->handle, CURLOPT_TIMEOUT, ceil($timeout));
  325. }
  326. else {
  327. // phpcs:ignore PHPCompatibility.Constants.NewConstants.curlopt_timeout_msFound
  328. curl_setopt($this->handle, CURLOPT_TIMEOUT_MS, round($timeout * 1000));
  329. }
  330. if (is_int($options['connect_timeout']) || $this->version < self::CURL_7_16_2) {
  331. curl_setopt($this->handle, CURLOPT_CONNECTTIMEOUT, ceil($options['connect_timeout']));
  332. }
  333. else {
  334. // phpcs:ignore PHPCompatibility.Constants.NewConstants.curlopt_connecttimeout_msFound
  335. curl_setopt($this->handle, CURLOPT_CONNECTTIMEOUT_MS, round($options['connect_timeout'] * 1000));
  336. }
  337. curl_setopt($this->handle, CURLOPT_URL, $url);
  338. curl_setopt($this->handle, CURLOPT_REFERER, $url);
  339. curl_setopt($this->handle, CURLOPT_USERAGENT, $options['useragent']);
  340. if (!empty($headers)) {
  341. curl_setopt($this->handle, CURLOPT_HTTPHEADER, $headers);
  342. }
  343. if ($options['protocol_version'] === 1.1) {
  344. curl_setopt($this->handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  345. }
  346. else {
  347. curl_setopt($this->handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  348. }
  349. if ($options['blocking'] === true) {
  350. curl_setopt($this->handle, CURLOPT_HEADERFUNCTION, array($this, 'stream_headers'));
  351. curl_setopt($this->handle, CURLOPT_WRITEFUNCTION, array($this, 'stream_body'));
  352. curl_setopt($this->handle, CURLOPT_BUFFERSIZE, Requests::BUFFER_SIZE);
  353. }
  354. }
  355. /**
  356. * Process a response
  357. *
  358. * @param string $response Response data from the body
  359. * @param array $options Request options
  360. * @return string|false HTTP response data including headers. False if non-blocking.
  361. * @throws Requests_Exception
  362. */
  363. public function process_response($response, $options) {
  364. if ($options['blocking'] === false) {
  365. $fake_headers = '';
  366. $options['hooks']->dispatch('curl.after_request', array(&$fake_headers));
  367. return false;
  368. }
  369. if ($options['filename'] !== false && $this->stream_handle) {
  370. fclose($this->stream_handle);
  371. $this->headers = trim($this->headers);
  372. }
  373. else {
  374. $this->headers .= $response;
  375. }
  376. if (curl_errno($this->handle)) {
  377. $error = sprintf(
  378. 'cURL error %s: %s',
  379. curl_errno($this->handle),
  380. curl_error($this->handle)
  381. );
  382. throw new Requests_Exception($error, 'curlerror', $this->handle);
  383. }
  384. $this->info = curl_getinfo($this->handle);
  385. $options['hooks']->dispatch('curl.after_request', array(&$this->headers, &$this->info));
  386. return $this->headers;
  387. }
  388. /**
  389. * Collect the headers as they are received
  390. *
  391. * @param resource $handle cURL resource
  392. * @param string $headers Header string
  393. * @return integer Length of provided header
  394. */
  395. public function stream_headers($handle, $headers) {
  396. // Why do we do this? cURL will send both the final response and any
  397. // interim responses, such as a 100 Continue. We don't need that.
  398. // (We may want to keep this somewhere just in case)
  399. if ($this->done_headers) {
  400. $this->headers = '';
  401. $this->done_headers = false;
  402. }
  403. $this->headers .= $headers;
  404. if ($headers === "\r\n") {
  405. $this->done_headers = true;
  406. }
  407. return strlen($headers);
  408. }
  409. /**
  410. * Collect data as it's received
  411. *
  412. * @since 1.6.1
  413. *
  414. * @param resource $handle cURL resource
  415. * @param string $data Body data
  416. * @return integer Length of provided data
  417. */
  418. public function stream_body($handle, $data) {
  419. $this->hooks->dispatch('request.progress', array($data, $this->response_bytes, $this->response_byte_limit));
  420. $data_length = strlen($data);
  421. // Are we limiting the response size?
  422. if ($this->response_byte_limit) {
  423. if ($this->response_bytes === $this->response_byte_limit) {
  424. // Already at maximum, move on
  425. return $data_length;
  426. }
  427. if (($this->response_bytes + $data_length) > $this->response_byte_limit) {
  428. // Limit the length
  429. $limited_length = ($this->response_byte_limit - $this->response_bytes);
  430. $data = substr($data, 0, $limited_length);
  431. }
  432. }
  433. if ($this->stream_handle) {
  434. fwrite($this->stream_handle, $data);
  435. }
  436. else {
  437. $this->response_data .= $data;
  438. }
  439. $this->response_bytes += strlen($data);
  440. return $data_length;
  441. }
  442. /**
  443. * Format a URL given GET data
  444. *
  445. * @param string $url
  446. * @param array|object $data Data to build query using, see {@see https://secure.php.net/http_build_query}
  447. * @return string URL with data
  448. */
  449. protected static function format_get($url, $data) {
  450. if (!empty($data)) {
  451. $query = '';
  452. $url_parts = parse_url($url);
  453. if (empty($url_parts['query'])) {
  454. $url_parts['query'] = '';
  455. }
  456. else {
  457. $query = $url_parts['query'];
  458. }
  459. $query .= '&' . http_build_query($data, null, '&');
  460. $query = trim($query, '&');
  461. if (empty($url_parts['query'])) {
  462. $url .= '?' . $query;
  463. }
  464. else {
  465. $url = str_replace($url_parts['query'], $query, $url);
  466. }
  467. }
  468. return $url;
  469. }
  470. /**
  471. * Whether this transport is valid
  472. *
  473. * @codeCoverageIgnore
  474. * @return boolean True if the transport is valid, false otherwise.
  475. */
  476. public static function test($capabilities = array()) {
  477. if (!function_exists('curl_init') || !function_exists('curl_exec')) {
  478. return false;
  479. }
  480. // If needed, check that our installed curl version supports SSL
  481. if (isset($capabilities['ssl']) && $capabilities['ssl']) {
  482. $curl_version = curl_version();
  483. if (!(CURL_VERSION_SSL & $curl_version['features'])) {
  484. return false;
  485. }
  486. }
  487. return true;
  488. }
  489. /**
  490. * Get the correct "Expect" header for the given request data.
  491. *
  492. * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD.
  493. * @return string The "Expect" header.
  494. */
  495. protected function get_expect_header($data) {
  496. if (!is_array($data)) {
  497. return strlen((string) $data) >= 1048576 ? '100-Continue' : '';
  498. }
  499. $bytesize = 0;
  500. $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($data));
  501. foreach ($iterator as $datum) {
  502. $bytesize += strlen((string) $datum);
  503. if ($bytesize >= 1048576) {
  504. return '100-Continue';
  505. }
  506. }
  507. return '';
  508. }
  509. }