Stream.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  10. * @link https://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Http\Client\Adapter;
  15. use Cake\Core\Exception\Exception;
  16. use Cake\Http\Client\Request;
  17. use Cake\Http\Client\Response;
  18. use Cake\Network\Exception\HttpException;
  19. /**
  20. * Implements sending Cake\Http\Client\Request
  21. * via php's stream API.
  22. *
  23. * This approach and implementation is partly inspired by Aura.Http
  24. */
  25. class Stream
  26. {
  27. /**
  28. * Context resource used by the stream API.
  29. *
  30. * @var resource|null
  31. */
  32. protected $_context;
  33. /**
  34. * Array of options/content for the HTTP stream context.
  35. *
  36. * @var array
  37. */
  38. protected $_contextOptions;
  39. /**
  40. * Array of options/content for the SSL stream context.
  41. *
  42. * @var array
  43. */
  44. protected $_sslContextOptions;
  45. /**
  46. * The stream resource.
  47. *
  48. * @var resource|null
  49. */
  50. protected $_stream;
  51. /**
  52. * Connection error list.
  53. *
  54. * @var array
  55. */
  56. protected $_connectionErrors = [];
  57. /**
  58. * Send a request and get a response back.
  59. *
  60. * @param \Cake\Http\Client\Request $request The request object to send.
  61. * @param array $options Array of options for the stream.
  62. * @return array Array of populated Response objects
  63. */
  64. public function send(Request $request, array $options)
  65. {
  66. $this->_stream = null;
  67. $this->_context = null;
  68. $this->_contextOptions = [];
  69. $this->_sslContextOptions = [];
  70. $this->_connectionErrors = [];
  71. $this->_buildContext($request, $options);
  72. return $this->_send($request);
  73. }
  74. /**
  75. * Create the response list based on the headers & content
  76. *
  77. * Creates one or many response objects based on the number
  78. * of redirects that occurred.
  79. *
  80. * @param array $headers The list of headers from the request(s)
  81. * @param string $content The response content.
  82. * @return \Cake\Http\Client\Response[] The list of responses from the request(s)
  83. */
  84. public function createResponses($headers, $content)
  85. {
  86. $indexes = $responses = [];
  87. foreach ($headers as $i => $header) {
  88. if (strtoupper(substr($header, 0, 5)) === 'HTTP/') {
  89. $indexes[] = $i;
  90. }
  91. }
  92. $last = count($indexes) - 1;
  93. foreach ($indexes as $i => $start) {
  94. $end = isset($indexes[$i + 1]) ? $indexes[$i + 1] - $start : null;
  95. $headerSlice = array_slice($headers, $start, $end);
  96. $body = $i == $last ? $content : '';
  97. $responses[] = $this->_buildResponse($headerSlice, $body);
  98. }
  99. return $responses;
  100. }
  101. /**
  102. * Build the stream context out of the request object.
  103. *
  104. * @param \Cake\Http\Client\Request $request The request to build context from.
  105. * @param array $options Additional request options.
  106. * @return void
  107. */
  108. protected function _buildContext(Request $request, $options)
  109. {
  110. $this->_buildContent($request, $options);
  111. $this->_buildHeaders($request, $options);
  112. $this->_buildOptions($request, $options);
  113. $url = $request->getUri();
  114. $scheme = parse_url($url, PHP_URL_SCHEME);
  115. if ($scheme === 'https') {
  116. $this->_buildSslContext($request, $options);
  117. }
  118. $this->_context = stream_context_create([
  119. 'http' => $this->_contextOptions,
  120. 'ssl' => $this->_sslContextOptions,
  121. ]);
  122. }
  123. /**
  124. * Build the header context for the request.
  125. *
  126. * Creates cookies & headers.
  127. *
  128. * @param \Cake\Http\Client\Request $request The request being sent.
  129. * @param array $options Array of options to use.
  130. * @return void
  131. */
  132. protected function _buildHeaders(Request $request, $options)
  133. {
  134. $headers = [];
  135. foreach ($request->getHeaders() as $name => $values) {
  136. $headers[] = sprintf('%s: %s', $name, implode(', ', $values));
  137. }
  138. $this->_contextOptions['header'] = implode("\r\n", $headers);
  139. }
  140. /**
  141. * Builds the request content based on the request object.
  142. *
  143. * If the $request->body() is a string, it will be used as is.
  144. * Array data will be processed with Cake\Http\Client\FormData
  145. *
  146. * @param \Cake\Http\Client\Request $request The request being sent.
  147. * @param array $options Array of options to use.
  148. * @return void
  149. */
  150. protected function _buildContent(Request $request, $options)
  151. {
  152. $body = $request->getBody();
  153. if (empty($body)) {
  154. $this->_contextOptions['content'] = '';
  155. return;
  156. }
  157. $body->rewind();
  158. $this->_contextOptions['content'] = $body->getContents();
  159. }
  160. /**
  161. * Build miscellaneous options for the request.
  162. *
  163. * @param \Cake\Http\Client\Request $request The request being sent.
  164. * @param array $options Array of options to use.
  165. * @return void
  166. */
  167. protected function _buildOptions(Request $request, $options)
  168. {
  169. $this->_contextOptions['method'] = $request->getMethod();
  170. $this->_contextOptions['protocol_version'] = $request->getProtocolVersion();
  171. $this->_contextOptions['ignore_errors'] = true;
  172. if (isset($options['timeout'])) {
  173. $this->_contextOptions['timeout'] = $options['timeout'];
  174. }
  175. // Redirects are handled in the client layer because of cookie handling issues.
  176. $this->_contextOptions['max_redirects'] = 0;
  177. if (isset($options['proxy']['proxy'])) {
  178. $this->_contextOptions['request_fulluri'] = true;
  179. $this->_contextOptions['proxy'] = $options['proxy']['proxy'];
  180. }
  181. }
  182. /**
  183. * Build SSL options for the request.
  184. *
  185. * @param \Cake\Http\Client\Request $request The request being sent.
  186. * @param array $options Array of options to use.
  187. * @return void
  188. */
  189. protected function _buildSslContext(Request $request, $options)
  190. {
  191. $sslOptions = [
  192. 'ssl_verify_peer',
  193. 'ssl_verify_peer_name',
  194. 'ssl_verify_depth',
  195. 'ssl_allow_self_signed',
  196. 'ssl_cafile',
  197. 'ssl_local_cert',
  198. 'ssl_passphrase',
  199. ];
  200. if (empty($options['ssl_cafile'])) {
  201. $options['ssl_cafile'] = CORE_PATH . 'config' . DIRECTORY_SEPARATOR . 'cacert.pem';
  202. }
  203. if (!empty($options['ssl_verify_host'])) {
  204. $url = $request->getUri();
  205. $host = parse_url($url, PHP_URL_HOST);
  206. $this->_sslContextOptions['peer_name'] = $host;
  207. }
  208. foreach ($sslOptions as $key) {
  209. if (isset($options[$key])) {
  210. $name = substr($key, 4);
  211. $this->_sslContextOptions[$name] = $options[$key];
  212. }
  213. }
  214. }
  215. /**
  216. * Open the stream and send the request.
  217. *
  218. * @param \Cake\Http\Client\Request $request The request object.
  219. * @return array Array of populated Response objects
  220. * @throws \Cake\Network\Exception\HttpException
  221. */
  222. protected function _send(Request $request)
  223. {
  224. $deadline = false;
  225. if (isset($this->_contextOptions['timeout']) && $this->_contextOptions['timeout'] > 0) {
  226. $deadline = time() + $this->_contextOptions['timeout'];
  227. }
  228. $url = $request->getUri();
  229. $this->_open($url);
  230. $content = '';
  231. $timedOut = false;
  232. while (!feof($this->_stream)) {
  233. if ($deadline !== false) {
  234. stream_set_timeout($this->_stream, max($deadline - time(), 1));
  235. }
  236. $content .= fread($this->_stream, 8192);
  237. $meta = stream_get_meta_data($this->_stream);
  238. if ($meta['timed_out'] || ($deadline !== false && time() > $deadline)) {
  239. $timedOut = true;
  240. break;
  241. }
  242. }
  243. $meta = stream_get_meta_data($this->_stream);
  244. fclose($this->_stream);
  245. if ($timedOut) {
  246. throw new HttpException('Connection timed out ' . $url, 504);
  247. }
  248. $headers = $meta['wrapper_data'];
  249. if (isset($headers['headers']) && is_array($headers['headers'])) {
  250. $headers = $headers['headers'];
  251. }
  252. return $this->createResponses($headers, $content);
  253. }
  254. /**
  255. * Build a response object
  256. *
  257. * @param array $headers Unparsed headers.
  258. * @param string $body The response body.
  259. *
  260. * @return \Cake\Http\Client\Response
  261. */
  262. protected function _buildResponse($headers, $body)
  263. {
  264. return new Response($headers, $body);
  265. }
  266. /**
  267. * Open the socket and handle any connection errors.
  268. *
  269. * @param string $url The url to connect to.
  270. * @return void
  271. * @throws \Cake\Core\Exception\Exception
  272. */
  273. protected function _open($url)
  274. {
  275. set_error_handler(function ($code, $message) {
  276. $this->_connectionErrors[] = $message;
  277. });
  278. $this->_stream = fopen($url, 'rb', false, $this->_context);
  279. restore_error_handler();
  280. if (!$this->_stream || !empty($this->_connectionErrors)) {
  281. throw new Exception(implode("\n", $this->_connectionErrors));
  282. }
  283. }
  284. /**
  285. * Get the context options
  286. *
  287. * Useful for debugging and testing context creation.
  288. *
  289. * @return array
  290. */
  291. public function contextOptions()
  292. {
  293. return array_merge($this->_contextOptions, $this->_sslContextOptions);
  294. }
  295. }
  296. // @deprecated Add backwards compat alias.
  297. class_alias('Cake\Http\Client\Adapter\Stream', 'Cake\Network\Http\Adapter\Stream');