/
CookieCollection.php
406 lines (369 loc) · 12.2 KB
/
CookieCollection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.5.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Http\Cookie;
use ArrayIterator;
use Cake\Http\Client\Response as ClientResponse;
use Countable;
use DateTimeImmutable;
use DateTimeZone;
use InvalidArgumentException;
use IteratorAggregate;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
/**
* Cookie Collection
*
* Provides an immutable collection of cookies objects. Adding or removing
* to a collection returns a *new* collection that you must retain.
*/
class CookieCollection implements IteratorAggregate, Countable
{
/**
* Cookie objects
*
* @var \Cake\Http\Cookie\CookieInterface[]
*/
protected $cookies = [];
/**
* Constructor
*
* @param array $cookies Array of cookie objects
*/
public function __construct(array $cookies = [])
{
$this->checkCookies($cookies);
foreach ($cookies as $cookie) {
$this->cookies[$cookie->getId()] = $cookie;
}
}
/**
* Create a Cookie Collection from an array of Set-Cookie Headers
*
* @param array $header The array of set-cookie header values.
* @return static
*/
public static function createFromHeader(array $header)
{
$cookies = static::parseSetCookieHeader($header);
return new static($cookies);
}
/**
* Create a new collection from the cookies in a ServerRequest
*
* @param \Psr\Http\Message\ServerRequestInterface $request The request to extract cookie data from
* @return static
*/
public static function createFromServerRequest(ServerRequestInterface $request)
{
$data = $request->getCookieParams();
$cookies = [];
foreach ($data as $name => $value) {
$cookies[] = new Cookie($name, $value);
}
return new static($cookies);
}
/**
* Get the number of cookies in the collection.
*
* @return int
*/
public function count()
{
return count($this->cookies);
}
/**
* Add a cookie and get an updated collection.
*
* Cookies are stored by id. This means that there can be duplicate
* cookies if a cookie collection is used for cookies across multiple
* domains. This can impact how get(), has() and remove() behave.
*
* @param \Cake\Http\Cookie\CookieInterface $cookie Cookie instance to add.
* @return static
*/
public function add(CookieInterface $cookie)
{
$new = clone $this;
$new->cookies[$cookie->getId()] = $cookie;
return $new;
}
/**
* Get the first cookie by name.
*
* @param string $name The name of the cookie.
* @return \Cake\Http\Cookie\CookieInterface|null
*/
public function get($name)
{
$key = mb_strtolower($name);
foreach ($this->cookies as $cookie) {
if (mb_strtolower($cookie->getName()) === $key) {
return $cookie;
}
}
return null;
}
/**
* Check if a cookie with the given name exists
*
* @param string $name The cookie name to check.
* @return bool True if the cookie exists, otherwise false.
*/
public function has($name)
{
$key = mb_strtolower($name);
foreach ($this->cookies as $cookie) {
if (mb_strtolower($cookie->getName()) === $key) {
return true;
}
}
return false;
}
/**
* Create a new collection with all cookies matching $name removed.
*
* If the cookie is not in the collection, this method will do nothing.
*
* @param string $name The name of the cookie to remove.
* @return static
*/
public function remove($name)
{
$new = clone $this;
$key = mb_strtolower($name);
foreach ($new->cookies as $i => $cookie) {
if (mb_strtolower($cookie->getName()) === $key) {
unset($new->cookies[$i]);
}
}
return $new;
}
/**
* Checks if only valid cookie objects are in the array
*
* @param array $cookies Array of cookie objects
* @return void
* @throws \InvalidArgumentException
*/
protected function checkCookies(array $cookies)
{
foreach ($cookies as $index => $cookie) {
if (!$cookie instanceof CookieInterface) {
throw new InvalidArgumentException(
sprintf(
'Expected `%s[]` as $cookies but instead got `%s` at index %d',
static::class,
getTypeName($cookie),
$index
)
);
}
}
}
/**
* Gets the iterator
*
* @return \ArrayIterator
*/
public function getIterator()
{
return new ArrayIterator($this->cookies);
}
/**
* Add cookies that match the path/domain/expiration to the request.
*
* This allows CookieCollections to be used as a 'cookie jar' in an HTTP client
* situation. Cookies that match the request's domain + path that are not expired
* when this method is called will be applied to the request.
*
* @param \Psr\Http\Message\RequestInterface $request The request to update.
* @param array $extraCookies Associative array of additional cookies to add into the request. This
* is useful when you have cookie data from outside the collection you want to send.
* @return \Psr\Http\Message\RequestInterface An updated request.
*/
public function addToRequest(RequestInterface $request, array $extraCookies = [])
{
$uri = $request->getUri();
$cookies = $this->findMatchingCookies(
$uri->getScheme(),
$uri->getHost(),
$uri->getPath() ?: '/'
);
$cookies = array_merge($cookies, $extraCookies);
$cookiePairs = [];
foreach ($cookies as $key => $value) {
$cookiePairs[] = sprintf("%s=%s", rawurlencode($key), rawurlencode($value));
}
if (empty($cookiePairs)) {
return $request;
}
return $request->withHeader('Cookie', implode('; ', $cookiePairs));
}
/**
* Find cookies matching the scheme, host, and path
*
* @param string $scheme The http scheme to match
* @param string $host The host to match.
* @param string $path The path to match
* @return array An array of cookie name/value pairs
*/
protected function findMatchingCookies($scheme, $host, $path)
{
$out = [];
$now = new DateTimeImmutable('now', new DateTimeZone('UTC'));
foreach ($this->cookies as $cookie) {
if ($scheme === 'http' && $cookie->isSecure()) {
continue;
}
if (strpos($path, $cookie->getPath()) !== 0) {
continue;
}
$domain = $cookie->getDomain();
$leadingDot = substr($domain, 0, 1) === '.';
if ($leadingDot) {
$domain = ltrim($domain, '.');
}
if ($cookie->isExpired($now)) {
continue;
}
$pattern = '/' . preg_quote($domain, '/') . '$/';
if (!preg_match($pattern, $host)) {
continue;
}
$out[$cookie->getName()] = $cookie->getValue();
}
return $out;
}
/**
* Create a new collection that includes cookies from the response.
*
* @param \Psr\Http\Message\ResponseInterface $response Response to extract cookies from.
* @param \Psr\Http\Message\RequestInterface $request Request to get cookie context from.
* @return static
*/
public function addFromResponse(ResponseInterface $response, RequestInterface $request)
{
$uri = $request->getUri();
$host = $uri->getHost();
$path = $uri->getPath() ?: '/';
$cookies = static::parseSetCookieHeader($response->getHeader('Set-Cookie'));
$cookies = $this->setRequestDefaults($cookies, $host, $path);
$new = clone $this;
foreach ($cookies as $cookie) {
$new->cookies[$cookie->getId()] = $cookie;
}
$new->removeExpiredCookies($host, $path);
return $new;
}
/**
* Apply path and host to the set of cookies if they are not set.
*
* @param array $cookies An array of cookies to update.
* @param string $host The host to set.
* @param string $path The path to set.
* @return array An array of updated cookies.
*/
protected function setRequestDefaults(array $cookies, $host, $path)
{
$out = [];
foreach ($cookies as $name => $cookie) {
if (!$cookie->getDomain()) {
$cookie = $cookie->withDomain($host);
}
if (!$cookie->getPath()) {
$cookie = $cookie->withPath($path);
}
$out[] = $cookie;
}
return $out;
}
/**
* Parse Set-Cookie headers into array
*
* @param array $values List of Set-Cookie Header values.
* @return \Cake\Http\Cookie\Cookie[] An array of cookie objects
*/
protected static function parseSetCookieHeader($values)
{
$cookies = [];
foreach ($values as $value) {
$value = rtrim($value, ';');
$parts = preg_split('/\;[ \t]*/', $value);
$name = false;
$cookie = [
'value' => '',
'path' => '',
'domain' => '',
'secure' => false,
'httponly' => false,
'expires' => null,
'max-age' => null
];
foreach ($parts as $i => $part) {
if (strpos($part, '=') !== false) {
list($key, $value) = explode('=', $part, 2);
} else {
$key = $part;
$value = true;
}
if ($i === 0) {
$name = $key;
$cookie['value'] = urldecode($value);
continue;
}
$key = strtolower($key);
if (array_key_exists($key, $cookie) && !strlen($cookie[$key])) {
$cookie[$key] = $value;
}
}
$expires = null;
if ($cookie['max-age'] !== null) {
$expires = new DateTimeImmutable('@' . (time() + $cookie['max-age']));
} elseif ($cookie['expires']) {
$expires = new DateTimeImmutable('@' . strtotime($cookie['expires']));
}
$cookies[] = new Cookie(
$name,
$cookie['value'],
$expires,
$cookie['path'],
$cookie['domain'],
$cookie['secure'],
$cookie['httponly']
);
}
return $cookies;
}
/**
* Remove expired cookies from the collection.
*
* @param string $host The host to check for expired cookies on.
* @param string $path The path to check for expired cookies on.
* @return void
*/
protected function removeExpiredCookies($host, $path)
{
$time = new DateTimeImmutable('now', new DateTimeZone('UTC'));
$hostPattern = '/' . preg_quote($host, '/') . '$/';
foreach ($this->cookies as $i => $cookie) {
$expired = $cookie->isExpired($time);
$pathMatches = strpos($path, $cookie->getPath()) === 0;
$hostMatches = preg_match($hostPattern, $cookie->getDomain());
if ($pathMatches && $hostMatches && $expired) {
unset($this->cookies[$i]);
}
}
}
}