Skip to content

Commit 9fb7c9b

Browse files
committed
urls with utf-8 characters #189
1 parent 5001d7e commit 9fb7c9b

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

demo/index.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ function getUrl()
4747
$url .= "&{$name}={$value}";
4848
}
4949

50-
if (!filter_var($url, FILTER_VALIDATE_URL)) {
51-
return 'http://doNotTryToXSS.invalid';
52-
}
53-
5450
return $url;
5551
}
5652

src/Http/Url.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ protected function buildUrl()
504504
*/
505505
protected function parseUrl($url)
506506
{
507-
$this->info = parse_url($url);
507+
$this->info = self::parse($url);
508508

509509
if (isset($this->info['path'])) {
510510
$this->setPath($this->info['path']);
@@ -610,12 +610,43 @@ private function setPath($path)
610610
}
611611
}
612612

613-
private function getSuffixes()
613+
private static function getSuffixes()
614614
{
615615
if (self::$public_suffix_list === null) {
616616
self::$public_suffix_list = include __DIR__.'/../resources/public_suffix_list.php';
617617
}
618618

619619
return self::$public_suffix_list;
620620
}
621+
622+
/**
623+
* UTF-8 compatible parse_url
624+
* http://php.net/manual/en/function.parse-url.php#114817
625+
*
626+
* @param string $url
627+
*
628+
* @return string
629+
*/
630+
private static function parse($url)
631+
{
632+
$enc_url = preg_replace_callback(
633+
'%[^:/@?&=#]+%usD',
634+
function ($matches) {
635+
return urlencode($matches[0]);
636+
},
637+
$url
638+
);
639+
640+
$parts = parse_url($enc_url);
641+
642+
if ($parts === false) {
643+
throw new \InvalidArgumentException('Malformed URL: ' . $url);
644+
}
645+
646+
foreach($parts as $name => $value) {
647+
$parts[$name] = urldecode($value);
648+
}
649+
650+
return $parts;
651+
}
621652
}

src/Utils.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public static function toUtf8($content, $charset)
211211
}
212212

213213
if (function_exists('iconv')) {
214-
return iconv($charset, 'UTF-8', $content);
214+
return iconv($charset, 'UTF-8//TRANSLIT//IGNORE', $content);
215215
}
216216

217217
return $content;

0 commit comments

Comments
 (0)