private function dechunk()
{
if ($this->chunkLenRemaining !== null) {
goto dechunk;
}
determine_chunk_size:
if (false === ($lineEndPos = strpos($this->buffer, "\r\n"))) {
goto more_data_needed;
} elseif ($lineEndPos === 0) {
throw new ParseException($this->getParsedMessageArray(), $msg = 'Invalid new line; hexadecimal chunk size expected', $code = 400, $previousException = null);
}
$line = substr($this->buffer, 0, $lineEndPos);
$hex = strtolower(trim(ltrim($line, '0'))) ?: 0;
$dec = hexdec($hex);
if ($hex == dechex($dec)) {
$this->chunkLenRemaining = $dec;
} else {
throw new ParseException($this->getParsedMessageArray(), $msg = 'Invalid hexadecimal chunk size', $code = 400, $previousException = null);
}
$this->buffer = substr($this->buffer, $lineEndPos + 2);
if (!$dec) {
return true;
}
dechunk:
$bufferLen = strlen($this->buffer);
// These first two (extreme) edge cases prevent errors where the packet boundary ends after
// the \r and before the \n at the end of a chunk.
if ($bufferLen === $this->chunkLenRemaining) {
goto more_data_needed;
} elseif ($bufferLen === $this->chunkLenRemaining + 1) {
goto more_data_needed;
} elseif ($bufferLen >= $this->chunkLenRemaining + 2) {
$chunk = substr($this->buffer, 0, $this->chunkLenRemaining);
$this->buffer = substr($this->buffer, $this->chunkLenRemaining + 2);
$this->chunkLenRemaining = null;
$this->addToBody($chunk);
goto determine_chunk_size;
} else {
$this->addToBody($this->buffer);
$this->buffer = '';
$this->chunkLenRemaining -= $bufferLen;
goto more_data_needed;
}
more_data_needed:
return false;
}