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

ZF-7756: cache raw body after first retrieval

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

+ 14 - 5
library/Zend/Controller/Request/Http.php

@@ -85,6 +85,12 @@ class Zend_Controller_Request_Http extends Zend_Controller_Request_Abstract
     protected $_params = array();
 
     /**
+     * Raw request body
+     * @var string|false
+     */
+    protected $_rawBody;
+
+    /**
      * Alias keys for request parameters
      * @var array
      */
@@ -919,13 +925,16 @@ class Zend_Controller_Request_Http extends Zend_Controller_Request_Abstract
      */
     public function getRawBody()
     {
-        $body = file_get_contents('php://input');
+        if (null === $this->_rawBody) {
+            $body = file_get_contents('php://input');
 
-        if (strlen(trim($body)) > 0) {
-            return $body;
+            if (strlen(trim($body)) > 0) {
+                $this->_rawBody = $body;
+            } else {
+                $this->_rawBody = false;
+            }
         }
-
-        return false;
+        return $this->_rawBody;
     }
 
     /**

+ 8 - 0
tests/Zend/Controller/Request/HttpTest.php

@@ -756,6 +756,14 @@ class Zend_Controller_Request_HttpTest extends PHPUnit_Framework_TestCase
 
         $this->assertEquals('192.168.1.12', $request->getClientIp(false));
     }
+
+    /**
+     * @group ZF-7756
+     */
+    public function testCallingGetRawBodyMultipleTimesShouldReturnSameValue()
+    {
+        $this->markTestSkipped('Impossible to populate php://input to test this');
+    }
 }
 
 // Call Zend_Controller_Request_HttpTest::main() if this source file is executed directly.