Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge 40eed95 into 7561382
Browse files Browse the repository at this point in the history
  • Loading branch information
snapshotpl committed Jul 19, 2018
2 parents 7561382 + 40eed95 commit a75383c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 76 deletions.
73 changes: 43 additions & 30 deletions src/MessageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,12 @@ public function getHeaders()
*/
public function hasHeader($header)
{
return isset($this->headerNames[strtolower($header)]);
return $this->hasHeaderNormalized(strtolower($header));
}

private function hasHeaderNormalized($normalizedHeader)
{
return isset($this->headerNames[$normalizedHeader]);
}

/**
Expand All @@ -142,13 +147,13 @@ public function hasHeader($header)
*/
public function getHeader($header)
{
if (! $this->hasHeader($header)) {
$normalized = strtolower($header);

if (! $this->hasHeaderNormalized($normalized)) {
return [];
}

$header = $this->headerNames[strtolower($header)];

return $this->headers[$header];
return $this->headers[$this->headerNames[$normalized]];
}

/**
Expand Down Expand Up @@ -198,21 +203,16 @@ public function getHeaderLine($name)
*/
public function withHeader($header, $value)
{
$this->assertHeader($header);
$new = clone $this;
$new->assertHeader($header);

$normalized = strtolower($header);

$new = clone $this;
if ($new->hasHeader($header)) {
if ($new->hasHeaderNormalized($normalized)) {
unset($new->headers[$new->headerNames[$normalized]]);
}

$value = $this->filterHeaderValue($value);

$new->headerNames[$normalized] = $header;
$new->headers[$header] = $value;

return $new;
return $this->addNewHeader($new, $normalized, $header, $value);
}

/**
Expand All @@ -236,18 +236,31 @@ public function withAddedHeader($header, $value)
{
$this->assertHeader($header);

if (! $this->hasHeader($header)) {
return $this->withHeader($header, $value);
$normalized = strtolower($header);

$new = clone $this;

if (! $this->hasHeaderNormalized($normalized)) {
return $new->addNewHeader($new, $normalized, $header, $value);
}

$header = $this->headerNames[strtolower($header)];
$header = $new->headerNames[$normalized];

$new = clone $this;
$value = $this->filterHeaderValue($value);
$value = $new->filterHeaderValue($value);
$new->headers[$header] = array_merge($this->headers[$header], $value);
return $new;
}

private function addNewHeader(self $instance, $normalizedHeader, $header, $value)
{
$value = $instance->filterHeaderValue($value);

$instance->headerNames[$normalizedHeader] = $header;
$instance->headers[$header] = $value;

return $instance;
}

/**
* Return an instance without the specified header.
*
Expand All @@ -262,14 +275,18 @@ public function withAddedHeader($header, $value)
*/
public function withoutHeader($header)
{
if (! $this->hasHeader($header)) {
return clone $this;
$normalized = strtolower($header);

if ($normalized === 'host') {
return $this;
}

$normalized = strtolower($header);
$original = $this->headerNames[$normalized];
if (! $this->hasHeaderNormalized($normalized)) {
return clone $this;
}

$new = clone $this;
$original = $new->headerNames[$normalized];
unset($new->headers[$original], $new->headerNames[$normalized]);
return $new;
}
Expand Down Expand Up @@ -330,19 +347,15 @@ private function getStream($stream, $modeIfNotInstance)
*/
private function setHeaders(array $originalHeaders)
{
$headerNames = $headers = [];
$this->headerNames = $this->headers = [];

foreach ($originalHeaders as $header => $value) {
$value = $this->filterHeaderValue($value);
$normalized = strtolower($header);

$this->assertHeader($header);

$headerNames[strtolower($header)] = $header;
$headers[$header] = $value;
$this->addNewHeader($this, $normalized, $header, $value);
}

$this->headerNames = $headerNames;
$this->headers = $headers;
}

/**
Expand Down
35 changes: 0 additions & 35 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,39 +35,4 @@ public function __construct($uri = null, $method = null, $body = 'php://temp', a
{
$this->initialize($uri, $method, $body, $headers);
}

/**
* {@inheritdoc}
*/
public function getHeaders()
{
$headers = $this->headers;
if (! $this->hasHeader('host')
&& $this->uri->getHost()
) {
$headers['Host'] = [$this->getHostFromUri()];
}

return $headers;
}

/**
* {@inheritdoc}
*/
public function getHeader($header)
{
if (! $this->hasHeader($header)) {
if (strtolower($header) === 'host'
&& $this->uri->getHost()
) {
return [$this->getHostFromUri()];
}

return [];
}

$header = $this->headerNames[strtolower($header)];

return $this->headers[$header];
}
}
20 changes: 9 additions & 11 deletions src/RequestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,13 @@ public function getUri()
*/
public function withUri(UriInterface $uri, $preserveHost = false)
{
$normalizedHeader = 'host';
$new = clone $this;
$new->uri = $uri;

if ($preserveHost && $this->hasHeader('Host')) {
$hasHeader = isset($this->headerNames[$normalizedHeader]);

if ($preserveHost && isset($hasHeader)) {
return $new;
}

Expand All @@ -267,18 +270,13 @@ public function withUri(UriInterface $uri, $preserveHost = false)
$host .= ':' . $uri->getPort();
}

$new->headerNames['host'] = 'Host';

// Remove an existing host header if present, regardless of current
// de-normalization of the header name.
// @see https://github.com/zendframework/zend-diactoros/issues/91
foreach (array_keys($new->headers) as $header) {
if (strtolower($header) === 'host') {
unset($new->headers[$header]);
}
if ($hasHeader) {
unset($new->headers[$new->headerNames[$normalizedHeader]]);
}

$new->headers['Host'] = [$host];
$header = 'Host';
$new->headerNames[$normalizedHeader] = $header;
$new->headers[$header] = [$host];

return $new;
}
Expand Down

0 comments on commit a75383c

Please sign in to comment.