From df8828ec8c697e77f57eb569d76b677acde49246 Mon Sep 17 00:00:00 2001 From: Matteo <5571458+mtagliab@users.noreply.github.com> Date: Fri, 20 Sep 2019 15:43:54 +0200 Subject: [PATCH 1/9] Remove ending colon from URI without port --- src/Uri.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Uri.php b/src/Uri.php index 964ba85ee..3e27270ea 100644 --- a/src/Uri.php +++ b/src/Uri.php @@ -301,10 +301,16 @@ 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); + + // 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); + } $this->setPort((int) $port); $authority = substr($authority, 0, -$portLength); From 3383c18e423884171a78b905c299e4b175831be7 Mon Sep 17 00:00:00 2001 From: Matteo <5571458+mtagliab@users.noreply.github.com> Date: Fri, 20 Sep 2019 15:45:05 +0200 Subject: [PATCH 2/9] Added test to check colon is correctly removed --- test/UriTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) 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); + + } } From 5ae77d3ef1937bb6e128b6582b7ff27273338765 Mon Sep 17 00:00:00 2001 From: Matteo <5571458+mtagliab@users.noreply.github.com> Date: Mon, 23 Sep 2019 08:47:38 +0200 Subject: [PATCH 3/9] Fix - removed double port assign --- src/Uri.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Uri.php b/src/Uri.php index 3e27270ea..d9bdcf842 100644 --- a/src/Uri.php +++ b/src/Uri.php @@ -312,7 +312,6 @@ public function parse($uri) $this->setPort((int) $port); } - $this->setPort((int) $port); $authority = substr($authority, 0, -$portLength); } From a11fde29ef71d209b22c0c6f04dbe976ba011030 Mon Sep 17 00:00:00 2001 From: Matteo <5571458+mtagliab@users.noreply.github.com> Date: Mon, 30 Sep 2019 10:54:01 +0200 Subject: [PATCH 4/9] Fix indentation --- src/Uri.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Uri.php b/src/Uri.php index d9bdcf842..449bc086e 100644 --- a/src/Uri.php +++ b/src/Uri.php @@ -305,10 +305,10 @@ public function parse($uri) if ($nMatches === 1) { $portLength = strlen($matches[0]); $port = substr($matches[0], 1); - + // If authority ends with colon, port will be empty string. // Remove the colon from authority, but keeps port null - if($port !== ''){ + if ($port !== '') { $this->setPort((int) $port); } From 8cc05d9a9ed2f80a4a3fd71bed6b1d8d458a1d5a Mon Sep 17 00:00:00 2001 From: Matteo <5571458+mtagliab@users.noreply.github.com> Date: Mon, 30 Sep 2019 10:55:03 +0200 Subject: [PATCH 5/9] Fix indentation --- test/UriTest.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/UriTest.php b/test/UriTest.php index 42791dafc..45f5080e5 100644 --- a/test/UriTest.php +++ b/test/UriTest.php @@ -1371,14 +1371,13 @@ 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); - } } From 212c81d6d270a6d0a2129290f0e9c2c1bc81e349 Mon Sep 17 00:00:00 2001 From: Matteo <5571458+mtagliab@users.noreply.github.com> Date: Mon, 30 Sep 2019 13:23:07 +0200 Subject: [PATCH 6/9] Fixed problem with substr backwards compatibility https://www.php.net/manual/en/migration70.incompatible.php#119151 substr behavior has changed from PHP 5.6 to PHP 7.0 --- src/Uri.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Uri.php b/src/Uri.php index 449bc086e..da4e82cd5 100644 --- a/src/Uri.php +++ b/src/Uri.php @@ -308,7 +308,7 @@ public function parse($uri) // If authority ends with colon, port will be empty string. // Remove the colon from authority, but keeps port null - if ($port !== '') { + if ($port && $port !== '') { $this->setPort((int) $port); } From 9055a4c19038bc2920fdd6873c6cb7fcfb505d07 Mon Sep 17 00:00:00 2001 From: Matteo <5571458+mtagliab@users.noreply.github.com> Date: Mon, 30 Sep 2019 13:43:32 +0200 Subject: [PATCH 7/9] Inverted order of parameters --- test/UriTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/UriTest.php b/test/UriTest.php index 45f5080e5..5515fbb80 100644 --- a/test/UriTest.php +++ b/test/UriTest.php @@ -1377,7 +1377,7 @@ public function testUriWithEndingColonWithoutPort() $uriString = 'http://www.example.com:'; $uri = new Uri($uriString); - $this->assertSame($uri->getHost(), 'www.example.com'); - $this->assertSame($uri->getPort(), null); + $this->assertSame('www.example.com', $uri->getHost()); + $this->assertSame(null, $uri->getPort()); } } From c6a0127a9d78120b34238664ba205282b0827bac Mon Sep 17 00:00:00 2001 From: Matteo <5571458+mtagliab@users.noreply.github.com> Date: Mon, 30 Sep 2019 13:44:09 +0200 Subject: [PATCH 8/9] Removed strict compare --- src/Uri.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Uri.php b/src/Uri.php index da4e82cd5..a46193d97 100644 --- a/src/Uri.php +++ b/src/Uri.php @@ -308,7 +308,7 @@ public function parse($uri) // If authority ends with colon, port will be empty string. // Remove the colon from authority, but keeps port null - if ($port && $port !== '') { + if ($port) { $this->setPort((int) $port); } From 1cb32bea7e46e395d68313f029dd93f73bb284e8 Mon Sep 17 00:00:00 2001 From: webimpress Date: Mon, 7 Oct 2019 14:13:04 +0100 Subject: [PATCH 9/9] Use better assertion --- test/UriTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/UriTest.php b/test/UriTest.php index 5515fbb80..139332c3d 100644 --- a/test/UriTest.php +++ b/test/UriTest.php @@ -1378,6 +1378,6 @@ public function testUriWithEndingColonWithoutPort() $uri = new Uri($uriString); $this->assertSame('www.example.com', $uri->getHost()); - $this->assertSame(null, $uri->getPort()); + $this->assertNull($uri->getPort()); } }