Skip to content

Commit 94301b1

Browse files
committed
Add hack for PHP 5.3 parse_url behavior. Fixes #49.
1 parent 188ea20 commit 94301b1

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/Pdp/Parser.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,16 @@
2121
*/
2222
class Parser
2323
{
24+
/**
25+
* @var string RFC 3986 compliant scheme regex pattern
26+
*
27+
* @see https://tools.ietf.org/html/rfc3986#section-3.1
28+
*/
2429
const SCHEME_PATTERN = '#^([a-zA-Z][a-zA-Z0-9+\-.]*)://#';
30+
31+
/**
32+
* @var string IP address regex pattern
33+
*/
2534
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]?)$/';
2635

2736
/**
@@ -67,7 +76,9 @@ public function parseUrl($url)
6776
);
6877

6978
if (preg_match(self::SCHEME_PATTERN, $url) === 0) {
70-
$url = '//' . preg_replace('#^//#', '', $url, 1);
79+
// Wacky scheme required to overcome parse_url behavior in PHP lt 5.4.7
80+
// See https://github.com/jeremykendall/php-domain-parser/issues/49
81+
$url = 'php-lt-5.4.7-hack://' . preg_replace('#^//#', '', $url, 1);
7182
}
7283

7384
$parts = pdp_parse_url($url);
@@ -76,6 +87,12 @@ public function parseUrl($url)
7687
throw new \InvalidArgumentException(sprintf('Invalid url %s', $url));
7788
}
7889

90+
if ($parts['scheme'] === 'php-lt-5.4.7-hack') {
91+
// Remove wacky scheme required to overcome parse_url behavior in PHP lt 5.4.7
92+
// See https://github.com/jeremykendall/php-domain-parser/issues/49
93+
$parts['scheme'] = null;
94+
}
95+
7996
$elem = (array) $parts + $elem;
8097

8198
$host = $this->parseHost($parts['host']);

tests/src/Pdp/ParserTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,12 @@ public function testpdp_parse_urlCanReturnCorrectHost($url, $publicSuffix, $regi
155155

156156
/**
157157
* @group issue46
158+
* @group issue49
158159
*
159160
* Don't add a scheme to schemeless URLs
160161
*
161162
* @see https://github.com/jeremykendall/php-domain-parser/issues/46
163+
* @see https://github.com/jeremykendall/php-domain-parser/issues/49
162164
*/
163165
public function testDoNotPrependSchemeToSchemelessUrls()
164166
{

0 commit comments

Comments
 (0)