/usr/share/php/Aws/WrappedHttpHandler.php is in php-aws-sdk 3.15.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | <?php
namespace Aws;
use Aws\Api\Parser\Exception\ParserException;
use GuzzleHttp\Promise;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* Converts an HTTP handler into a Command HTTP handler.
*
* HTTP handlers have the following signature:
* function(RequestInterface $request, array $options) : PromiseInterface
*
* The promise returned form an HTTP handler must resolve to a PSR-7 response
* object when fulfilled or an error array when rejected. The error array
* can contain the following data:
*
* - exception: (required, Exception) Exception that was encountered.
* - response: (ResponseInterface) PSR-7 response that was received (if a
* response) was received.
* - connection_error: (bool) True if the error is the result of failing to
* connect.
*/
class WrappedHttpHandler
{
private $httpHandler;
private $parser;
private $errorParser;
private $exceptionClass;
/**
* @param callable $httpHandler Function that accepts a request and array
* of request options and returns a promise
* that fulfills with a response or rejects
* with an error array.
* @param callable $parser Function that accepts a response object
* and returns an AWS result object.
* @param callable $errorParser Function that parses a response object
* into AWS error data.
* @param string $exceptionClass Exception class to throw.
*/
public function __construct(
callable $httpHandler,
callable $parser,
callable $errorParser,
$exceptionClass = 'Aws\Exception\AwsException'
) {
$this->httpHandler = $httpHandler;
$this->parser = $parser;
$this->errorParser = $errorParser;
$this->exceptionClass = $exceptionClass;
}
/**
* 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;
return Promise\promise_for($fn($request, $command['@http'] ?: []))
->then(
function (ResponseInterface $res) use ($command, $request) {
return $this->parseResponse($command, $request, $res);
},
function ($err) use ($request, $command) {
if (is_array($err)) {
$exception = $this->parseError($err, $request, $command);
return new Promise\RejectedPromise($exception);
}
return new Promise\RejectedPromise($err);
}
);
}
/**
* @param CommandInterface $command
* @param RequestInterface $request
* @param ResponseInterface $response
*
* @return ResultInterface
*/
private function parseResponse(
CommandInterface $command,
RequestInterface $request,
ResponseInterface $response
) {
$parser = $this->parser;
$status = $response->getStatusCode();
$result = $status < 300
? $parser($command, $response)
: new Result();
$metadata = [
'statusCode' => $status,
'effectiveUri' => (string) $request->getUri(),
'headers' => []
];
// 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.
*
* @return \Exception
*/
private function parseError(
array $err,
RequestInterface $request,
CommandInterface $command
) {
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']);
$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']);
return new $this->exceptionClass(
sprintf(
'Error executing "%s" on "%s"; %s',
$command->getName(),
$request->getUri(),
$serviceError
),
$command,
$parts,
$err['exception']
);
}
}
|