Skip to content

Commit 9eb3c09

Browse files
committed
added missing port to the Host when not-standard
1 parent f61a4c3 commit 9eb3c09

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/functions.php

+8
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,14 @@ function modify_request(RequestInterface $request, array $changes)
209209
// Remove the host header if one is on the URI
210210
if ($host = $changes['uri']->getHost()) {
211211
$changes['set_headers']['Host'] = $host;
212+
213+
if ($port = $changes['uri']->getPort()) {
214+
$standardPorts = ['http' => 80, 'https' => 443];
215+
$scheme = $changes['uri']->getScheme();
216+
if (isset($standardPorts[$scheme]) && $port != $standardPorts[$scheme]) {
217+
$changes['set_headers']['Host'] .= ':'.$port;
218+
}
219+
}
212220
}
213221
$uri = $changes['uri'];
214222
}

tests/FunctionsTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,16 @@ public function testCanModifyRequestWithUri()
546546
$this->assertEquals('www.foo.com', (string) $r2->getHeaderLine('host'));
547547
}
548548

549+
public function testCanModifyRequestWithUriAndPort()
550+
{
551+
$r1 = new Psr7\Request('GET', 'http://foo.com:8000');
552+
$r2 = Psr7\modify_request($r1, [
553+
'uri' => new Psr7\Uri('http://www.foo.com:8000')
554+
]);
555+
$this->assertEquals('http://www.foo.com:8000', (string) $r2->getUri());
556+
$this->assertEquals('www.foo.com:8000', (string) $r2->getHeaderLine('host'));
557+
}
558+
549559
public function testCanModifyRequestWithCaseInsensitiveHeader()
550560
{
551561
$r1 = new Psr7\Request('GET', 'http://foo.com', ['User-Agent' => 'foo']);

0 commit comments

Comments
 (0)