From 61bf248481da1101d432a57a9c2748a8af7016e0 Mon Sep 17 00:00:00 2001 From: danil zakablukovskii Date: Sat, 15 Apr 2017 19:58:47 +0200 Subject: [PATCH 1/2] catch Guzzle parser exception --- src/Request.php | 8 +++++++- tests/RequestTest.php | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/Request.php b/src/Request.php index 3f53ceb..7f5ff8e 100644 --- a/src/Request.php +++ b/src/Request.php @@ -132,7 +132,13 @@ public function handleData($data) $this->buffer .= $data; if (false !== strpos($this->buffer, "\r\n\r\n")) { - list($response, $bodyChunk) = $this->parseResponse($this->buffer); + try { + list($response, $bodyChunk) = $this->parseResponse($this->buffer); + } catch (\InvalidArgumentException $exception) { + $this->emit('error', [$exception, $this]); + + return; + } $this->buffer = null; diff --git a/tests/RequestTest.php b/tests/RequestTest.php index 3008f14..9142334 100644 --- a/tests/RequestTest.php +++ b/tests/RequestTest.php @@ -220,6 +220,28 @@ public function requestShouldEmitErrorIfConnectionEmitsError() $request->handleError(new \Exception('test')); } + /** @test */ + public function requestShouldEmitErrorIfGuzzleParseThrowsException() + { + $requestData = new RequestData('GET', 'http://www.example.com'); + $request = new Request($this->connector, $requestData); + + $this->successfulConnectionMock(); + + $handler = $this->createCallableMock(); + $handler->expects($this->once()) + ->method('__invoke') + ->with( + $this->isInstanceOf('\InvalidArgumentException'), + $this->isInstanceOf('React\HttpClient\Request') + ); + + $request->on('error', $handler); + + $request->writeHead(); + $request->handleData("\r\n\r\n"); + } + /** * @test * @expectedException Exception From 1e683d340052c6ed9f8dd9b230184be521bf07ea Mon Sep 17 00:00:00 2001 From: danil zakablukovskii Date: Fri, 28 Apr 2017 13:46:02 +0200 Subject: [PATCH 2/2] unsubscribe from the stream --- src/Request.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Request.php b/src/Request.php index 7f5ff8e..714eefc 100644 --- a/src/Request.php +++ b/src/Request.php @@ -136,8 +136,6 @@ public function handleData($data) list($response, $bodyChunk) = $this->parseResponse($this->buffer); } catch (\InvalidArgumentException $exception) { $this->emit('error', [$exception, $this]); - - return; } $this->buffer = null; @@ -147,6 +145,10 @@ public function handleData($data) $this->stream->removeListener('end', array($this, 'handleEnd')); $this->stream->removeListener('error', array($this, 'handleError')); + if (!isset($response)) { + return; + } + $this->response = $response; $response->on('end', function () {