Skip to content

Commit 7a5e73e

Browse files
committed
Merge pull request #23 from jeremykendall/fix/22
Adds the ability to properly parse single label domains (e.g., localhost, foo, etc.)
2 parents 7a6c3c9 + 0706423 commit 7a5e73e

File tree

9 files changed

+74
-21
lines changed

9 files changed

+74
-21
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
tags
2-
tests/log
32
data/*.txt
43
composer.lock
54
vendor
65
*.swp
76
docs
87
*~
8+
build

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ Array
190190
[query] =>
191191
[fragment] =>
192192
)
193+
Host: http://www.waxaudio.com.au/
193194
```
194195

195196
### Example Script ###

bin/parse

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ try {
2424
$parser = new Pdp\Parser($manager->getList());
2525
$url = $parser->parseUrl($domain);
2626
print_r($url->toArray());
27+
echo sprintf('Host: %s', $url) . PHP_EOL;
2728
} catch (\Exception $e) {
2829
die($e->getMessage() . PHP_EOL);
2930
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
}
1111
],
1212
"bin": [
13-
"bin/parser",
13+
"bin/parse",
1414
"bin/pdp-psl"
1515
],
1616
"keywords": [

library/Pdp/Parser.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,23 @@ public function parseUrl($url)
9191
*/
9292
public function parseHost($host)
9393
{
94+
$subdomain = null;
95+
$registerableDomain = null;
96+
$publicSuffix = null;
97+
98+
// Fixes #22: Single label domains are set as Host::$host and all other
99+
// properties are null.
100+
if (strpos($host, '.') !== false) {
101+
$subdomain = $this->getSubdomain($host);
102+
$registerableDomain = $this->getRegisterableDomain($host);
103+
$publicSuffix = $this->getPublicSuffix($host);
104+
}
105+
94106
return new Host(
95-
$this->getSubdomain($host),
96-
$this->getRegisterableDomain($host),
97-
$this->getPublicSuffix($host)
107+
$subdomain,
108+
$registerableDomain,
109+
$publicSuffix,
110+
$host
98111
);
99112
}
100113

@@ -110,6 +123,13 @@ public function getPublicSuffix($host)
110123
return null;
111124
}
112125

126+
// Fixes #22: If a single label domain makes it this far (e.g.,
127+
// localhost, foo, etc.), this stops it from incorrectly being set as
128+
// the public suffix.
129+
if (strpos($host, '.') === false) {
130+
return null;
131+
}
132+
113133
$host = strtolower($host);
114134
$parts = array_reverse(explode('.', $host));
115135
$publicSuffix = array();

library/Pdp/Uri/Url/Host.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,25 @@ class Host
3030
*/
3131
private $publicSuffix;
3232

33+
/**
34+
* @var string host Entire host part
35+
*/
36+
private $host;
37+
3338
/**
3439
* Public constructor
3540
*
3641
* @param string|null $subdomain Subdomain portion of host
37-
* @param string $registerableDomain Registerable domain portion of host
38-
* @param string $publicSuffix Public suffix portion of host
42+
* @param string|null $registerableDomain Registerable domain portion of host
43+
* @param string|null $publicSuffix Public suffix portion of host
44+
* @param string $host OPTIONAL Entire host part
3945
*/
40-
public function __construct($subdomain, $registerableDomain, $publicSuffix)
46+
public function __construct($subdomain, $registerableDomain, $publicSuffix, $host = null)
4147
{
4248
$this->subdomain = $subdomain;
4349
$this->registerableDomain = $registerableDomain;
4450
$this->publicSuffix = $publicSuffix;
51+
$this->host = $host;
4552
}
4653

4754
/**
@@ -51,13 +58,17 @@ public function __construct($subdomain, $registerableDomain, $publicSuffix)
5158
*/
5259
public function __toString()
5360
{
54-
$host = $this->registerableDomain;
55-
56-
if ($this->subdomain) {
57-
$host = $this->subdomain . '.' . $host;
61+
if ($this->host !== null) {
62+
return $this->host;
5863
}
5964

60-
return $host;
65+
// retain only the elements that are not empty
66+
$str = array_filter(
67+
array($this->subdomain, $this->registerableDomain),
68+
'strlen'
69+
);
70+
71+
return implode('.', $str);
6172
}
6273

6374
/**
@@ -71,6 +82,7 @@ public function toArray()
7182
'subdomain' => $this->subdomain,
7283
'registerableDomain' => $this->registerableDomain,
7384
'publicSuffix' => $this->publicSuffix,
85+
'host' => $this->host,
7486
);
7587
}
7688

phpunit.xml.dist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020

2121
<logging>
2222
<log type="coverage-html"
23-
target="tests/log/report"
23+
target="build/coverage"
2424
charset="UTF-8"
2525
yui="true"
2626
highlight="true"
2727
lowUpperBound="40"
2828
highLowerBound="70" />
29-
<log type="testdox-html" target="tests/log/testdox.html" />
29+
<log type="testdox-html" target="build/coverage/log/testdox.html" />
3030
</logging>
3131

3232
</phpunit>

tests/library/Pdp/ParserTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public function parseDataProvider()
135135
// Test ftp support - https://github.com/jeremykendall/php-domain-parser/issues/18
136136
array('ftp://www.waxaudio.com.au/audio/albums/the_mashening', 'com.au', 'waxaudio.com.au', 'www', 'www.waxaudio.com.au'),
137137
array('ftps://test.k12.ak.us', 'k12.ak.us', 'test.k12.ak.us', null, 'test.k12.ak.us'),
138+
array('http://localhost', null, null, null, 'localhost'),
138139
);
139140
}
140141
}

tests/library/Pdp/Uri/Url/HostTest.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,24 @@ public function test__toString($publicSuffix, $registerableDomain, $subdomain, $
1212
$host = new Host(
1313
$subdomain,
1414
$registerableDomain,
15-
$publicSuffix
15+
$publicSuffix,
16+
$hostPart
1617
);
1718

1819
$this->assertEquals($hostPart, $host->__toString());
1920
}
2021

22+
public function test__toStringWhenHostPartIsNull()
23+
{
24+
$host = new Host(
25+
'www',
26+
'example.com',
27+
'com'
28+
);
29+
30+
$this->assertEquals('www.example.com', $host->__toString());
31+
}
32+
2133
/**
2234
* @dataProvider hostDataProvider
2335
*/
@@ -26,15 +38,18 @@ public function test__get($publicSuffix, $registerableDomain, $subdomain, $hostP
2638
$parts = array(
2739
'subdomain' => $subdomain,
2840
'registerableDomain' => $registerableDomain,
29-
'publicSuffix' => $publicSuffix
41+
'publicSuffix' => $publicSuffix,
42+
'host' => $hostPart,
3043
);
3144

3245
$host = new Host(
3346
$parts['subdomain'],
3447
$parts['registerableDomain'],
35-
$parts['publicSuffix']
48+
$parts['publicSuffix'],
49+
$parts['host']
3650
);
3751

52+
$this->assertSame($hostPart, $host->host);
3853
$this->assertSame($parts['subdomain'], $host->subdomain);
3954
$this->assertEquals($parts['registerableDomain'], $host->registerableDomain);
4055
$this->assertEquals($parts['publicSuffix'], $host->publicSuffix);
@@ -48,13 +63,15 @@ public function testToArray($publicSuffix, $registerableDomain, $subdomain, $hos
4863
$parts = array(
4964
'subdomain' => $subdomain,
5065
'registerableDomain' => $registerableDomain,
51-
'publicSuffix' => $publicSuffix
66+
'publicSuffix' => $publicSuffix,
67+
'host' => $hostPart,
5268
);
5369

5470
$host = new Host(
5571
$parts['subdomain'],
5672
$parts['registerableDomain'],
57-
$parts['publicSuffix']
73+
$parts['publicSuffix'],
74+
$parts['host']
5875
);
5976

6077
$this->assertEquals($parts, $host->toArray());
@@ -67,7 +84,8 @@ public function hostDataProvider()
6784
array('com', 'example.com', null, 'example.com'),
6885
array('com', 'cnn.com', 'edition', 'edition.cnn.com'),
6986
array('org', 'wikipedia.org', 'en', 'en.wikipedia.org'),
70-
array('uk.com', 'example.uk.com', 'a.b', 'a.b.example.uk.com')
87+
array('uk.com', 'example.uk.com', 'a.b', 'a.b.example.uk.com'),
88+
array(null, null, null, 'localhost'),
7189
);
7290
}
7391
}

0 commit comments

Comments
 (0)