| 1 |
<?php |
| 2 |
|
| 3 |
namespace Aws\S3\Parser; |
| 4 |
|
| 5 |
use Aws\Api\ErrorParser\XmlErrorParser; |
| 6 |
use Aws\Api\Parser\AbstractParser; |
| 7 |
use Aws\Api\Parser\Exception\ParserException; |
| 8 |
use Aws\Api\Service; |
| 9 |
use Aws\Api\StructureShape; |
| 10 |
use Aws\CommandInterface; |
| 11 |
use Aws\Exception\AwsException; |
| 12 |
use Aws\ResultInterface; |
| 13 |
use Psr\Http\Message\ResponseInterface; |
| 14 |
use Psr\Http\Message\StreamInterface; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
final class S3Parser extends AbstractParser |
| 23 |
{ |
| 24 |
|
| 25 |
private $protocolParser; |
| 26 |
|
| 27 |
private $errorParser; |
| 28 |
|
| 29 |
private $exceptionClass; |
| 30 |
|
| 31 |
private $s3ResultMutators; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public function __construct( |
| 40 |
AbstractParser $protocolParser, |
| 41 |
XmlErrorParser $errorParser, |
| 42 |
Service $api, |
| 43 |
string $exceptionClass = AwsException::class |
| 44 |
) |
| 45 |
{ |
| 46 |
parent::__construct($api); |
| 47 |
$this->protocolParser = $protocolParser; |
| 48 |
$this->errorParser = $errorParser; |
| 49 |
$this->exceptionClass = $exceptionClass; |
| 50 |
$this->s3ResultMutators = []; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public function __invoke( |
| 62 |
CommandInterface $command, |
| 63 |
ResponseInterface $response |
| 64 |
):? ResultInterface |
| 65 |
{ |
| 66 |
|
| 67 |
$this->parse200Error($command, $response); |
| 68 |
|
| 69 |
try { |
| 70 |
$parseFn = $this->protocolParser; |
| 71 |
$result = $parseFn($command, $response); |
| 72 |
} catch (ParserException $e) { |
| 73 |
|
| 74 |
throw new $this->exceptionClass( |
| 75 |
"Error parsing response for {$command->getName()}:" |
| 76 |
. " AWS parsing error: {$e->getMessage()}", |
| 77 |
$command, |
| 78 |
['connection_error' => true, 'exception' => $e], |
| 79 |
$e |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
return $this->executeS3ResultMutators($result, $command, $response); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
private function parse200Error( |
| 97 |
CommandInterface $command, |
| 98 |
ResponseInterface $response |
| 99 |
): void |
| 100 |
{ |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
if (200 !== $response->getStatusCode() |
| 105 |
|| !$this->shouldBeConsidered200Error($command->getName()) |
| 106 |
|| !$response->getBody()->isSeekable()) { |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
if (!$this->isFirstRootElementError($response->getBody())) { |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
try { |
| 116 |
$errorParserFn = $this->errorParser; |
| 117 |
$parsedError = $errorParserFn($response, $command); |
| 118 |
} catch (ParserException $e) { |
| 119 |
|
| 120 |
$parsedError = [ |
| 121 |
'code' => 'ConnectionError', |
| 122 |
'message' => "An error connecting to the service occurred" |
| 123 |
. " while performing the " . $command->getName() |
| 124 |
. " operation." |
| 125 |
]; |
| 126 |
} |
| 127 |
|
| 128 |
if (isset($parsedError['code']) && isset($parsedError['message'])) { |
| 129 |
throw new $this->exceptionClass( |
| 130 |
$parsedError['message'], |
| 131 |
$command, |
| 132 |
[ |
| 133 |
'connection_error' => true, |
| 134 |
'code' => $parsedError['code'], |
| 135 |
'message' => $parsedError['message'] |
| 136 |
] |
| 137 |
); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
private function shouldBeConsidered200Error($commandName): bool |
| 151 |
{ |
| 152 |
$operation = $this->api->getOperation($commandName); |
| 153 |
$output = $operation->getOutput(); |
| 154 |
foreach ($output->getMembers() as $_ => $memberProps) { |
| 155 |
if (!empty($memberProps['eventstream']) || !empty($memberProps['streaming'])) { |
| 156 |
return false; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
return true; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
private function isFirstRootElementError(StreamInterface $responseBody): bool |
| 174 |
{ |
| 175 |
static $pattern = '/<\?xml version="1\.0" encoding="UTF-8"\?>\s*<Error>/'; |
| 176 |
|
| 177 |
$reducedBodyContent = $responseBody->read(64); |
| 178 |
$foundErrorElement = preg_match($pattern, $reducedBodyContent); |
| 179 |
|
| 180 |
|
| 181 |
$responseBody->rewind(); |
| 182 |
|
| 183 |
return $foundErrorElement; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
private function executeS3ResultMutators( |
| 197 |
ResultInterface $result, |
| 198 |
CommandInterface $command, |
| 199 |
ResponseInterface $response |
| 200 |
): ResultInterface |
| 201 |
{ |
| 202 |
foreach ($this->s3ResultMutators as $mutator) { |
| 203 |
$result = $mutator($result, $command, $response); |
| 204 |
} |
| 205 |
|
| 206 |
return $result; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public function addS3ResultMutator( |
| 217 |
string $mutatorName, |
| 218 |
S3ResultMutator $s3ResultMutator |
| 219 |
): void |
| 220 |
{ |
| 221 |
if (isset($this->s3ResultMutators[$mutatorName])) { |
| 222 |
trigger_error( |
| 223 |
"The S3 Result Mutator {$mutatorName} already exists!", |
| 224 |
E_USER_WARNING |
| 225 |
); |
| 226 |
|
| 227 |
return; |
| 228 |
} |
| 229 |
|
| 230 |
$this->s3ResultMutators[$mutatorName] = $s3ResultMutator; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
public function removeS3ResultMutator(string $mutatorName): void |
| 240 |
{ |
| 241 |
if (!isset($this->s3ResultMutators[$mutatorName])) { |
| 242 |
trigger_error( |
| 243 |
"The S3 Result Mutator {$mutatorName} does not exist!", |
| 244 |
E_USER_WARNING |
| 245 |
); |
| 246 |
|
| 247 |
return; |
| 248 |
} |
| 249 |
|
| 250 |
unset($this->s3ResultMutators[$mutatorName]); |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
public function getS3ResultMutators(): array |
| 259 |
{ |
| 260 |
return $this->s3ResultMutators; |
| 261 |
} |
| 262 |
|
| 263 |
public function parseMemberFromStream( |
| 264 |
StreamInterface $stream, |
| 265 |
StructureShape $member, |
| 266 |
$response |
| 267 |
) |
| 268 |
{ |
| 269 |
return $this->protocolParser->parseMemberFromStream( |
| 270 |
$stream, |
| 271 |
$member, |
| 272 |
$response |
| 273 |
); |
| 274 |
} |
| 275 |
} |