瀏覽代碼

Merge branch 'hotfix/402'

Close #402
Matthew Weier O'Phinney 11 年之前
父節點
當前提交
b2643af366

+ 12 - 7
library/Zend/Http/Client.php

@@ -269,7 +269,7 @@ class Zend_Http_Client
      *
      * @var resource
      */
-    static protected $_fileInfoDb = null;
+    protected static $_fileInfoDb = null;
 
     /**
      * Constructor method. Will create a new HTTP client. Accepts the target
@@ -386,13 +386,17 @@ class Zend_Http_Client
     public function setMethod($method = self::GET)
     {
         if (! preg_match('/^[^\x00-\x1f\x7f-\xff\(\)<>@,;:\\\\"\/\[\]\?={}\s]+$/', $method)) {
-            /** @see Zend_Http_Client_Exception */
             require_once 'Zend/Http/Client/Exception.php';
             throw new Zend_Http_Client_Exception("'{$method}' is not a valid HTTP request method.");
         }
 
-        if (($method == self::POST || $method == self::PUT || $method == self::DELETE || $method == self::PATCH)
-             && $this->enctype === null) {
+        if (($method == self::POST
+                || $method == self::PUT
+                || $method == self::DELETE
+                || $method == self::PATCH
+                || $method == self::OPTIONS)
+            && $this->enctype === null
+        ) {
             $this->setEncType(self::ENC_URLENCODED);
         }
 
@@ -761,7 +765,7 @@ class Zend_Http_Client
             'ctype'    => $ctype,
             'data'     => $data
         );
-        
+
         $this->body_field_order[$formname] = self::VTYPE_FILE;
 
         return $this;
@@ -1456,7 +1460,8 @@ class Zend_Http_Client
      * @param array $headers Associative array of optional headers @example ("Content-Transfer-Encoding" => "binary")
      * @return string
      */
-    public static function encodeFormData($boundary, $name, $value, $filename = null, $headers = array()) {
+    public static function encodeFormData($boundary, $name, $value, $filename = null, $headers = array())
+    {
         $ret = "--{$boundary}\r\n" .
             'Content-Disposition: form-data; name="' . $name .'"';
 
@@ -1531,7 +1536,7 @@ class Zend_Http_Client
      * @param  string $prefix
      * @return array
      */
-    static protected function _flattenParametersArray($parray, $prefix = null)
+    protected static function _flattenParametersArray($parray, $prefix = null)
     {
         if (! is_array($parray)) {
             return $parray;

+ 24 - 2
library/Zend/Http/Client/Adapter/Curl.php

@@ -222,7 +222,17 @@ class Zend_Http_Client_Adapter_Curl implements Zend_Http_Client_Adapter_Interfac
         }
 
         // Set timeout
-        curl_setopt($this->_curl, CURLOPT_CONNECTTIMEOUT, $this->_config['timeout']);
+        if (defined('CURLOPT_CONNECTTIMEOUT_MS')) {
+            curl_setopt($this->_curl, CURLOPT_CONNECTTIMEOUT_MS, $this->_config['timeout'] * 1000);
+        } else {
+            curl_setopt($this->_curl, CURLOPT_CONNECTTIMEOUT, $this->_config['timeout']);
+        }
+
+        if (defined('CURLOPT_TIMEOUT_MS')) {
+            curl_setopt($this->_curl, CURLOPT_TIMEOUT_MS, $this->_config['timeout'] * 1000);
+        } else {
+            curl_setopt($this->_curl, CURLOPT_TIMEOUT, $this->_config['timeout']);
+        }
 
         // Set Max redirects
         curl_setopt($this->_curl, CURLOPT_MAXREDIRS, $this->_config['maxredirects']);
@@ -320,6 +330,11 @@ class Zend_Http_Client_Adapter_Curl implements Zend_Http_Client_Adapter_Interfac
                 }
                 break;
 
+            case Zend_Http_Client::PATCH:
+                $curlMethod = CURLOPT_CUSTOMREQUEST;
+                $curlValue = "PATCH";
+                break;
+
             case Zend_Http_Client::DELETE:
                 $curlMethod = CURLOPT_CUSTOMREQUEST;
                 $curlValue = "DELETE";
@@ -355,7 +370,7 @@ class Zend_Http_Client_Adapter_Curl implements Zend_Http_Client_Adapter_Interfac
         $curlHttp = ($httpVersion == 1.1) ? CURL_HTTP_VERSION_1_1 : CURL_HTTP_VERSION_1_0;
 
         // mark as HTTP request and set HTTP method
-        curl_setopt($this->_curl, $curlHttp, true);
+        curl_setopt($this->_curl, CURLOPT_HTTP_VERSION, $curlHttp);
         curl_setopt($this->_curl, $curlMethod, $curlValue);
 
         if($this->out_stream) {
@@ -367,6 +382,7 @@ class Zend_Http_Client_Adapter_Curl implements Zend_Http_Client_Adapter_Interfac
         } else {
             // ensure headers are also returned
             curl_setopt($this->_curl, CURLOPT_HEADER, true);
+            curl_setopt($this->_curl, CURLINFO_HEADER_OUT, true);
 
             // ensure actual response is returned
             curl_setopt($this->_curl, CURLOPT_RETURNTRANSFER, true);
@@ -393,9 +409,15 @@ class Zend_Http_Client_Adapter_Curl implements Zend_Http_Client_Adapter_Interfac
         } elseif ($method == Zend_Http_Client::PUT) {
             // This is a PUT by a setRawData string, not by file-handle
             curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $body);
+        } elseif ($method == Zend_Http_Client::PATCH) {
+            // This is a PATCH by a setRawData string
+            curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $body);
         } elseif ($method == Zend_Http_Client::DELETE) {
             // This is a DELETE by a setRawData string
             curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $body);
+        } elseif ($method == Zend_Http_Client::OPTIONS) {
+            // This is an OPTIONS by a setRawData string
+            curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $body);
         }
 
         // set additional curl options

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

@@ -134,7 +134,7 @@ abstract class Zend_Http_Client_CommonHttpTests extends PHPUnit_Framework_TestCa
      */
     public function testSimpleRequests()
     {
-        $methods = array('GET', 'POST', 'OPTIONS', 'PUT', 'DELETE');
+        $methods = array('GET', 'POST', 'OPTIONS', 'PUT', 'PATCH', 'DELETE');
 
         foreach ($methods as $method) {
             $res = $this->client->request($method);
@@ -194,7 +194,7 @@ abstract class Zend_Http_Client_CommonHttpTests extends PHPUnit_Framework_TestCa
         $res = $this->client->request('POST');
         $this->assertEquals(serialize($params), $res->getBody(), "POST data integrity test failed");
     }
-    
+
     /**
      * Test we can properly send PUT parameters with
      * application/x-www-form-urlencoded content type
@@ -209,7 +209,7 @@ abstract class Zend_Http_Client_CommonHttpTests extends PHPUnit_Framework_TestCa
         $res = $this->client->request('PUT');
         $this->assertEquals(serialize($params), $res->getBody(), "PUT data integrity test failed");
     }
-    
+
     /**
      * Test we can properly send PUT parameters without
      * content type, relying on default content type (urlencoded)
@@ -224,7 +224,67 @@ abstract class Zend_Http_Client_CommonHttpTests extends PHPUnit_Framework_TestCa
         $res = $this->client->request('PUT');
         $this->assertEquals(serialize($params), $res->getBody(), "PUT data integrity test failed for default content-type");
     }
-    
+
+    /**
+     * Test we can properly send PATCH parameters with
+     * application/x-www-form-urlencoded content type
+     *
+     * @dataProvider parameterArrayProvider
+     */
+    public function testPatchDataUrlEncoded($params)
+    {
+        $this->client->setUri($this->baseuri . 'testPatchData.php');
+        $this->client->setEncType(Zend_Http_Client::ENC_URLENCODED);
+        $this->client->setParameterPost($params);
+        $res = $this->client->request('PATCH');
+        $this->assertEquals(serialize($params), $res->getBody(), "PATCH data integrity test failed");
+    }
+
+    /**
+     * Test we can properly send PATCH parameters without
+     * content type, relying on default content type (urlencoded)
+     *
+     * @dataProvider parameterArrayProvider
+     */
+    public function testPatchDataDefault($params)
+    {
+        $this->client->setUri($this->baseuri . 'testPatchData.php');
+        // note that no content type is set
+        $this->client->setParameterPost($params);
+        $res = $this->client->request('PATCH');
+        $this->assertEquals(serialize($params), $res->getBody(), "PATCH data integrity test failed for default content-type");
+    }
+
+    /**
+     * Test we can properly send OPTIONS parameters with
+     * application/x-www-form-urlencoded content type
+     *
+     * @dataProvider parameterArrayProvider
+     */
+    public function testOptionsDataUrlEncoded($params)
+    {
+        $this->client->setUri($this->baseuri . 'testOptionsData.php');
+        $this->client->setEncType(Zend_Http_Client::ENC_URLENCODED);
+        $this->client->setParameterPost($params);
+        $res = $this->client->request('OPTIONS');
+        $this->assertEquals(serialize($params), $res->getBody(), "OPTIONS data integrity test failed");
+    }
+
+    /**
+     * Test we can properly send OPTIONS parameters without
+     * content type, relying on default content type (urlencoded)
+     *
+     * @dataProvider parameterArrayProvider
+     */
+    public function testOptionsDataDefault($params)
+    {
+        $this->client->setUri($this->baseuri . 'testOptionsData.php');
+        // note that no content type is set
+        $this->client->setParameterPost($params);
+        $res = $this->client->request('OPTIONS');
+        $this->assertEquals(serialize($params), $res->getBody(), "OPTIONS data integrity test failed for default content-type");
+    }
+
     /**
      * Test we can properly send parameters with
      * application/x-www-form-urlencoded content type
@@ -254,7 +314,7 @@ abstract class Zend_Http_Client_CommonHttpTests extends PHPUnit_Framework_TestCa
         $res = $this->client->request('POST');
         $this->assertEquals(serialize($params), $res->getBody(), "POST data integrity test failed");
     }
-    
+
     /**
      * Test we can properly send PUT parameters with
      * multipart/form-data content type
@@ -271,35 +331,69 @@ abstract class Zend_Http_Client_CommonHttpTests extends PHPUnit_Framework_TestCa
         $responseText = $response->getBody();
         $this->_checkPresence($responseText, $params);
     }
-    
+
+    /**
+     * Test we can properly send PATCH parameters with
+     * multipart/form-data content type
+     *
+     * @dataProvider parameterArrayProvider
+     */
+    public function testPatchDataMultipart($params)
+    {
+        $this->client->setUri($this->baseuri . 'testRawPatchData.php');
+        $this->client->setParameterPost($params);
+        $this->client->setMethod(Zend_Http_Client::PATCH);
+        $this->client->setEncType(Zend_Http_Client::ENC_FORMDATA);
+        $response = $this->client->request();
+        $responseText = $response->getBody();
+        $this->_checkPresence($responseText, $params);
+    }
+
+    /**
+     * Test we can properly send OPTIONS parameters with
+     * multipart/form-data content type
+     *
+     * @dataProvider parameterArrayProvider
+     */
+    public function testOptionsDataMultipart($params)
+    {
+        $this->client->setUri($this->baseuri . 'testRawOptionsData.php');
+        $this->client->setParameterPost($params);
+        $this->client->setMethod(Zend_Http_Client::OPTIONS);
+        $this->client->setEncType(Zend_Http_Client::ENC_FORMDATA);
+        $response = $this->client->request();
+        $responseText = $response->getBody();
+        $this->_checkPresence($responseText, $params);
+    }
+
     /**
      * Checks the presence of keys (non-numeric only) and values
      * in the raw form data reponse for a PUT or DELETE request recursively
-     * 
+     *
      * An example response (I do not know of a better way to check it):
      * -----ZENDHTTPCLIENT-c63973519a6bb3ec45495876f5e15828
      * Content-Disposition: form-data; name="quest"
-     * 
+     *
      * To seek the holy grail
      * -----ZENDHTTPCLIENT-c63973519a6bb3ec45495876f5e15828
      * Content-Disposition: form-data; name="YourMother"
-     * 
+     *
      * Was a hamster
      * -----ZENDHTTPCLIENT-c63973519a6bb3ec45495876f5e15828
      * Content-Disposition: form-data; name="specialChars"
-     * 
+     *
      * <>$+ &?=[]^%
      * -----ZENDHTTPCLIENT-c63973519a6bb3ec45495876f5e15828
      * Content-Disposition: form-data; name="array[]"
-     * 
+     *
      * firstItem
      * -----ZENDHTTPCLIENT-c63973519a6bb3ec45495876f5e15828
      * Content-Disposition: form-data; name="array[]"
-     * 
+     *
      * secondItem
      * -----ZENDHTTPCLIENT-c63973519a6bb3ec45495876f5e15828
      * Content-Disposition: form-data; name="array[]"
-     * 
+     *
      * 3rdItem
      * -----ZENDHTTPCLIENT-c63973519a6bb3ec45495876f5e15828--
      * @param string $responseText
@@ -319,19 +413,19 @@ abstract class Zend_Http_Client_CommonHttpTests extends PHPUnit_Framework_TestCa
                 if (!is_int($key)) {
                     $this->assertGreaterThan(
                         0,
-                        strpos($responseText, $key), 
+                        strpos($responseText, $key),
                         "key '$key' is missing from the reponse for raw multipart PUT or DELETE request"
                     );
                 }
                 $this->assertGreaterThan(
                     0,
-                    strpos($responseText, $value), 
+                    strpos($responseText, $value),
                     "value '$value' is missing from the reponse for raw multipart PUT or DELETE request"
                 );
             }
         }
     }
-    
+
     /**
      * Test we can properly send DELETE parameters with
      * multipart/form-data content type
@@ -360,12 +454,12 @@ abstract class Zend_Http_Client_CommonHttpTests extends PHPUnit_Framework_TestCa
         $res = $this->client->setRawData($data, 'text/html')->request('POST');
         $this->assertEquals($data, $res->getBody(), 'Response body does not contain the expected data');
     }
-    
+
     /**
      * Test using raw HTTP PUT data
      *
      * @group ZF-11030
-     * 
+     *
      */
     public function testRawPutData()
     {
@@ -376,12 +470,12 @@ abstract class Zend_Http_Client_CommonHttpTests extends PHPUnit_Framework_TestCa
         $response = $this->client->request();
         $this->assertEquals($data, $response->getBody(), 'Response body does not contain the expected data');
     }
-    
+
     /**
      * Test using raw HTTP DELETE data
      *
      * @group ZF-11030
-     * 
+     *
      */
     public function testRawDeleteData()
     {
@@ -1186,7 +1280,7 @@ abstract class Zend_Http_Client_CommonHttpTests extends PHPUnit_Framework_TestCa
         $this->assertNotContains('Content-Type: text/plain', $request);
 
     }
-    
+
     /**
      * @group ZF-11598
      */
@@ -1196,7 +1290,7 @@ abstract class Zend_Http_Client_CommonHttpTests extends PHPUnit_Framework_TestCa
         $adapter = $client->getAdapter();
         $this->assertTrue(!empty($adapter));
     }
-    
+
     /**
      * Internal helpder function to get the contents of test files
      *
@@ -1214,7 +1308,7 @@ abstract class Zend_Http_Client_CommonHttpTests extends PHPUnit_Framework_TestCa
      *
      * @return array
      */
-    static public function parameterArrayProvider()
+    public static function parameterArrayProvider()
     {
         return array(
             array(
@@ -1255,7 +1349,7 @@ abstract class Zend_Http_Client_CommonHttpTests extends PHPUnit_Framework_TestCa
      *
      * @return array
      */
-    static public function invalidConfigProvider()
+    public static function invalidConfigProvider()
     {
         return array(
             array(false),

+ 25 - 0
tests/Zend/Http/Client/_files/testOptionsData.php

@@ -0,0 +1,25 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Http
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id
+ */
+$variables = array();
+$data = file_get_contents('php://input');
+parse_str($data, $variables);
+echo serialize($variables);

+ 25 - 0
tests/Zend/Http/Client/_files/testPatchData.php

@@ -0,0 +1,25 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Http
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id
+ */
+$variables = array();
+$data = file_get_contents('php://input');
+parse_str($data, $variables);
+echo serialize($variables);

+ 22 - 0
tests/Zend/Http/Client/_files/testRawOptionsData.php

@@ -0,0 +1,22 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Http
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id
+ */
+echo file_get_contents('php://input');

+ 22 - 0
tests/Zend/Http/Client/_files/testRawPatchData.php

@@ -0,0 +1,22 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Http
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id
+ */
+echo file_get_contents('php://input');