/dev/tests/functional/lib/Magento/Mtf/Util/Protocol/CurlTransport.php

https://gitlab.com/LisovyiEvhenii/ismextensions · PHP · 283 lines · 132 code · 25 blank · 126 comment · 10 complexity · 3a31b2a932bf62462c63ef25c29b5c62 MD5 · raw file

  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magento.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magento.com for more information.
  20. *
  21. * @category Tests
  22. * @package Tests_Functional
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. namespace Magento\Mtf\Util\Protocol;
  27. /**
  28. * HTTP CURL Adapter.
  29. */
  30. class CurlTransport implements CurlInterface
  31. {
  32. /**
  33. * Parameters array
  34. *
  35. * @var array
  36. */
  37. protected $_config = [];
  38. /**
  39. * Curl handle.
  40. *
  41. * @var resource
  42. */
  43. protected $_resource;
  44. /**
  45. * Allow parameters.
  46. *
  47. * @var array
  48. */
  49. protected $_allowedParams = [
  50. 'timeout' => CURLOPT_TIMEOUT,
  51. 'maxredirects' => CURLOPT_MAXREDIRS,
  52. 'proxy' => CURLOPT_PROXY,
  53. 'ssl_cert' => CURLOPT_SSLCERT,
  54. 'userpwd' => CURLOPT_USERPWD
  55. ];
  56. /**
  57. * Array of CURL options.
  58. *
  59. * @var array
  60. */
  61. protected $_options = [];
  62. /**
  63. * Apply current configuration array to curl resource.
  64. *
  65. * @return $this
  66. */
  67. protected function _applyConfig()
  68. {
  69. // apply additional options to cURL
  70. foreach ($this->_options as $option => $value) {
  71. curl_setopt($this->_getResource(), $option, $value);
  72. }
  73. if (empty($this->_config)) {
  74. return $this;
  75. }
  76. $verifyPeer = isset($this->_config['verifypeer']) ? : 0;
  77. curl_setopt($this->_getResource(), CURLOPT_SSL_VERIFYPEER, $verifyPeer);
  78. $verifyHost = isset($this->_config['verifyhost']) ? : 0;
  79. curl_setopt($this->_getResource(), CURLOPT_SSL_VERIFYHOST, $verifyHost);
  80. foreach ($this->_config as $param => $curlOption) {
  81. if (array_key_exists($param, $this->_allowedParams)) {
  82. curl_setopt($this->_getResource(), $this->_allowedParams[$param], $this->_config[$param]);
  83. }
  84. }
  85. return $this;
  86. }
  87. /**
  88. * Set array of additional cURL options.
  89. *
  90. * @param array $options
  91. * @return $this
  92. */
  93. public function setOptions(array $options = [])
  94. {
  95. $this->_options = $options;
  96. return $this;
  97. }
  98. /**
  99. * Add additional option to cURL.
  100. *
  101. * @param int $option
  102. * @param mixed $value
  103. * @return $this
  104. */
  105. public function addOption($option, $value)
  106. {
  107. $this->_options[$option] = $value;
  108. return $this;
  109. }
  110. /**
  111. * Set the configuration array for the adapter.
  112. *
  113. * @param array $config [optional]
  114. * @return $this
  115. */
  116. public function setConfig($config = [])
  117. {
  118. $this->_config = $config;
  119. return $this;
  120. }
  121. /**
  122. * Send request to the remote server.
  123. *
  124. * @param $method
  125. * @param $url
  126. * @param string $http_ver [optional]
  127. * @param array $headers [optional]
  128. * @param array $params [optional]
  129. * @return void
  130. */
  131. public function write($method, $url, $http_ver = '1.1', $headers = [], $params = [])
  132. {
  133. $this->_applyConfig();
  134. $options = [
  135. CURLOPT_URL => $url,
  136. CURLOPT_RETURNTRANSFER => true,
  137. CURLOPT_FOLLOWLOCATION => true,
  138. CURLOPT_HTTPHEADER => $headers,
  139. CURLOPT_COOKIEFILE => ''
  140. ];
  141. if ($method == CurlInterface::POST) {
  142. $options[CURLOPT_POST] = true;
  143. $options[CURLOPT_POSTFIELDS] = $params;
  144. } elseif ($method == CurlInterface::GET) {
  145. $options[CURLOPT_HTTPGET] = true;
  146. }
  147. curl_setopt_array($this->_getResource(), $options);
  148. }
  149. /**
  150. * Read response from server.
  151. *
  152. * @return string
  153. */
  154. public function read()
  155. {
  156. $response = curl_exec($this->_getResource());
  157. return $response;
  158. }
  159. /**
  160. * Close the connection to the server.
  161. *
  162. * @return void
  163. */
  164. public function close()
  165. {
  166. curl_close($this->_getResource());
  167. $this->_resource = null;
  168. }
  169. /**
  170. * Returns a cURL handle on success.
  171. *
  172. * @return resource
  173. */
  174. protected function _getResource()
  175. {
  176. if (is_null($this->_resource)) {
  177. $this->_resource = curl_init();
  178. }
  179. return $this->_resource;
  180. }
  181. /**
  182. * Get last error number.
  183. *
  184. * @return int
  185. */
  186. public function getErrno()
  187. {
  188. return curl_errno($this->_getResource());
  189. }
  190. /**
  191. * Get string with last error for the current session.
  192. *
  193. * @return string
  194. */
  195. public function getError()
  196. {
  197. return curl_error($this->_getResource());
  198. }
  199. /**
  200. * Get information regarding a specific transfer.
  201. *
  202. * @param int $opt CURLINFO option
  203. * @return mixed
  204. */
  205. public function getInfo($opt = 0)
  206. {
  207. return curl_getinfo($this->_getResource(), $opt);
  208. }
  209. /**
  210. * curl_multi_* requests support.
  211. *
  212. * @param array $urls
  213. * @param array $options [optional]
  214. * @return array
  215. */
  216. public function multiRequest($urls, $options = [])
  217. {
  218. $handles = [];
  219. $result = [];
  220. $multihandle = curl_multi_init();
  221. foreach ($urls as $key => $url) {
  222. $handles[$key] = curl_init();
  223. curl_setopt($handles[$key], CURLOPT_URL, $url);
  224. curl_setopt($handles[$key], CURLOPT_HEADER, 0);
  225. curl_setopt($handles[$key], CURLOPT_RETURNTRANSFER, 1);
  226. if (!empty($options)) {
  227. curl_setopt_array($handles[$key], $options);
  228. }
  229. curl_multi_add_handle($multihandle, $handles[$key]);
  230. }
  231. $process = null;
  232. do {
  233. curl_multi_exec($multihandle, $process);
  234. usleep(100);
  235. } while ($process > 0);
  236. foreach ($handles as $key => $handle) {
  237. $result[$key] = curl_multi_getcontent($handle);
  238. curl_multi_remove_handle($multihandle, $handle);
  239. }
  240. curl_multi_close($multihandle);
  241. return $result;
  242. }
  243. /**
  244. * Extract the response code from a response string.
  245. *
  246. * @param string $response_str
  247. * @return int
  248. */
  249. public static function extractCode($response_str)
  250. {
  251. preg_match("|^HTTP/[\d\.x]+ (\d+)|", $response_str, $m);
  252. if (isset($m[1])) {
  253. return (int)$m[1];
  254. } else {
  255. return false;
  256. }
  257. }
  258. }