/** * Update a command with the content and tree hash headers, as needed. * * @param PreparedEvent $event Event emitted. * * @throws \RuntimeException if the body is not seekable. */ public function onPrepared(PreparedEvent $event) { $command = $event->getCommand(); $name = $command->getName(); // Determine if there is a need to calculate any hashes. $needsHashes = ($name === 'UploadArchive' || $name === 'UploadPart') && (!$command['checksum'] || !$command['ContentSHA256']); if ($needsHashes) { $body = $event->getRequest()->getBody(); if (!$body->isSeekable()) { throw new CouldNotCreateChecksumException('sha256'); } // Add a tree hash if not provided. if (!$command['checksum']) { $body = new HashingStream($body, new TreeHash(), function ($result) use($command, $event) { $event->getRequest()->setHeader('x-amz-sha256-tree-hash', bin2hex($result)); }); } // Add a linear content hash if not provided. if (!$command['ContentSHA256']) { $body = new HashingStream($body, new PhpHash('sha256'), function ($result) use($command) { $command['ContentSHA256'] = bin2hex($result); }); } // Read the stream in order to calculate the hashes. while (!$body->eof()) { $body->read(1048576); } $body->seek(0); } // Set the content hash header if there is a value to set. if ($hash = $command['ContentSHA256']) { $event->getRequest()->addHeader('x-amz-content-sha256', $hash); } }
/** * Add extra parameter 'calledMethod'. * * React on prepared event, before send. */ public function onPrepared(PreparedEvent $event) { $command = $event->getCommand(); $request = $event->getRequest(); $body = (array) json_decode((string) $request->getBody()); $body['calledMethod'] = $command->getName(); $event->getRequest()->setBody(Stream::factory(json_encode($body))); }
/** * Add OAuth token in header when needed * * @param PreparedEvent $event */ public function onPrepare(PreparedEvent $event) { $command = $event->getCommand(); $operation = $this->description->getOperation($command->getName()); // If this operation require an OAuth scope if ($operation->getData('scope')) { $token = $this->getOAuthToken($operation->getData('scope')); $event->getRequest()->addHeader('Authorization', sprintf('Bearer %s', $token)); } // For Sso methods, client ID and secret are passed as basic auth if (in_array($operation->getName(), ['getSsoToken', 'refreshSsoToken'])) { $event->getRequest()->setHeader('Authorization', 'Basic ' . base64_encode(sprintf("%s:%s", $this->config['ssoClientId'], $this->config['ssoSecret']))); $event->getRequest()->setHeader('Accept', 'application/json'); } }
public function onPrepared(PreparedEvent $event) { $command = $event->getCommand(); if ($value = $command[$this->paramName]) { $event->getRequest()->getConfig()->set('save_to', $value); } }
public function onPrepared(PreparedEvent $event) { $command = $event->getCommand(); $operation = $this->description->getOperation($command->getName()); if ($operation->getData('refresh-token-data') === true) { $event->getRequest()->setBody(Stream::factory(json_encode(['refreshToken' => $this->refreshToken->getRefreshToken()]))); } }
public function setHeaders(PreparedEvent $event) { $request = $event->getRequest(); if (!$request->hasHeader('Content-Type')) { $request->addHeader('Content-Type', 'application/json'); } $request->addHeader('Accept', 'application/json'); }
public function onPrepared(PreparedEvent $event) { $command = $event->getCommand(); $operation = $this->description->getOperation($command->getName()); if ($operation->getData('access-token-auth') !== false) { $event->getRequest()->addHeader('Authorization', 'Bearer ' . $this->accessToken->getAccessToken()); } }
/** * @param PreparedEvent $event */ public function onPrepared(PreparedEvent $event) { // Supports the following options: // 'connect_timeout', 'timeout', 'verify', 'ssl_key', // 'cert', 'proxy', 'debug', 'save_to', 'stream', // 'expect', 'future' $event->getRequest()->getConfig()->overwriteWith($this->getRequestOptions($event)); }
public function setCredentials(PreparedEvent $event) { $request = $event->getRequest(); if (strtolower($event->getCommand()->getName()) !== 'getcookie') { $request->addHeader('SubscriptionToken', $this->credentials->getSubscriptionToken()); } else { $request->removeHeader('SubscriptionToken'); } }
public function onPrepared(PreparedEvent $event) { $command = $event->getCommand(); if ($command->hasParam('QueueUrl')) { $request = $event->getRequest(); $url = Url::fromString($request->getUrl()); $request->setUrl($url->combine($command['QueueUrl'])); } }
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 onPrepared(PreparedEvent $event) { if ($this->userToken === null) { return; } $command = $event->getCommand(); $operation = $this->description->getOperation($command->getName()); if ($operation->getData('user-token-authentication') === true) { $query = $event->getRequest()->getQuery(); $query->add('user_api_token', $this->userToken->getToken()); } }
/** * Event to add Segment.io Specific data to the Event Messages * * @param PreparedEvent $event The PreparedEvent * * @return bool */ public function onPrepared(PreparedEvent $event) { $command = $event->getCommand(); $operation = $this->description->getOperation($command->getName()); if (!$operation->getData('batching')) { return false; } $parameters = json_decode($event->getRequest()->getBody()->getContents(), true); $this->enqueue(array_merge($parameters, ['action' => $command->getName()])); $event->intercept(['success' => true, 'batched' => true]); return true; }
/** * Handle the before trigger * * @param PreparedEvent event */ public function onBefore(PreparedEvent $e) { $request = $e->getRequest(); if (empty($request)) { return; } $path = $request->getPath(); if (empty($path)) { return; } $path = rawurldecode($path); $path = str_replace('//', '/', $path); $request->setPath($path); }
public function onPrepared(PreparedEvent $event) { if ($this->apiKeyToken === null) { return; } $command = $event->getCommand(); $operation = $this->description->getOperation($command->getName()); if ($operation->getData('api-key-authentication') === true) { $query = $event->getRequest()->getQuery(); $query->add('api_key', $this->apiKeyToken->getApiKey()); $query->add('developer_key', $this->apiKeyToken->getDeveloperKey()); $query->add('client_key', $this->apiKeyToken->getClientKey()); } }
/** * Changes how buckets are referenced in the HTTP request * * @param PreparedEvent $event Event emitted */ public function setBucketStyle(PreparedEvent $event) { $command = $event->getCommand(); $request = $event->getRequest(); $bucket = $command['Bucket']; $path = $request->getPath(); if (!$bucket || isset(self::$exclusions[$command->getName()])) { return; } if ($this->bucketEndpoint) { $path = $this->removeBucketFromPath($path, $bucket); } elseif (!$command['PathStyle'] && S3Client::isBucketDnsCompatible($bucket) && !($request->getScheme() == 'https' && strpos($bucket, '.'))) { // Switch to virtual if PathStyle is disabled, or not a DNS // compatible bucket name, or the scheme is https and there are no // dots in the hostheader (avoids SSL issues). $request->setHost($bucket . '.' . $request->getHost()); $path = $this->removeBucketFromPath($path, $bucket); } // Modify the Key to make sure the key is encoded, but slashes are not. if ($command['Key']) { $path = S3Client::encodeKey(rawurldecode($path)); } $request->setPath($path); }
public function onPrepared(PreparedEvent $event) { // no requests have a body but EAN/Akamai still requires this header $event->getRequest()->setHeader('Content-Length', '0'); }
public function setCredentials(PreparedEvent $event) { $request = $event->getRequest(); $request->addHeaders($this->credentials->toArray()); }