/** * @param RequestInterface $originalRequest * @param ResponseInterface|null $response * * @return Span */ public function fromIncomingResponse(RequestInterface $originalRequest, ResponseInterface $response = null) { $span = $originalRequest->getHeader('X-B3-SpanId'); $trace = $originalRequest->getHeader('X-B3-TraceId'); if (empty($span) || empty($trace)) { throw new \InvalidArgumentException('Unable to find the original request properties'); } return new Span(Identifier::fromString($span), $this->getName($originalRequest), Identifier::fromString($trace), [new Annotation(Annotation::CLIENT_RECEIVE, $this->clock->microseconds(), $this->endpointResolver->resolve())], [new BinaryAnnotation('http.status', $response !== null ? $response->getStatusCode() : 0, BinaryAnnotation::TYPE_INTEGER_16)]); }
private function createCanonicalizedString(RequestInterface $request, $expires = null) { $buffer = $request->getMethod() . "\n"; // Add the interesting headers foreach ($this->signableHeaders as $header) { $buffer .= $request->getHeader($header) . "\n"; } $date = $expires ?: $request->getHeader('date'); $buffer .= "{$date}\n" . $this->createCanonicalizedAmzHeaders($request) . $this->createCanonicalizedResource($request); return $buffer; }
private function applyBody(RequestInterface $request, array &$options) { if ($request->hasHeader('Content-Length')) { $size = (int) $request->getHeader('Content-Length'); } else { $size = null; } $request->getBody()->seek(0); // You can send the body as a string using curl's CURLOPT_POSTFIELDS if ($size !== null && $size < 32768 || isset($request->getConfig()['curl']['body_as_string'])) { $options[CURLOPT_POSTFIELDS] = $request->getBody()->getContents(); // Don't duplicate the Content-Length header $this->removeHeader('Content-Length', $options); $this->removeHeader('Transfer-Encoding', $options); } else { $options[CURLOPT_UPLOAD] = true; // Let cURL handle setting the Content-Length header if ($size !== null) { $options[CURLOPT_INFILESIZE] = $size; $this->removeHeader('Content-Length', $options); } } // If the Expect header is not present, prevent curl from adding it if (!$request->hasHeader('Expect')) { $options[CURLOPT_HTTPHEADER][] = 'Expect:'; } }
/** * Asserts that the authorization header is correct. * * @param RequestInterface $request Request to test * * @return void */ protected function assertAuthorization(RequestInterface $request) { list($alg, $digest) = explode(' ', $request->getHeader('Authorization')); $this->assertEquals('Basic', $alg); $expected = self::MERCHANT_ID . ':' . self::SHARED_SECRET; $this->assertEquals($expected, base64_decode($digest)); }
/** * @param RequestInterface $httpRequest The HTTP request before it is sent. * @return bool false if the request needs to be authorized */ private function isRequestAuthorized(RequestInterface $httpRequest) { $authorization = trim($httpRequest->getHeader('Authorization')); if (!$authorization) { return false; } else { return strpos($authorization, 'Basic') === 0; } }
/** * @param RequestInterface|ResponseInterface $message * @return string */ protected function formatBody($message) { $header = $message->getHeader('Content-Type'); if (JsonStringFormatter::isJsonHeader($header)) { $formatter = new JsonStringFormatter(); return $formatter->format($message->getBody()); } elseif (XmlStringFormatter::isXmlHeader($header)) { $formatter = new XmlStringFormatter(); return $formatter->format($message->getBody()); } $factory = new StringFactoryFormatter(); return $factory->format($message->getBody()); }
private function getRequestCookieValues(HttpRequest $request) { if (!$request->hasHeader('Cookie')) { return []; } $cookieStrings = explode(';', $request->getHeader('Cookie')); $values = []; foreach ($cookieStrings as $cookieString) { $cookieString = trim($cookieString); $currentValues = explode('=', $cookieString); $values[$currentValues[0]] = $currentValues[1]; } return $values; }
private function add_decode_content(RequestInterface $request, RequestMediator $mediator, &$options, $value) { if (!$request->hasHeader('Accept-Encoding')) { $options[CURLOPT_ENCODING] = ''; // Don't let curl send the header over the wire $options[CURLOPT_HTTPHEADER][] = 'Accept-Encoding:'; } else { $options[CURLOPT_ENCODING] = $request->getHeader('Accept-Encoding'); } }
/** * Apply POST fields and files to a request to attempt to give an accurate * representation. * * @param RequestInterface $request Request to update * @param array $body Body to apply */ protected function addPostData(RequestInterface $request, array $body) { static $fields = ['string' => true, 'array' => true, 'NULL' => true, 'boolean' => true, 'double' => true, 'integer' => true]; $post = new PostBody(); foreach ($body as $key => $value) { if (isset($fields[gettype($value)])) { $post->setField($key, $value); } elseif ($value instanceof PostFileInterface) { $post->addFile($value); } else { $post->addFile(new PostFile($key, $value)); } } if ($request->getHeader('Content-Type') == 'multipart/form-data') { $post->forceMultipartUpload(true); } $request->setBody($post); }
private function createStreamResource(RequestInterface $request, array $options, $context, &$http_response_header) { $url = $request->getUrl(); // Add automatic gzip decompression if (strpos($request->getHeader('Accept-Encoding'), 'gzip') !== false) { $url = 'compress.zlib://' . $url; } return $this->createResource(function () use($url, &$http_response_header, $context) { if (false === strpos($url, 'http')) { trigger_error("URL is invalid: {$url}", E_USER_WARNING); return null; } return fopen($url, 'r', null, $context); }, $request, $options); }
private static function isJson(RequestInterface $request) { return preg_match('#^application/json#', $request->getHeader('Content-Type')); }
/** * Hash a request URL into a string that returns cache metadata. * * @param RequestInterface $request The Request to generate the cache key * for. * @param array $vary (optional) An array of headers to vary * the cache key by. * * @return string */ private function getCacheKey(RequestInterface $request, array $vary = []) { $key = $request->getMethod() . ' ' . $request->getUrl(); // If Vary headers have been passed in, fetch each header and add it to // the cache key. foreach ($vary as $header) { $key .= " {$header}: " . $request->getHeader($header); } return $this->keyPrefix . md5($key); }
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; }