Skip to content

Commit

Permalink
control connection auto close on error
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangut committed Apr 21, 2017
1 parent 175274c commit d8a28e1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
35 changes: 32 additions & 3 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ class Client
*/
private $transport;

/**
* Auto close on error.
*
* @var bool
*/
private $closeOnError = true;

/**
* @param TransportInterface|null $transport
*/
Expand Down Expand Up @@ -61,6 +68,26 @@ public function getTransport()
return $this->transport;
}

/**
* Set automatic connection close on error.
*
* @param bool $closeOnError
*/
public function setCloseOnError($closeOnError)
{
$this->closeOnError = $closeOnError === true;
}

/**
* Check if automatic connection close on error is active.
*
* @return bool
*/
public function isCloseOnError()
{
return $this->closeOnError;
}

/**
* Run PSR7 request.
*
Expand Down Expand Up @@ -90,7 +117,9 @@ public function request(
$flags
);
} catch (TransportException $exception) {
$transport->close();
if ($this->closeOnError) {
$transport->close();
}

// Bubble exception
throw $exception;
Expand Down Expand Up @@ -141,7 +170,7 @@ protected function getTransferHeaders(array $transferHeaders, $transferContent,
];

foreach ($transferHeaders as $header) {
if (preg_match('/^HTTP\/(1\.\d) +([1-5][0-9]{2}) +.+$/', $header, $matches)) {
if (preg_match('/^HTTP\/(1\.\d) +([1-5]\d{2}) +.+$/', $header, $matches)) {
$responseHeaders['Protocol-Version'] = $matches[1];
$responseHeaders['Status'] = $matches[2];
} elseif (strpos($header, ':') !== false) {
Expand Down Expand Up @@ -178,7 +207,7 @@ protected function populateResponse(ResponseInterface $response, array $headers,
$response = $response->withHeader($name, (string) $value);
}

$body = new Stream('php://temp', 'r+');
$body = new Stream('php://temp', 'wb+');
$body->write($content);

return $response->withBody($body);
Expand Down
2 changes: 0 additions & 2 deletions src/Transport/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,6 @@ public function request($method, $uri, array $headers = [], array $vars = [], ar
array_key_exists($errCode, static::$errorCategoryMap) ? static::$errorCategoryMap [$errCode] : ''
);

$this->close();

throw $exception;
}

Expand Down
13 changes: 12 additions & 1 deletion tests/Spiral/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
class ClientTest extends \PHPUnit_Framework_TestCase
{
public function testGettersSetters()
public function testTransport()
{
/* @var TransportInterface $transport */
$transport = $this->getMockBuilder(TransportInterface::class)
Expand All @@ -35,6 +35,17 @@ public function testGettersSetters()
static::assertEquals($transport, $client->getTransport());
}

public function testAutoClose()
{
$client = new Client();

static::assertTrue($client->isCloseOnError());

$client->setCloseOnError(false);

static::assertFalse($client->isCloseOnError());
}

public function testBadRequest()
{
$request = new Request('http://fake_made_up_web.com', 'GET');
Expand Down

0 comments on commit d8a28e1

Please sign in to comment.