Creates a cookie (an instance of this class) by a provided
raw header string like "foo=507d9f20317a5; path=/; domain=.example.org"
This is is an implementation of the algorithm explained in RFC 6265, Section 5.2
A basic statement of this algorithm is to "ignore the set-cookie-string entirely"
in case a required condition is not met. In these cases this function will return NULL
rather than the created cookie.
public static createFromRawSetCookieHeader ( string $header ) : |
||
$header | string | The Set-Cookie string without the actual "Set-Cookie:" part |
Résultat |
/** * Creates a response from the given raw, that is plain text, HTTP response. * * @param string $rawResponse * @param Response $parentResponse Parent response, if called recursively * * @throws \InvalidArgumentException * @return Response */ 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; }
/** * @test */ public function createCookieFromRawSetsHttpOnlyIfPresent() { $cookie = Cookie::createFromRawSetCookieHeader('ckName=someValue; HttpOnly; more=nothing'); $this->assertTrue($cookie->isHttpOnly()); }