From 971ff29a567e09dc70551966a8b1f8c61ab414ca Mon Sep 17 00:00:00 2001 From: Benni Mack Date: Wed, 21 Mar 2018 22:23:47 +0100 Subject: [PATCH] [BUGFIX] Allow PSR-7 Uris to allow no port at all In order to set a PSR-7 based uri to the default port the specs allow to set $uri->withPort(null) but our tests and implementation restrict that. Resolves: #84518 Releases: master, 8.7 Change-Id: Ic2c3d70fca35a767c7ed9d324eb93b30c66bbd3e Reviewed-on: https://review.typo3.org/56416 Tested-by: TYPO3com Reviewed-by: Mathias Brodala Tested-by: Mathias Brodala Reviewed-by: Mathias Schreiber Tested-by: Mathias Schreiber Reviewed-by: Georg Ringer Tested-by: Georg Ringer --- typo3/sysext/core/Classes/Http/Uri.php | 16 +++++++++------- typo3/sysext/core/Tests/Unit/Http/UriTest.php | 13 ++++++++++++- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/typo3/sysext/core/Classes/Http/Uri.php b/typo3/sysext/core/Classes/Http/Uri.php index 7d1b2740ac27..0bc59af819ea 100644 --- a/typo3/sysext/core/Classes/Http/Uri.php +++ b/typo3/sysext/core/Classes/Http/Uri.php @@ -434,14 +434,16 @@ public function withHost($host) */ public function withPort($port) { - if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($port) === false) { - $argumentType = is_object($port) ? get_class($port) : gettype($port); - throw new \InvalidArgumentException('Invalid port "' . $argumentType . '" specified, must be an integer.', 1436717324); - } + if ($port !== null) { + if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($port) === false) { + $argumentType = is_object($port) ? get_class($port) : gettype($port); + throw new \InvalidArgumentException('Invalid port "' . $argumentType . '" specified, must be an integer.', 1436717324); + } - $port = (int)$port; - if ($port < 1 || $port > 65535) { - throw new \InvalidArgumentException('Invalid port "' . $port . '" specified, must be a valid TCP/UDP port.', 1436717326); + $port = (int)$port; + if ($port < 1 || $port > 65535) { + throw new \InvalidArgumentException('Invalid port "' . $port . '" specified, must be a valid TCP/UDP port.', 1436717326); + } } $clonedObject = clone $this; diff --git a/typo3/sysext/core/Tests/Unit/Http/UriTest.php b/typo3/sysext/core/Tests/Unit/Http/UriTest.php index 85dd41bc0c7f..fb26993c7853 100644 --- a/typo3/sysext/core/Tests/Unit/Http/UriTest.php +++ b/typo3/sysext/core/Tests/Unit/Http/UriTest.php @@ -97,6 +97,18 @@ public function withHostReturnsNewInstanceWithProvidedHost() $this->assertEquals('https://user:pass@framework.zend.com:3001/foo?bar=baz#quz', (string) $new); } + /** + * @test + */ + public function withPortAndNullValueReturnsInstanceWithProvidedPort() + { + $uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz'); + $new = $uri->withPort(null); + $this->assertEquals( + 'https://user:pass@local.example.com/foo?bar=baz#quz', + (string) $new + ); + } /** * @return array */ @@ -130,7 +142,6 @@ public function withPortReturnsNewInstanceWithProvidedPort($port) public function invalidPortsDataProviderType() { return [ - 'null' => [null], 'false' => [false], 'string' => ['string'], 'array' => [[3000]],