Skip to content

Commit

Permalink
Merge pull request #358 from yguedidi/replace-punycode
Browse files Browse the repository at this point in the history
Replace Punycode by Symfony polyfill
  • Loading branch information
j0k3r authored Feb 24, 2025
2 parents 3a481db + da1941b commit 75437c1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"simplepie/simplepie": "^1.7",
"smalot/pdfparser": "^1.1",
"symfony/options-resolver": "^3.4|^4.4|^5.3|^6.0|^7.0",
"true/punycode": "^2.1",
"symfony/polyfill-intl-idn": "^1.26",
"guzzlehttp/psr7": "^1.5.0|^2.0"
},
"require-dev": {
Expand Down
21 changes: 16 additions & 5 deletions src/Graby.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use Readability\Readability;
use Smalot\PdfParser\Parser as PdfParser;
use Symfony\Component\OptionsResolver\OptionsResolver;
use TrueBV\Punycode;
use Symfony\Polyfill\Intl\Idn\Idn as SymfonyIdn;

/**
* @todo add proxy
Expand All @@ -39,7 +39,6 @@ class Graby

/** @var ConfigBuilder */
private $configBuilder;
private $punycode;

private $imgNoReferrer = false;
private $prefetchedContent;
Expand Down Expand Up @@ -118,8 +117,6 @@ public function __construct($config = [], ?ClientInterface $client = null, ?Conf
$this->config['http_client'],
$this->logger
);

$this->punycode = new Punycode();
}

/**
Expand Down Expand Up @@ -495,7 +492,21 @@ private function validateUrl($url)
$uri = new Uri((string) $url);

if (preg_match('/[\x80-\xff]/', $uri->getHost())) {
$uri = $uri->withHost($this->punycode->encode($uri->getHost()));
$uriIdnSafe = \defined('INTL_IDNA_VARIANT_UTS46') ? idn_to_ascii(
$uri->getHost(),
\IDNA_DEFAULT,
\INTL_IDNA_VARIANT_UTS46
) : SymfonyIdn::idn_to_ascii(
$uri->getHost(),
SymfonyIdn::IDNA_DEFAULT,
SymfonyIdn::INTL_IDNA_VARIANT_UTS46
);

if (false === $uriIdnSafe) {
throw new \InvalidArgumentException(\sprintf('Url "%s" is not valid IDN to ascii.', $url));
}

$uri = $uri->withHost($uriIdnSafe);
}

if (\strlen($uri->getPath()) && preg_match('/[\x80-\xff]/', $uri->getPath())) {
Expand Down

0 comments on commit 75437c1

Please sign in to comment.