Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we also unsubscribe from the stream as we do here https://github.com/djagya/http-client/blob/61bf248481da1101d432a57a9c2748a8af7016e0/src/Request.php#L145 since the response parsing has failed and nothing new coming from the stream could change that.

Ping @clue

}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I ask you to amend your changes to return within this block instead? :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I just wasn't sure if we need to clear the buffer and remove listeners if we emit an error.
Similar changes in react/http made me think that they must be removed/buffer must be cleared even if the response parsing was not successful.
Isn't that the case here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@djagya It looks like your analysis may be right and makes perfect sense here, sorry for the confusion 👍 Can you update this again?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@clue done, sorry for the delay


$this->buffer = null;

Expand Down
22 changes: 22 additions & 0 deletions tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down