Stream.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Network\Http\Adapter;
  15. use Cake\Core\Exception\Exception;
  16. use Cake\Network\Http\FormData;
  17. use Cake\Network\Http\Request;
  18. use Cake\Network\Http\Response;
  19. /**
  20. * Implements sending Cake\Network\Http\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
  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
  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\Network\Http\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 = [];
  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 array 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[] = new Response($headerSlice, $body);
  98. }
  99. return $responses;
  100. }
  101. /**
  102. * Build the stream context out of the request object.
  103. *
  104. * @param \Cake\Network\Http\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->url();
  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\Network\Http\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->headers() as $name => $value) {
  136. $headers[] = "$name: $value";
  137. }
  138. $cookies = [];
  139. foreach ($request->cookies() as $name => $value) {
  140. $cookies[] = "$name=$value";
  141. }
  142. if ($cookies) {
  143. $headers[] = 'Cookie: ' . implode('; ', $cookies);
  144. }
  145. $this->_contextOptions['header'] = implode("\r\n", $headers);
  146. }
  147. /**
  148. * Builds the request content based on the request object.
  149. *
  150. * If the $request->body() is a string, it will be used as is.
  151. * Array data will be processed with Cake\Network\Http\FormData
  152. *
  153. * @param \Cake\Network\Http\Request $request The request being sent.
  154. * @param array $options Array of options to use.
  155. * @return void
  156. */
  157. protected function _buildContent(Request $request, $options)
  158. {
  159. $content = $request->body();
  160. if (empty($content)) {
  161. return;
  162. }
  163. if (is_string($content)) {
  164. $this->_contextOptions['content'] = $content;
  165. return;
  166. }
  167. if (is_array($content)) {
  168. $formData = new FormData();
  169. $formData->addMany($content);
  170. $type = 'multipart/form-data; boundary="' . $formData->boundary() . '"';
  171. $request->header('Content-Type', $type);
  172. $this->_contextOptions['content'] = (string)$formData;
  173. return;
  174. }
  175. $this->_contextOptions['content'] = $content;
  176. }
  177. /**
  178. * Build miscellaneous options for the request.
  179. *
  180. * @param \Cake\Network\Http\Request $request The request being sent.
  181. * @param array $options Array of options to use.
  182. * @return void
  183. */
  184. protected function _buildOptions(Request $request, $options)
  185. {
  186. $this->_contextOptions['method'] = $request->method();
  187. $this->_contextOptions['protocol_version'] = $request->version();
  188. $this->_contextOptions['ignore_errors'] = true;
  189. if (isset($options['timeout'])) {
  190. $this->_contextOptions['timeout'] = $options['timeout'];
  191. }
  192. if (isset($options['redirect'])) {
  193. $this->_contextOptions['max_redirects'] = (int)$options['redirect'];
  194. }
  195. }
  196. /**
  197. * Build SSL options for the request.
  198. *
  199. * @param \Cake\Network\Http\Request $request The request being sent.
  200. * @param array $options Array of options to use.
  201. * @return void
  202. */
  203. protected function _buildSslContext(Request $request, $options)
  204. {
  205. $sslOptions = [
  206. 'ssl_verify_peer',
  207. 'ssl_verify_depth',
  208. 'ssl_allow_self_signed',
  209. 'ssl_cafile',
  210. 'ssl_local_cert',
  211. 'ssl_passphrase',
  212. ];
  213. if (empty($options['ssl_cafile'])) {
  214. $options['ssl_cafile'] = CORE_PATH . 'config' . DS . 'cacert.pem';
  215. }
  216. if (!empty($options['ssl_verify_host'])) {
  217. $url = $request->url();
  218. $host = parse_url($url, PHP_URL_HOST);
  219. $this->_sslContextOptions['peer_name'] = $host;
  220. }
  221. foreach ($sslOptions as $key) {
  222. if (isset($options[$key])) {
  223. $name = substr($key, 4);
  224. $this->_sslContextOptions[$name] = $options[$key];
  225. }
  226. }
  227. }
  228. /**
  229. * Open the stream and send the request.
  230. *
  231. * @param \Cake\Network\Http\Request $request The request object.
  232. * @return array Array of populated Response objects
  233. * @throws \Cake\Core\Exception\Exception
  234. */
  235. protected function _send(Request $request)
  236. {
  237. $url = $request->url();
  238. $this->_open($url);
  239. $content = '';
  240. while (!feof($this->_stream)) {
  241. $content .= fread($this->_stream, 8192);
  242. }
  243. $meta = stream_get_meta_data($this->_stream);
  244. fclose($this->_stream);
  245. if ($meta['timed_out']) {
  246. throw new Exception('Connection timed out ' . $url);
  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. * Open the socket and handle any connection errors.
  256. *
  257. * @param string $url The url to connect to.
  258. * @return void
  259. * @throws \Cake\Core\Exception\Exception
  260. */
  261. protected function _open($url)
  262. {
  263. set_error_handler([$this, '_connectionErrorHandler']);
  264. $this->_stream = fopen($url, 'rb', false, $this->_context);
  265. restore_error_handler();
  266. if (!$this->_stream || !empty($this->_connectionErrors)) {
  267. throw new Exception(implode("\n", $this->_connectionErrors));
  268. }
  269. }
  270. /**
  271. * Local error handler to capture errors triggered during
  272. * stream connection.
  273. *
  274. * @param int $code Error code.
  275. * @param string $message Error message.
  276. * @return void
  277. */
  278. protected function _connectionErrorHandler($code, $message)
  279. {
  280. $this->_connectionErrors[] = $message;
  281. }
  282. /**
  283. * Get the context options
  284. *
  285. * Useful for debugging and testing context creation.
  286. *
  287. * @return array
  288. */
  289. public function contextOptions()
  290. {
  291. return array_merge($this->_contextOptions, $this->_sslContextOptions);
  292. }
  293. }