SdkStreamHandler.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. <?php
  2. /**
  3. * Copyright 2019 Huawei Technologies Co.,Ltd.
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
  5. * this file except in compliance with the License. You may obtain a copy of the
  6. * License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software distributed
  11. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. * specific language governing permissions and limitations under the License.
  14. *
  15. */
  16. namespace Obs\Internal\Common;
  17. use GuzzleHttp\Exception\RequestException;
  18. use GuzzleHttp\Exception\ConnectException;
  19. use GuzzleHttp\Promise\FulfilledPromise;
  20. use GuzzleHttp\Promise\PromiseInterface;
  21. use GuzzleHttp\Psr7;
  22. use GuzzleHttp\TransferStats;
  23. use Psr\Http\Message\RequestInterface;
  24. use Psr\Http\Message\ResponseInterface;
  25. use Psr\Http\Message\StreamInterface;
  26. class SdkStreamHandler
  27. {
  28. private $lastHeaders = [];
  29. public function __invoke(RequestInterface $request, array $options)
  30. {
  31. if (isset($options['delay'])) {
  32. usleep($options['delay'] * 1000);
  33. }
  34. $startTime = isset($options['on_stats']) ? microtime(true) : null;
  35. try {
  36. $request = $request->withoutHeader('Expect');
  37. if (0 === $request->getBody()->getSize()) {
  38. $request = $request->withHeader('Content-Length', 0);
  39. }
  40. return $this->createResponse(
  41. $request,
  42. $options,
  43. $this->createStream($request, $options),
  44. $startTime
  45. );
  46. } catch (\InvalidArgumentException $e) {
  47. throw $e;
  48. } catch (\Exception $e) {
  49. $message = $e->getMessage();
  50. if (strpos($message, 'getaddrinfo')
  51. || strpos($message, 'Connection refused')
  52. || strpos($message, "couldn't connect to host")
  53. ) {
  54. $e = new ConnectException($e->getMessage(), $request, $e);
  55. }
  56. $e = RequestException::wrapException($request, $e);
  57. $this->invokeStats($options, $request, $startTime, null, $e);
  58. return \GuzzleHttp\Promise\rejection_for($e);
  59. }
  60. }
  61. private function invokeStats(
  62. array $options,
  63. RequestInterface $request,
  64. $startTime,
  65. ResponseInterface $response = null,
  66. $error = null
  67. ) {
  68. if (isset($options['on_stats'])) {
  69. $stats = new TransferStats(
  70. $request,
  71. $response,
  72. microtime(true) - $startTime,
  73. $error,
  74. []
  75. );
  76. call_user_func($options['on_stats'], $stats);
  77. }
  78. }
  79. private function createResponse(
  80. RequestInterface $request,
  81. array $options,
  82. $stream,
  83. $startTime
  84. ) {
  85. $hdrs = $this->lastHeaders;
  86. $this->lastHeaders = [];
  87. $parts = explode(' ', array_shift($hdrs), 3);
  88. $ver = explode('/', $parts[0])[1];
  89. $status = $parts[1];
  90. $reason = isset($parts[2]) ? $parts[2] : null;
  91. $headers = \GuzzleHttp\headers_from_lines($hdrs);
  92. list ($stream, $headers) = $this->checkDecode($options, $headers, $stream);
  93. $stream = Psr7\stream_for($stream);
  94. $sink = $stream;
  95. if (strcasecmp('HEAD', $request->getMethod())) {
  96. $sink = $this->createSink($stream, $options);
  97. }
  98. $response = new Psr7\Response($status, $headers, $sink, $ver, $reason);
  99. if (isset($options['on_headers'])) {
  100. try {
  101. $options['on_headers']($response);
  102. } catch (\Exception $e) {
  103. $msg = 'An error was encountered during the on_headers event';
  104. $ex = new RequestException($msg, $request, $response, $e);
  105. return \GuzzleHttp\Promise\rejection_for($ex);
  106. }
  107. }
  108. if ($sink !== $stream) {
  109. $this->drain(
  110. $stream,
  111. $sink,
  112. $response->getHeaderLine('Content-Length')
  113. );
  114. }
  115. $this->invokeStats($options, $request, $startTime, $response, null);
  116. return new FulfilledPromise($response);
  117. }
  118. private function createSink(StreamInterface $stream, array $options)
  119. {
  120. if (!empty($options['stream'])) {
  121. return $stream;
  122. }
  123. $sink = isset($options['sink'])
  124. ? $options['sink']
  125. : fopen('php://temp', 'r+');
  126. return is_string($sink)
  127. ? new Psr7\LazyOpenStream($sink, 'w+')
  128. : Psr7\stream_for($sink);
  129. }
  130. private function checkDecode(array $options, array $headers, $stream)
  131. {
  132. if (!empty($options['decode_content'])) {
  133. $normalizedKeys = \GuzzleHttp\normalize_header_keys($headers);
  134. if (isset($normalizedKeys['content-encoding'])) {
  135. $encoding = $headers[$normalizedKeys['content-encoding']];
  136. if ($encoding[0] === 'gzip' || $encoding[0] === 'deflate') {
  137. $stream = new Psr7\InflateStream(
  138. Psr7\stream_for($stream)
  139. );
  140. $headers['x-encoded-content-encoding']
  141. = $headers[$normalizedKeys['content-encoding']];
  142. unset($headers[$normalizedKeys['content-encoding']]);
  143. if (isset($normalizedKeys['content-length'])) {
  144. $headers['x-encoded-content-length']
  145. = $headers[$normalizedKeys['content-length']];
  146. $length = (int) $stream->getSize();
  147. if ($length === 0) {
  148. unset($headers[$normalizedKeys['content-length']]);
  149. } else {
  150. $headers[$normalizedKeys['content-length']] = [$length];
  151. }
  152. }
  153. }
  154. }
  155. }
  156. return [$stream, $headers];
  157. }
  158. private function drain(
  159. StreamInterface $source,
  160. StreamInterface $sink,
  161. $contentLength
  162. ) {
  163. Psr7\copy_to_stream(
  164. $source,
  165. $sink,
  166. (strlen($contentLength) > 0 && (int) $contentLength > 0) ? (int) $contentLength : -1
  167. );
  168. $sink->seek(0);
  169. $source->close();
  170. return $sink;
  171. }
  172. private function createResource(callable $callback)
  173. {
  174. $errors = null;
  175. set_error_handler(function ($_, $msg, $file, $line) use (&$errors) {
  176. $errors[] = [
  177. 'message' => $msg,
  178. 'file' => $file,
  179. 'line' => $line
  180. ];
  181. return true;
  182. });
  183. $resource = $callback();
  184. restore_error_handler();
  185. if (!$resource) {
  186. $message = 'Error creating resource: ';
  187. foreach ($errors as $err) {
  188. foreach ($err as $key => $value) {
  189. $message .= "[$key] $value" . PHP_EOL;
  190. }
  191. }
  192. throw new \RuntimeException(trim($message));
  193. }
  194. return $resource;
  195. }
  196. private function createStream(RequestInterface $request, array $options)
  197. {
  198. static $methods;
  199. if (!$methods) {
  200. $methods = array_flip(get_class_methods(__CLASS__));
  201. }
  202. if ($request->getProtocolVersion() == '1.1'
  203. && !$request->hasHeader('Connection')
  204. ) {
  205. $request = $request->withHeader('Connection', 'close');
  206. }
  207. if (!isset($options['verify'])) {
  208. $options['verify'] = true;
  209. }
  210. $params = [];
  211. $context = $this->getDefaultContext($request, $options);
  212. if (isset($options['on_headers']) && !is_callable($options['on_headers'])) {
  213. throw new \InvalidArgumentException('on_headers must be callable');
  214. }
  215. if (!empty($options)) {
  216. foreach ($options as $key => $value) {
  217. $method = "add_{$key}";
  218. if (isset($methods[$method])) {
  219. $this->{$method}($request, $context, $value, $params);
  220. }
  221. }
  222. }
  223. if (isset($options['stream_context'])) {
  224. if (!is_array($options['stream_context'])) {
  225. throw new \InvalidArgumentException('stream_context must be an array');
  226. }
  227. $context = array_replace_recursive(
  228. $context,
  229. $options['stream_context']
  230. );
  231. }
  232. if (isset($options['auth'])
  233. && is_array($options['auth'])
  234. && isset($options['auth'][2])
  235. && 'ntlm' == $options['auth'][2]
  236. ) {
  237. throw new \InvalidArgumentException('Microsoft NTLM authentication only supported with curl handler');
  238. }
  239. $uri = $this->resolveHost($request, $options);
  240. $context = $this->createResource(
  241. function () use ($context, $params) {
  242. return stream_context_create($context, $params);
  243. }
  244. );
  245. return $this->createResource(
  246. function () use ($uri, &$http_response_header, $context, $options) {
  247. $resource = fopen((string) $uri, 'r', null, $context);
  248. $this->lastHeaders = $http_response_header;
  249. if (isset($options['read_timeout'])) {
  250. $readTimeout = $options['read_timeout'];
  251. $sec = (int) $readTimeout;
  252. $usec = ($readTimeout - $sec) * 100000;
  253. stream_set_timeout($resource, $sec, $usec);
  254. }
  255. return $resource;
  256. }
  257. );
  258. }
  259. private function resolveHost(RequestInterface $request, array $options)
  260. {
  261. $uri = $request->getUri();
  262. if (isset($options['force_ip_resolve']) && !filter_var($uri->getHost(), FILTER_VALIDATE_IP)) {
  263. if ('v4' === $options['force_ip_resolve']) {
  264. $records = dns_get_record($uri->getHost(), DNS_A);
  265. if (!isset($records[0]['ip'])) {
  266. throw new ConnectException(sprintf("Could not resolve IPv4 address for host '%s'", $uri->getHost()), $request);
  267. }
  268. $uri = $uri->withHost($records[0]['ip']);
  269. } elseif ('v6' === $options['force_ip_resolve']) {
  270. $records = dns_get_record($uri->getHost(), DNS_AAAA);
  271. if (!isset($records[0]['ipv6'])) {
  272. throw new ConnectException(sprintf("Could not resolve IPv6 address for host '%s'", $uri->getHost()), $request);
  273. }
  274. $uri = $uri->withHost('[' . $records[0]['ipv6'] . ']');
  275. }
  276. }
  277. return $uri;
  278. }
  279. private function getDefaultContext(RequestInterface $request)
  280. {
  281. $headers = '';
  282. foreach ($request->getHeaders() as $name => $value) {
  283. foreach ($value as $val) {
  284. $headers .= "$name: $val\r\n";
  285. }
  286. }
  287. $context = [
  288. 'http' => [
  289. 'method' => $request->getMethod(),
  290. 'header' => $headers,
  291. 'protocol_version' => $request->getProtocolVersion(),
  292. 'ignore_errors' => true,
  293. 'follow_location' => 0,
  294. ],
  295. ];
  296. $body = (string) $request->getBody();
  297. if (!empty($body)) {
  298. $context['http']['content'] = $body;
  299. if (!$request->hasHeader('Content-Type')) {
  300. $context['http']['header'] .= "Content-Type:\r\n";
  301. }
  302. }
  303. $context['http']['header'] = rtrim($context['http']['header']);
  304. return $context;
  305. }
  306. private function add_proxy(RequestInterface $request, &$options, $value, &$params)
  307. {
  308. if (!is_array($value)) {
  309. $options['http']['proxy'] = $value;
  310. } else {
  311. $scheme = $request->getUri()->getScheme();
  312. if (isset($value[$scheme])) {
  313. if (!isset($value['no'])
  314. || !\GuzzleHttp\is_host_in_noproxy(
  315. $request->getUri()->getHost(),
  316. $value['no']
  317. )
  318. ) {
  319. $options['http']['proxy'] = $value[$scheme];
  320. }
  321. }
  322. }
  323. }
  324. private function add_timeout(RequestInterface $request, &$options, $value, &$params)
  325. {
  326. if ($value > 0) {
  327. $options['http']['timeout'] = $value;
  328. }
  329. }
  330. private function add_verify(RequestInterface $request, &$options, $value, &$params)
  331. {
  332. if ($value === true) {
  333. if (PHP_VERSION_ID < 50600) {
  334. $options['ssl']['cafile'] = \GuzzleHttp\default_ca_bundle();
  335. }
  336. } elseif (is_string($value)) {
  337. $options['ssl']['cafile'] = $value;
  338. if (!file_exists($value)) {
  339. throw new \RuntimeException("SSL CA bundle not found: $value");
  340. }
  341. } elseif ($value === false) {
  342. $options['ssl']['verify_peer'] = false;
  343. $options['ssl']['verify_peer_name'] = false;
  344. return;
  345. } else {
  346. throw new \InvalidArgumentException('Invalid verify request option');
  347. }
  348. $options['ssl']['verify_peer'] = true;
  349. $options['ssl']['verify_peer_name'] = true;
  350. $options['ssl']['allow_self_signed'] = false;
  351. }
  352. private function add_cert(RequestInterface $request, &$options, $value, &$params)
  353. {
  354. if (is_array($value)) {
  355. $options['ssl']['passphrase'] = $value[1];
  356. $value = $value[0];
  357. }
  358. if (!file_exists($value)) {
  359. throw new \RuntimeException("SSL certificate not found: {$value}");
  360. }
  361. $options['ssl']['local_cert'] = $value;
  362. }
  363. private function add_progress(RequestInterface $request, &$options, $value, &$params)
  364. {
  365. $this->addNotification(
  366. $params,
  367. function ($code, $a, $b, $c, $transferred, $total) use ($value) {
  368. if ($code == STREAM_NOTIFY_PROGRESS) {
  369. $value($total, $transferred, null, null);
  370. }
  371. }
  372. );
  373. }
  374. private function add_debug(RequestInterface $request, &$options, $value, &$params)
  375. {
  376. if ($value === false) {
  377. return;
  378. }
  379. static $map = [
  380. STREAM_NOTIFY_CONNECT => 'CONNECT',
  381. STREAM_NOTIFY_AUTH_REQUIRED => 'AUTH_REQUIRED',
  382. STREAM_NOTIFY_AUTH_RESULT => 'AUTH_RESULT',
  383. STREAM_NOTIFY_MIME_TYPE_IS => 'MIME_TYPE_IS',
  384. STREAM_NOTIFY_FILE_SIZE_IS => 'FILE_SIZE_IS',
  385. STREAM_NOTIFY_REDIRECTED => 'REDIRECTED',
  386. STREAM_NOTIFY_PROGRESS => 'PROGRESS',
  387. STREAM_NOTIFY_FAILURE => 'FAILURE',
  388. STREAM_NOTIFY_COMPLETED => 'COMPLETED',
  389. STREAM_NOTIFY_RESOLVE => 'RESOLVE',
  390. ];
  391. static $args = ['severity', 'message', 'message_code',
  392. 'bytes_transferred', 'bytes_max'];
  393. $value = \GuzzleHttp\debug_resource($value);
  394. $ident = $request->getMethod() . ' ' . $request->getUri()->withFragment('');
  395. $this->addNotification(
  396. $params,
  397. function () use ($ident, $value, $map, $args) {
  398. $passed = func_get_args();
  399. $code = array_shift($passed);
  400. fprintf($value, '<%s> [%s] ', $ident, $map[$code]);
  401. foreach (array_filter($passed) as $i => $v) {
  402. fwrite($value, $args[$i] . ': "' . $v . '" ');
  403. }
  404. fwrite($value, "\n");
  405. }
  406. );
  407. }
  408. private function addNotification(array &$params, callable $notify)
  409. {
  410. if (!isset($params['notification'])) {
  411. $params['notification'] = $notify;
  412. } else {
  413. $params['notification'] = $this->callArray([
  414. $params['notification'],
  415. $notify
  416. ]);
  417. }
  418. }
  419. private function callArray(array $functions)
  420. {
  421. return function () use ($functions) {
  422. $args = func_get_args();
  423. foreach ($functions as $fn) {
  424. call_user_func_array($fn, $args);
  425. }
  426. };
  427. }
  428. }