forked from cakephp/cakephp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCookieCollection.php
More file actions
120 lines (111 loc) · 3.52 KB
/
Copy pathCookieCollection.php
File metadata and controls
120 lines (111 loc) · 3.52 KB
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
<?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.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Network\Http;
use Cake\Network\Http\Response;
/**
* Container class for cookies used in Http\Client.
*
* Provides cookie jar like features for storing cookies between
* requests, as well as appending cookies to new requests.
*/
class CookieCollection
{
/**
* The cookies stored in this jar.
*
* @var array
*/
protected $_cookies = [];
/**
* Store the cookies from a response.
*
* Store the cookies that haven't expired. If a cookie has been expired
* and is currently stored, it will be removed.
*
* @param \Cake\Network\Http\Response $response The response to read cookies from
* @param string $url The request URL used for default host/path values.
* @return void
*/
public function store(Response $response, $url)
{
$host = parse_url($url, PHP_URL_HOST);
$path = parse_url($url, PHP_URL_PATH);
$path = $path ?: '/';
$cookies = $response->cookies();
foreach ($cookies as $name => $cookie) {
if (empty($cookie['domain'])) {
$cookie['domain'] = $host;
}
if (empty($cookie['path'])) {
$cookie['path'] = $path;
}
$key = implode(';', [$cookie['name'], $cookie['domain'], $cookie['path']]);
$expires = isset($cookie['expires']) ? $cookie['expires'] : false;
$expiresTime = false;
if ($expires) {
$expiresTime = strtotime($expires);
}
if ($expiresTime && $expiresTime <= time()) {
unset($this->_cookies[$key]);
continue;
}
$this->_cookies[$key] = $cookie;
}
}
/**
* Get stored cookies for a URL.
*
* Finds matching stored cookies and returns a simple array
* of name => value
*
* @param string $url The URL to find cookies for.
* @return array
*/
public function get($url)
{
$path = parse_url($url, PHP_URL_PATH) ?: '/';
$host = parse_url($url, PHP_URL_HOST);
$scheme = parse_url($url, PHP_URL_SCHEME);
$out = [];
foreach ($this->_cookies as $cookie) {
if ($scheme === 'http' && !empty($cookie['secure'])) {
continue;
}
if (strpos($path, $cookie['path']) !== 0) {
continue;
}
$leadingDot = $cookie['domain'][0] === '.';
if (!$leadingDot && $host !== $cookie['domain']) {
continue;
}
if ($leadingDot) {
$pattern = '/' . preg_quote(substr($cookie['domain'], 1), '/') . '$/';
if (!preg_match($pattern, $host)) {
continue;
}
}
$out[$cookie['name']] = $cookie['value'];
}
return $out;
}
/**
* Get all the stored cookies.
*
* @return array
*/
public function getAll()
{
return array_values($this->_cookies);
}
}