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

Use the Host port when port forwarding is detected #4

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Helper/ServerUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ protected function detectHost()
$this->setHost(substr($_SERVER['HTTP_HOST'], 0, 0-strlen($portStr)));
return;
}

// If the Host header contains a port, and it differs from the
// SERVER_PORT, use the port from the Host header. Typically
// this is a situation where port-forwarding is in use, and you
// want to present a URI using the public port.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment should be:

// If the Host header contains a port, then use this port. Typically
// this is a situation where port-forwarding is in use, and you
// want to present a URI using the public port.

or similar as we don't actually check that it differs from SERVER_PORT (or need to).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or similar as we don't actually check that it differs from SERVER_PORT (or need to).

There's a block immediately before it that does, actually. :) This is the condition that we hit if the SERVER_PORT and the HTTP_HOST's port are not equal.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... we should probably extract the port from HTTP_HOST once then :)

if (preg_match('/^(?P<host>.*?):(?P<port>\d+)$/', $_SERVER['HTTP_HOST'], $matches)) {
$this->setPort((int) $matches['port']);
$this->setHost(rtrim($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());
}
}