diff --git a/src/PhpEnvironment/Request.php b/src/PhpEnvironment/Request.php index 87620c4d10..713363eaca 100644 --- a/src/PhpEnvironment/Request.php +++ b/src/PhpEnvironment/Request.php @@ -264,6 +264,16 @@ public function setServer(ParametersInterface $server) if (isset($this->serverParams['SERVER_PORT'])) { $port = (int) $this->serverParams['SERVER_PORT']; } + // Check for missinterpreted IPv6-Address + // Reported at least for Safari on Windows + if (isset($this->serverParams['SERVER_ADDR']) && preg_match('/^\[[0-9a-fA-F\:]+\]$/', $host)) { + $host = '[' . $this->serverParams['SERVER_ADDR'] . ']'; + if ($port . ']' == substr($host, strrpos($host, ':')+1)) { + // The last digit of the IPv6-Address has been taken as port + // Unset the port so the default port can be used + $port = null; + } + } } elseif ($this->getHeaders()->get('host')) { $host = $this->getHeaders()->get('host')->getFieldValue(); // works for regname, IPv4 & IPv6 diff --git a/test/PhpEnvironment/RequestTest.php b/test/PhpEnvironment/RequestTest.php index 3d1759fa30..ac20469a0a 100644 --- a/test/PhpEnvironment/RequestTest.php +++ b/test/PhpEnvironment/RequestTest.php @@ -301,6 +301,7 @@ public static function serverHostnameProvider() 'REQUEST_URI' => 'http://test.example.com/news', ), 'test.example.com', + '80', '/news', ), array( @@ -309,6 +310,30 @@ public static function serverHostnameProvider() 'REQUEST_URI' => 'http://test.example.com/news', ), 'test.example.com', + '80', + '/news', + ), + array( + array( + 'SERVER_NAME' => '[1:2:3:4:5:6::6]', + 'SERVER_ADDR' => '1:2:3:4:5:6::6', + 'SERVER_PORT' => '80', + 'REQUEST_URI' => 'http://[1:2:3:4:5:6::6]/news', + ), + '[1:2:3:4:5:6::6]', + '80', + '/news', + ), + // Test for broken $_SERVER implementation from Windows-Safari + array( + array( + 'SERVER_NAME' => '[1:2:3:4:5:6:]', + 'SERVER_ADDR' => '1:2:3:4:5:6::6', + 'SERVER_PORT' => '6', + 'REQUEST_URI' => 'http://[1:2:3:4:5:6::6]/news', + ), + '[1:2:3:4:5:6::6]', + '80', '/news', ), array( @@ -318,6 +343,7 @@ public static function serverHostnameProvider() 'REQUEST_URI' => 'http://test.example.com/news', ), 'test.example.com', + '8080', '/news', ), array( @@ -328,6 +354,7 @@ public static function serverHostnameProvider() 'REQUEST_URI' => 'https://test.example.com/news', ), 'test.example.com', + '443', '/news', ), ); @@ -339,7 +366,7 @@ public static function serverHostnameProvider() * @param string $name * @param string $value */ - public function testServerHostnameProvider(array $server, $expectedHost, $expectedRequestUri) + public function testServerHostnameProvider(array $server, $expectedHost, $expectedPort, $expectedRequestUri) { $_SERVER = $server; $request = new Request(); @@ -347,6 +374,9 @@ public function testServerHostnameProvider(array $server, $expectedHost, $expect $host = $request->getUri()->getHost(); $this->assertEquals($expectedHost, $host); + $port = $request->getUri()->getPort(); + $this->assertEquals($expectedPort, $port); + $requestUri = $request->getRequestUri(); $this->assertEquals($expectedRequestUri, $requestUri); }