Skip to content

Ensure checking for 0 length chunk, when we should check for it #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
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") {
Copy link
Member

Choose a reason for hiding this comment

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

IMO it would be cleaner to parse this like a normal chunk header in order to support chunk extensions etc. as usual. But I guess this is good as-is because it already fixes a particular bug, so let's get this in first 👍

Copy link
Member Author

@WyriHaximus WyriHaximus Dec 2, 2016

Choose a reason for hiding this comment

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

Tests already check for end of body extensions: https://github.com/WyriHaximus/http-client/blob/26f43a22e23cc8dc879bbee0c392bbd56b1ce023/tests/DecodeChunkedStreamTest.php#L145 But does it could be a bit better, lets try to get that into v0.4.16, I'll get this out the door in v0.4.15.

// 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);
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy