Exception
11 months ago
AbstractParser.php
11 months ago
AbstractRestParser.php
11 months ago
Crc32ValidatingParser.php
11 months ago
DecodingEventStreamIterator.php
11 months ago
EventParsingIterator.php
11 months ago
JsonParser.php
11 months ago
JsonRpcParser.php
11 months ago
MetadataParserTrait.php
11 months ago
PayloadParserTrait.php
11 months ago
QueryParser.php
11 months ago
RestJsonParser.php
11 months ago
RestXmlParser.php
11 months ago
XmlParser.php
11 months ago
Crc32ValidatingParser.php
55 lines
| 1 | <?php |
| 2 | namespace Aws\Api\Parser; |
| 3 | |
| 4 | use Aws\Api\StructureShape; |
| 5 | use Aws\CommandInterface; |
| 6 | use Aws\Exception\AwsException; |
| 7 | use Psr\Http\Message\ResponseInterface; |
| 8 | use Psr\Http\Message\StreamInterface; |
| 9 | use GuzzleHttp\Psr7; |
| 10 | |
| 11 | /** |
| 12 | * @internal Decorates a parser and validates the x-amz-crc32 header. |
| 13 | */ |
| 14 | class Crc32ValidatingParser extends AbstractParser |
| 15 | { |
| 16 | /** |
| 17 | * @param callable $parser Parser to wrap. |
| 18 | */ |
| 19 | public function __construct(callable $parser) |
| 20 | { |
| 21 | $this->parser = $parser; |
| 22 | } |
| 23 | |
| 24 | public function __invoke( |
| 25 | CommandInterface $command, |
| 26 | ResponseInterface $response |
| 27 | ) { |
| 28 | if ($expected = $response->getHeaderLine('x-amz-crc32')) { |
| 29 | $hash = hexdec(Psr7\Utils::hash($response->getBody(), 'crc32b')); |
| 30 | if ($expected != $hash) { |
| 31 | throw new AwsException( |
| 32 | "crc32 mismatch. Expected {$expected}, found {$hash}.", |
| 33 | $command, |
| 34 | [ |
| 35 | 'code' => 'ClientChecksumMismatch', |
| 36 | 'connection_error' => true, |
| 37 | 'response' => $response |
| 38 | ] |
| 39 | ); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | $fn = $this->parser; |
| 44 | return $fn($command, $response); |
| 45 | } |
| 46 | |
| 47 | public function parseMemberFromStream( |
| 48 | StreamInterface $stream, |
| 49 | StructureShape $member, |
| 50 | $response |
| 51 | ) { |
| 52 | return $this->parser->parseMemberFromStream($stream, $member, $response); |
| 53 | } |
| 54 | } |
| 55 |