1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\Client\Plugin; |
4
|
|
|
|
5
|
|
|
use Http\Client\Exception\TransferException; |
6
|
|
|
use Http\Message\Cookie; |
7
|
|
|
use Http\Message\CookieJar; |
8
|
|
|
use Psr\Http\Message\RequestInterface; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Handle request cookies. |
13
|
|
|
* |
14
|
|
|
* @author Joel Wurtz <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class CookiePlugin implements Plugin |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Cookie storage. |
20
|
|
|
* |
21
|
|
|
* @var CookieJar |
22
|
|
|
*/ |
23
|
|
|
private $cookieJar; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param CookieJar $cookieJar |
27
|
|
|
*/ |
28
|
10 |
|
public function __construct(CookieJar $cookieJar) |
29
|
|
|
{ |
30
|
10 |
|
$this->cookieJar = $cookieJar; |
31
|
10 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
8 |
|
public function handleRequest(RequestInterface $request, callable $next, callable $first) |
37
|
1 |
|
{ |
38
|
8 |
|
foreach ($this->cookieJar->getCookies() as $cookie) { |
39
|
6 |
|
if ($cookie->isExpired()) { |
40
|
1 |
|
continue; |
41
|
|
|
} |
42
|
|
|
|
43
|
5 |
|
if (!$cookie->matchDomain($request->getUri()->getHost())) { |
44
|
1 |
|
continue; |
45
|
|
|
} |
46
|
|
|
|
47
|
4 |
|
if (!$cookie->matchPath($request->getUri()->getPath())) { |
48
|
1 |
|
continue; |
49
|
|
|
} |
50
|
|
|
|
51
|
3 |
|
if ($cookie->isSecure() && ($request->getUri()->getScheme() !== 'https')) { |
52
|
1 |
|
continue; |
53
|
|
|
} |
54
|
|
|
|
55
|
2 |
|
$request = $request->withAddedHeader('Cookie', sprintf('%s=%s', $cookie->getName(), $cookie->getValue())); |
56
|
8 |
|
} |
57
|
|
|
|
58
|
8 |
|
return $next($request)->then(function (ResponseInterface $response) use ($request) { |
59
|
2 |
|
if ($response->hasHeader('Set-Cookie')) { |
60
|
2 |
|
$setCookies = $response->getHeader('Set-Cookie'); |
61
|
|
|
|
62
|
2 |
|
foreach ($setCookies as $setCookie) { |
63
|
2 |
|
$cookie = $this->createCookie($request, $setCookie); |
64
|
|
|
|
65
|
|
|
// Cookie invalid do not use it |
66
|
1 |
|
if (null === $cookie) { |
67
|
|
|
continue; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// Restrict setting cookie from another domain |
71
|
1 |
|
if (false === strpos($cookie->getDomain(), $request->getUri()->getHost())) { |
72
|
|
|
continue; |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
$this->cookieJar->addCookie($cookie); |
76
|
1 |
|
} |
77
|
1 |
|
} |
78
|
|
|
|
79
|
1 |
|
return $response; |
80
|
8 |
|
}); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Creates a cookie from a string. |
85
|
|
|
* |
86
|
|
|
* @param RequestInterface $request |
87
|
|
|
* @param $setCookie |
88
|
|
|
* |
89
|
|
|
* @return Cookie|null |
90
|
|
|
* |
91
|
|
|
* @throws \Http\Client\Exception\TransferException |
92
|
|
|
*/ |
93
|
2 |
|
private function createCookie(RequestInterface $request, $setCookie) |
94
|
|
|
{ |
95
|
2 |
|
$parts = array_map('trim', explode(';', $setCookie)); |
96
|
|
|
|
97
|
2 |
|
if (empty($parts) || !strpos($parts[0], '=')) { |
98
|
|
|
return; |
99
|
|
|
} |
100
|
|
|
|
101
|
2 |
|
list($name, $cookieValue) = $this->createValueKey(array_shift($parts)); |
102
|
|
|
|
103
|
2 |
|
$maxAge = null; |
104
|
2 |
|
$expires = null; |
105
|
2 |
|
$domain = $request->getUri()->getHost(); |
106
|
2 |
|
$path = $request->getUri()->getPath(); |
107
|
2 |
|
$secure = false; |
108
|
2 |
|
$httpOnly = false; |
109
|
|
|
|
110
|
|
|
// Add the cookie pieces into the parsed data array |
111
|
2 |
|
foreach ($parts as $part) { |
112
|
2 |
|
list($key, $value) = $this->createValueKey($part); |
113
|
|
|
|
114
|
2 |
|
switch (strtolower($key)) { |
115
|
2 |
|
case 'expires': |
116
|
2 |
|
$expires = \DateTime::createFromFormat(\DateTime::COOKIE, $value); |
117
|
|
|
|
118
|
2 |
|
if (true !== ($expires instanceof \DateTime)) { |
119
|
1 |
|
throw new TransferException( |
120
|
1 |
|
sprintf( |
121
|
1 |
|
'Cookie header `%s` expires value `%s` could not be converted to date', |
122
|
1 |
|
$name, |
123
|
|
|
$value |
124
|
1 |
|
) |
125
|
1 |
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
1 |
|
break; |
129
|
|
|
|
130
|
1 |
|
case 'max-age': |
131
|
1 |
|
$maxAge = (int) $value; |
132
|
1 |
|
break; |
133
|
|
|
|
134
|
1 |
|
case 'domain': |
135
|
1 |
|
$domain = $value; |
136
|
1 |
|
break; |
137
|
|
|
|
138
|
1 |
|
case 'path': |
139
|
1 |
|
$path = $value; |
140
|
1 |
|
break; |
141
|
|
|
|
142
|
1 |
|
case 'secure': |
143
|
1 |
|
$secure = true; |
144
|
1 |
|
break; |
145
|
|
|
|
146
|
1 |
|
case 'httponly': |
147
|
1 |
|
$httpOnly = true; |
148
|
1 |
|
break; |
149
|
1 |
|
} |
150
|
1 |
|
} |
151
|
|
|
|
152
|
1 |
|
return new Cookie($name, $cookieValue, $maxAge, $domain, $path, $secure, $httpOnly, $expires); |
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Separates key/value pair from cookie. |
157
|
|
|
* |
158
|
|
|
* @param $part |
159
|
|
|
* |
160
|
|
|
* @return array |
161
|
|
|
*/ |
162
|
2 |
|
private function createValueKey($part) |
163
|
|
|
{ |
164
|
2 |
|
$parts = explode('=', $part, 2); |
165
|
2 |
|
$key = trim($parts[0]); |
166
|
2 |
|
$value = isset($parts[1]) ? trim($parts[1]) : true; |
167
|
|
|
|
168
|
2 |
|
return [$key, $value]; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.