Преглед изворни кода

Fixing ZF-9404: proper handling of more than one Content-length headers in response

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@21777 44c647ce-9c0f-0410-b52a-842ac1e357ba
shahar пре 16 година
родитељ
комит
890de9d2c4

+ 9 - 1
library/Zend/Http/Client/Adapter/Socket.php

@@ -417,10 +417,18 @@ class Zend_Http_Client_Adapter_Socket implements Zend_Http_Client_Adapter_Interf
         // Else, if we got the content-length header, read this number of bytes
         } elseif (isset($headers['content-length'])) {
 
+            // If we got more than one Content-Length header (see ZF-9404) use
+            // the last value sent
+            if (is_array($headers['content-length'])) {
+                $contentLength = $headers['content-length'][count($headers['content-length']) - 1]; 
+            } else {
+                $contentLength = $headers['content-length'];
+            }
+            
             $current_pos = ftell($this->socket);
             $chunk = '';
 
-            for ($read_to = $current_pos + $headers['content-length'];
+            for ($read_to = $current_pos + $contentLength;
                  $read_to > $current_pos;
                  $current_pos = ftell($this->socket)) {
 

+ 24 - 1
tests/Zend/Http/Client/CommonHttpTests.php

@@ -844,7 +844,7 @@ abstract class Zend_Http_Client_CommonHttpTests extends PHPUnit_Framework_TestCa
      * Test that lines that might be evaluated as boolean false do not break
      * the reading prematurely.
      *
-     * @see http://framework.zend.com/issues/browse/ZF-4238
+     * @link http://framework.zend.com/issues/browse/ZF-4238
      */
     public function testZF4238FalseLinesInResponse()
     {
@@ -939,6 +939,29 @@ abstract class Zend_Http_Client_CommonHttpTests extends PHPUnit_Framework_TestCa
     }
     
     /**
+     * Test that we can deal with double Content-Length headers
+     * 
+     * @link http://framework.zend.com/issues/browse/ZF-9404
+     */
+    public function testZF9404DoubleContentLengthHeader()
+    {
+        $this->client->setUri($this->baseuri . 'ZF9404-doubleContentLength.php');
+        $expect = filesize(dirname(realpath(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ZF9404-doubleContentLength.php');
+        
+        $response = $this->client->request();
+        if (! $response->isSuccessful()) {
+            throw new ErrorException("Error requesting test URL");
+        }
+        
+        $clen = $response->getHeader('content-length');
+        if (! (is_array($clen))) {
+            $this->markTestSkipped("Didn't get multiple Content-length headers");
+        }
+        
+        $this->assertEquals($expect, strlen($response->getBody()));
+    }
+    
+    /**
      * Internal helpder function to get the contents of test files
      *
      * @param  string $file

+ 8 - 0
tests/Zend/Http/Client/_files/ZF9404-doubleContentLength.php

@@ -0,0 +1,8 @@
+<?php
+
+$clength = filesize(__FILE__);
+
+header("Content-length: $clength");
+header("Content-length: $clength", false);
+
+readfile(__FILE__);