namespace GuzzleHttp\Psr7;
use Psr\Http\Message\StreamInterface;
* Trait implementing functionality common to requests and responses.
/** @var array Map of all registered headers, as original name => array of values */
/** @var array Map of lowercase header name => original name at registration */
private $headerNames = [];
private $protocol = '1.1';
/** @var StreamInterface|null */
public function getProtocolVersion()
public function withProtocolVersion($version)
if ($this->protocol === $version) {
$new->protocol = $version;
public function getHeaders()
public function hasHeader($header)
return isset($this->headerNames[strtolower($header)]);
public function getHeader($header)
$header = strtolower($header);
if (!isset($this->headerNames[$header])) {
$header = $this->headerNames[$header];
return $this->headers[$header];
public function getHeaderLine($header)
return implode(', ', $this->getHeader($header));
public function withHeader($header, $value)
$this->assertHeader($header);
$value = $this->normalizeHeaderValue($value);
$normalized = strtolower($header);
if (isset($new->headerNames[$normalized])) {
unset($new->headers[$new->headerNames[$normalized]]);
$new->headerNames[$normalized] = $header;
$new->headers[$header] = $value;
public function withAddedHeader($header, $value)
$this->assertHeader($header);
$value = $this->normalizeHeaderValue($value);
$normalized = strtolower($header);
if (isset($new->headerNames[$normalized])) {
$header = $this->headerNames[$normalized];
$new->headers[$header] = array_merge($this->headers[$header], $value);
$new->headerNames[$normalized] = $header;
$new->headers[$header] = $value;
public function withoutHeader($header)
$normalized = strtolower($header);
if (!isset($this->headerNames[$normalized])) {
$header = $this->headerNames[$normalized];
unset($new->headers[$header], $new->headerNames[$normalized]);
public function getBody()
$this->stream = Utils::streamFor('');
public function withBody(StreamInterface $body)
if ($body === $this->stream) {
private function setHeaders(array $headers)
$this->headerNames = $this->headers = [];
foreach ($headers as $header => $value) {
// 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);
$this->headerNames[$normalized] = $header;
$this->headers[$header] = $value;
private function normalizeHeaderValue($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
* @param string[] $values Header values
* @return string[] Trimmed header values
* @see https://tools.ietf.org/html/rfc7230#section-3.2.4
private function trimHeaderValues(array $values)
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));
private function assertHeader($header)
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)
throw new \InvalidArgumentException('Header name can not be empty.');