Skip to content
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
7 changes: 7 additions & 0 deletions src/ChunkedDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ public function handleData($data)
$hexValue = $array[0];
}

if ($hexValue !== '') {
$hexValue = ltrim($hexValue, "0");
if ($hexValue === '') {
$hexValue = "0";
}
}

$this->chunkSize = hexdec($hexValue);
if (dechex($this->chunkSize) !== $hexValue) {
$this->handleError(new \Exception($hexValue . ' is not a valid hexadecimal number'));
Expand Down
51 changes: 51 additions & 0 deletions tests/ChunkedDecoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,4 +396,55 @@ public function testOutputStreamCanCloseInputStream()

$this->assertFalse($input->isReadable());
}

public function testLeadingZerosWillBeIgnored()
{
$this->parser->on('data', $this->expectCallableConsecutive(2, array('hello', 'hello world')));
$this->parser->on('error', $this->expectCallableNever());
$this->parser->on('end', $this->expectCallableNever());
$this->parser->on('close', $this->expectCallableNever());

$this->input->emit('data', array("00005\r\nhello\r\n"));
$this->input->emit('data', array("0000b\r\nhello world\r\n"));
}

public function testLeadingZerosInEndChunkWillBeIgnored()
{
$this->parser->on('data', $this->expectCallableNever());
$this->parser->on('error', $this->expectCallableNever());
$this->parser->on('end', $this->expectCallableOnce());
$this->parser->on('close', $this->expectCallableOnce());

$this->input->emit('data', array("0000\r\n\r\n"));
}

public function testLeadingZerosInInvalidChunk()
{
$this->parser->on('data', $this->expectCallableNever());
$this->parser->on('error', $this->expectCallableOnce());
$this->parser->on('end', $this->expectCallableNever());
$this->parser->on('close', $this->expectCallableOnce());

$this->input->emit('data', array("0000hello\r\n\r\n"));
}

public function testEmptyHeaderLeadsToError()
{
$this->parser->on('data', $this->expectCallableNever());
$this->parser->on('error', $this->expectCallableOnce());
$this->parser->on('end', $this->expectCallableNever());
$this->parser->on('close', $this->expectCallableOnce());

$this->input->emit('data', array("\r\n\r\n"));
}

public function testEmptyHeaderAndFilledBodyLeadsToError()
{
$this->parser->on('data', $this->expectCallableNever());
$this->parser->on('error', $this->expectCallableOnce());
$this->parser->on('end', $this->expectCallableNever());
$this->parser->on('close', $this->expectCallableOnce());

$this->input->emit('data', array("\r\nhello\r\n"));
}
}