diff --git a/src/Curl/Url.php b/src/Curl/Url.php index e7987cc..0defdaf 100644 --- a/src/Curl/Url.php +++ b/src/Curl/Url.php @@ -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); } diff --git a/tests/PHPCurlClass/UrlTest.php b/tests/PHPCurlClass/UrlTest.php index bd5dacd..6bee7ec 100644 --- a/tests/PHPCurlClass/UrlTest.php +++ b/tests/PHPCurlClass/UrlTest.php @@ -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); + } }