Skip to content

Commit

Permalink
Fix zendframework#3161 by checking if the server port already exists …
Browse files Browse the repository at this point in the history
…in the host

This occurs when running the PHP built-in webserver. HTTP_HOST is set
with the host passed to the script (which can include the port number).
SERVER_PORT is also set.
  • Loading branch information
rmasters committed Dec 4, 2012
1 parent e221eaf commit 50320b9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
11 changes: 10 additions & 1 deletion library/Zend/View/Helper/ServerUrl.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function setHost($host)
if (($scheme == 'http' && (null === $port || $port == 80)) if (($scheme == 'http' && (null === $port || $port == 80))
|| ($scheme == 'https' && (null === $port || $port == 443)) || ($scheme == 'https' && (null === $port || $port == 443))
) { ) {
$this->host = $host;; $this->host = $host;
return $this; return $this;
} }


Expand Down Expand Up @@ -180,6 +180,15 @@ protected function detectHost()
} }


if (isset($_SERVER['HTTP_HOST']) && !empty($_SERVER['HTTP_HOST'])) { 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)));
return;
}
}

$this->setHost($_SERVER['HTTP_HOST']); $this->setHost($_SERVER['HTTP_HOST']);
return; return;
} }
Expand Down
9 changes: 9 additions & 0 deletions tests/ZendTest/View/Helper/ServerUrlTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ public function testConstructorWithHostIncludingPortAndHttpsTrue()
$this->assertEquals('https://example.com:8181', $url->__invoke()); $this->assertEquals('https://example.com:8181', $url->__invoke());
} }


public function testConstructorWithHttpHostIncludingPortAndPortSet()
{
$_SERVER['HTTP_HOST'] = 'example.com:8181';
$_SERVER['SERVER_PORT'] = 8181;

$url = new Helper\ServerUrl();
$this->assertEquals('http://example.com:8181', $url->__invoke());
}

public function testConstructorWithHttpHostAndServerNameAndPortSet() public function testConstructorWithHttpHostAndServerNameAndPortSet()
{ {
$_SERVER['HTTP_HOST'] = 'example.com'; $_SERVER['HTTP_HOST'] = 'example.com';
Expand Down

0 comments on commit 50320b9

Please sign in to comment.