Skip to content

Commit d806917

Browse files
committed
ResolvedDomain should handle domain with root label
1 parent f6581fd commit d806917

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

src/ResolvedDomain.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,10 @@ public function withSuffix(DomainNameProvider|Host|Stringable|string|int|null $s
199199
$suffix = Suffix::fromUnknown($suffix);
200200
}
201201

202-
$newDomain = $this->domain->withoutRootLabel()->slice(count($this->suffix))->append($suffix);
202+
$domain = $this->domain->withoutRootLabel()->slice(count($this->suffix))->append($suffix);
203203

204204
return new self(
205-
$this->domain->isAbsolute() ? $newDomain->withRootLabel() : $newDomain,
205+
$this->domain->isAbsolute() ? $domain->withRootLabel() : $domain,
206206
$suffix->normalize($this->domain)
207207
);
208208
}
@@ -217,15 +217,20 @@ public function withSubDomain(DomainNameProvider|Host|Stringable|string|int|null
217217
}
218218

219219
$subDomain = RegisteredName::fromIDNA2008($subDomain);
220-
if ('' === $subDomain->withoutRootLabel()->value()) {
221-
throw SyntaxError::dueToMalformedValue($subDomain->toString());
220+
if ($subDomain->isAbsolute()) {
221+
$subDomain = $subDomain->withoutRootLabel();
222+
if (null === $subDomain->value()) {
223+
throw SyntaxError::dueToMalformedValue($subDomain->withRootLabel()->toString());
224+
}
222225
}
223226

224227
if ($this->subDomain->value() === $subDomain->value()) {
225228
return $this;
226229
}
227230

228-
return new self($this->registrableDomain->prepend($subDomain), $this->suffix);
231+
$domain = $this->registrableDomain->prepend($subDomain);
232+
233+
return new self($this->domain->isAbsolute() ? $domain->withRootLabel() : $domain, $this->suffix);
229234
}
230235

231236
/**
@@ -238,6 +243,13 @@ public function withSecondLevelDomain(DomainNameProvider|Host|Stringable|string|
238243
}
239244

240245
$label = RegisteredName::fromIDNA2008($label);
246+
if ($label->isAbsolute()) {
247+
if (2 !== count($label)) {
248+
throw UnableToResolveDomain::dueToInvalidSecondLevelDomain($label);
249+
}
250+
$label = $label->withoutRootLabel();
251+
}
252+
241253
if (1 !== count($label)) {
242254
throw UnableToResolveDomain::dueToInvalidSecondLevelDomain($label);
243255
}

src/ResolvedDomainTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,13 @@ public static function withSldWorksProvider(): iterable
533533
'expectedSld' => 'www',
534534
'expectedHost' => 'www.com',
535535
],
536+
[
537+
'host' => 'example.com',
538+
'publicSuffix' => 'com',
539+
'sld' => 'www.',
540+
'expectedSld' => 'www',
541+
'expectedHost' => 'www.com',
542+
],
536543
[
537544
'host' => 'www.example.com',
538545
'publicSuffix' => 'com',
@@ -547,6 +554,13 @@ public static function withSldWorksProvider(): iterable
547554
'expectedSld' => 'hamburger',
548555
'expectedHost' => 'www.hamburger.co.uk',
549556
],
557+
[
558+
'host' => 'www.bbc.co.uk',
559+
'publicSuffix' => 'co.uk',
560+
'sld' => 'hamburger.',
561+
'expectedSld' => 'hamburger',
562+
'expectedHost' => 'www.hamburger.co.uk',
563+
],
550564
];
551565
}
552566

@@ -557,6 +571,20 @@ public function testItCanNotAppendAnEmptySLD(): void
557571
ResolvedDomain::fromICANN('private.ulb.ac.be', 2)->withSecondLevelDomain(null);
558572
}
559573

574+
public function testItCanNotAppendAnEmptyRootLabelSLD(): void
575+
{
576+
$this->expectException(SyntaxError::class);
577+
578+
ResolvedDomain::fromICANN('private.ulb.ac.be', 2)->withSecondLevelDomain('.');
579+
}
580+
581+
public function testItCanNotAppendAnInvalidRootLabelSLD(): void
582+
{
583+
$this->expectException(UnableToResolveDomain::class);
584+
585+
ResolvedDomain::fromICANN('private.ulb.ac.be', 2)->withSecondLevelDomain('toto.foo.');
586+
}
587+
560588
public function testItCanNotAppendASLDToAResolvedDomainWithoutSuffix(): void
561589
{
562590
$this->expectException(UnableToResolveDomain::class);
@@ -571,6 +599,13 @@ public function testItCanNotAppendAnInvalidSLDToAResolvedDomain(): void
571599
ResolvedDomain::fromIANA('private.ulb.ac.be')->withSecondLevelDomain('foo.bar');
572600
}
573601

602+
public function testItCanNotAppendAnInvalidSubDomainToAResolvedDomain(): void
603+
{
604+
$this->expectException(SyntaxError::class);
605+
606+
ResolvedDomain::fromIANA('private.ulb.ac.be')->withSubDomain('');
607+
}
608+
574609
public function testItReturnsTheInstanceWhenTheSLDIsEqual(): void
575610
{
576611
$domain = ResolvedDomain::fromICANN('private.ulb.ac.be', 2);

src/Suffix.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ private static function setDomainName(int|DomainNameProvider|Host|string|Stringa
9090
$domain = RegisteredName::fromIDNA2008($domain);
9191
}
9292

93-
if ('' === $domain->label(0)) {
93+
if ($domain->isAbsolute()) {
9494
throw SyntaxError::dueToInvalidSuffix($domain);
9595
}
9696

0 commit comments

Comments
 (0)