Skip to content
Snippets Groups Projects
Commit 72e0e009 authored by zniki.ru's avatar zniki.ru Committed by MegaChriz
Browse files

Issue #2584443 by zniki.ru: http_request_create_absolute_url() ignore last path in $base_url

parent 3a2b4ded
No related branches found
No related tags found
No related merge requests found
......@@ -440,27 +440,48 @@ function http_request_create_absolute_url($url, $base_url) {
// Produces variables $scheme, $host, $user, $pass, $path, $query and
// $fragment.
$parsed_url = parse_url($base_url);
if ($parsed_url === FALSE) {
// Invalid $base_url.
return FALSE;
}
$path = dirname($parsed_url['path']);
$path = isset($parsed_url['path']) ? $parsed_url['path'] : '';
if (strlen($path) > 0 && substr($path, -1) != '/') {
// Path ends not with '/', so remove all before previous '/'.
$path = dirname($path);
}
// Adding to the existing path.
$cparts = array();
if ($url{0} == '/') {
$cparts = array_filter(explode("/", $url));
}
else {
// Backtracking from the existing path.
$cparts = array_merge(array_filter(explode("/", $path)), array_filter(explode("/", $url)));
foreach ($cparts as $i => $part) {
if ($part == '.') {
$cparts[$i] = NULL;
}
if ($part == '..') {
$cparts[$i - 1] = NULL;
$cparts[$i] = NULL;
}
$path_cparts = array_filter(explode("/", $path));
$url_cparts = array_filter(explode("/", $url));
$cparts = array_merge($path_cparts, $url_cparts);
}
$remove_parts = 0;
// Start from behind.
$reverse_cparts = array_reverse($cparts);
foreach ($reverse_cparts as $i => &$part) {
if ($part == '.') {
$part = NULL;
}
elseif ($part == '..') {
$part = NULL;
$remove_parts++;
}
elseif ($remove_parts > 0) {
// If the current part isn't "..", and we had ".." before, then delete
// the part.
$part = NULL;
$remove_parts--;
}
$cparts = array_filter($cparts);
}
$cparts = array_filter(array_reverse($reverse_cparts));
$path = implode("/", $cparts);
// Build the prefix to the path.
......
......@@ -51,4 +51,309 @@ EOF;
$this->assertEqual($links[0], 'http://example.com/rss.xml');
}
/**
* Tests http_request_create_absolute_url().
*/
public function testHTTPRequestCreateAbsoluteUrl() {
$test_urls = array(
// Rels that do not start with "/".
array(
'rel' => 'h',
'base' => 'http://www',
'expected' => 'http://www/h',
),
array(
'rel' => 'h',
'base' => 'http://www/',
'expected' => 'http://www/h',
),
array(
'rel' => 'h',
'base' => 'http://www/?c;d=e#f',
'expected' => 'http://www/h',
),
array(
'rel' => 'h',
'base' => 'http://www/a/b',
'expected' => 'http://www/a/h',
),
array(
'rel' => 'h/j',
'base' => 'http://www',
'expected' => 'http://www/h/j',
),
array(
'rel' => 'h/j',
'base' => 'http://www/',
'expected' => 'http://www/h/j',
),
array(
'rel' => 'h/j',
'base' => 'http://www/?c;d=e#f',
'expected' => 'http://www/h/j',
),
array(
'rel' => 'h/j',
'base' => 'http://www/a/b',
'expected' => 'http://www/a/h/j',
),
array(
'rel' => 'h/j',
'base' => 'http://www/a/b/',
'expected' => 'http://www/a/b/h/j',
),
// Rels that start with "/".
array(
'rel' => '/h',
'base' => 'http://www',
'expected' => 'http://www/h',
),
array(
'rel' => '/h',
'base' => 'http://www/',
'expected' => 'http://www/h',
),
array(
'rel' => '/h',
'base' => 'http://www/?c;d=e#f',
'expected' => 'http://www/h',
),
array(
'rel' => '/h',
'base' => 'http://www/a/b',
'expected' => 'http://www/h',
),
array(
'rel' => '/h/j',
'base' => 'http://www',
'expected' => 'http://www/h/j',
),
array(
'rel' => '/h/j',
'base' => 'http://www/',
'expected' => 'http://www/h/j',
),
array(
'rel' => '/h/j',
'base' => 'http://www/?c;d=e#f',
'expected' => 'http://www/h/j',
),
array(
'rel' => '/h/j',
'base' => 'http://www/a/b',
'expected' => 'http://www/h/j',
),
array(
'rel' => '/h/j',
'base' => 'http://www/a/b/',
'expected' => 'http://www/h/j',
),
// Rels that contain ".".
array(
'rel' => './h',
'base' => 'http://www',
'expected' => 'http://www/h',
),
array(
'rel' => './h',
'base' => 'http://www/',
'expected' => 'http://www/h',
),
array(
'rel' => './h',
'base' => 'http://www/?c;d=e#f',
'expected' => 'http://www/h',
),
array(
'rel' => './h',
'base' => 'http://www/a/b',
'expected' => 'http://www/a/h',
),
array(
'rel' => './h/j',
'base' => 'http://www',
'expected' => 'http://www/h/j',
),
array(
'rel' => './h/j',
'base' => 'http://www/',
'expected' => 'http://www/h/j',
),
array(
'rel' => './h/j',
'base' => 'http://www/?c;d=e#f',
'expected' => 'http://www/h/j',
),
array(
'rel' => './h/j',
'base' => 'http://www/a/b',
'expected' => 'http://www/a/h/j',
),
array(
'rel' => './h/j',
'base' => 'http://www/a/b/',
'expected' => 'http://www/a/b/h/j',
),
array(
'rel' => 'h/./j',
'base' => 'http://www',
'expected' => 'http://www/h/j',
),
array(
'rel' => 'h/./j',
'base' => 'http://www/',
'expected' => 'http://www/h/j',
),
array(
'rel' => 'h/./j',
'base' => 'http://www/?c;d=e#f',
'expected' => 'http://www/h/j',
),
array(
'rel' => 'h/./j',
'base' => 'http://www/a/b',
'expected' => 'http://www/a/h/j',
),
array(
'rel' => 'h/./j',
'base' => 'http://www/a/b/',
'expected' => 'http://www/a/b/h/j',
),
array(
'rel' => '/h/./j',
'base' => 'http://www',
'expected' => 'http://www/h/j',
),
array(
'rel' => '/h/./j',
'base' => 'http://www/',
'expected' => 'http://www/h/j',
),
array(
'rel' => '/h/./j',
'base' => 'http://www/?c;d=e#f',
'expected' => 'http://www/h/j',
),
array(
'rel' => '/h/./j',
'base' => 'http://www/a/b',
'expected' => 'http://www/h/j',
),
array(
'rel' => '/h/./j',
'base' => 'http://www/a/b/',
'expected' => 'http://www/h/j',
),
// Rels that starts with "../".
array(
'rel' => '../h/j',
'base' => 'http://www',
'expected' => 'http://www/h/j',
),
array(
'rel' => '../h/j',
'base' => 'http://www/',
'expected' => 'http://www/h/j',
),
array(
'rel' => '../h/j',
'base' => 'http://www/?c;d=e#f',
'expected' => 'http://www/h/j',
),
array(
'rel' => '../h/j',
'base' => 'http://www/a/b',
'expected' => 'http://www/h/j',
),
array(
'rel' => '../h/j',
'base' => 'http://www/a/b/',
'expected' => 'http://www/a/h/j',
),
array(
'rel' => '../h/j',
'base' => 'http://www/a/b/c/',
'expected' => 'http://www/a/b/h/j',
),
// Rels that start with "../../".
array(
'rel' => '../../h/j',
'base' => 'http://www',
'expected' => 'http://www/h/j',
),
array(
'rel' => '../../h/j',
'base' => 'http://www/',
'expected' => 'http://www/h/j',
),
array(
'rel' => '../../h/j',
'base' => 'http://www/?c;d=e#f',
'expected' => 'http://www/h/j',
),
array(
'rel' => '../../h/j',
'base' => 'http://www/a/b',
'expected' => 'http://www/h/j',
),
array(
'rel' => '../../h/j',
'base' => 'http://www/a/b/',
'expected' => 'http://www/h/j',
),
array(
'rel' => '../../h/j',
'base' => 'http://www/a/b/c/',
'expected' => 'http://www/a/h/j',
),
array(
'rel' => '../../h/j',
'base' => 'http://www/a/b/c/d',
'expected' => 'http://www/a/h/j',
),
// Crazy rels.
array(
'rel' => 'h/../../j/./k',
'base' => 'http://www/a/b/c/',
'expected' => 'http://www/a/b/j/k',
),
array(
'rel' => 'h/../../j/./k',
'base' => 'http://www/a/b/c/d',
'expected' => 'http://www/a/b/j/k',
),
array(
'rel' => '../../../',
'base' => 'http://www/a/b/c/',
'expected' => 'http://www/',
),
array(
'rel' => 'h/j/k/../../',
'base' => 'http://www/a/b/c/',
'expected' => 'http://www/a/b/c/h',
),
);
foreach ($test_urls as $test_url) {
$result_url = http_request_create_absolute_url($test_url['rel'], $test_url['base']);
$this->assertEqual($test_url['expected'], $result_url, format_string('Creating an absolute URL from base @base and rel @rel resulted into @expected (actual: @actual).', array(
'@actual' => var_export($result_url, TRUE),
'@expected' => var_export($test_url['expected'], TRUE),
'@rel' => var_export($test_url['rel'], TRUE),
'@base' => var_export($test_url['base'], TRUE),
)));
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment