瀏覽代碼

Fixing ZF-10277: proper parsing of headers passed as indexed array to the constructor

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@22810 44c647ce-9c0f-0410-b52a-842ac1e357ba
shahar 15 年之前
父節點
當前提交
19e4a8e59d
共有 2 個文件被更改,包括 65 次插入12 次删除
  1. 13 11
      library/Zend/Http/Response.php
  2. 52 1
      tests/Zend/Http/ResponseTest.php

+ 13 - 11
library/Zend/Http/Response.php

@@ -141,14 +141,14 @@ class Zend_Http_Response
      *
      * If no message is passed, the message will be guessed according to the response code.
      *
-     * @param int $code Response code (200, 404, ...)
-     * @param array $headers Headers array
+     * @param int    $code Response code (200, 404, ...)
+     * @param array  $headers Headers array
      * @param string $body Response body
      * @param string $version HTTP version
      * @param string $message Response code as text
      * @throws Zend_Http_Exception
      */
-    public function __construct($code, $headers, $body = null, $version = '1.1', $message = null)
+    public function __construct($code, array $headers, $body = null, $version = '1.1', $message = null)
     {
         // Make sure the response code is valid and set it
         if (self::responseCodeAsText($code) === null) {
@@ -158,15 +158,17 @@ class Zend_Http_Response
 
         $this->code = $code;
 
-        // Make sure we got valid headers and set them
-        if (! is_array($headers)) {
-            require_once 'Zend/Http/Exception.php';
-            throw new Zend_Http_Exception('No valid headers were passed');
-    }
-
         foreach ($headers as $name => $value) {
-            if (is_int($name))
-                list($name, $value) = explode(": ", $value, 1);
+            if (is_int($name)) {
+                $header = explode(":", $value, 2);
+                if (count($header) != 2) {
+                    require_once 'Zend/Http/Exception.php';
+                    throw new Zend_Http_Exception("'{$value}' is not a valid HTTP header");
+                }
+                
+                $name  = trim($header[0]);
+                $value = trim($header[1]);
+            }
 
             $this->headers[ucwords(strtolower($name))] = $value;
         }

+ 52 - 1
tests/Zend/Http/ResponseTest.php

@@ -335,7 +335,58 @@ class Zend_Http_ResponseTest extends PHPUnit_Framework_TestCase
         $response = Zend_Http_Response::fromString($this->readResponse('response_multibyte_body'));
         $this->assertEquals($md5, md5($response->getBody()));
     }
-
+    
+    /**
+     * Test that headers are properly set when passed to the constructor as an associative array
+     * 
+     * @group ZF-10277
+     */
+    public function testConstructorWithHeadersAssocArray()
+    {
+        $response = new Zend_Http_Response(200, array(
+            'content-type' => 'text/plain',
+            'x-foo'        => 'bar:baz'
+        ));
+        
+        $this->assertEquals('text/plain', $response->getHeader('content-type'));
+        $this->assertEquals('bar:baz', $response->getHeader('x-foo'));
+    }
+    
+    /**
+     * Test that headers are properly parsed when passed to the constructor as an indexed array
+     *
+     * @link  http://framework.zend.com/issues/browse/ZF-10277
+     * @group ZF-10277
+     */
+    public function testConstructorWithHeadersIndexedArrayZF10277()
+    {
+        $response = new Zend_Http_Response(200, array(
+            'content-type: text/plain',
+            'x-foo: bar:baz'
+        ));
+        
+        $this->assertEquals('text/plain', $response->getHeader('content-type'));
+        $this->assertEquals('bar:baz', $response->getHeader('x-foo'));
+    }
+    
+    /**
+     * Test that headers are properly parsed when passed to the constructor as 
+     * an indexed array with no whitespace after the ':' sign
+     *
+     * @link  http://framework.zend.com/issues/browse/ZF-10277
+     * @group ZF-10277
+     */
+    public function testConstructorWithHeadersIndexedArrayNoWhitespace()
+    {
+        $response = new Zend_Http_Response(200, array(
+            'content-type:text/plain',
+            'x-foo:bar:baz'
+        ));
+        
+        $this->assertEquals('text/plain', $response->getHeader('content-type'));
+        $this->assertEquals('bar:baz', $response->getHeader('x-foo'));
+    }
+    
     /**
      * Helper function: read test response from file
      *