Fix #505: Allow requests with Cyrillic characters in url

This commit is contained in:
Zach Borboa
2018-04-20 01:03:05 -07:00
parent 75597b28c4
commit cfad5c4d3c
2 changed files with 36 additions and 0 deletions
+27
View File
@@ -150,6 +150,33 @@ class Url
*/
private function parseUrl($url)
{
// ALPHA = A-Z / a-z
$alpha = 'A-Za-z';
// DIGIT = 0-9
$digit = '0-9';
// unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
$unreserved = $alpha . $digit . preg_quote('-._~');
// sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
// / "*" / "+" / "," / ";" / "="
$sub_delims = preg_quote('!$&\'()*+,;=');
// HEXDIG = DIGIT / "A" / "B" / "C" / "D" / "E" / "F"
$hexdig = $digit . 'A-F';
// "The uppercase hexadecimal digits 'A' through 'F' are equivalent to
// the lowercase digits 'a' through 'f', respectively."
$hexdig .= 'a-f';
$pattern = '/(?:[^' . $unreserved . $sub_delims . preg_quote(':@%/?', '/') . ']++|%(?![' . $hexdig . ']{2}))/';
$url = preg_replace_callback(
$pattern,
function ($matches) {
return rawurlencode($matches[0]);
},
$url
);
return parse_url($url);
}
+9
View File
@@ -108,4 +108,13 @@ class UrlTest extends \PHPUnit\Framework\TestCase
$this->assertEquals($test['expected'], $actual_path);
}
}
public function testCyrillicChars()
{
$path_part = 'Банан-комнатный-саженцы-банана';
$original_url = 'https://www.example.com/path/' . $path_part . '/page.html';
$expected_url = 'https://www.example.com/path/' . rawurlencode($path_part) . '/page.html';
$url = new Url($original_url);
$this->assertEquals($expected_url, $url);
}
}