Skip to content

Commit

Permalink
Respect content-length to prevent indefinite buffering
Browse files Browse the repository at this point in the history
  • Loading branch information
jmoo committed Jun 23, 2018
1 parent 46ffa86 commit a9c27ec
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Io/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,14 @@ public function bufferResponse(ResponseInterface $response)

// buffer stream and resolve with buffered body
$messageFactory = $this->messageFactory;
return Stream\buffer($stream)->then(

if ($response->hasHeader('content-length')) {
$maxLength = intval($response->getHeaderLine('content-length'));
} else {
$maxLength = null;
}

return Stream\buffer($stream, $maxLength)->then(
function ($body) use ($response, $messageFactory) {
return $response->withBody($messageFactory->body($body));
},
Expand Down
28 changes: 28 additions & 0 deletions tests/Io/TransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,34 @@ public function testReceivingStreamingBodyWillResolveWithBufferedResponseByDefau
$this->assertEquals('hello world', (string)$response->getBody());
}

public function testReceivingStreamingBodyWillResolveWithBufferedResponseWhenContentLengthIsReached()
{
$messageFactory = new MessageFactory();
$loop = Factory::create();

$stream = new ThroughStream();
$loop->addTimer(0.001, function () use ($stream) {
$stream->emit('data', array('hello world'));
});

$request = $this->getMockBuilder('Psr\Http\Message\RequestInterface')->getMock();

$headers = array('Content-Length' => '11');
$response = $messageFactory->response(1.1, 200, 'OK', $headers, $stream);

// mock sender to resolve promise with the given $response in response to the given $request
$sender = $this->makeSenderMock();
$sender->expects($this->once())->method('send')->with($this->equalTo($request))->willReturn(Promise\resolve($response));

$transaction = new Transaction($request, $sender, array(), $messageFactory);
$promise = $transaction->send();

$response = Block\await($promise, $loop);

$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('hello world', (string)$response->getBody());
}

/**
* @expectedException RuntimeException
*/
Expand Down

0 comments on commit a9c27ec

Please sign in to comment.