2020 * @license http://framework.zend.com/license/new-bsd New BSD License
2121 */
2222
23- /**
24- * @namespace
25- */
2623namespace Zend \Http \Client \Adapter ;
27- use Zend \Http \Client \Adapter as HttpAdapter ,
28- Zend \Http \Client \Adapter \Exception as AdapterException ,
29- Zend \Http \Client ,
30- Zend \Http \Request ;
24+
25+ use Traversable ;
26+ use Zend \Stdlib \ArrayUtils ;
27+ use Zend \Http \Client \Adapter \AdapterInterface as HttpAdapter ;
28+ use Zend \Http \Client \Adapter \Exception as AdapterException ;
29+ use Zend \Http \Client ;
30+ use Zend \Http \Request ;
3131
3232/**
3333 * An adapter class for Zend\Http\Client based on the curl extension.
3939 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
4040 * @license http://framework.zend.com/license/new-bsd New BSD License
4141 */
42- class Curl implements HttpAdapter, Stream
42+ class Curl implements HttpAdapter, StreamInterface
4343{
4444 /**
4545 * Parameters array
@@ -86,10 +86,10 @@ class Curl implements HttpAdapter, Stream
8686 /**
8787 * Adapter constructor
8888 *
89- * Config is set using setConfig ()
89+ * Config is set using setOptions ()
9090 *
9191 * @return void
92- * @throws \Zend\Http\Client\Adapter\Exception
92+ * @throws AdapterException\InitializationException
9393 */
9494 public function __construct ()
9595 {
@@ -118,32 +118,33 @@ public function __construct()
118118 /**
119119 * Set the configuration array for the adapter
120120 *
121- * @throws \Zend\Http\Client\Adapter\Exception
122- * @param \Zend\Config\Config | array $config
123- * @return \Zend\Http\Client\Adapter\Curl
121+ * @param array|Traversable $options
122+ * @return Curl
123+ * @throws AdapterException\InvalidArgumentException
124124 */
125- public function setConfig ( $ config = array ())
125+ public function setOptions ( $ options = array ())
126126 {
127- if ($ config instanceof \Zend \Config \Config) {
128- $ config = $ config ->toArray ();
129- } elseif (!is_array ($ config )) {
127+ if ($ options instanceof Traversable) {
128+ $ options = ArrayUtils::iteratorToArray ($ options );
129+ }
130+ if (!is_array ($ options )) {
130131 throw new AdapterException \InvalidArgumentException (
131- 'Array or Zend\Config\Config object expected, got ' . gettype ($ config )
132+ 'Array or Traversable object expected, got ' . gettype ($ options )
132133 );
133134 }
134135
135136 /** Config Key Normalization */
136- foreach ($ config as $ k => $ v ) {
137- unset($ config [$ k ]); // unset original value
138- $ config [str_replace (array ('- ' , '_ ' , ' ' , '. ' ), '' , strtolower ($ k ))] = $ v ; // replace w/ normalized
137+ foreach ($ options as $ k => $ v ) {
138+ unset($ options [$ k ]); // unset original value
139+ $ options [str_replace (array ('- ' , '_ ' , ' ' , '. ' ), '' , strtolower ($ k ))] = $ v ; // replace w/ normalized
139140 }
140141
141- if (isset ($ config ['proxyuser ' ]) && isset ($ config ['proxypass ' ])) {
142- $ this ->setCurlOption (CURLOPT_PROXYUSERPWD , $ config ['proxyuser ' ].": " .$ config ['proxypass ' ]);
143- unset($ config ['proxyuser ' ], $ config ['proxypass ' ]);
142+ if (isset ($ options ['proxyuser ' ]) && isset ($ options ['proxypass ' ])) {
143+ $ this ->setCurlOption (CURLOPT_PROXYUSERPWD , $ options ['proxyuser ' ].": " .$ options ['proxypass ' ]);
144+ unset($ options ['proxyuser ' ], $ options ['proxypass ' ]);
144145 }
145146
146- foreach ($ config as $ k => $ v ) {
147+ foreach ($ options as $ k => $ v ) {
147148 $ option = strtolower ($ k );
148149 switch ($ option ) {
149150 case 'proxyhost ' :
@@ -176,7 +177,7 @@ public function getConfig()
176177 *
177178 * @param string|int $option
178179 * @param mixed $value
179- * @return Zend\Http\Adapter\ Curl
180+ * @return Curl
180181 */
181182 public function setCurlOption ($ option , $ value )
182183 {
@@ -194,7 +195,7 @@ public function setCurlOption($option, $value)
194195 * @param int $port
195196 * @param boolean $secure
196197 * @return void
197- * @throws \Zend\Http\Client\Adapter\Exception if unable to connect
198+ * @throws AdapterException\RuntimeException if unable to connect
198199 */
199200 public function connect ($ host , $ port = 80 , $ secure = false )
200201 {
@@ -253,7 +254,7 @@ public function connect($host, $port = 80, $secure = false)
253254 * @param array $headers
254255 * @param string $body
255256 * @return string $request
256- * @throws \Zend\Http\Client\Adapter\Exception If connection fails, connected to wrong host, no PUT file defined, unsupported method, or unsupported cURL option
257+ * @throws AdapterException\RuntimeException If connection fails, connected to wrong host, no PUT file defined, unsupported method, or unsupported cURL option
257258 */
258259 public function write ($ method , $ uri , $ httpVersion = 1.1 , $ headers = array (), $ body = '' )
259260 {
@@ -283,15 +284,15 @@ public function write($method, $uri, $httpVersion = 1.1, $headers = array(), $bo
283284 case 'PUT ' :
284285 // There are two different types of PUT request, either a Raw Data string has been set
285286 // or CURLOPT_INFILE and CURLOPT_INFILESIZE are used.
286- if (is_resource ($ body )) {
287+ if (is_resource ($ body )) {
287288 $ this ->config ['curloptions ' ][CURLOPT_INFILE ] = $ body ;
288289 }
289290 if (isset ($ this ->config ['curloptions ' ][CURLOPT_INFILE ])) {
290291 // Now we will probably already have Content-Length set, so that we have to delete it
291292 // from $headers at this point:
292293 foreach ($ headers AS $ k => $ header ) {
293294 if (preg_match ('/Content-Length:\s*(\d+)/i ' , $ header , $ m )) {
294- if (is_resource ($ body )) {
295+ if (is_resource ($ body )) {
295296 $ this ->config ['curloptions ' ][CURLOPT_INFILESIZE ] = (int )$ m [1 ];
296297 }
297298 unset($ headers [$ k ]);
@@ -302,7 +303,7 @@ public function write($method, $uri, $httpVersion = 1.1, $headers = array(), $bo
302303 throw new AdapterException \RuntimeException ("Cannot set a file-handle for cURL option CURLOPT_INFILE without also setting its size in CURLOPT_INFILESIZE. " );
303304 }
304305
305- if (is_resource ($ body )) {
306+ if (is_resource ($ body )) {
306307 $ body = '' ;
307308 }
308309
@@ -327,18 +328,18 @@ public function write($method, $uri, $httpVersion = 1.1, $headers = array(), $bo
327328 $ curlMethod = CURLOPT_CUSTOMREQUEST ;
328329 $ curlValue = "TRACE " ;
329330 break ;
330-
331+
331332 case 'HEAD ' :
332333 $ curlMethod = CURLOPT_CUSTOMREQUEST ;
333334 $ curlValue = "HEAD " ;
334335 break ;
335336
336337 default :
337338 // For now, through an exception for unsupported request methods
338- throw new AdapterException \InvalidArgumentException ("Method currently not supported " );
339+ throw new AdapterException \InvalidArgumentException ("Method ' $ method ' currently not supported " );
339340 }
340341
341- if (is_resource ($ body ) && $ curlMethod != CURLOPT_UPLOAD ) {
342+ if (is_resource ($ body ) && $ curlMethod != CURLOPT_UPLOAD ) {
342343 throw new AdapterException \RuntimeException ("Streaming requests are allowed only with PUT " );
343344 }
344345
@@ -349,7 +350,7 @@ public function write($method, $uri, $httpVersion = 1.1, $headers = array(), $bo
349350 curl_setopt ($ this ->curl , $ curlHttp , true );
350351 curl_setopt ($ this ->curl , $ curlMethod , $ curlValue );
351352
352- if ($ this ->outputStream ) {
353+ if ($ this ->outputStream ) {
353354 // headers will be read into the response
354355 curl_setopt ($ this ->curl , CURLOPT_HEADER , false );
355356 curl_setopt ($ this ->curl , CURLOPT_HEADERFUNCTION , array ($ this , "readHeader " ));
@@ -363,9 +364,22 @@ public function write($method, $uri, $httpVersion = 1.1, $headers = array(), $bo
363364 curl_setopt ($ this ->curl , CURLOPT_RETURNTRANSFER , true );
364365 }
365366
367+ // Treating basic auth headers in a special way
368+ if (array_key_exists ('Authorization ' , $ headers ) && 'Basic ' == substr ($ headers ['Authorization ' ], 0 , 5 )) {
369+ curl_setopt ($ this ->curl , CURLOPT_HTTPAUTH , CURLAUTH_BASIC );
370+ curl_setopt ($ this ->curl , CURLOPT_USERPWD , base64_decode (substr ($ headers ['Authorization ' ], 6 )));
371+ unset($ headers ['Authorization ' ]);
372+ }
373+
366374 // set additional headers
367- $ headers ['Accept ' ] = '' ;
368- curl_setopt ($ this ->curl , CURLOPT_HTTPHEADER , $ headers );
375+ if (!isset ($ headers ['Accept ' ])) {
376+ $ headers ['Accept ' ] = '' ;
377+ }
378+ $ curlHeaders = array ();
379+ foreach ($ headers as $ key => $ value ) {
380+ $ curlHeaders [] = $ key . ': ' . $ value ;
381+ }
382+ curl_setopt ($ this ->curl , CURLOPT_HTTPHEADER , $ curlHeaders );
369383
370384 /**
371385 * Make sure POSTFIELDS is set after $curlMethod is set:
@@ -401,7 +415,7 @@ public function write($method, $uri, $httpVersion = 1.1, $headers = array(), $bo
401415 $ response = curl_exec ($ this ->curl );
402416
403417 // if we used streaming, headers are already there
404- if (!is_resource ($ this ->outputStream )) {
418+ if (!is_resource ($ this ->outputStream )) {
405419 $ this ->response = $ response ;
406420 }
407421
@@ -452,7 +466,7 @@ public function read()
452466 */
453467 public function close ()
454468 {
455- if (is_resource ($ this ->curl )) {
469+ if (is_resource ($ this ->curl )) {
456470 curl_close ($ this ->curl );
457471 }
458472 $ this ->curl = null ;
@@ -473,7 +487,7 @@ public function getHandle()
473487 * Set output stream for the response
474488 *
475489 * @param resource $stream
476- * @return \Zend\Http\Client\Adapter\Socket
490+ * @return Curl
477491 */
478492 public function setOutputStream ($ stream )
479493 {
0 commit comments