Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
feeds
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
drupal.org
feeds
Commits
72e0e009
Commit
72e0e009
authored
9 years ago
by
zniki.ru
Committed by
MegaChriz
9 years ago
Browse files
Options
Downloads
Patches
Plain Diff
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
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
libraries/http_request.inc
+32
-11
32 additions, 11 deletions
libraries/http_request.inc
tests/http_request.test
+305
-0
305 additions, 0 deletions
tests/http_request.test
with
337 additions
and
11 deletions
libraries/http_request.inc
+
32
−
11
View file @
72e0e009
...
@@ -440,27 +440,48 @@ function http_request_create_absolute_url($url, $base_url) {
...
@@ -440,27 +440,48 @@ function http_request_create_absolute_url($url, $base_url) {
// Produces variables $scheme, $host, $user, $pass, $path, $query and
// Produces variables $scheme, $host, $user, $pass, $path, $query and
// $fragment.
// $fragment.
$parsed_url
=
parse_url
(
$base_url
);
$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.
// Adding to the existing path.
$cparts
=
array
();
if
(
$url
{
0
}
==
'/'
)
{
if
(
$url
{
0
}
==
'/'
)
{
$cparts
=
array_filter
(
explode
(
"/"
,
$url
));
$cparts
=
array_filter
(
explode
(
"/"
,
$url
));
}
}
else
{
else
{
// Backtracking from the existing path.
// Backtracking from the existing path.
$cparts
=
array_merge
(
array_filter
(
explode
(
"/"
,
$path
)),
array_filter
(
explode
(
"/"
,
$url
)));
$path_cparts
=
array_filter
(
explode
(
"/"
,
$path
));
foreach
(
$cparts
as
$i
=>
$part
)
{
$url_cparts
=
array_filter
(
explode
(
"/"
,
$url
));
if
(
$part
==
'.'
)
{
$cparts
=
array_merge
(
$path_cparts
,
$url_cparts
);
$cparts
[
$i
]
=
NULL
;
}
}
if
(
$part
==
'..'
)
{
$remove_parts
=
0
;
$cparts
[
$i
-
1
]
=
NULL
;
// Start from behind.
$cparts
[
$i
]
=
NULL
;
$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
);
$path
=
implode
(
"/"
,
$cparts
);
// Build the prefix to the path.
// Build the prefix to the path.
...
...
This diff is collapsed.
Click to expand it.
tests/http_request.test
+
305
−
0
View file @
72e0e009
...
@@ -51,4 +51,309 @@ EOF;
...
@@ -51,4 +51,309 @@ EOF;
$this
->
assertEqual
(
$links
[
0
],
'http://example.com/rss.xml'
);
$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
),
)));
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment