44
55namespace Rowbot \URL \Component \Host ;
66
7+ use Rowbot \Idna \Idna ;
78use Rowbot \URL \Component \Host \Exception \IDNATransformException ;
89use Rowbot \URL \Component \Host \Exception \InvalidHostException ;
910use Rowbot \URL \Component \Host \Exception \InvalidIPv4AddressException ;
@@ -28,27 +29,32 @@ class HostParser
2829 private const FORBIDDEN_OPAQUE_HOST_CODEPOINTS = '\x00\x09\x0A\x0D\x20#\/:<>?@[ \\\\\]^ ' ;
2930 private const FORBIDDEN_HOST_CODEPOINTS = self ::FORBIDDEN_OPAQUE_HOST_CODEPOINTS . '% ' ;
3031
32+ /**
33+ * @see https://url.spec.whatwg.org/#concept-domain-to-ascii
34+ */
3135 private function domainToAscii (string $ domain , bool $ beStrict = false ): StringHost
3236 {
33- $ flags = IDNA ::CHECK_BIDI | IDNA ::CHECK_JOINERS | IDNA ::NONTRANSITIONAL_PROCESSING ;
34-
35- if ($ beStrict ) {
36- $ flags |= IDNA ::USE_STD3_ASCII_RULES | IDNA ::VERIFY_DNS_LENGTH ;
37+ $ result = Idna::toAscii ($ domain , [
38+ 'CheckHyphens ' => false ,
39+ 'CheckBidi ' => true ,
40+ 'CheckJoiners ' => true ,
41+ 'UseSTD3ASCIIRules ' => $ beStrict ,
42+ 'Transitional_Processing ' => false ,
43+ 'VerifyDnsLength ' => $ beStrict ,
44+ ]);
45+ $ convertedDomain = $ result ->getDomain ();
46+
47+ if ($ convertedDomain === '' ) {
48+ // Validation error.
49+ throw new IDNATransformException ();
3750 }
3851
39- try {
40- $ result = IDNA ::toAscii ($ domain , $ flags );
41-
42- if ($ result === '' ) {
43- // Validation error.
44- throw new IDNATransformException ();
45- }
46-
47- return new StringHost ($ result );
48- } catch (IDNATransformException $ e ) {
52+ if ($ result ->hasErrors ()) {
4953 // Validation error.
50- throw $ e ;
54+ throw new IDNATransformException () ;
5155 }
56+
57+ return new StringHost ($ convertedDomain );
5258 }
5359
5460 /**
0 commit comments