From 0c8af4992cb0f7589dcafaad65ada12753c64594 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Wed, 23 Nov 2022 21:14:33 +0300 Subject: initial --- vendor/aws/aws-sdk-php/src/WrappedHttpHandler.php | 207 ++++++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 vendor/aws/aws-sdk-php/src/WrappedHttpHandler.php (limited to 'vendor/aws/aws-sdk-php/src/WrappedHttpHandler.php') diff --git a/vendor/aws/aws-sdk-php/src/WrappedHttpHandler.php b/vendor/aws/aws-sdk-php/src/WrappedHttpHandler.php new file mode 100644 index 0000000..511f513 --- /dev/null +++ b/vendor/aws/aws-sdk-php/src/WrappedHttpHandler.php @@ -0,0 +1,207 @@ +httpHandler = $httpHandler; + $this->parser = $parser; + $this->errorParser = $errorParser; + $this->exceptionClass = $exceptionClass; + $this->collectStats = $collectStats; + } + + /** + * Calls the simpler HTTP specific handler and wraps the returned promise + * with AWS specific values (e.g., a result object or AWS exception). + * + * @param CommandInterface $command Command being executed. + * @param RequestInterface $request Request to send. + * + * @return Promise\PromiseInterface + */ + public function __invoke( + CommandInterface $command, + RequestInterface $request + ) { + $fn = $this->httpHandler; + $options = $command['@http'] ?: []; + $stats = []; + if ($this->collectStats || !empty($options['collect_stats'])) { + $options['http_stats_receiver'] = static function ( + array $transferStats + ) use (&$stats) { + $stats = $transferStats; + }; + } elseif (isset($options['http_stats_receiver'])) { + throw new \InvalidArgumentException('Providing a custom HTTP stats' + . ' receiver to Aws\WrappedHttpHandler is not supported.'); + } + + return Promise\Create::promiseFor($fn($request, $options)) + ->then( + function ( + ResponseInterface $res + ) use ($command, $request, &$stats) { + return $this->parseResponse($command, $request, $res, $stats); + }, + function ($err) use ($request, $command, &$stats) { + if (is_array($err)) { + $err = $this->parseError( + $err, + $request, + $command, + $stats + ); + } + return new Promise\RejectedPromise($err); + } + ); + } + + /** + * @param CommandInterface $command + * @param RequestInterface $request + * @param ResponseInterface $response + * @param array $stats + * + * @return ResultInterface + */ + private function parseResponse( + CommandInterface $command, + RequestInterface $request, + ResponseInterface $response, + array $stats + ) { + $parser = $this->parser; + $status = $response->getStatusCode(); + $result = $status < 300 + ? $parser($command, $response) + : new Result(); + + $metadata = [ + 'statusCode' => $status, + 'effectiveUri' => (string) $request->getUri(), + 'headers' => [], + 'transferStats' => [], + ]; + if (!empty($stats)) { + $metadata['transferStats']['http'] = [$stats]; + } + + // Bring headers into the metadata array. + foreach ($response->getHeaders() as $name => $values) { + $metadata['headers'][strtolower($name)] = $values[0]; + } + + $result['@metadata'] = $metadata; + + return $result; + } + + /** + * Parses a rejection into an AWS error. + * + * @param array $err Rejection error array. + * @param RequestInterface $request Request that was sent. + * @param CommandInterface $command Command being sent. + * @param array $stats Transfer statistics + * + * @return \Exception + */ + private function parseError( + array $err, + RequestInterface $request, + CommandInterface $command, + array $stats + ) { + if (!isset($err['exception'])) { + throw new \RuntimeException('The HTTP handler was rejected without an "exception" key value pair.'); + } + + $serviceError = "AWS HTTP error: " . $err['exception']->getMessage(); + + if (!isset($err['response'])) { + $parts = ['response' => null]; + } else { + try { + $parts = call_user_func( + $this->errorParser, + $err['response'], + $command + ); + $serviceError .= " {$parts['code']} ({$parts['type']}): " + . "{$parts['message']} - " . $err['response']->getBody(); + } catch (ParserException $e) { + $parts = []; + $serviceError .= ' Unable to parse error information from ' + . "response - {$e->getMessage()}"; + } + + $parts['response'] = $err['response']; + } + + $parts['exception'] = $err['exception']; + $parts['request'] = $request; + $parts['connection_error'] = !empty($err['connection_error']); + $parts['transfer_stats'] = $stats; + + return new $this->exceptionClass( + sprintf( + 'Error executing "%s" on "%s"; %s', + $command->getName(), + $request->getUri(), + $serviceError + ), + $command, + $parts, + $err['exception'] + ); + } +} -- cgit v1.2.3