Skip to content

Commit

Permalink
Merge pull request #73 from maciejmrozinski/chunk_size_leading_zeros
Browse files Browse the repository at this point in the history
Trim leading zeros from chunk size, Varnish Cache problem
  • Loading branch information
WyriHaximus authored Mar 1, 2017
2 parents ec3f739 + 3c9ab0a commit 3a90b2a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/ChunkedStreamDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ protected function iterateBuffer()
if (strpos($lengthChunk, ';') !== false) {
list($lengthChunk) = explode(';', $lengthChunk, 2);
}
if ($lengthChunk !== '') {
$lengthChunk = ltrim($lengthChunk, "0");
if ($lengthChunk === '') {
$lengthChunk = "0";
}
}
if (dechex(hexdec($lengthChunk)) !== $lengthChunk) {
$this->emit('error', [
new Exception('Unable to validate "' . $lengthChunk . '" as chunk length header'),
Expand Down
33 changes: 32 additions & 1 deletion tests/DecodeChunkedStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,37 @@ public function provideChunkedEncoding()
["6\r\nWi\r\n", "ki\r\n5\r\npedia\r\ne\r\n in\r\n\r\nchunks.\r\n0\r\n\r\n"],
"Wi\r\nkipedia in\r\n\r\nchunks."
],
'varnish-type-response-1' => [
["0017\r\nWikipedia in\r\n\r\nchunks.\r\n0\r\n\r\n"]
],
'varnish-type-response-2' => [
["000017\r\nWikipedia in\r\n\r\nchunks.\r\n0\r\n\r\n"]
],
'varnish-type-response-3' => [
["017\r\nWikipedia in\r\n\r\nchunks.\r\n0\r\n\r\n"]
],
'varnish-type-response-4' => [
["004\r\nWiki\r\n005\r\npedia\r\n00e\r\n in\r\n\r\nchunks.\r\n0\r\n\r\n"]
],
'varnish-type-response-5' => [
["000004\r\nWiki\r\n00005\r\npedia\r\n000e\r\n in\r\n\r\nchunks.\r\n0\r\n\r\n"]
],
'varnish-type-response-extra-line' => [
["006\r\nWi\r\nki\r\n005\r\npedia\r\n00e\r\n in\r\n\r\nchunks.\r\n0\r\n\r\n"],
"Wi\r\nkipedia in\r\n\r\nchunks."
],
'varnish-type-response-random' => [
[str_repeat("0", rand(0, 10)), "4\r\nWiki\r\n", str_repeat("0", rand(0, 10)), "5\r\npedia\r\n", str_repeat("0", rand(0, 10)), "e\r\n in\r\n\r\nchunks.\r\n0\r\n\r\n"]
],
'end-chunk-zero-check-1' => [
["4\r\nWiki\r\n5\r\npedia\r\ne\r\n in\r\n\r\nchunks.\r\n00\r\n\r\n"]
],
'end-chunk-zero-check-2' => [
["4\r\nWiki\r\n5\r\npedia\r\ne\r\n in\r\n\r\nchunks.\r\n000\r\n\r\n"]
],
'end-chunk-zero-check-3' => [
["00004\r\nWiki\r\n005\r\npedia\r\ne\r\n in\r\n\r\nchunks.\r\n0000\r\n\r\n"]
]
];
}

Expand Down Expand Up @@ -78,7 +109,7 @@ public function provideInvalidChunkedEncoding()
],
'header-chunk-to-long' => [
str_split(str_repeat('a', 2015) . "\r\nWi\r\nki\r\n5\r\npedia\r\ne\r\n in\r\n\r\nchunks.\r\n0\r\n\r\n")
],
]
];
}

Expand Down

0 comments on commit 3a90b2a

Please sign in to comment.