Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit d181c52

Browse files
committed
Merge branch 'feature/http-client-refactor' of https://github.com/SocalNick/zf2 into hotfix/http-client-extension-refactor
6 parents 3c463f1 + d562686 + 67b42b2 + bdb1dae + 9809630 + 2ed4afa commit d181c52

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed

src/Client.php

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -821,23 +821,10 @@ public function send(Request $request = null)
821821
throw new Client\Exception\RuntimeException('Adapter does not support streaming');
822822
}
823823

824-
// Open the connection, send the request and read the response
825-
$this->adapter->connect($uri->getHost(), $uri->getPort(), $secure);
824+
// calling protected method to allow extending classes
825+
// to wrap the interaction with the adapter
826+
$response = $this->doRequest($uri, $method, $secure, $headers, $body);
826827

827-
if($this->config['outputstream']) {
828-
if($this->adapter instanceof Client\Adapter\Stream) {
829-
$stream = $this->openTempStream();
830-
$this->adapter->setOutputStream($stream);
831-
} else {
832-
throw new Exception\RuntimeException('Adapter does not support streaming');
833-
}
834-
}
835-
836-
// HTTP connection
837-
$this->lastRawRequest = $this->adapter->write($method,
838-
$uri, $this->config['httpversion'], $headers, $body);
839-
840-
$response = $this->adapter->read();
841828
if (! $response) {
842829
throw new Exception\RuntimeException('Unable to read response, or response is empty');
843830
}
@@ -1274,6 +1261,39 @@ protected function flattenParametersArray($parray, $prefix = null)
12741261
return $parameters;
12751262
}
12761263

1264+
/**
1265+
* Separating this from send method allows subclasses to wrap
1266+
* the interaction with the adapter
1267+
*
1268+
* @param Http $uri
1269+
* @param string $secure
1270+
* @param string $method
1271+
* @param array $headers
1272+
* @param string $body
1273+
* @return string the raw response
1274+
* @throws Exception\RuntimeException
1275+
*/
1276+
protected function doRequest(Http $uri, $method, $secure = false, $headers = array(), $body = '')
1277+
{
1278+
// Open the connection, send the request and read the response
1279+
$this->adapter->connect($uri->getHost(), $uri->getPort(), $secure);
1280+
1281+
if($this->config['outputstream']) {
1282+
if($this->adapter instanceof Client\Adapter\Stream) {
1283+
$stream = $this->openTempStream();
1284+
$this->adapter->setOutputStream($stream);
1285+
} else {
1286+
throw new Exception\RuntimeException('Adapter does not support streaming');
1287+
}
1288+
}
1289+
1290+
// HTTP connection
1291+
$this->lastRawRequest = $this->adapter->write($method,
1292+
$uri, $this->config['httpversion'], $headers, $body);
1293+
1294+
return $this->adapter->read();
1295+
}
1296+
12771297
/**
12781298
* Returns length of binary string in bytes
12791299
*

src/Client/Adapter/Curl.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,11 @@ public function write($method, $uri, $httpVersion = 1.1, $headers = array(), $bo
365365

366366
// set additional headers
367367
$headers['Accept'] = '';
368-
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
368+
$curlHeaders = array();
369+
foreach ($headers as $key => $value) {
370+
$curlHeaders[] = $key . ': ' . $value;
371+
}
372+
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $curlHeaders);
369373

370374
/**
371375
* Make sure POSTFIELDS is set after $curlMethod is set:

0 commit comments

Comments
 (0)