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 = $this->parseExpires($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
|
|
|
/** |
172
|
|
|
* Parses cookie "expires" value. |
173
|
|
|
* |
174
|
|
|
* @param string $expires |
175
|
|
|
* |
176
|
|
|
* @return \DateTime|false |
177
|
|
|
* |
178
|
|
|
* @see https://tools.ietf.org/html/rfc6265#section-5.1.1 |
179
|
|
|
* @see https://github.com/salesforce/tough-cookie/blob/master/lib/cookie.js |
180
|
|
|
*/ |
181
|
|
|
private function parseExpires($expires) |
182
|
|
|
{ |
183
|
|
|
/* |
184
|
|
|
* RFC6265 5.1.1: |
185
|
|
|
* 2. Process each date-token sequentially in the order the date-tokens |
186
|
|
|
* appear in the cookie-date |
187
|
|
|
*/ |
188
|
|
|
$tokens = preg_split('/[\x09\x20-\x2F\x3B-\x40\x5B-\x60\x7B-\x7E]/', $expires); |
189
|
|
|
if (!is_array($tokens)) { |
190
|
|
|
return false; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$hour = null; |
194
|
|
|
$minutes = null; |
195
|
|
|
$seconds = null; |
196
|
|
|
$day = null; |
197
|
|
|
$month = null; |
198
|
|
|
$year = null; |
199
|
|
|
|
200
|
|
|
foreach ($tokens as $token) { |
201
|
|
|
$token = trim($token); |
202
|
|
|
if ('' === $token) { |
203
|
|
|
continue; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/* |
207
|
|
|
* 2.1. If the found-time flag is not set and the token matches the time |
208
|
|
|
* production, set the found-time flag and set the hour- value, |
209
|
|
|
* minute-value, and second-value to the numbers denoted by the digits in |
210
|
|
|
* the date-token, respectively. Skip the remaining sub-steps and continue |
211
|
|
|
* to the next date-token. |
212
|
|
|
*/ |
213
|
|
|
if (null === $seconds) { |
214
|
|
|
preg_match('/^(\d{1,2})\D*:(\d{1,2})\D*:(\d{1,2})\D*$/', $token, $match); |
215
|
|
|
if (count($match) > 0) { |
216
|
|
|
$hour = (int) $match[1]; |
217
|
|
|
$minutes = (int) $match[2]; |
218
|
|
|
$seconds = (int) $match[3]; |
219
|
|
|
/* |
220
|
|
|
* [fail if] |
221
|
|
|
* - the hour-value is greater than 23, |
222
|
|
|
* - the minute-value is greater than 59, or |
223
|
|
|
* - the second-value is greater than 59. |
224
|
|
|
*/ |
225
|
|
|
if ($hour > 23 || $minutes > 59 || $seconds > 59) { |
226
|
|
|
return false; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
continue; |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/* |
234
|
|
|
* 2.2. If the found-day-of-month flag is not set and the date-token matches |
235
|
|
|
* the day-of-month production, set the found-day-of- month flag and set |
236
|
|
|
* the day-of-month-value to the number denoted by the date-token. Skip |
237
|
|
|
* the remaining sub-steps and continue to the next date-token. |
238
|
|
|
*/ |
239
|
|
|
if (null === $day) { |
240
|
|
|
preg_match('/^(\d{1,2})\D*$/', $token, $match); |
241
|
|
|
if (count($match) > 0) { |
242
|
|
|
$day = (int) $match[1]; |
243
|
|
|
/* |
244
|
|
|
* [fail if] the day-of-month-value is less than 1 or greater than 31 |
245
|
|
|
*/ |
246
|
|
|
if ($day < 1 || $day > 31) { |
247
|
|
|
return false; |
248
|
|
|
} |
249
|
|
|
continue; |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/* |
254
|
|
|
* 2.3. If the found-month flag is not set and the date-token matches the |
255
|
|
|
* month production, set the found-month flag and set the month-value to |
256
|
|
|
* the month denoted by the date-token. Skip the remaining sub-steps and |
257
|
|
|
* continue to the next date-token. |
258
|
|
|
*/ |
259
|
|
|
if (null === $month) { |
260
|
|
|
preg_match('/^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/i', $token, $match); |
261
|
|
|
if (count($match) > 0) { |
262
|
|
|
$month = array_search( |
263
|
|
|
strtolower($match[1]), |
264
|
|
|
[ |
265
|
|
|
'jan', |
266
|
|
|
'feb', |
267
|
|
|
'mar', |
268
|
|
|
'apr', |
269
|
|
|
'may', |
270
|
|
|
'jun', |
271
|
|
|
'jul', |
272
|
|
|
'aug', |
273
|
|
|
'sep', |
274
|
|
|
'oct', |
275
|
|
|
'nov', |
276
|
|
|
'dec' |
277
|
|
|
], |
278
|
|
|
true |
279
|
|
|
); |
280
|
|
|
continue; |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/* |
285
|
|
|
* 2.4. If the found-year flag is not set and the date-token matches the year |
286
|
|
|
* production, set the found-year flag and set the year-value to the number |
287
|
|
|
* denoted by the date-token. Skip the remaining sub-steps and continue to |
288
|
|
|
* the next date-token. |
289
|
|
|
*/ |
290
|
|
|
if (null === $year) { |
291
|
|
|
preg_match(' /^(\d{2}|\d{4})$/', $token, $match); |
292
|
|
|
if (count($match) > 0) { |
293
|
|
|
$year = (int) $match[1]; |
294
|
|
|
/* |
295
|
|
|
* 3. If the year-value is greater than or equal to 70 and less |
296
|
|
|
* than or equal to 99, increment the year-value by 1900. |
297
|
|
|
* 4. If the year-value is greater than or equal to 0 and less |
298
|
|
|
* than or equal to 69, increment the year-value by 2000. |
299
|
|
|
*/ |
300
|
|
|
if (70 <= $year && $year <= 99) { |
301
|
|
|
$year += 1900; |
302
|
|
|
} elseif (0 <= $year && $year <= 69) { |
303
|
|
|
$year += 2000; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
if ($year < 1601) { |
307
|
|
|
return false; // 5. ... the year-value is less than 1601 |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
if (null === $seconds || null === $day || null === $month || null === $year) { |
314
|
|
|
/* |
315
|
|
|
* 5. ... at least one of the found-day-of-month, found-month, found- |
316
|
|
|
* year, or found-time flags is not set, |
317
|
|
|
*/ |
318
|
|
|
return false; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
// UTC/GMT format required by cookies. |
322
|
|
|
$time = new \DateTime('now', new \DateTimeZone('UTC')); |
323
|
|
|
$time->setDate($year, $month, $day); |
324
|
|
|
$time->setTime($hour, $minutes, $seconds); |
325
|
|
|
|
326
|
|
|
return $time; |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
|
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.