Skip to content

Commit

Permalink
Merge pull request #71 from WyriHaximus/fix-for-67-3rd-round
Browse files Browse the repository at this point in the history
Ensure checking for 0 length chunk, when we should check for it
  • Loading branch information
WyriHaximus authored Dec 2, 2016
2 parents f26f91c + 26f43a2 commit 4428d56
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
17 changes: 10 additions & 7 deletions src/ChunkedStreamDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ protected function iterateBuffer()
}

if ($this->nextChunkIsLength) {
if (substr($this->buffer, 0, 3) === "0\r\n") {
// We've reached the end of the stream
$this->reachedEnd = true;
$this->emit('end');
$this->close();
return false;
}

$crlfPosition = strpos($this->buffer, static::CRLF);
if ($crlfPosition === false && strlen($this->buffer) > 1024) {
$this->emit('error', [
Expand Down Expand Up @@ -131,13 +139,6 @@ protected function iterateBuffer()

$this->nextChunkIsLength = true;
$this->buffer = substr($this->buffer, 2);

if (substr($this->buffer, 0, 3) === "0\r\n") {
$this->reachedEnd = true;
$this->emit('end');
$this->close();
return false;
}
return true;
}

Expand Down Expand Up @@ -183,6 +184,8 @@ public function close()
/** @internal */
public function handleEnd()
{
$this->handleData('');

if ($this->closed) {
return;
}
Expand Down
29 changes: 27 additions & 2 deletions tests/DecodeChunkedStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public function testChunkedEncoding(array $strings, $expected = "Wikipedia in\r\
$response->on('data', function ($data) use (&$buffer) {
$buffer .= $data;
});
$response->on('error', function (Exception $exception) {
throw $exception;
$response->on('error', function ($error) {
$this->fail((string)$error);
});
foreach ($strings as $string) {
$stream->write($string);
Expand Down Expand Up @@ -104,6 +104,9 @@ public function testHandleEnd()
$ended = false;
$stream = new ThroughStream();
$response = new ChunkedStreamDecoder($stream);
$response->on('error', function ($error) {
$this->fail((string)$error);
});
$response->on('end', function () use (&$ended) {
$ended = true;
});
Expand Down Expand Up @@ -132,6 +135,9 @@ public function testHandleEndTrailers()
$ended = false;
$stream = new ThroughStream();
$response = new ChunkedStreamDecoder($stream);
$response->on('error', function ($error) {
$this->fail((string)$error);
});
$response->on('end', function () use (&$ended) {
$ended = true;
});
Expand All @@ -140,4 +146,23 @@ public function testHandleEndTrailers()

$this->assertTrue($ended);
}

public function testHandleEndEnsureNoError()
{
$ended = false;
$stream = new ThroughStream();
$response = new ChunkedStreamDecoder($stream);
$response->on('error', function ($error) {
$this->fail((string)$error);
});
$response->on('end', function () use (&$ended) {
$ended = true;
});

$stream->write("4\r\nWiki\r\n");
$stream->write("0\r\n\r\n");
$stream->end();

$this->assertTrue($ended);
}
}

0 comments on commit 4428d56

Please sign in to comment.