public function __invoke($retries, AbstractTransferEvent $event) { if (!($response = $event->getResponse())) { return RetrySubscriber::DEFER; } if (!$response->hasHeader('x-amz-crc32')) { return RetrySubscriber::DEFER; } $hash = hexdec(Utils::hash($response->getBody(), 'crc32b')); return (int) $response->getHeader('x-amz-crc32') !== $hash ? RetrySubscriber::RETRY : RetrySubscriber::DEFER; }
public function setMd5(PreparedEvent $event) { $client = $event->getClient(); $command = $event->getCommand(); $body = $event->getRequest()->getBody(); // If ContentMD5 is set or there is no body, there is nothing to do. if ($command['ContentMD5'] || !$body) { return; } // If and MD5 is required or enabled, add one. $optional = $client->getConfig('calculate_md5') && in_array($command->getName(), self::$canMd5); if (in_array($command->getName(), self::$requireMd5) || $optional) { // Throw exception is calculating and MD5 would result in an error. if (!$body->isSeekable()) { throw new CouldNotCreateChecksumException('md5'); } // Set the Content-MD5 header. $event->getRequest()->setHeader('Content-MD5', base64_encode(Utils::hash($body, 'md5', true))); } }
public function testCalculatesHashSeeksToOriginalPosition() { $s = Stream::factory('foobazbar'); $s->seek(4); $this->assertEquals(md5('foobazbar'), Utils::hash($s, 'md5')); $this->assertEquals(4, $s->tell()); }
/** * Create a cache key for a response's body * * @param string $url URL of the entry * @param StreamInterface $body Response body * * @return string */ private function getBodyKey($url, StreamInterface $body) { return $this->keyPrefix . md5($url) . Stream\Utils::hash($body, 'md5'); }
protected function getPayload(RequestInterface $request) { // Calculate the request signature payload if ($request->hasHeader('x-amz-content-sha256')) { // Handle streaming operations (e.g. Glacier.UploadArchive) return (string) $request->getHeader('x-amz-content-sha256'); } if ($body = $request->getBody()) { if (!$body->isSeekable()) { throw new CouldNotCreateChecksumException('sha256'); } return Utils::hash($body, 'sha256'); } return self::EMPTY_PAYLOAD; }