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

Commit 4beaf9f

Browse files
committed
Merge branch 'weierophinney-hotfix/91'
2 parents 7aa2682 + e5bbef1 commit 4beaf9f

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@ All notable changes to this project will be documented in this file, in reverse
2121
- [#96](https://github.com/zendframework/zend-diactoros/pull/96) updates
2222
`withPort()` to allow `null` port values (indicating usage of default for
2323
the given scheme).
24-
- [#98](https://github.com/zendframework/zend-diactoros/pull/98) updates
25-
the default JSON flags used by `JsonResponse` to include
26-
`JSON_UNESCAPED_SLASHES`, which still conforms with RFC 4627, and is a more
27-
sane default.
24+
- [#91](https://github.com/zendframework/zend-diactoros/pull/91) fixes the
25+
logic of `withUri()` to do a case-insensitive check for an existing `Host`
26+
header, replacing it with the new one.
2827

2928
## 1.1.3 - 2015-08-10
3029

@@ -205,7 +204,7 @@ immediately.
205204
- [#57](https://github.com/zendframework/zend-diactoros/pull/57) fixes the
206205
behavior of how the `ServerRequestFactory` marshals upload files when they are
207206
represented as a nested associative array.
208-
- [#49](https://github.com/zendframework/zend-diactoros/pull/49) provides several
207+
- [#49](https://github.com/zendframework/zend-diactoros/pull/49) provides several
209208
fixes that ensure that Diactoros complies with the PSR-7 specification:
210209
- `MessageInterface::getHeaderLine()` MUST return a string (that string CAN be
211210
empty). Previously, Diactoros would return `null`.

src/RequestTrait.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,16 @@ public function withUri(UriInterface $uri, $preserveHost = false)
249249
}
250250

251251
$new->headerNames['host'] = 'Host';
252+
253+
// Remove an existing host header if present, regardless of current
254+
// de-normalization of the header name.
255+
// @see https://github.com/zendframework/zend-diactoros/issues/91
256+
foreach (array_keys($new->headers) as $header) {
257+
if (strtolower($header) === 'host') {
258+
unset($new->headers[$header]);
259+
}
260+
}
261+
252262
$new->headers['Host'] = [$host];
253263

254264
return $new;

test/RequestTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,4 +455,46 @@ public function testConstructorRaisesExceptionForHeadersWithCRLFVectors($name, $
455455
$this->setExpectedException('InvalidArgumentException');
456456
$request = new Request(null, null, 'php://memory', [$name => $value]);
457457
}
458+
459+
public function hostHeaderKeys()
460+
{
461+
return [
462+
'lowercase' => ['host'],
463+
'mixed-4' => ['hosT'],
464+
'mixed-3-4' => ['hoST'],
465+
'reverse-titlecase' => ['hOST'],
466+
'uppercase' => ['HOST'],
467+
'mixed-1-2-3' => ['HOSt'],
468+
'mixed-1-2' => ['HOst'],
469+
'titlecase' => ['Host'],
470+
'mixed-1-4' => ['HosT'],
471+
'mixed-1-2-4' => ['HOsT'],
472+
'mixed-1-3-4' => ['HoST'],
473+
'mixed-1-3' => ['HoSt'],
474+
'mixed-2-3' => ['hOSt'],
475+
'mixed-2-4' => ['hOsT'],
476+
'mixed-2' => ['hOst'],
477+
'mixed-3' => ['hoSt'],
478+
];
479+
}
480+
481+
/**
482+
* @group 91
483+
* @dataProvider hostHeaderKeys
484+
*/
485+
public function testWithUriAndNoPreserveHostWillOverwriteHostHeaderRegardlessOfOriginalCase($hostKey)
486+
{
487+
$request = (new Request())
488+
->withHeader($hostKey, 'example.com');
489+
490+
$uri = new Uri('http://example.org/foo/bar');
491+
$new = $request->withUri($uri);
492+
$host = $new->getHeaderLine('host');
493+
$this->assertEquals('example.org', $host);
494+
$headers = $new->getHeaders();
495+
$this->assertArrayHasKey('Host', $headers);
496+
if ($hostKey !== 'Host') {
497+
$this->assertArrayNotHasKey($hostKey, $headers);
498+
}
499+
}
458500
}

0 commit comments

Comments
 (0)