Skip to content

Commit bf41fb1

Browse files
committed
Add support for RFC 3986 compliant URL schemes. #46.
Reference: https://tools.ietf.org/html/rfc3986#section-3.1
1 parent b4e816a commit bf41fb1

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

src/Pdp/Parser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*/
2020
class Parser
2121
{
22-
const SCHEME_PATTERN = '#^(http|ftp)s?://#i';
22+
const SCHEME_PATTERN = '#^([a-zA-Z][a-zA-Z0-9+\-.]*)://#';
2323
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
/**

src/pdp-parse-url.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@
2424
*/
2525
function pdp_parse_url($url, $component = -1)
2626
{
27+
$pattern = '%([a-zA-Z][a-zA-Z0-9+\-.]*)?(:?//)?([^:/@?&=#\[\]]+)%usD';
28+
2729
$enc_url = preg_replace_callback(
28-
'%[^:/@?&=#\[\]]+%usD',
30+
$pattern,
2931
function ($matches) {
30-
return urlencode($matches[0]);
32+
$encoded = urlencode($matches[3]);
33+
return sprintf('%s%s%s', $matches[1], $matches[2], $encoded);
3134
},
3235
$url
3336
);
@@ -40,6 +43,10 @@ function ($matches) {
4043

4144
if (is_array($parts)) {
4245
foreach ($parts as $name => $value) {
46+
if ($name === 'scheme') {
47+
continue;
48+
}
49+
4350
$parts[$name] = urldecode($value);
4451
}
4552
} else {

tests/src/Pdp/ParserTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ public function parseDataProvider()
185185
// Test ftp support - https://github.com/jeremykendall/php-domain-parser/issues/18
186186
array('ftp://www.waxaudio.com.au/audio/albums/the_mashening', 'com.au', 'waxaudio.com.au', 'www', 'www.waxaudio.com.au'),
187187
array('ftps://test.k12.ak.us', 'k12.ak.us', 'test.k12.ak.us', null, 'test.k12.ak.us'),
188+
// Test support for RFC 3986 compliant schemes
189+
// https://github.com/jeremykendall/php-domain-parser/issues/46
190+
array('fake-scheme+RFC-3986.compliant://example.com', 'com', 'example.com', null, 'example.com'),
188191
array('http://localhost', null, null, null, 'localhost'),
189192
array('test.museum', 'museum', 'test.museum', null, 'test.museum'),
190193
array('bob.smith.name', 'name', 'smith.name', 'bob', 'bob.smith.name'),

0 commit comments

Comments
 (0)