Просмотр исходного кода

ZF-10163: remove port duplication on redirects

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@23240 44c647ce-9c0f-0410-b52a-842ac1e357ba
matthew 15 лет назад
Родитель
Сommit
c14bc2ae00

+ 4 - 1
library/Zend/Controller/Action/Helper/Redirector.php

@@ -215,7 +215,10 @@ class Zend_Controller_Action_Helper_Redirector extends Zend_Controller_Action_He
             $port  = (isset($_SERVER['SERVER_PORT'])?$_SERVER['SERVER_PORT']:80);
             $port  = (isset($_SERVER['SERVER_PORT'])?$_SERVER['SERVER_PORT']:80);
             $uri   = $proto . '://' . $host;
             $uri   = $proto . '://' . $host;
             if ((('http' == $proto) && (80 != $port)) || (('https' == $proto) && (443 != $port))) {
             if ((('http' == $proto) && (80 != $port)) || (('https' == $proto) && (443 != $port))) {
-                $uri .= ':' . $port;
+                // do not append if HTTP_HOST already contains port
+                if (strrchr($host, ':') === false) {
+                    $uri .= ':' . $port;
+                }
             }
             }
             $url = $uri . '/' . ltrim($url, '/');
             $url = $uri . '/' . ltrim($url, '/');
         }
         }

+ 30 - 7
tests/Zend/Controller/Action/Helper/RedirectorTest.php

@@ -417,13 +417,8 @@ class Zend_Controller_Action_Helper_RedirectorTest extends PHPUnit_Framework_Tes
         $this->redirector->setUseAbsoluteUri(true);
         $this->redirector->setUseAbsoluteUri(true);
         $this->redirector->gotoUrl('/bar/baz');
         $this->redirector->gotoUrl('/bar/baz');
         $headers = $this->response->getHeaders();
         $headers = $this->response->getHeaders();
-        $uri = false;
-        foreach ($headers as $header) {
-            if ('Location' == $header['name']) {
-                $uri = $header['value'];
-            }
-        }
-        if (!$uri) {
+
+        if (!($uri = $this->_parseLocationHeaderValue())) {
             $this->fail('No redirect header set in response');
             $this->fail('No redirect header set in response');
         }
         }
 
 
@@ -431,6 +426,23 @@ class Zend_Controller_Action_Helper_RedirectorTest extends PHPUnit_Framework_Tes
     }
     }
 
 
     /**
     /**
+     * ZF-10163
+     */
+    public function testUseAbsoluteUriStripsPortFromServerHttpHost()
+    {
+        $_SERVER['HTTP_HOST']   = 'foobar.example.com:8080';
+        $_SERVER['SERVER_PORT'] = '8080';
+        $this->redirector->setUseAbsoluteUri(true);
+        $this->redirector->gotoUrl('/bar/baz');
+
+        if (!($uri = $this->_parseLocationHeaderValue())) {
+            $this->fail('No redirect header set in response');
+        }
+
+        $this->assertEquals('http://foobar.example.com:8080/bar/baz', $uri);
+    }
+
+    /**
      * ZF-2602
      * ZF-2602
      */
      */
     public function testPassingEmptyStringToGotoUrlRedirectsToRoot()
     public function testPassingEmptyStringToGotoUrlRedirectsToRoot()
@@ -525,6 +537,17 @@ class Zend_Controller_Action_Helper_RedirectorTest extends PHPUnit_Framework_Tes
     }
     }
 
 
     /**#@-*/
     /**#@-*/
+
+    protected function _parseLocationHeaderValue()
+    {
+        $headers = $this->response->getHeaders();
+
+        foreach ($headers as $header) {
+            if ('Location' == $header['name']) {
+                return $header['value'];
+            }
+        }
+    }
 }
 }
 
 
 /**
 /**