<?php declare(strict_types=1); namespace GuzzleHttp\Psr7; use Psr\Http\Message\MessageInterface; use Psr\Http\Message\StreamInterface; /** * Trait implementing functionality common to requests and responses. */ trait MessageTrait { /** @var array<string, string[]> Map of all registered headers, as original name => array of values */ private $headers = []; /** @var array<string, string> Map of lowercase header name => original name at registration */ private $headerNames = []; /** @var string */ private $protocol = '1.1'; /** @var StreamInterface|null */ private $stream; public function getProtocolVersion(): string { return $this->protocol; } public function withProtocolVersion($version): MessageInterface { if ($this->protocol === $version) { return $this; } $new = clone $this; $new->protocol = $version; return $new; } public function getHeaders(): array { return $this->headers; } public function hasHeader($header): bool { return isset($this->headerNames[strtolower($header)]); } public function getHeader($header): array { $header = strtolower($header); if (!isset($this->headerNames[$header])) { return []; } $header = $this->headerNames[$header]; return $this->headers[$header]; } public function getHeaderLine($header): string { return implode(', ', $this->getHeader($header)); } public function withHeader($header, $value): MessageInterface { $this->assertHeader($header); $value = $this->normalizeHeaderValue($value); $normalized = strtolower($header); $new = clone $this; if (isset($new->headerNames[$normalized])) { unset($new->headers[$new->headerNames[$normalized]]); } $new->headerNames[$normalized] = $header; $new->headers[$header] = $value; return $new; } public function withAddedHeader($header, $value): MessageInterface { $this->assertHeader($header); $value = $this->normalizeHeaderValue($value); $normalized = strtolower($header); $new = clone $this; if (isset($new->headerNames[$normalized])) { $header = $this->headerNames[$normalized]; $new->headers[$header] = array_merge($this->headers[$header], $value); } else { $new->headerNames[$normalized] = $header; $new->headers[$header] = $value; } return $new; } public function withoutHeader($header): MessageInterface { $normalized = strtolower($header); if (!isset($this->headerNames[$normalized])) { return $this; } $header = $this->headerNames[$normalized]; $new = clone $this; unset($new->headers[$header], $new->headerNames[$normalized]); return $new; } public function getBody(): StreamInterface { if (!$this->stream) { $this->stream = Utils::streamFor(''); } return $this->stream; } public function withBody(StreamInterface $body): MessageInterface { if ($body === $this->stream) { return $this; } $new = clone $this; $new->stream = $body; return $new; } /** * @param array<string|int, string|string[]> $headers */ private function setHeaders(array $headers): void { $this->headerNames = $this->headers = []; foreach ($headers as $header => $value) { if (is_int($header)) { // Numeric array keys are converted to int by PHP but having a header name '123' is not forbidden by the spec // and also allowed in withHeader(). So we need to cast it to string again for the following assertion to pass. $header = (string) $header; } $this->assertHeader($header); $value = $this->normalizeHeaderValue($value); $normalized = strtolower($header); if (isset($this->headerNames[$normalized])) { $header = $this->headerNames[$normalized]; $this->headers[$header] = array_merge($this->headers[$header], $value); } else { $this->headerNames[$normalized] = $header; $this->headers[$header] = $value; } } } /** * @param mixed $value * * @return string[] */ private function normalizeHeaderValue($value): array { if (!is_array($value)) { return $this->trimHeaderValues([$value]); } if (count($value) === 0) { throw new \InvalidArgumentException('Header value can not be an empty array.'); } return $this->trimHeaderValues($value); } /** * Trims whitespace from the header values. * * Spaces and tabs ought to be excluded by parsers when extracting the field value from a header field. * * header-field = field-name ":" OWS field-value OWS * OWS = *( SP / HTAB ) * * @param mixed[] $values Header values * * @return string[] Trimmed header values * * @see https://tools.ietf.org/html/rfc7230#section-3.2.4 */ private function trimHeaderValues(array $values): array { return array_map(function ($value) { if (!is_scalar($value) && null !== $value) { throw new \InvalidArgumentException(sprintf( 'Header value must be scalar or null but %s provided.', is_object($value) ? get_class($value) : gettype($value) )); } return trim((string) $value, " \t"); }, array_values($values)); } /** * @see https://tools.ietf.org/html/rfc7230#section-3.2 * * @param mixed $header */ private function assertHeader($header): void { if (!is_string($header)) { throw new \InvalidArgumentException(sprintf( 'Header name must be a string but %s provided.', is_object($header) ? get_class($header) : gettype($header) )); } if (! preg_match('/^[a-zA-Z0-9\'`#$%&*+.^_|~!-]+$/', $header)) { throw new \InvalidArgumentException( sprintf( '"%s" is not valid header name', $header ) ); } } }
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
Exception | Folder | 0755 |
|
|
AppendStream.php | File | 5.84 KB | 0755 |
|
BufferStream.php | File | 3.17 KB | 0755 |
|
CachingStream.php | File | 4.42 KB | 0755 |
|
DroppingStream.php | File | 1.12 KB | 0755 |
|
FnStream.php | File | 4.38 KB | 0755 |
|
Header.php | File | 2.04 KB | 0755 |
|
HttpFactory.php | File | 3.02 KB | 0755 |
|
InflateStream.php | File | 1.3 KB | 0755 |
|
LazyOpenStream.php | File | 899 B | 0755 |
|
LimitStream.php | File | 4.14 KB | 0755 |
|
Message.php | File | 8.01 KB | 0755 |
|
MessageTrait.php | File | 6.46 KB | 0755 |
|
MimeType.php | File | 4.2 KB | 0755 |
|
MultipartStream.php | File | 4.67 KB | 0755 |
|
NoSeekStream.php | File | 470 B | 0755 |
|
PumpStream.php | File | 4.5 KB | 0755 |
|
Query.php | File | 3.55 KB | 0755 |
|
Request.php | File | 3.81 KB | 0755 |
|
Response.php | File | 4.79 KB | 0755 |
|
Rfc7230.php | File | 665 B | 0755 |
|
ServerRequest.php | File | 9.36 KB | 0755 |
|
Stream.php | File | 7.12 KB | 0755 |
|
StreamDecoratorTrait.php | File | 3.28 KB | 0755 |
|
StreamWrapper.php | File | 4.01 KB | 0755 |
|
UploadedFile.php | File | 4.75 KB | 0755 |
|
Uri.php | File | 21.33 KB | 0755 |
|
UriNormalizer.php | File | 8.22 KB | 0755 |
|
UriResolver.php | File | 8.36 KB | 0755 |
|
Utils.php | File | 13.91 KB | 0755 |
|