From 765fcd091c5b1aec0e6678c4b9325ade8bc60a86 Mon Sep 17 00:00:00 2001 From: Ignace Nyamagana Butera Date: Fri, 25 May 2018 14:12:17 +0200 Subject: [PATCH 1/2] make string comparison more strict --- src/Uri.php | 42 +++++++++++++++++++----------------------- test/UriTest.php | 8 ++++++++ 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/Uri.php b/src/Uri.php index 8900e374..b66c0f49 100644 --- a/src/Uri.php +++ b/src/Uri.php @@ -98,7 +98,7 @@ public function __construct($uri = '') )); } - if (! empty($uri)) { + if ('' !== $uri) { $this->parseUri($uri); } } @@ -147,12 +147,12 @@ public function getScheme() */ public function getAuthority() { - if (empty($this->host)) { + if ('' === $this->host) { return ''; } $authority = $this->host; - if (! empty($this->userInfo)) { + if ('' !== $this->userInfo) { $authority = $this->userInfo . '@' . $authority; } @@ -476,27 +476,26 @@ private static function createUriString($scheme, $authority, $path, $query, $fra { $uri = ''; - if (! empty($scheme)) { + if ('' !== $scheme) { $uri .= sprintf('%s:', $scheme); } - if (! empty($authority)) { + if ('' !== $authority) { $uri .= '//' . $authority; } - if ($path) { - if (empty($path) || '/' !== substr($path, 0, 1)) { - $path = '/' . $path; - } - - $uri .= $path; + if ('' !== $path && '/' !== substr($path, 0, 1)) { + $path = '/' . $path; } - if ($query) { + $uri .= $path; + + + if ('' !== $query) { $uri .= sprintf('?%s', $query); } - if ($fragment) { + if ('' !== $fragment) { $uri .= sprintf('#%s', $fragment); } @@ -513,14 +512,11 @@ private static function createUriString($scheme, $authority, $path, $query, $fra */ private function isNonStandardPort($scheme, $host, $port) { - if (! $scheme) { - if ($host && ! $port) { - return false; - } - return true; + if ('' === $scheme) { + return '' === $host || null !== $port; } - if (! $host || ! $port) { + if ('' === $host || null === $port) { return false; } @@ -539,7 +535,7 @@ private function filterScheme($scheme) $scheme = strtolower($scheme); $scheme = preg_replace('#:(//)?$#', '', $scheme); - if (empty($scheme)) { + if ('' === $scheme) { return ''; } @@ -585,7 +581,7 @@ private function filterPath($path) $path ); - if (empty($path)) { + if ('' === $path) { // No path return $path; } @@ -609,7 +605,7 @@ private function filterPath($path) */ private function filterQuery($query) { - if (! empty($query) && strpos($query, '?') === 0) { + if ('' !== $query && strpos($query, '?') === 0) { $query = substr($query, 1); } @@ -653,7 +649,7 @@ private function splitQueryValue($value) */ private function filterFragment($fragment) { - if (! empty($fragment) && strpos($fragment, '#') === 0) { + if ('' !== $fragment && strpos($fragment, '#') === 0) { $fragment = '%23' . substr($fragment, 1); } diff --git a/test/UriTest.php b/test/UriTest.php index 1decccff..e49e203e 100644 --- a/test/UriTest.php +++ b/test/UriTest.php @@ -689,4 +689,12 @@ public function testHostIsLowercaseWhenIsSetViwWithHost() $uri = (new Uri())->withHost('NEW-HOST.COM'); $this->assertSame('new-host.com', $uri->getHost()); } + + + public function testUriDistinguishZeroFromEmptyString() + { + $expected = 'https://0:0@0:1/0?0#0'; + $uri = new Uri($expected); + $this->assertSame($expected, (string) $uri); + } } From 1eac731cdf4679d7d4b9d6706449f5e2da5ed92c Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Tue, 29 May 2018 11:53:02 -0500 Subject: [PATCH 2/2] Adds CHANGELOG entry for #301 --- CHANGELOG.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c79a6d1..c0733048 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ All notable changes to this project will be documented in this file, in reverse chronological order by release. -## 1.7.2 - TBD +## 1.7.2 - 2018-05-29 ### Added @@ -22,7 +22,8 @@ All notable changes to this project will be documented in this file, in reverse ### Fixed -- Nothing. +- [#301](https://github.com/zendframework/zend-diactoros/pull/301) adds stricter comparisons within the `uri` class to ensure non-empty + values are not treated as empty. ## 1.7.1 - 2018-02-26