瀏覽代碼

Fixing ZF-1850 again, patch contributed by Matt Pinkston
- If encodecookies is FALSE, cookies captured from HTTP responses are no longer encoded
- Added unit tests that reproduce the problem


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

shahar 15 年之前
父節點
當前提交
4afbc8df0b

+ 1 - 1
library/Zend/Http/Client.php

@@ -1013,7 +1013,7 @@ class Zend_Http_Client
 
             // Load cookies into cookie jar
             if (isset($this->cookiejar)) {
-                $this->cookiejar->addCookiesFromResponse($response, $uri);
+                $this->cookiejar->addCookiesFromResponse($response, $uri, $this->config['encodecookies']);
             }
 
             // If we got redirected, look for the Location header

+ 1 - 1
library/Zend/Http/Cookie.php

@@ -277,7 +277,7 @@ class Zend_Http_Cookie
      *
      * @param string $cookieStr
      * @param Zend_Uri_Http|string $refUri Reference URI for default values (domain, path)
-     * @param boolean $encodeValue Weither or not the cookie's value should be
+     * @param boolean $encodeValue Whether or not the cookie's value should be
      *                             passed through urlencode/urldecode
      * @return Zend_Http_Cookie A new Zend_Http_Cookie object or false on failure.
      */

+ 7 - 5
library/Zend/Http/CookieJar.php

@@ -116,11 +116,12 @@ class Zend_Http_CookieJar implements Countable, IteratorAggregate
      *
      * @param Zend_Http_Cookie|string $cookie
      * @param Zend_Uri_Http|string    $ref_uri Optional reference URI (for domain, path, secure)
+     * @param boolean $encodeValue
      */
-    public function addCookie($cookie, $ref_uri = null)
+    public function addCookie($cookie, $ref_uri = null, $encodeValue = true)
     {
         if (is_string($cookie)) {
-            $cookie = Zend_Http_Cookie::fromString($cookie, $ref_uri);
+            $cookie = Zend_Http_Cookie::fromString($cookie, $ref_uri, $encodeValue);
         }
 
         if ($cookie instanceof Zend_Http_Cookie) {
@@ -142,8 +143,9 @@ class Zend_Http_CookieJar implements Countable, IteratorAggregate
      *
      * @param Zend_Http_Response $response
      * @param Zend_Uri_Http|string $ref_uri Requested URI
+     * @param boolean $encodeValue
      */
-    public function addCookiesFromResponse($response, $ref_uri)
+    public function addCookiesFromResponse($response, $ref_uri, $encodeValue = true)
     {
         if (! $response instanceof Zend_Http_Response) {
             require_once 'Zend/Http/Exception.php';
@@ -155,10 +157,10 @@ class Zend_Http_CookieJar implements Countable, IteratorAggregate
 
         if (is_array($cookie_hdrs)) {
             foreach ($cookie_hdrs as $cookie) {
-                $this->addCookie($cookie, $ref_uri);
+                $this->addCookie($cookie, $ref_uri, $encodeValue);
             }
         } elseif (is_string($cookie_hdrs)) {
-            $this->addCookie($cookie_hdrs, $ref_uri);
+            $this->addCookie($cookie_hdrs, $ref_uri, $encodeValue);
         }
     }
 

+ 44 - 0
tests/Zend/Http/Client/StaticTest.php

@@ -291,6 +291,50 @@ class Zend_Http_Client_StaticTest extends PHPUnit_Framework_TestCase
     {
         $this->_client->setCookieJar('cookiejar');
     }
+    
+    /**
+     * Test that when the encodecookies flag is set to FALSE, cookies captured
+     * from a response by Zend_Http_CookieJar are not encoded
+     * 
+     * @group ZF-1850
+     */
+    public function testCaptureCookiesNoEncodeZF1850()
+    {
+        $cookieName = "cookieWithSpecialChars";
+        $cookieValue = "HID=XXXXXX&UN=XXXXXXX&UID=XXXXX";
+        
+        $adapter = new Zend_Http_Client_Adapter_Test();
+        $adapter->setResponse(
+        	"HTTP/1.0 200 OK\r\n" . 
+            "Content-type: text/plain\r\n" . 
+            "Content-length: 2\r\n" . 
+            "Connection: close\r\n" . 
+            "Set-Cookie: $cookieName=$cookieValue; path=/\r\n" . 
+            "\r\n" . 
+            "OK"
+        );
+        
+        $this->_client->setUri('http://example.example/test');
+        $this->_client->setConfig(array(
+            'adapter'       => $adapter,
+            'encodecookies' => false
+        ));
+        
+        $this->_client->setCookieJar();
+        
+        // First request is expected to set the cookie
+        $this->_client->request();
+        
+        // Next request should contain the cookie
+        $this->_client->request();
+        
+        $request = $this->_client->getLastRequest();
+        if (! preg_match("/^Cookie: $cookieName=([^;]+)/m", $request, $match)) {
+            $this->fail("Could not find cookie in request");
+        }
+        
+        $this->assertEquals($cookieValue, $match[1]);
+    }
 
     /**
      * Configuration Handling