Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/http_build_url.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,16 @@ 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 {
$url['path'] = $parts['path'];
}
}

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);

Expand Down Expand Up @@ -156,7 +156,13 @@ 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 .= '/';
}

if (!empty($url['query'])) {
Expand Down
46 changes: 36 additions & 10 deletions tests/HttpBuildUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,35 @@ 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(
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',
Expand Down Expand Up @@ -69,7 +86,7 @@ public function trailingSlashProvider()
)
),
array(
'http://example.com:81?a=b',
'http://example.com:81/?a=b',
array(
'scheme' => 'http',
'host' => 'example.com',
Expand Down Expand Up @@ -151,9 +168,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);
}
Expand Down Expand Up @@ -182,10 +199,19 @@ 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/path1/path2/path3/?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'),
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/path1/donuts/brownies?a=b#files'),

);
}

Expand All @@ -207,10 +233,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/'),
);
}
}