| 1 |
<?php |
| 2 |
namespace Aws; |
| 3 |
|
| 4 |
use Aws\Api\Parser\Exception\ParserException; |
| 5 |
use Aws\Exception\AwsException; |
| 6 |
use GuzzleHttp\Promise; |
| 7 |
use Psr\Http\Message\RequestInterface; |
| 8 |
use Psr\Http\Message\ResponseInterface; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
class WrappedHttpHandler |
| 27 |
{ |
| 28 |
private $httpHandler; |
| 29 |
private $parser; |
| 30 |
private $errorParser; |
| 31 |
private $exceptionClass; |
| 32 |
private $collectStats; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public function __construct( |
| 48 |
callable $httpHandler, |
| 49 |
callable $parser, |
| 50 |
callable $errorParser, |
| 51 |
$exceptionClass = AwsException::class, |
| 52 |
$collectStats = false |
| 53 |
) { |
| 54 |
$this->httpHandler = $httpHandler; |
| 55 |
$this->parser = $parser; |
| 56 |
$this->errorParser = $errorParser; |
| 57 |
$this->exceptionClass = $exceptionClass; |
| 58 |
$this->collectStats = $collectStats; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public function __invoke( |
| 71 |
CommandInterface $command, |
| 72 |
RequestInterface $request |
| 73 |
) { |
| 74 |
$fn = $this->httpHandler; |
| 75 |
$options = $command['@http'] ?: []; |
| 76 |
$stats = []; |
| 77 |
if ($this->collectStats || !empty($options['collect_stats'])) { |
| 78 |
$options['http_stats_receiver'] = static function ( |
| 79 |
array $transferStats |
| 80 |
) use (&$stats) { |
| 81 |
$stats = $transferStats; |
| 82 |
}; |
| 83 |
} elseif (isset($options['http_stats_receiver'])) { |
| 84 |
throw new \InvalidArgumentException('Providing a custom HTTP stats' |
| 85 |
. ' receiver to Aws\WrappedHttpHandler is not supported.'); |
| 86 |
} |
| 87 |
|
| 88 |
return Promise\Create::promiseFor($fn($request, $options)) |
| 89 |
->then( |
| 90 |
function ( |
| 91 |
ResponseInterface $res |
| 92 |
) use ($command, $request, &$stats) { |
| 93 |
return $this->parseResponse($command, $request, $res, $stats); |
| 94 |
}, |
| 95 |
function ($err) use ($request, $command, &$stats) { |
| 96 |
if (is_array($err)) { |
| 97 |
$err = $this->parseError( |
| 98 |
$err, |
| 99 |
$request, |
| 100 |
$command, |
| 101 |
$stats |
| 102 |
); |
| 103 |
} |
| 104 |
return new Promise\RejectedPromise($err); |
| 105 |
} |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
private function parseResponse( |
| 118 |
CommandInterface $command, |
| 119 |
RequestInterface $request, |
| 120 |
ResponseInterface $response, |
| 121 |
array $stats |
| 122 |
) { |
| 123 |
$parser = $this->parser; |
| 124 |
$status = $response->getStatusCode(); |
| 125 |
$result = $status < 300 |
| 126 |
? $parser($command, $response) |
| 127 |
: new Result(); |
| 128 |
|
| 129 |
$metadata = [ |
| 130 |
'statusCode' => $status, |
| 131 |
'effectiveUri' => (string) $request->getUri(), |
| 132 |
'headers' => [], |
| 133 |
'transferStats' => [], |
| 134 |
]; |
| 135 |
if (!empty($stats)) { |
| 136 |
$metadata['transferStats']['http'] = [$stats]; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
foreach ($response->getHeaders() as $name => $values) { |
| 141 |
$metadata['headers'][strtolower($name)] = $values[0]; |
| 142 |
} |
| 143 |
|
| 144 |
$result['@metadata'] = $metadata; |
| 145 |
|
| 146 |
return $result; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
private function parseError( |
| 160 |
array $err, |
| 161 |
RequestInterface $request, |
| 162 |
CommandInterface $command, |
| 163 |
array $stats |
| 164 |
) { |
| 165 |
if (!isset($err['exception'])) { |
| 166 |
throw new \RuntimeException('The HTTP handler was rejected without an "exception" key value pair.'); |
| 167 |
} |
| 168 |
|
| 169 |
$serviceError = "AWS HTTP error:\n"; |
| 170 |
|
| 171 |
if (!isset($err['response'])) { |
| 172 |
$parts = ['response' => null]; |
| 173 |
$serviceError .= $err['exception']->getMessage(); |
| 174 |
} else { |
| 175 |
try { |
| 176 |
$parts = call_user_func( |
| 177 |
$this->errorParser, |
| 178 |
$err['response'], |
| 179 |
$command |
| 180 |
); |
| 181 |
|
| 182 |
$serviceError .= "{$parts['code']} ({$parts['type']}): " |
| 183 |
. "{$parts['message']}"; |
| 184 |
} catch (ParserException $e) { |
| 185 |
$parts = []; |
| 186 |
$serviceError .= ' Unable to parse error information from ' |
| 187 |
. "response - {$e->getMessage()}"; |
| 188 |
} |
| 189 |
|
| 190 |
$parts['response'] = $err['response']; |
| 191 |
} |
| 192 |
|
| 193 |
$parts['exception'] = $err['exception']; |
| 194 |
$parts['request'] = $request; |
| 195 |
$parts['connection_error'] = !empty($err['connection_error']); |
| 196 |
$parts['transfer_stats'] = $stats; |
| 197 |
|
| 198 |
return new $this->exceptionClass( |
| 199 |
sprintf( |
| 200 |
'Error executing "%s" on "%s"; %s', |
| 201 |
$command->getName(), |
| 202 |
$request->getUri(), |
| 203 |
$serviceError |
| 204 |
), |
| 205 |
$command, |
| 206 |
$parts, |
| 207 |
$err['exception'] |
| 208 |
); |
| 209 |
} |
| 210 |
} |