Skip to content

Commit 7804d7d

Browse files
committed
Fixes #43
1 parent ad8cc6e commit 7804d7d

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/Pdp/Parser.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
* @copyright Copyright (c) 2014 Jeremy Kendall (http://about.me/jeremykendall)
88
* @license http://github.com/jeremykendall/php-domain-parser/blob/master/LICENSE MIT License
99
*/
10-
1110
namespace Pdp;
1211

1312
use Pdp\Uri\Url;
@@ -21,6 +20,7 @@
2120
class Parser
2221
{
2322
const SCHEME_PATTERN = '#^(http|ftp)s?://#i';
23+
const IP_ADDRESS_PATTERN = '/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/';
2424

2525
/**
2626
* @var PublicSuffixList Public Suffix List
@@ -104,7 +104,8 @@ public function parseHost($host)
104104

105105
// Fixes #22: Single label domains are set as Host::$host and all other
106106
// properties are null.
107-
if (strpos($host, '.') !== false) {
107+
// Fixes #43: Ip Addresses should not be parsed
108+
if ($this->isMutliLabelDomain($host) || !$this->isIpv4Address($host)) {
108109
$subdomain = $this->getSubdomain($host);
109110
$registerableDomain = $this->getRegisterableDomain($host);
110111
$publicSuffix = $this->getPublicSuffix($host);
@@ -186,7 +187,12 @@ public function getPublicSuffix($host)
186187
// Fixes #22: If a single label domain makes it this far (e.g.,
187188
// localhost, foo, etc.), this stops it from incorrectly being set as
188189
// the public suffix.
189-
if (strpos($host, '.') === false) {
190+
if (!$this->isMutliLabelDomain($host)) {
191+
return;
192+
}
193+
194+
// Fixes #43
195+
if ($this->isIpv4Address($host)) {
190196
return;
191197
}
192198

@@ -304,4 +310,30 @@ protected function denormalize($part)
304310

305311
return $part;
306312
}
313+
314+
/**
315+
* Tests host for presence of '.'
316+
*
317+
* Related to #22
318+
*
319+
* @param string $host Host part of url
320+
* @return bool True if multi-label domain, false otherwise
321+
*/
322+
protected function isMutliLabelDomain($host)
323+
{
324+
return strpos($host, '.') !== false;
325+
}
326+
327+
/**
328+
* Tests host to determine if it is an IP address
329+
*
330+
* Related to #43
331+
*
332+
* @param string $host Host part of url
333+
* @return bool True if host is an ip address, false otherwise
334+
*/
335+
protected function isIpv4Address($host)
336+
{
337+
return preg_match(self::IP_ADDRESS_PATTERN, $host) === 1;
338+
}
307339
}

tests/src/Pdp/ParserTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ public function parseDataProvider()
202202
array('http://[::1]/', null, null, null, '[::1]'),
203203
array('http://[2001:db8:85a3:8d3:1319:8a2e:370:7348]/', null, null, null, '[2001:db8:85a3:8d3:1319:8a2e:370:7348]'),
204204
array('https://[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443/', null, null, null, '[2001:db8:85a3:8d3:1319:8a2e:370:7348]'),
205+
// Test IP address: Fixes #43
206+
array('http://192.168.1.2/', null, null, null, '192.168.1.2'),
207+
array('http://127.0.0.1:443', null, null, null, '127.0.0.1'),
208+
array('http://67.196.2.34/whois-archive/latest.php?page=2479', null, null, null, '67.196.2.34'),
205209
// Link-local addresses and zone indices
206210
array('http://[fe80::3%25eth0]', null, null, null, '[fe80::3%25eth0]'),
207211
array('http://[fe80::1%2511]', null, null, null, '[fe80::1%2511]'),

0 commit comments

Comments
 (0)