protected function iterateBuffer()
{
if (strlen($this->buffer) <= 1) {
return false;
}
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', [new Exception('Chunk length header longer then 1024 bytes')]);
$this->close();
return false;
}
if ($crlfPosition === false) {
return false;
// Chunk header hasn't completely come in yet
}
$this->nextChunkIsLength = false;
$lengthChunk = substr($this->buffer, 0, $crlfPosition);
if (strpos($lengthChunk, ';') !== false) {
list($lengthChunk) = explode(';', $lengthChunk, 2);
}
if (dechex(hexdec($lengthChunk)) !== $lengthChunk) {
$this->emit('error', [new Exception('Unable to validate "' . $lengthChunk . '" as chunk length header')]);
$this->close();
return false;
}
$this->remainingLength = hexdec($lengthChunk);
$this->buffer = substr($this->buffer, $crlfPosition + 2);
return true;
}
if ($this->remainingLength > 0) {
$chunkLength = $this->getChunkLength();
if ($chunkLength === 0) {
return true;
}
$this->emit('data', array(substr($this->buffer, 0, $chunkLength), $this));
$this->remainingLength -= $chunkLength;
$this->buffer = substr($this->buffer, $chunkLength);
return true;
}
$this->nextChunkIsLength = true;
$this->buffer = substr($this->buffer, 2);
return true;
}