/** * @param RequestInterface|EntityEnclosingRequestInterface $request * @param Credentials $credentials */ public function signRequest($request, $credentials) { $request->setHeader('X-HMB-Signature-Method', self::DEFAULT_METHOD); $request->setHeader('X-HMB-Signature-Version', self::DEFAULT_SIGN_VERSION); $request->setHeader('X-HMB-TimeStamp', time()); $contentMd5 = $request instanceof EntityEnclosingRequestInterface ? md5($request->getBody()) : ''; if ($contentMd5) { $request->setHeader('Content-MD5', $contentMd5); } $sign = array(); $sign[] = strtoupper($request->getMethod()); $sign[] = $request->getHost(); if ($request->getHeader('Content-MD5')) { $sign[] = $request->getHeader('Content-MD5'); } if ($request->getHeader('Content-Type')) { $sign[] = $request->getHeader('Content-Type'); } $sign[] = $request->getHeader('X-HMB-Signature-Method'); $sign[] = $request->getHeader('X-HMB-Signature-Version'); $sign[] = $request->getHeader('X-HMB-TimeStamp'); if ($request->getHeader('X-HMB-User-Session-Token')) { $sign[] = $request->getHeader('X-HMB-User-Session-Token'); } $sign[] = $request->getQuery(true) ? $request->getPath() . '?' . $request->getQuery(true) : $request->getPath(); $signature = base64_encode(hash_hmac(strtolower($request->getHeader('X-HMB-Signature-Method')), implode("\n", $sign), $credentials->getSecret())); $request->setHeader('Authorization', sprintf('%s %s:%s', self::AUTHORIZATION_SCHME, $credentials->getKey(), $signature)); }
protected function addExpectHeader(EntityEnclosingRequestInterface $request, EntityBodyInterface $body, $expect) { if ($expect === false) { $request->removeHeader('Expect'); } elseif ($expect !== true) { $expect = $expect ?: 1048576; if (is_numeric($expect) && $body->getSize()) { if ($body->getSize() < $expect) { $request->removeHeader('Expect'); } else { $request->setHeader('Expect', '100-Continue'); } } } }
/** * Add the appropriate expect header to a request * * @param EntityEnclosingRequestInterface $request Request to update * @param EntityBodyInterface $body Entity body of the request * @param string|int $expect Expect header setting */ protected function addExpectHeader(EntityEnclosingRequestInterface $request, EntityBodyInterface $body, $expect) { // Allow the `expect` data parameter to be set to remove the Expect header from the request if ($expect === false) { $request->removeHeader('Expect'); } elseif ($expect !== true) { // Default to using a MB as the point in which to start using the expect header $expect = $expect ?: 1048576; // If the expect_header value is numeric then only add if the size is greater than the cutoff if (is_numeric($expect) && $body->getSize()) { if ($body->getSize() < $expect) { $request->removeHeader('Expect'); } else { $request->setHeader('Expect', '100-Continue'); } } } }