diff --git a/src/Uri.php b/src/Uri.php index 964ba85ee..449bc086e 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..42791dafc 100644 --- a/test/UriTest.php +++ b/test/UriTest.php @@ -1371,4 +1371,14 @@ public function testReservedCharsInPathUnencoded() $uri->toString() ); } + + public function testUriWithEndingColonWithoutPort() + { + $uriString = 'http://www.example.com:'; + $uri = new Uri($uriString); + + $this->assertSame($uri->getHost(), 'www.example.com'); + $this->assertSame($uri->getPort(), null); + + } }