1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\Client\Common\Plugin; |
4
|
|
|
|
5
|
|
|
use Http\Client\Common\Plugin; |
6
|
|
|
use Http\Client\Exception\TransferException; |
7
|
|
|
use Http\Message\Cookie; |
8
|
|
|
use Http\Message\CookieJar; |
9
|
|
|
use Psr\Http\Message\RequestInterface; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Handle request cookies. |
14
|
|
|
* |
15
|
|
|
* @author Joel Wurtz <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
final class CookiePlugin implements Plugin |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Cookie storage. |
21
|
|
|
* |
22
|
|
|
* @var CookieJar |
23
|
|
|
*/ |
24
|
|
|
private $cookieJar; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param CookieJar $cookieJar |
28
|
|
|
*/ |
29
|
10 |
|
public function __construct(CookieJar $cookieJar) |
30
|
|
|
{ |
31
|
10 |
|
$this->cookieJar = $cookieJar; |
32
|
10 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
8 |
|
public function handleRequest(RequestInterface $request, callable $next, callable $first) |
38
|
|
|
{ |
39
|
8 |
|
foreach ($this->cookieJar->getCookies() as $cookie) { |
40
|
6 |
|
if ($cookie->isExpired()) { |
41
|
1 |
|
continue; |
42
|
|
|
} |
43
|
|
|
|
44
|
5 |
|
if (!$cookie->matchDomain($request->getUri()->getHost())) { |
45
|
1 |
|
continue; |
46
|
|
|
} |
47
|
|
|
|
48
|
4 |
|
if (!$cookie->matchPath($request->getUri()->getPath())) { |
49
|
1 |
|
continue; |
50
|
|
|
} |
51
|
|
|
|
52
|
3 |
|
if ($cookie->isSecure() && ($request->getUri()->getScheme() !== 'https')) { |
53
|
1 |
|
continue; |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
$request = $request->withAddedHeader('Cookie', sprintf('%s=%s', $cookie->getName(), $cookie->getValue())); |
57
|
8 |
|
} |
58
|
|
|
|
59
|
8 |
|
return $next($request)->then(function (ResponseInterface $response) use ($request) { |
60
|
2 |
|
if ($response->hasHeader('Set-Cookie')) { |
61
|
2 |
|
$setCookies = $response->getHeader('Set-Cookie'); |
62
|
|
|
|
63
|
2 |
|
foreach ($setCookies as $setCookie) { |
64
|
2 |
|
$cookie = $this->createCookie($request, $setCookie); |
65
|
|
|
|
66
|
|
|
// Cookie invalid do not use it |
67
|
1 |
|
if (null === $cookie) { |
68
|
|
|
continue; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Restrict setting cookie from another domain |
72
|
1 |
|
if (false === strpos($cookie->getDomain(), $request->getUri()->getHost())) { |
73
|
|
|
continue; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
$this->cookieJar->addCookie($cookie); |
77
|
1 |
|
} |
78
|
1 |
|
} |
79
|
|
|
|
80
|
1 |
|
return $response; |
81
|
8 |
|
}); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Creates a cookie from a string. |
86
|
|
|
* |
87
|
|
|
* @param RequestInterface $request |
88
|
|
|
* @param $setCookie |
89
|
|
|
* |
90
|
|
|
* @return Cookie|null |
91
|
|
|
* |
92
|
|
|
* @throws TransferException |
93
|
|
|
*/ |
94
|
2 |
|
private function createCookie(RequestInterface $request, $setCookie) |
95
|
|
|
{ |
96
|
2 |
|
$parts = array_map('trim', explode(';', $setCookie)); |
97
|
|
|
|
98
|
2 |
|
if (empty($parts) || !strpos($parts[0], '=')) { |
99
|
|
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
2 |
|
list($name, $cookieValue) = $this->createValueKey(array_shift($parts)); |
103
|
|
|
|
104
|
2 |
|
$maxAge = null; |
105
|
2 |
|
$expires = null; |
106
|
2 |
|
$domain = $request->getUri()->getHost(); |
107
|
2 |
|
$path = $request->getUri()->getPath(); |
108
|
2 |
|
$secure = false; |
109
|
2 |
|
$httpOnly = false; |
110
|
|
|
|
111
|
|
|
// Add the cookie pieces into the parsed data array |
112
|
2 |
|
foreach ($parts as $part) { |
113
|
2 |
|
list($key, $value) = $this->createValueKey($part); |
114
|
|
|
|
115
|
2 |
|
switch (strtolower($key)) { |
116
|
2 |
|
case 'expires': |
117
|
2 |
|
$expires = \DateTime::createFromFormat(\DateTime::COOKIE, $value); |
118
|
|
|
|
119
|
2 |
|
if (true !== ($expires instanceof \DateTime)) { |
120
|
1 |
|
throw new TransferException( |
121
|
1 |
|
sprintf( |
122
|
1 |
|
'Cookie header `%s` expires value `%s` could not be converted to date', |
123
|
1 |
|
$name, |
124
|
|
|
$value |
125
|
1 |
|
) |
126
|
1 |
|
); |
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.