2022-01-21 08:23:52 +00:00
|
|
|
<?php
|
|
|
|
|
2022-01-24 08:08:35 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-01-21 08:23:52 +00:00
|
|
|
namespace GuzzleHttp\Psr7;
|
|
|
|
|
2022-01-24 08:08:35 +00:00
|
|
|
use Psr\Http\Message\MessageInterface;
|
2022-01-21 08:23:52 +00:00
|
|
|
use Psr\Http\Message\StreamInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trait implementing functionality common to requests and responses.
|
|
|
|
*/
|
|
|
|
trait MessageTrait
|
|
|
|
{
|
2022-01-24 08:08:35 +00:00
|
|
|
/** @var array<string, string[]> Map of all registered headers, as original name => array of values */
|
2022-01-21 08:23:52 +00:00
|
|
|
private $headers = [];
|
|
|
|
|
2022-01-24 08:08:35 +00:00
|
|
|
/** @var array<string, string> Map of lowercase header name => original name at registration */
|
2022-01-21 08:23:52 +00:00
|
|
|
private $headerNames = [];
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $protocol = '1.1';
|
|
|
|
|
|
|
|
/** @var StreamInterface|null */
|
|
|
|
private $stream;
|
|
|
|
|
2022-01-24 08:08:35 +00:00
|
|
|
public function getProtocolVersion(): string
|
2022-01-21 08:23:52 +00:00
|
|
|
{
|
|
|
|
return $this->protocol;
|
|
|
|
}
|
|
|
|
|
2022-01-24 08:08:35 +00:00
|
|
|
public function withProtocolVersion($version): MessageInterface
|
2022-01-21 08:23:52 +00:00
|
|
|
{
|
|
|
|
if ($this->protocol === $version) {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
$new = clone $this;
|
|
|
|
$new->protocol = $version;
|
|
|
|
return $new;
|
|
|
|
}
|
|
|
|
|
2022-01-24 08:08:35 +00:00
|
|
|
public function getHeaders(): array
|
2022-01-21 08:23:52 +00:00
|
|
|
{
|
|
|
|
return $this->headers;
|
|
|
|
}
|
|
|
|
|
2022-01-24 08:08:35 +00:00
|
|
|
public function hasHeader($header): bool
|
2022-01-21 08:23:52 +00:00
|
|
|
{
|
|
|
|
return isset($this->headerNames[strtolower($header)]);
|
|
|
|
}
|
|
|
|
|
2022-01-24 08:08:35 +00:00
|
|
|
public function getHeader($header): array
|
2022-01-21 08:23:52 +00:00
|
|
|
{
|
|
|
|
$header = strtolower($header);
|
|
|
|
|
|
|
|
if (!isset($this->headerNames[$header])) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$header = $this->headerNames[$header];
|
|
|
|
|
|
|
|
return $this->headers[$header];
|
|
|
|
}
|
|
|
|
|
2022-01-24 08:08:35 +00:00
|
|
|
public function getHeaderLine($header): string
|
2022-01-21 08:23:52 +00:00
|
|
|
{
|
|
|
|
return implode(', ', $this->getHeader($header));
|
|
|
|
}
|
|
|
|
|
2022-01-24 08:08:35 +00:00
|
|
|
public function withHeader($header, $value): MessageInterface
|
2022-01-21 08:23:52 +00:00
|
|
|
{
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2022-01-24 08:08:35 +00:00
|
|
|
public function withAddedHeader($header, $value): MessageInterface
|
2022-01-21 08:23:52 +00:00
|
|
|
{
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2022-01-24 08:08:35 +00:00
|
|
|
public function withoutHeader($header): MessageInterface
|
2022-01-21 08:23:52 +00:00
|
|
|
{
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2022-01-24 08:08:35 +00:00
|
|
|
public function getBody(): StreamInterface
|
2022-01-21 08:23:52 +00:00
|
|
|
{
|
|
|
|
if (!$this->stream) {
|
|
|
|
$this->stream = Utils::streamFor('');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->stream;
|
|
|
|
}
|
|
|
|
|
2022-01-24 08:08:35 +00:00
|
|
|
public function withBody(StreamInterface $body): MessageInterface
|
2022-01-21 08:23:52 +00:00
|
|
|
{
|
|
|
|
if ($body === $this->stream) {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
$new = clone $this;
|
|
|
|
$new->stream = $body;
|
|
|
|
return $new;
|
|
|
|
}
|
|
|
|
|
2022-01-24 08:08:35 +00:00
|
|
|
/**
|
|
|
|
* @param array<string|int, string|string[]> $headers
|
|
|
|
*/
|
|
|
|
private function setHeaders(array $headers): void
|
2022-01-21 08:23:52 +00:00
|
|
|
{
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 08:08:35 +00:00
|
|
|
/**
|
|
|
|
* @param mixed $value
|
|
|
|
*
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
private function normalizeHeaderValue($value): array
|
2022-01-21 08:23:52 +00:00
|
|
|
{
|
|
|
|
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 )
|
|
|
|
*
|
2022-01-24 08:08:35 +00:00
|
|
|
* @param mixed[] $values Header values
|
2022-01-21 08:23:52 +00:00
|
|
|
*
|
|
|
|
* @return string[] Trimmed header values
|
|
|
|
*
|
|
|
|
* @see https://tools.ietf.org/html/rfc7230#section-3.2.4
|
|
|
|
*/
|
2022-01-24 08:08:35 +00:00
|
|
|
private function trimHeaderValues(array $values): array
|
2022-01-21 08:23:52 +00:00
|
|
|
{
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2022-01-24 08:08:35 +00:00
|
|
|
/**
|
|
|
|
* @see https://tools.ietf.org/html/rfc7230#section-3.2
|
|
|
|
*
|
|
|
|
* @param mixed $header
|
|
|
|
*/
|
|
|
|
private function assertHeader($header): void
|
2022-01-21 08:23:52 +00:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2022-01-24 08:08:35 +00:00
|
|
|
if (! preg_match('/^[a-zA-Z0-9\'`#$%&*+.^_|~!-]+$/', $header)) {
|
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
sprintf(
|
|
|
|
'"%s" is not valid header name',
|
|
|
|
$header
|
|
|
|
)
|
|
|
|
);
|
2022-01-21 08:23:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|