Permalink
Browse files

Merge pull request #74 from kriswallsmith/curl

one curl instance per request
  • Loading branch information...
2 parents 8ffbb97 + e1293a5 commit 11dbe695155f3241bfef47ec9beeeffc8aa40b26 @kriswallsmith committed May 15, 2012
Showing with 48 additions and 31 deletions.
  1. +32 −31 lib/Buzz/Client/Curl.php
  2. +1 −0 lib/Buzz/Client/MultiCurl.php
  3. +15 −0 test/Buzz/Test/Client/FunctionalTest.php
View
63 lib/Buzz/Client/Curl.php
@@ -6,7 +6,7 @@
class Curl extends AbstractClient implements ClientInterface
{
- protected $curl;
+ protected $options = array();
static protected function createCurlHandle()
{
@@ -24,9 +24,6 @@ static protected function setCurlOptsFromRequest($curl, Message\Request $request
CURLOPT_CUSTOMREQUEST => $request->getMethod(),
CURLOPT_URL => $request->getHost().$request->getResource(),
CURLOPT_HTTPHEADER => $request->getHeaders(),
- CURLOPT_HTTPGET => false,
- CURLOPT_NOBODY => false,
- CURLOPT_POSTFIELDS => null,
);
switch ($request->getMethod()) {
@@ -46,11 +43,11 @@ static protected function setCurlOptsFromRequest($curl, Message\Request $request
// remove the content-type header
if (is_array($fields)) {
- $options[CURLOPT_HTTPHEADER] = array_filter($options[CURLOPT_HTTPHEADER], function($header)
- {
+ $options[CURLOPT_HTTPHEADER] = array_filter($options[CURLOPT_HTTPHEADER], function($header) {
return 0 !== stripos($header, 'Content-Type: ');
});
}
+
break;
}
@@ -103,50 +100,54 @@ static private function getPostFields(Message\Request $request)
return $multipart ? $fields : http_build_query($fields);
}
- public function __construct()
- {
- $this->curl = static::createCurlHandle();
- }
-
- public function getCurl()
+ /**
+ * Stashes a cURL option to be set on send, when the resource is created.
+ *
+ * If the supplied value it set to null the option will be removed.
+ *
+ * @param integer $option The option
+ * @param mixed $value The value
+ */
+ public function setOption($option, $value)
{
- return $this->curl;
+ if (null === $value) {
+ unset($this->options[$option]);
+ } else {
+ $this->options[$option] = $value;
+ }
}
public function send(Message\Request $request, Message\Response $response)
{
- if (false === is_resource($this->curl)) {
- $this->curl = static::createCurlHandle();
- }
+ $curl = static::createCurlHandle();
- $this->prepare($request, $response, $this->curl);
+ $this->prepare($request, $response, $curl);
+
+ $data = curl_exec($curl);
- $data = curl_exec($this->curl);
if (false === $data) {
- $errorMsg = curl_error($this->curl);
- $errorNo = curl_errno($this->curl);
+ $errorMsg = curl_error($curl);
+ $errorNo = curl_errno($curl);
throw new \RuntimeException($errorMsg, $errorNo);
}
$response->fromString(static::getLastResponse($data));
+
+ curl_close($curl);
}
protected function prepare(Message\Request $request, Message\Response $response, $curl)
{
static::setCurlOptsFromRequest($curl, $request);
- curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout);
- curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0 < $this->maxRedirects);
- curl_setopt($curl, CURLOPT_MAXREDIRS, $this->maxRedirects);
- curl_setopt($curl, CURLOPT_FAILONERROR, !$this->ignoreErrors);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $this->verifyPeer);
- }
+ curl_setopt($curl, CURLOPT_TIMEOUT, $this->getTimeout());
+ curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0 < $this->getMaxRedirects());
+ curl_setopt($curl, CURLOPT_MAXREDIRS, $this->getMaxRedirects());
+ curl_setopt($curl, CURLOPT_FAILONERROR, !$this->getIgnoreErrors());
+ curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $this->getVerifyPeer());
- public function __destruct()
- {
- if (is_resource($this->curl)) {
- curl_close($this->curl);
- }
+ // finally apply the manually set options
+ curl_setopt_array($curl, $this->options);
}
}
View
1 lib/Buzz/Client/MultiCurl.php
@@ -6,6 +6,7 @@
class MultiCurl extends Curl implements BatchClientInterface
{
+ protected $curl;
protected $queue = array();
public function __construct()
View
15 test/Buzz/Test/Client/FunctionalTest.php
@@ -37,6 +37,21 @@ public function testRequestMethods($client, $method)
/**
* @dataProvider provideClient
*/
+ public function testGetContentType($client)
+ {
+ $request = new Request();
+ $request->fromUrl($_SERVER['TEST_SERVER']);
+ $response = new Response();
+ $client->send($request, $response);
+
+ $data = json_decode($response->getContent(), true);
+
+ $this->assertArrayNotHasKey('CONTENT_TYPE', $data['SERVER']);
+ }
+
+ /**
+ * @dataProvider provideClient
+ */
public function testFormPost($client)
{
$request = new FormRequest();

0 comments on commit 11dbe69

Please sign in to comment.