Parcourir la source

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?
Daniele Alessandri il y a 10 ans
Parent
commit
c3a44b8e2e
2 fichiers modifiés avec 32 ajouts et 0 suppressions
  1. 16 0
      src/Connection/Parameters.php
  2. 16 0
      tests/Predis/Connection/ParametersTest.php

+ 16 - 0
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']);

+ 16 - 0
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