Skip to content
This repository was archived by the owner on Jul 25, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Punycode.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,27 @@ public function __construct($encoding = 'UTF-8')
public function encode($input)
{
$input = mb_strtolower($input, $this->encoding);

$isFQDN = false;
if (substr($input, -1) === '.') {
$isFQDN = true;
$input = substr($input, 0, -1);
}
$parts = explode('.', $input);

foreach ($parts as &$part) {
$length = strlen($part);
if ($length < 1) {
throw new LabelOutOfBoundsException(sprintf('The length of any one label is limited to between 1 and 63 octets, but %s given.', $length));
}
$part = $this->encodePart($part);
}

$output = implode('.', $parts);
if ($isFQDN) {
$output .= '.';
}

$length = strlen($output);
if ($length > 255) {
throw new DomainOutOfBoundsException(sprintf('A full domain name is limited to 255 octets (including the separators), %s given.', $length));
Expand Down Expand Up @@ -176,7 +188,14 @@ protected function encodePart($input)
public function decode($input)
{
$input = strtolower($input);

$isFQDN = false;
if (substr($input, -1) === '.') {
$isFQDN = true;
$input = substr($input, 0, -1);
}
$parts = explode('.', $input);

foreach ($parts as &$part) {
$length = strlen($part);
if ($length > 63 || $length < 1) {
Expand All @@ -190,6 +209,9 @@ public function decode($input)
$part = $this->decodePart($part);
}
$output = implode('.', $parts);
if ($isFQDN) {
$output .= '.';
}
$length = strlen($output);
if ($length > 255) {
throw new DomainOutOfBoundsException(sprintf('A full domain name is limited to 255 octets (including the separators), %s given.', $length));
Expand Down
15 changes: 15 additions & 0 deletions tests/PunycodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ public function domainNamesProvider()
'άέήίΰαβγδεζηθικλμνξοπρσστυφχ.com',
'xn--hxacdefghijklmnopqrstuvw0caz0a1a2a.com'
),
array(
'你好.世界.',
'xn--6qq79v.xn--rhqv96g.'
),
);
}

Expand Down Expand Up @@ -201,6 +205,12 @@ public function invalidUtf8DomainNamesProvider()
'\TrueBV\Exception\LabelOutOfBoundsException',
'The length of any one label is limited to between 1 and 63 octets, but 0 given.',
),
array(
'你好.世界..',
'\TrueBV\Exception\LabelOutOfBoundsException',
'The length of any one label is limited to between 1 and 63 octets, but 0 given.',
),


);
}
Expand Down Expand Up @@ -253,6 +263,11 @@ public function invalidAsciiDomainNameProvider()
'\TrueBV\Exception\DomainOutOfBoundsException',
'A full domain name is limited to 255 octets (including the separators), 256 given.',
),
array(
'xn--6qq79v.xn--rhqv96g..',
'\TrueBV\Exception\LabelOutOfBoundsException',
'The length of any one label is limited to between 1 and 63 octets, but 0 given.',
),
);
}
}