From c3a44b8e2e7d80dc20c63d2c451e6bb0cd7e7184 Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Wed, 29 Jul 2015 16:44:11 +0200 Subject: [PATCH] Strip brackets from host when parsing embedded IPv6 address. I don't know why PHP's parse_url() does not do that, it does not make sense when the IP is by itself so maybe it is a bug? --- src/Connection/Parameters.php | 16 ++++++++++++++++ tests/Predis/Connection/ParametersTest.php | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/Connection/Parameters.php b/src/Connection/Parameters.php index b2e8d25b..5cd17329 100644 --- a/src/Connection/Parameters.php +++ b/src/Connection/Parameters.php @@ -88,6 +88,14 @@ class Parameters implements ParametersInterface throw new \InvalidArgumentException("Invalid parameters URI: $uri"); } + if ( + isset($parsed['host']) + && false !== strpos($parsed['host'], '[') + && false !== strpos($parsed['host'], ']') + ) { + $parsed['host'] = substr($parsed['host'], 1, -1); + } + if (isset($parsed['query'])) { parse_str($parsed['query'], $queryarray); unset($parsed['query']); @@ -121,6 +129,14 @@ class Parameters implements ParametersInterface throw new \InvalidArgumentException("Invalid parameters URI: $uri"); } + if ( + isset($parsed['host']) + && false !== strpos($parsed['host'], '[') + && false !== strpos($parsed['host'], ']') + ) { + $parsed['host'] = substr($parsed['host'], 1, -1); + } + if (isset($parsed['query'])) { parse_str($parsed['query'], $queryarray); unset($parsed['query']); diff --git a/tests/Predis/Connection/ParametersTest.php b/tests/Predis/Connection/ParametersTest.php index 0504a4de..51b081db 100644 --- a/tests/Predis/Connection/ParametersTest.php +++ b/tests/Predis/Connection/ParametersTest.php @@ -260,6 +260,22 @@ class ParametersTest extends PredisTestCase $this->assertSame($expected, Parameters::parse($uri)); } + /** + * @group disconnected + */ + public function testParsingURIWithEmbeddedIPV6AddressShouldStripBracketsFromHost() + { + $uri = 'tcp://[::1]:7000'; + + $expected = array( + 'scheme' => 'tcp', + 'host' => '::1', + 'port' => 7000, + ); + + $this->assertSame($expected, Parameters::parse($uri)); + } + /** * @group disconnected * @expectedException \InvalidArgumentException