Browse Source

ZF-11188
Updated Zend_Uri_Http to accept IPv6 URIs as per RFC 2732


git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@23901 44c647ce-9c0f-0410-b52a-842ac1e357ba

adamlundrigan 14 years ago
parent
commit
3cdd149475
2 changed files with 20 additions and 9 deletions
  1. 5 9
      library/Zend/Uri/Http.php
  2. 15 0
      tests/Zend/Uri/HttpTest.php

+ 5 - 9
library/Zend/Uri/Http.php

@@ -217,24 +217,20 @@ class Zend_Uri_Http extends Zend_Uri
 
         // Additional decomposition to get username, password, host, and port
         $combo   = isset($matches[3]) === true ? $matches[3] : '';
-        $pattern = '~^(([^:@]*)(:([^@]*))?@)?([^:]+)(:(.*))?$~';
+        $pattern = '~^(([^:@]*)(:([^@]*))?@)?((?(?=[[])[[][^]]+[]]|[^:]+))(:(.*))?$~';        
         $status  = @preg_match($pattern, $combo, $matches);
         if ($status === false) {
             require_once 'Zend/Uri/Exception.php';
             throw new Zend_Uri_Exception('Internal error: authority decomposition failed');
         }
-
-        // Failed decomposition; no further processing needed
-        if ($status === false) {
-            return;
-        }
-
+        
         // Save remaining URI components
         $this->_username = isset($matches[2]) === true ? $matches[2] : '';
         $this->_password = isset($matches[4]) === true ? $matches[4] : '';
-        $this->_host     = isset($matches[5]) === true ? $matches[5] : '';
+        $this->_host     = isset($matches[5]) === true 
+                         ? preg_replace('~^\[([^]]+)\]$~', '\1', $matches[5])  // Strip wrapper [] from IPv6 literal
+                         : '';
         $this->_port     = isset($matches[7]) === true ? $matches[7] : '';
-
     }
 
     /**

+ 15 - 0
tests/Zend/Uri/HttpTest.php

@@ -430,4 +430,19 @@ class Zend_Uri_HttpTest extends PHPUnit_Framework_TestCase
         ), $uri->getQueryAsArray());
         $this->assertEquals('a=1&c=3', $uri->getQuery());
     }
+    
+    /**
+     * @group ZF-11188
+     * @see http://www.ietf.org/rfc/rfc2732.txt
+     */
+    public function testParserSupportsLiteralIpv6AddressesInUri()
+    {
+      $this->assertTrue(Zend_Uri_Http::fromString('http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html')->valid());
+      $this->assertTrue(Zend_Uri_Http::fromString('http://[1080:0:0:0:8:800:200C:417A]/index.html')->valid());
+      $this->assertTrue(Zend_Uri_Http::fromString('http://[3ffe:2a00:100:7031::1]')->valid());
+      $this->assertTrue(Zend_Uri_Http::fromString('http://[1080::8:800:200C:417A]/foo')->valid());
+      $this->assertTrue(Zend_Uri_Http::fromString('http://[::192.9.5.5]/ipng')->valid());
+      $this->assertTrue(Zend_Uri_Http::fromString('http://[::FFFF:129.144.52.38]:80/index.html')->valid());
+      $this->assertTrue(Zend_Uri_Http::fromString('http://[2010:836B:4179::836B:4179]')->valid());
+    }
 }