From 15f6d78e5a630a11d1d94026ae7db9ecac0d5b5a Mon Sep 17 00:00:00 2001 From: Marc Easen Date: Tue, 5 Jul 2016 08:40:48 +0100 Subject: [PATCH 1/6] Updated the unit tests to align to 1.7.x --- tests/HttpBuildUrlTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/HttpBuildUrlTest.php b/tests/HttpBuildUrlTest.php index 40a3f15..e4afab4 100644 --- a/tests/HttpBuildUrlTest.php +++ b/tests/HttpBuildUrlTest.php @@ -30,14 +30,14 @@ public function trailingSlashProvider() { return array( array( - 'http://example.com', + 'http://example.com/', array( 'scheme' => 'http', 'host' => 'example.com' ) ), array( - 'http://example.com', + 'http://example.com/', array( 'scheme' => 'http', 'host' => 'example.com', @@ -69,7 +69,7 @@ public function trailingSlashProvider() ) ), array( - 'http://example.com:81?a=b', + 'http://example.com:81/?a=b', array( 'scheme' => 'http', 'host' => 'example.com', @@ -207,10 +207,10 @@ public function bitmaskProvider() array('HTTP_URL_STRIP_PASS', 'http://user@www.example.com:8080/pub/index.php?a=b#files'), array('HTTP_URL_STRIP_AUTH', 'http://www.example.com:8080/pub/index.php?a=b#files'), array('HTTP_URL_STRIP_PORT', 'http://user:pass@www.example.com/pub/index.php?a=b#files'), - array('HTTP_URL_STRIP_PATH', 'http://user:pass@www.example.com:8080?a=b#files'), + array('HTTP_URL_STRIP_PATH', 'http://user:pass@www.example.com:8080/?a=b#files'), array('HTTP_URL_STRIP_QUERY', 'http://user:pass@www.example.com:8080/pub/index.php#files'), array('HTTP_URL_STRIP_FRAGMENT', 'http://user:pass@www.example.com:8080/pub/index.php?a=b'), - array('HTTP_URL_STRIP_ALL', 'http://www.example.com'), + array('HTTP_URL_STRIP_ALL', 'http://www.example.com/'), ); } } From d8665b71a5a263c77d2589b307af99379c08f935 Mon Sep 17 00:00:00 2001 From: Marc Easen Date: Tue, 5 Jul 2016 09:05:01 +0100 Subject: [PATCH 2/6] Add the slash inline with http 1.7.* --- src/http_build_url.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/http_build_url.php b/src/http_build_url.php index 0dfdbba..202b2cb 100644 --- a/src/http_build_url.php +++ b/src/http_build_url.php @@ -157,6 +157,8 @@ function http_build_url($url, $parts = array(), $flags = HTTP_URL_REPLACE, &$new if (!empty($url['path'])) { $parsed_string .= $url['path']; + } else { + $parsed_string .= '/'; } if (!empty($url['query'])) { From 000d31197b8edd7c8a0f8231f5cd59ee9bbfcd21 Mon Sep 17 00:00:00 2001 From: Marc Easen Date: Thu, 28 Apr 2016 09:44:02 +0100 Subject: [PATCH 3/6] Added support for HTTP_URL_JOIN_PATH where there is no file on the end of the URL --- tests/HttpBuildUrlTest.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/tests/HttpBuildUrlTest.php b/tests/HttpBuildUrlTest.php index e4afab4..ed56bf7 100644 --- a/tests/HttpBuildUrlTest.php +++ b/tests/HttpBuildUrlTest.php @@ -151,9 +151,9 @@ public function testJoinQuery($query, $expected) /** * @dataProvider pathProvider */ - public function testJoinPath($path, $expected) + public function testJoinPath($url, $path, $expected) { - $actual = http_build_url($this->full_url, array('path' => $path), HTTP_URL_JOIN_PATH); + $actual = http_build_url($url, array('path' => $path), HTTP_URL_JOIN_PATH); $this->assertSame($expected, $actual); } @@ -182,10 +182,14 @@ public function testBitmasks($constant, $expected) public function pathProvider() { + $url_minus_file = 'http://user:pass@www.example.com:8080/pub/?a=b#files'; return array( - array('/donuts/brownies', 'http://user:pass@www.example.com:8080/donuts/brownies?a=b#files'), - array('chicken/wings', 'http://user:pass@www.example.com:8080/pub/chicken/wings?a=b#files'), - array('sausage/bacon/', 'http://user:pass@www.example.com:8080/pub/sausage/bacon/?a=b#files') + array($this->full_url, '/donuts/brownies', 'http://user:pass@www.example.com:8080/donuts/brownies?a=b#files'), + array($this->full_url, 'chicken/wings', 'http://user:pass@www.example.com:8080/pub/chicken/wings?a=b#files'), + array($this->full_url, 'sausage/bacon/', 'http://user:pass@www.example.com:8080/pub/sausage/bacon/?a=b#files'), + array($url_minus_file, '/donuts/brownies', 'http://user:pass@www.example.com:8080/donuts/brownies?a=b#files'), + array($url_minus_file, 'chicken/wings', 'http://user:pass@www.example.com:8080/pub/chicken/wings?a=b#files'), + array($url_minus_file, 'sausage/bacon/', 'http://user:pass@www.example.com:8080/pub/sausage/bacon/?a=b#files'), ); } From 2ed293f6b92259780bb616ae081a23d68ca15eac Mon Sep 17 00:00:00 2001 From: Marc Easen Date: Thu, 28 Apr 2016 10:54:31 +0100 Subject: [PATCH 4/6] Fixed issues with rel paths in final URL --- src/http_build_url.php | 4 ++++ tests/HttpBuildUrlTest.php | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/src/http_build_url.php b/src/http_build_url.php index 202b2cb..d19ff48 100644 --- a/src/http_build_url.php +++ b/src/http_build_url.php @@ -156,6 +156,10 @@ function http_build_url($url, $parts = array(), $flags = HTTP_URL_REPLACE, &$new } if (!empty($url['path'])) { + // Clean up any /foo/../bar or /foo/./bar + for ($n=1; $n>0; $url['path'] = preg_replace(array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#'), '/', $url['path'], -1, $n)) {} + // Clean up any /../bar + $url['path'] = preg_replace(array('#/\.\./#'), '/', $url['path']); $parsed_string .= $url['path']; } else { $parsed_string .= '/'; diff --git a/tests/HttpBuildUrlTest.php b/tests/HttpBuildUrlTest.php index ed56bf7..72ac029 100644 --- a/tests/HttpBuildUrlTest.php +++ b/tests/HttpBuildUrlTest.php @@ -183,6 +183,7 @@ public function testBitmasks($constant, $expected) public function pathProvider() { $url_minus_file = 'http://user:pass@www.example.com:8080/pub/?a=b#files'; + $url_depth = 'http://user:pass@www.example.com:8080/pub1/pub2/pub3/?a=b#files'; return array( array($this->full_url, '/donuts/brownies', 'http://user:pass@www.example.com:8080/donuts/brownies?a=b#files'), array($this->full_url, 'chicken/wings', 'http://user:pass@www.example.com:8080/pub/chicken/wings?a=b#files'), @@ -190,6 +191,11 @@ public function pathProvider() array($url_minus_file, '/donuts/brownies', 'http://user:pass@www.example.com:8080/donuts/brownies?a=b#files'), array($url_minus_file, 'chicken/wings', 'http://user:pass@www.example.com:8080/pub/chicken/wings?a=b#files'), array($url_minus_file, 'sausage/bacon/', 'http://user:pass@www.example.com:8080/pub/sausage/bacon/?a=b#files'), + array($url_depth, '../donuts/brownies', 'http://user:pass@www.example.com:8080/pub1/pub2/donuts/brownies?a=b#files'), + array($url_depth, '/../donuts/brownies', 'http://user:pass@www.example.com:8080/donuts/brownies?a=b#files'), + array($url_depth, '../../donuts/brownies', 'http://user:pass@www.example.com:8080/pub1/donuts/brownies?a=b#files'), + array($url_depth, '../../../donuts/brownies', 'http://user:pass@www.example.com:8080/donuts/brownies?a=b#files'), + ); } From 25e8049f1781f42630af78b590bb8c05b6463cff Mon Sep 17 00:00:00 2001 From: Marc Easen Date: Tue, 5 Jul 2016 09:11:29 +0100 Subject: [PATCH 5/6] Updated the support of URL_JOIN_QUERY to align to PECL HTTP 1.7.x --- src/http_build_url.php | 4 ++-- tests/HttpBuildUrlTest.php | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/http_build_url.php b/src/http_build_url.php index d19ff48..ca577f6 100644 --- a/src/http_build_url.php +++ b/src/http_build_url.php @@ -103,8 +103,8 @@ function http_build_url($url, $parts = array(), $flags = HTTP_URL_REPLACE, &$new } } - if (isset($parts['query']) && ($flags & HTTP_URL_JOIN_QUERY)) { - if (isset($url['query'])) { + if (isset($parts['query'])) { + if ($flags & HTTP_URL_JOIN_QUERY && isset($url['query'])) { parse_str($url['query'], $url_query); parse_str($parts['query'], $parts_query); diff --git a/tests/HttpBuildUrlTest.php b/tests/HttpBuildUrlTest.php index 72ac029..93a8d52 100644 --- a/tests/HttpBuildUrlTest.php +++ b/tests/HttpBuildUrlTest.php @@ -26,6 +26,23 @@ public function testExampleOne() $this->assertSame($expected, $actual); } + public function testExampleTwo() + { + $expected = 'ftp://ftp.example.com/pub/files/current/?a=c'; + $actual = http_build_url( + "http://user@www.example.com/pub/index.php#files", + array( + "scheme" => "ftp", + "host" => "ftp.example.com", + "path" => "files/current/", + "query" => "a=c" + ), + HTTP_URL_STRIP_AUTH | HTTP_URL_JOIN_PATH | HTTP_URL_STRIP_FRAGMENT + ); + + $this->assertSame($expected, $actual); + } + public function trailingSlashProvider() { return array( From ab7a5698f09e59f9667f92e487927ea6b9b81992 Mon Sep 17 00:00:00 2001 From: Marc Easen Date: Tue, 5 Jul 2016 12:08:34 +0100 Subject: [PATCH 6/6] Tighten up the rtrim in the JOIN_PATH --- src/http_build_url.php | 2 +- tests/HttpBuildUrlTest.php | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/http_build_url.php b/src/http_build_url.php index ca577f6..9778887 100644 --- a/src/http_build_url.php +++ b/src/http_build_url.php @@ -95,7 +95,7 @@ function http_build_url($url, $parts = array(), $flags = HTTP_URL_REPLACE, &$new // Workaround for trailing slashes $url['path'] .= 'a'; $url['path'] = rtrim( - str_replace(basename($url['path']), '', $url['path']), + preg_replace('#' . basename($url['path']) . '$#', '', $url['path']), '/' ) . '/' . ltrim($parts['path'], '/'); } else { diff --git a/tests/HttpBuildUrlTest.php b/tests/HttpBuildUrlTest.php index 93a8d52..8646531 100644 --- a/tests/HttpBuildUrlTest.php +++ b/tests/HttpBuildUrlTest.php @@ -200,7 +200,7 @@ public function testBitmasks($constant, $expected) public function pathProvider() { $url_minus_file = 'http://user:pass@www.example.com:8080/pub/?a=b#files'; - $url_depth = 'http://user:pass@www.example.com:8080/pub1/pub2/pub3/?a=b#files'; + $url_depth = 'http://user:pass@www.example.com:8080/path1/path2/path3/?a=b#files'; return array( array($this->full_url, '/donuts/brownies', 'http://user:pass@www.example.com:8080/donuts/brownies?a=b#files'), array($this->full_url, 'chicken/wings', 'http://user:pass@www.example.com:8080/pub/chicken/wings?a=b#files'), @@ -208,10 +208,9 @@ public function pathProvider() array($url_minus_file, '/donuts/brownies', 'http://user:pass@www.example.com:8080/donuts/brownies?a=b#files'), array($url_minus_file, 'chicken/wings', 'http://user:pass@www.example.com:8080/pub/chicken/wings?a=b#files'), array($url_minus_file, 'sausage/bacon/', 'http://user:pass@www.example.com:8080/pub/sausage/bacon/?a=b#files'), - array($url_depth, '../donuts/brownies', 'http://user:pass@www.example.com:8080/pub1/pub2/donuts/brownies?a=b#files'), + array($url_depth, '../donuts/brownies', 'http://user:pass@www.example.com:8080/path1/path2/donuts/brownies?a=b#files'), array($url_depth, '/../donuts/brownies', 'http://user:pass@www.example.com:8080/donuts/brownies?a=b#files'), - array($url_depth, '../../donuts/brownies', 'http://user:pass@www.example.com:8080/pub1/donuts/brownies?a=b#files'), - array($url_depth, '../../../donuts/brownies', 'http://user:pass@www.example.com:8080/donuts/brownies?a=b#files'), + array($url_depth, '../../donuts/brownies', 'http://user:pass@www.example.com:8080/path1/donuts/brownies?a=b#files'), ); }