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

Commit 9894757

Browse files
committed
Merge branch 'weierophinney-hotfix/server-url-port-forward'
2 parents 7a0dd8f + 27fecdb commit 9894757

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/Helper/ServerUrl.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,23 @@ protected function detectHost()
7979

8080
if (isset($_SERVER['HTTP_HOST']) && !empty($_SERVER['HTTP_HOST'])) {
8181
// Detect if the port is set in SERVER_PORT and included in HTTP_HOST
82-
if (isset($_SERVER['SERVER_PORT'])) {
83-
$portStr = ':' . $_SERVER['SERVER_PORT'];
84-
if (substr($_SERVER['HTTP_HOST'], 0-strlen($portStr), strlen($portStr)) == $portStr) {
85-
$this->setHost(substr($_SERVER['HTTP_HOST'], 0, 0-strlen($portStr)));
82+
if (isset($_SERVER['SERVER_PORT'])
83+
&& preg_match('/^(?P<host>.*?):(?P<port>\d+)$/', $_SERVER['HTTP_HOST'], $matches)
84+
) {
85+
// If they are the same, set the host to just the hostname
86+
// portion of the Host header.
87+
if ((int) $matches['port'] === (int) $_SERVER['SERVER_PORT']) {
88+
$this->setHost($matches['host']);
8689
return;
8790
}
91+
92+
// At this point, we have a SERVER_PORT that differs from the
93+
// Host header, indicating we likely have a port-forwarding
94+
// situation. As such, we'll set the host and port from the
95+
// matched values.
96+
$this->setPort((int) $matches['port']);
97+
$this->setHost($matches['host']);
98+
return;
8899
}
89100

90101
$this->setHost($_SERVER['HTTP_HOST']);

test/Helper/ServerUrlTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,12 @@ public function testCanUseXForwardedPortIfProvided()
219219
$url->setUseProxy(true);
220220
$this->assertEquals('http://www.secondhost.org:8888', $url->__invoke());
221221
}
222+
223+
public function testUsesHostHeaderWhenPortForwardingDetected()
224+
{
225+
$_SERVER['HTTP_HOST'] = 'localhost:10088';
226+
$_SERVER['SERVER_PORT'] = 10081;
227+
$url = new Helper\ServerUrl();
228+
$this->assertEquals('http://localhost:10088', $url->__invoke());
229+
}
222230
}

0 commit comments

Comments
 (0)