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

Commit

Permalink
Merge branch 'weierophinney-hotfix/server-url-port-forward'
Browse files Browse the repository at this point in the history
  • Loading branch information
akrabat committed Jun 15, 2015
2 parents 7a0dd8f + 27fecdb commit 9894757
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/Helper/ServerUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,23 @@ protected function detectHost()

if (isset($_SERVER['HTTP_HOST']) && !empty($_SERVER['HTTP_HOST'])) {
// Detect if the port is set in SERVER_PORT and included in HTTP_HOST
if (isset($_SERVER['SERVER_PORT'])) {
$portStr = ':' . $_SERVER['SERVER_PORT'];
if (substr($_SERVER['HTTP_HOST'], 0-strlen($portStr), strlen($portStr)) == $portStr) {
$this->setHost(substr($_SERVER['HTTP_HOST'], 0, 0-strlen($portStr)));
if (isset($_SERVER['SERVER_PORT'])
&& preg_match('/^(?P<host>.*?):(?P<port>\d+)$/', $_SERVER['HTTP_HOST'], $matches)
) {
// If they are the same, set the host to just the hostname
// portion of the Host header.
if ((int) $matches['port'] === (int) $_SERVER['SERVER_PORT']) {
$this->setHost($matches['host']);
return;
}

// At this point, we have a SERVER_PORT that differs from the
// Host header, indicating we likely have a port-forwarding
// situation. As such, we'll set the host and port from the
// matched values.
$this->setPort((int) $matches['port']);
$this->setHost($matches['host']);
return;
}

$this->setHost($_SERVER['HTTP_HOST']);
Expand Down
8 changes: 8 additions & 0 deletions test/Helper/ServerUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,12 @@ public function testCanUseXForwardedPortIfProvided()
$url->setUseProxy(true);
$this->assertEquals('http://www.secondhost.org:8888', $url->__invoke());
}

public function testUsesHostHeaderWhenPortForwardingDetected()
{
$_SERVER['HTTP_HOST'] = 'localhost:10088';
$_SERVER['SERVER_PORT'] = 10081;
$url = new Helper\ServerUrl();
$this->assertEquals('http://localhost:10088', $url->__invoke());
}
}

0 comments on commit 9894757

Please sign in to comment.