PageRenderTime 27ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/modules/requests/Requests/Transport/cURL.php

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