protected static function getLine(StreamInterface $stream)
{
$line = '';
$crFound = false;
while (!$stream->eof()) {
$char = $stream->read(1);
if ($crFound && $char === self::LF) {
$crFound = false;
break;
}
// CR NOT followed by LF
if ($crFound && $char !== self::LF) {
throw new UnexpectedValueException('Unexpected carriage return detected');
}
// LF in isolation
if (!$crFound && $char === self::LF) {
throw new UnexpectedValueException('Unexpected line feed detected');
}
// CR found; do not append
if ($char === self::CR) {
$crFound = true;
continue;
}
// Any other character: append
$line .= $char;
}
// CR found at end of stream
if ($crFound) {
throw new UnexpectedValueException("Unexpected end of headers");
}
return $line;
}