Explorar o código

ZF-6933 - cURL Adapter now processes the same configuration options like proxy and handles them equally.

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@16501 44c647ce-9c0f-0410-b52a-842ac1e357ba
beberlei %!s(int64=16) %!d(string=hai) anos
pai
achega
db051688a8

+ 2 - 1
documentation/manual/en/module_specs/Zend_Http_Client-Adapters.xml

@@ -422,7 +422,8 @@ $client = new Zend_Http_Client($uri, $config);
 
         <para>
             By default the cURL adapter is configured to behave exactly like
-            the Socket Adapter. You can change the cURL options by either specifying
+            the Socket Adapter and it also accepts the same configuration parameters 
+            as the Socket and Proxy adapters. You can also change the cURL options by either specifying
             the 'curloptions' key in the constructor of the adapter or by calling
             <code>setCurlOption($name, $value)</code>. The <code>$name</code> key
             corresponds to the CURL_* constants of the cURL extension. You can

+ 17 - 1
library/Zend/Http/Client/Adapter/Curl.php

@@ -120,8 +120,24 @@ class Zend_Http_Client_Adapter_Curl implements Zend_Http_Client_Adapter_Interfac
             throw new Zend_Http_Client_Adapter_Exception('Http Adapter configuration expects an array, ' . gettype($config) . ' recieved.');
         }
 
+        if(isset($config['proxy_user']) && isset($config['proxy_pass'])) {
+            $this->setCurlOption(CURLOPT_PROXYUSERPWD, $config['proxy_user'].":".$config['proxy_pass']);
+            unset($config['proxy_user'], $config['proxy_pass']);
+        }
+
         foreach ($config as $k => $v) {
-            $this->_config[strtolower($k)] = $v;
+            $option = strtolower($k);
+            switch($option) {
+                case 'proxy_host':
+                    $this->setCurlOption(CURLOPT_PROXY, $v);
+                    break;
+                case 'proxy_port':
+                    $this->setCurlOption(CURLOPT_PROXYPORT, $v);
+                    break;
+                default:
+                    $this->_config[$option] = $v;
+                    break;
+            }
         }
 
         return $this;

+ 23 - 0
tests/Zend/Http/Client/CurlTest.php

@@ -185,6 +185,29 @@ class Zend_Http_Client_CurlTest extends Zend_Http_Client_SocketTest
         );
     }
 
+    public function testWorkWithProxyConfiguration()
+    {
+        $adapter = new Zend_Http_Client_Adapter_Curl();
+        $adapter->setConfig(array(
+            'proxy_host' => 'localhost',
+            'proxy_port' => 80,
+            'proxy_user' => 'foo',
+            'proxy_pass' => 'baz',
+        ));
+
+        $expected = array(
+            'curloptions' => array(
+                CURLOPT_PROXYUSERPWD => 'foo:baz',
+                CURLOPT_PROXY => 'localhost',
+                CURLOPT_PROXYPORT => 80,
+            ),
+        );
+
+        $this->assertEquals(
+            $expected, $this->readAttribute($adapter, '_config')
+        );
+    }
+
     /**
      * @group ZF-7040
      */