| 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 |
|
| 13 |
|
| 14 |
class Crc32ValidatingParser extends AbstractParser |
| 15 |
{ |
| 16 |
|
| 17 |
|
| 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 |
} |