Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround for parsing colons in partial URLs #12

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 9 additions & 1 deletion lib/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,21 @@ function normalize(string $uri) : string {
*/
function parse(string $uri) : array {

if (preg_match('/^[a-zA-Z]*:/u', $uri) === 0) {
// if no protocol is given and a colon is present,
// we need to encode it to avoid a PHP bug
$replaceRegExp = '/(?:[^[:ascii:]]|:)/u';
} else {
$replaceRegExp = '/[^[:ascii:]]/u';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indent

}

// Normally a URI must be ASCII, however. However, often it's not and
// parse_url might corrupt these strings.
//
// For that reason we take any non-ascii characters from the uri and
// uriencode them first.
$uri = preg_replace_callback(
'/[^[:ascii:]]/u',
$replaceRegExp,
function($matches) {
return rawurlencode($matches[0]);
},
Expand Down
26 changes: 26 additions & 0 deletions tests/ParseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,32 @@ function parseData() {
'query' => null,
'fragment' => null,
]
],
// See issue #9, parse_url doesn't like colons followed by numbers even
// though they are allowed since RFC 3986
[
'http://example.org/hello:12?foo=bar#test',
[
'scheme' => 'http',
'host' => 'example.org',
'path' => '/hello:12',
'port' => null,
'user' => null,
'query' => 'foo=bar',
'fragment' => 'test'
]
],
[
'/path/to/colon:34',
[
'scheme' => null,
'host' => null,
'path' => '/path/to/colon%3A34',
'port' => null,
'user' => null,
'query' => null,
'fragment' => null,
]
],

];
Expand Down