public function readResponse(Socket $socket, float $timeout = 0) : \Generator
{
$buffer = new Buffer();
try {
do {
$buffer->push(yield from $socket->read(0, null, $timeout));
} while (false === ($position = $buffer->search("\r\n")) && $buffer->getLength() < $this->maxStartLineLength);
if (false === $position) {
throw new MessageException(Response::REQUEST_HEADER_TOO_LARGE, sprintf('Message start line exceeded maximum size of %d bytes.', $this->maxStartLineLength));
}
$line = $buffer->shift($position + 2);
if (!preg_match("/^HTTP\\/(\\d+(?:\\.\\d+)?) (\\d{3})(?: (.+))?\r\n\$/i", $line, $matches)) {
throw new ParseException('Could not parse start line.');
}
$protocol = $matches[1];
$code = (int) $matches[2];
$reason = isset($matches[3]) ? $matches[3] : '';
$headers = (yield from $this->readHeaders($buffer, $socket, $timeout));
} finally {
$socket->unshift((string) $buffer);
}
return new BasicResponse($code, $headers, $socket, $reason, $protocol);
}