diff --git a/src/ChunkedDecoder.php b/src/ChunkedDecoder.php index a4bc6a0c..1d3bc16f 100644 --- a/src/ChunkedDecoder.php +++ b/src/ChunkedDecoder.php @@ -72,7 +72,7 @@ public function close() public function handleEnd() { if (!$this->closed) { - $this->handleError(new \Exception('Unexpected `end` event')); + $this->handleError(new \Exception('Unexpected end event')); } } @@ -90,17 +90,17 @@ public function handleData($data) while ($this->buffer !== '') { if (!$this->headerCompleted) { - $positionClrf = strpos($this->buffer, static::CRLF); + $positionCrlf = strpos($this->buffer, static::CRLF); - if ($positionClrf === false) { + if ($positionCrlf === false) { // Header shouldn't be bigger than 1024 bytes if (isset($this->buffer[static::MAX_CHUNK_HEADER_SIZE])) { - $this->handleError(new \Exception('Chunk header size inclusive extension bigger than 1024 bytes')); + $this->handleError(new \Exception('Chunk header size inclusive extension bigger than' . static::MAX_CHUNK_HEADER_SIZE. ' bytes')); } return; } - $header = strtolower((string)substr($this->buffer, 0, $positionClrf)); + $header = strtolower((string)substr($this->buffer, 0, $positionCrlf)); $hexValue = $header; if (strpos($header, ';') !== false) { @@ -114,7 +114,7 @@ public function handleData($data) return; } - $this->buffer = (string)substr($this->buffer, $positionClrf + 2); + $this->buffer = (string)substr($this->buffer, $positionCrlf + 2); $this->headerCompleted = true; if ($this->buffer === '') { return; @@ -129,7 +129,9 @@ public function handleData($data) $this->buffer = (string)substr($this->buffer, strlen($chunk)); } - if (strpos($this->buffer, static::CRLF) === 0) { + $positionCrlf = strpos($this->buffer, static::CRLF); + + if ($positionCrlf === 0) { if ($this->chunkSize === 0) { $this->emit('end'); $this->close(); @@ -139,11 +141,15 @@ public function handleData($data) $this->headerCompleted = false; $this->transferredSize = 0; $this->buffer = (string)substr($this->buffer, 2); - } else if ($this->chunkSize === $this->transferredSize && strlen($this->buffer) > 2) { + } + + if ($positionCrlf !== 0 && $this->chunkSize === $this->transferredSize && strlen($this->buffer) > 2) { // the first 2 characters are not CLRF, send error event $this->handleError(new \Exception('Chunk does not end with a CLRF')); return; - } else if (strlen($this->buffer) < 2) { + } + + if ($positionCrlf !== 0 && strlen($this->buffer) < 2) { // No CLRF found, wait for additional data which could be a CLRF return; }