public static function createFromRaw($rawResponse, Response $parentResponse = null)
{
$response = new static($parentResponse);
$lines = explode(chr(10), $rawResponse);
$statusLine = array_shift($lines);
if (substr($statusLine, 0, 5) !== 'HTTP/') {
throw new \InvalidArgumentException('The given raw HTTP message is not a valid response.', 1335175601);
}
list($version, $statusCode, $reasonPhrase) = explode(' ', $statusLine, 3);
$response->setVersion($version);
$response->setStatus((int) $statusCode, trim($reasonPhrase));
$parsingHeader = true;
$contentLines = [];
$headers = new Headers();
foreach ($lines as $line) {
if ($parsingHeader) {
if (trim($line) === '') {
$parsingHeader = false;
continue;
}
$fieldName = trim(substr($line, 0, strpos($line, ':')));
$fieldValue = trim(substr($line, strlen($fieldName) + 1));
if (strtoupper(substr($fieldName, 0, 10)) === 'SET-COOKIE') {
$cookie = Cookie::createFromRawSetCookieHeader($fieldValue);
if ($cookie !== null) {
$headers->setCookie($cookie);
}
} else {
$headers->set($fieldName, $fieldValue, false);
}
} else {
$contentLines[] = $line;
}
}
$content = implode(chr(10), $contentLines);
$response->setHeaders($headers);
$response->setContent($content);
return $response;
}