20
20
* @license http://framework.zend.com/license/new-bsd New BSD License
21
21
*/
22
22
23
- /**
24
- * @namespace
25
- */
26
23
namespace 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 ;
31
31
32
32
/**
33
33
* An adapter class for Zend\Http\Client based on the curl extension.
39
39
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
40
40
* @license http://framework.zend.com/license/new-bsd New BSD License
41
41
*/
42
- class Curl implements HttpAdapter, Stream
42
+ class Curl implements HttpAdapter, StreamInterface
43
43
{
44
44
/**
45
45
* Parameters array
@@ -86,10 +86,10 @@ class Curl implements HttpAdapter, Stream
86
86
/**
87
87
* Adapter constructor
88
88
*
89
- * Config is set using setConfig ()
89
+ * Config is set using setOptions ()
90
90
*
91
91
* @return void
92
- * @throws \Zend\Http\Client\Adapter\Exception
92
+ * @throws AdapterException\InitializationException
93
93
*/
94
94
public function __construct ()
95
95
{
@@ -118,32 +118,33 @@ public function __construct()
118
118
/**
119
119
* Set the configuration array for the adapter
120
120
*
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
124
124
*/
125
- public function setConfig ( $ config = array ())
125
+ public function setOptions ( $ options = array ())
126
126
{
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 )) {
130
131
throw new AdapterException \InvalidArgumentException (
131
- 'Array or Zend\Config\Config object expected, got ' . gettype ($ config )
132
+ 'Array or Traversable object expected, got ' . gettype ($ options )
132
133
);
133
134
}
134
135
135
136
/** 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
139
140
}
140
141
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 ' ]);
144
145
}
145
146
146
- foreach ($ config as $ k => $ v ) {
147
+ foreach ($ options as $ k => $ v ) {
147
148
$ option = strtolower ($ k );
148
149
switch ($ option ) {
149
150
case 'proxyhost ' :
@@ -176,7 +177,7 @@ public function getConfig()
176
177
*
177
178
* @param string|int $option
178
179
* @param mixed $value
179
- * @return Zend\Http\Adapter\ Curl
180
+ * @return Curl
180
181
*/
181
182
public function setCurlOption ($ option , $ value )
182
183
{
@@ -194,7 +195,7 @@ public function setCurlOption($option, $value)
194
195
* @param int $port
195
196
* @param boolean $secure
196
197
* @return void
197
- * @throws \Zend\Http\Client\Adapter\Exception if unable to connect
198
+ * @throws AdapterException\RuntimeException if unable to connect
198
199
*/
199
200
public function connect ($ host , $ port = 80 , $ secure = false )
200
201
{
@@ -253,7 +254,7 @@ public function connect($host, $port = 80, $secure = false)
253
254
* @param array $headers
254
255
* @param string $body
255
256
* @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
257
258
*/
258
259
public function write ($ method , $ uri , $ httpVersion = 1.1 , $ headers = array (), $ body = '' )
259
260
{
@@ -283,15 +284,15 @@ public function write($method, $uri, $httpVersion = 1.1, $headers = array(), $bo
283
284
case 'PUT ' :
284
285
// There are two different types of PUT request, either a Raw Data string has been set
285
286
// or CURLOPT_INFILE and CURLOPT_INFILESIZE are used.
286
- if (is_resource ($ body )) {
287
+ if (is_resource ($ body )) {
287
288
$ this ->config ['curloptions ' ][CURLOPT_INFILE ] = $ body ;
288
289
}
289
290
if (isset ($ this ->config ['curloptions ' ][CURLOPT_INFILE ])) {
290
291
// Now we will probably already have Content-Length set, so that we have to delete it
291
292
// from $headers at this point:
292
293
foreach ($ headers AS $ k => $ header ) {
293
294
if (preg_match ('/Content-Length:\s*(\d+)/i ' , $ header , $ m )) {
294
- if (is_resource ($ body )) {
295
+ if (is_resource ($ body )) {
295
296
$ this ->config ['curloptions ' ][CURLOPT_INFILESIZE ] = (int )$ m [1 ];
296
297
}
297
298
unset($ headers [$ k ]);
@@ -302,7 +303,7 @@ public function write($method, $uri, $httpVersion = 1.1, $headers = array(), $bo
302
303
throw new AdapterException \RuntimeException ("Cannot set a file-handle for cURL option CURLOPT_INFILE without also setting its size in CURLOPT_INFILESIZE. " );
303
304
}
304
305
305
- if (is_resource ($ body )) {
306
+ if (is_resource ($ body )) {
306
307
$ body = '' ;
307
308
}
308
309
@@ -327,18 +328,18 @@ public function write($method, $uri, $httpVersion = 1.1, $headers = array(), $bo
327
328
$ curlMethod = CURLOPT_CUSTOMREQUEST ;
328
329
$ curlValue = "TRACE " ;
329
330
break ;
330
-
331
+
331
332
case 'HEAD ' :
332
333
$ curlMethod = CURLOPT_CUSTOMREQUEST ;
333
334
$ curlValue = "HEAD " ;
334
335
break ;
335
336
336
337
default :
337
338
// 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 " );
339
340
}
340
341
341
- if (is_resource ($ body ) && $ curlMethod != CURLOPT_UPLOAD ) {
342
+ if (is_resource ($ body ) && $ curlMethod != CURLOPT_UPLOAD ) {
342
343
throw new AdapterException \RuntimeException ("Streaming requests are allowed only with PUT " );
343
344
}
344
345
@@ -349,7 +350,7 @@ public function write($method, $uri, $httpVersion = 1.1, $headers = array(), $bo
349
350
curl_setopt ($ this ->curl , $ curlHttp , true );
350
351
curl_setopt ($ this ->curl , $ curlMethod , $ curlValue );
351
352
352
- if ($ this ->outputStream ) {
353
+ if ($ this ->outputStream ) {
353
354
// headers will be read into the response
354
355
curl_setopt ($ this ->curl , CURLOPT_HEADER , false );
355
356
curl_setopt ($ this ->curl , CURLOPT_HEADERFUNCTION , array ($ this , "readHeader " ));
@@ -363,9 +364,22 @@ public function write($method, $uri, $httpVersion = 1.1, $headers = array(), $bo
363
364
curl_setopt ($ this ->curl , CURLOPT_RETURNTRANSFER , true );
364
365
}
365
366
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
+
366
374
// 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 );
369
383
370
384
/**
371
385
* Make sure POSTFIELDS is set after $curlMethod is set:
@@ -401,7 +415,7 @@ public function write($method, $uri, $httpVersion = 1.1, $headers = array(), $bo
401
415
$ response = curl_exec ($ this ->curl );
402
416
403
417
// if we used streaming, headers are already there
404
- if (!is_resource ($ this ->outputStream )) {
418
+ if (!is_resource ($ this ->outputStream )) {
405
419
$ this ->response = $ response ;
406
420
}
407
421
@@ -452,7 +466,7 @@ public function read()
452
466
*/
453
467
public function close ()
454
468
{
455
- if (is_resource ($ this ->curl )) {
469
+ if (is_resource ($ this ->curl )) {
456
470
curl_close ($ this ->curl );
457
471
}
458
472
$ this ->curl = null ;
@@ -473,7 +487,7 @@ public function getHandle()
473
487
* Set output stream for the response
474
488
*
475
489
* @param resource $stream
476
- * @return \Zend\Http\Client\Adapter\Socket
490
+ * @return Curl
477
491
*/
478
492
public function setOutputStream ($ stream )
479
493
{
0 commit comments