v1.4.0
This commit is contained in:
parent
916d7845be
commit
cc1bca74fa
1 changed files with 770 additions and 204 deletions
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace GuzzleHttp\Cookie;
|
||||
|
||||
use GuzzleHttp\Handler\HostValidator;
|
||||
use GuzzleHttp\Psr7;
|
||||
|
||||
/**
|
||||
|
|
@ -29,6 +30,11 @@ class SetCookie
|
|||
*/
|
||||
private $data;
|
||||
|
||||
/**
|
||||
* @var bool Whether this cookie was set without a Domain attribute
|
||||
*/
|
||||
private $hostOnly = false;
|
||||
|
||||
/**
|
||||
* Create a new SetCookie object from a string.
|
||||
*
|
||||
|
|
@ -76,6 +82,9 @@ class SetCookie
|
|||
continue 2;
|
||||
}
|
||||
}
|
||||
if (Psr7\Utils::caselessEquals('HostOnly', $key)) {
|
||||
continue;
|
||||
}
|
||||
$data[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
|
@ -90,6 +99,14 @@ class SetCookie
|
|||
{
|
||||
$this->data = self::$defaults;
|
||||
|
||||
if (\array_key_exists('HostOnly', $data)) {
|
||||
if (!\is_bool($data['HostOnly'])) {
|
||||
throw new \InvalidArgumentException('Cookie field "HostOnly" must be a boolean');
|
||||
}
|
||||
$this->setHostOnly($data['HostOnly']);
|
||||
unset($data['HostOnly']);
|
||||
}
|
||||
|
||||
if (isset($data['Name'])) {
|
||||
$this->setName($data['Name']);
|
||||
}
|
||||
|
|
@ -157,6 +174,9 @@ class SetCookie
|
|||
{
|
||||
$str = $this->data['Name'].'='.($this->data['Value'] ?? '').'; ';
|
||||
foreach ($this->data as $k => $v) {
|
||||
if ($k === 'Domain' && $this->getHostOnly()) {
|
||||
continue;
|
||||
}
|
||||
if ($k !== 'Name' && $k !== 'Value' && $v !== null && $v !== false) {
|
||||
if ($k === 'Expires') {
|
||||
$str .= 'Expires='.\gmdate('D, d M Y H:i:s \G\M\T', $v).'; ';
|
||||
|
|
@ -171,7 +191,12 @@ class SetCookie
|
|||
|
||||
public function toArray(): array
|
||||
{
|
||||
return $this->data;
|
||||
$data = $this->data;
|
||||
if ($this->getHostOnly()) {
|
||||
$data['HostOnly'] = true;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -246,6 +271,26 @@ class SetCookie
|
|||
$this->data['Domain'] = null === $domain ? null : (string) $domain;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get whether this cookie is scoped to the origin host only.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getHostOnly()
|
||||
{
|
||||
return $this->hostOnly;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether this cookie is scoped to the origin host only.
|
||||
*
|
||||
* @param bool $hostOnly Set to true for host-only cookies
|
||||
*/
|
||||
public function setHostOnly(bool $hostOnly): void
|
||||
{
|
||||
$this->hostOnly = $hostOnly;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the path.
|
||||
*
|
||||
|
|
@ -445,7 +490,11 @@ class SetCookie
|
|||
{
|
||||
$cookieDomain = $this->getDomain();
|
||||
if (null === $cookieDomain) {
|
||||
return true;
|
||||
return !$this->getHostOnly();
|
||||
}
|
||||
|
||||
if ($this->getHostOnly()) {
|
||||
return Psr7\Utils::asciiToLower($domain) === Psr7\Utils::asciiToLower($cookieDomain);
|
||||
}
|
||||
|
||||
// Remove the leading '.' as per spec in RFC 6265.
|
||||
|
|
@ -464,6 +513,12 @@ class SetCookie
|
|||
return true;
|
||||
}
|
||||
|
||||
// A percent-escaped cookie domain can decode to another host spelling.
|
||||
// Keep it exact-match-only to avoid extending that host's cookie scope.
|
||||
if (\strpos($cookieDomain, '%') !== false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// IP literals and numeric hosts are exact-match-only per RFC 6265.
|
||||
// Only the exact match above may succeed for those cookie domains.
|
||||
if (self::isIpAddressOrNumericHost($cookieDomain)) {
|
||||
|
|
@ -500,7 +555,14 @@ class SetCookie
|
|||
$labels = \explode('.', $host);
|
||||
$last = (string) \end($labels);
|
||||
|
||||
return $last !== '' && \ctype_digit($last);
|
||||
if ($last !== '' && \ctype_digit($last)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Apply the transport's decimal, octal and hexadecimal inet_aton-style
|
||||
// grammar. Omitting range checks conservatively holds some names to an
|
||||
// exact match.
|
||||
return HostValidator::isNumericIpv4Host(\rtrim($host, '.'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in a new issue