diff --git a/CHANGELOG.md b/CHANGELOG.md index 0bb812c63..36f8af353 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,7 +44,8 @@ All notable changes to this project will be documented in this file, in reverse ### Fixed -- Nothing. +- [#34](https://github.com/zendframework/zend-uri/pull/34) fixes hostname recognition + when port number is not provided. Additional colon is stripped out. ## 2.7.0 - 2019-02-27 diff --git a/src/Uri.php b/src/Uri.php index 964ba85ee..a46193d97 100644 --- a/src/Uri.php +++ b/src/Uri.php @@ -301,12 +301,17 @@ public function parse($uri) $this->setUserInfo($userInfo); } - $nMatches = preg_match('/:[\d]{1,5}$/', $authority, $matches); + $nMatches = preg_match('/:[\d]{0,5}$/', $authority, $matches); if ($nMatches === 1) { $portLength = strlen($matches[0]); $port = substr($matches[0], 1); - $this->setPort((int) $port); + // If authority ends with colon, port will be empty string. + // Remove the colon from authority, but keeps port null + if ($port) { + $this->setPort((int) $port); + } + $authority = substr($authority, 0, -$portLength); } diff --git a/test/UriTest.php b/test/UriTest.php index 16064bf15..139332c3d 100644 --- a/test/UriTest.php +++ b/test/UriTest.php @@ -1371,4 +1371,13 @@ public function testReservedCharsInPathUnencoded() $uri->toString() ); } + + public function testUriWithEndingColonWithoutPort() + { + $uriString = 'http://www.example.com:'; + $uri = new Uri($uriString); + + $this->assertSame('www.example.com', $uri->getHost()); + $this->assertNull($uri->getPort()); + } }