@@ -5,8 +5,7 @@ |
||
| 5 | 5 | /** |
| 6 | 6 | * Set-Cookie object |
| 7 | 7 | */ |
| 8 | -class SetCookie |
|
| 9 | -{ |
|
| 8 | +class SetCookie { |
|
| 10 | 9 | /** |
| 11 | 10 | * @var array |
| 12 | 11 | */ |
@@ -7,401 +7,401 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class SetCookie |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * @var array |
|
| 12 | - */ |
|
| 13 | - private static $defaults = ['Name' => null, 'Value' => null, 'Domain' => null, 'Path' => '/', 'Max-Age' => null, 'Expires' => null, 'Secure' => \false, 'Discard' => \false, 'HttpOnly' => \false]; |
|
| 14 | - /** |
|
| 15 | - * @var array Cookie data |
|
| 16 | - */ |
|
| 17 | - private $data; |
|
| 18 | - /** |
|
| 19 | - * Create a new SetCookie object from a string. |
|
| 20 | - * |
|
| 21 | - * @param string $cookie Set-Cookie header string |
|
| 22 | - */ |
|
| 23 | - public static function fromString(string $cookie) : self |
|
| 24 | - { |
|
| 25 | - // Create the default return array |
|
| 26 | - $data = self::$defaults; |
|
| 27 | - // Explode the cookie string using a series of semicolons |
|
| 28 | - $pieces = \array_filter(\array_map('trim', \explode(';', $cookie))); |
|
| 29 | - // The name of the cookie (first kvp) must exist and include an equal sign. |
|
| 30 | - if (!isset($pieces[0]) || \strpos($pieces[0], '=') === \false) { |
|
| 31 | - return new self($data); |
|
| 32 | - } |
|
| 33 | - // Add the cookie pieces into the parsed data array |
|
| 34 | - foreach ($pieces as $part) { |
|
| 35 | - $cookieParts = \explode('=', $part, 2); |
|
| 36 | - $key = \trim($cookieParts[0]); |
|
| 37 | - $value = isset($cookieParts[1]) ? \trim($cookieParts[1], " \n\r\t\x00\v") : \true; |
|
| 38 | - // Only check for non-cookies when cookies have been found |
|
| 39 | - if (!isset($data['Name'])) { |
|
| 40 | - $data['Name'] = $key; |
|
| 41 | - $data['Value'] = $value; |
|
| 42 | - } else { |
|
| 43 | - foreach (\array_keys(self::$defaults) as $search) { |
|
| 44 | - if (!\strcasecmp($search, $key)) { |
|
| 45 | - if ($search === 'Max-Age') { |
|
| 46 | - if (\is_numeric($value)) { |
|
| 47 | - $data[$search] = (int) $value; |
|
| 48 | - } |
|
| 49 | - } else { |
|
| 50 | - $data[$search] = $value; |
|
| 51 | - } |
|
| 52 | - continue 2; |
|
| 53 | - } |
|
| 54 | - } |
|
| 55 | - $data[$key] = $value; |
|
| 56 | - } |
|
| 57 | - } |
|
| 58 | - return new self($data); |
|
| 59 | - } |
|
| 60 | - /** |
|
| 61 | - * @param array $data Array of cookie data provided by a Cookie parser |
|
| 62 | - */ |
|
| 63 | - public function __construct(array $data = []) |
|
| 64 | - { |
|
| 65 | - $this->data = self::$defaults; |
|
| 66 | - if (isset($data['Name'])) { |
|
| 67 | - $this->setName($data['Name']); |
|
| 68 | - } |
|
| 69 | - if (isset($data['Value'])) { |
|
| 70 | - $this->setValue($data['Value']); |
|
| 71 | - } |
|
| 72 | - if (isset($data['Domain'])) { |
|
| 73 | - $this->setDomain($data['Domain']); |
|
| 74 | - } |
|
| 75 | - if (isset($data['Path'])) { |
|
| 76 | - $this->setPath($data['Path']); |
|
| 77 | - } |
|
| 78 | - if (isset($data['Max-Age'])) { |
|
| 79 | - $this->setMaxAge($data['Max-Age']); |
|
| 80 | - } |
|
| 81 | - if (isset($data['Expires'])) { |
|
| 82 | - $this->setExpires($data['Expires']); |
|
| 83 | - } |
|
| 84 | - if (isset($data['Secure'])) { |
|
| 85 | - $this->setSecure($data['Secure']); |
|
| 86 | - } |
|
| 87 | - if (isset($data['Discard'])) { |
|
| 88 | - $this->setDiscard($data['Discard']); |
|
| 89 | - } |
|
| 90 | - if (isset($data['HttpOnly'])) { |
|
| 91 | - $this->setHttpOnly($data['HttpOnly']); |
|
| 92 | - } |
|
| 93 | - // Set the remaining values that don't have extra validation logic |
|
| 94 | - foreach (\array_diff(\array_keys($data), \array_keys(self::$defaults)) as $key) { |
|
| 95 | - $this->data[$key] = $data[$key]; |
|
| 96 | - } |
|
| 97 | - // Extract the Expires value and turn it into a UNIX timestamp if needed |
|
| 98 | - if (!$this->getExpires() && $this->getMaxAge()) { |
|
| 99 | - // Calculate the Expires date |
|
| 100 | - $this->setExpires(\time() + $this->getMaxAge()); |
|
| 101 | - } elseif (null !== ($expires = $this->getExpires()) && !\is_numeric($expires)) { |
|
| 102 | - $this->setExpires($expires); |
|
| 103 | - } |
|
| 104 | - } |
|
| 105 | - public function __toString() |
|
| 106 | - { |
|
| 107 | - $str = $this->data['Name'] . '=' . ($this->data['Value'] ?? '') . '; '; |
|
| 108 | - foreach ($this->data as $k => $v) { |
|
| 109 | - if ($k !== 'Name' && $k !== 'Value' && $v !== null && $v !== \false) { |
|
| 110 | - if ($k === 'Expires') { |
|
| 111 | - $str .= 'Expires=' . \gmdate('D, d M Y H:i:s \\G\\M\\T', $v) . '; '; |
|
| 112 | - } else { |
|
| 113 | - $str .= ($v === \true ? $k : "{$k}={$v}") . '; '; |
|
| 114 | - } |
|
| 115 | - } |
|
| 116 | - } |
|
| 117 | - return \rtrim($str, '; '); |
|
| 118 | - } |
|
| 119 | - public function toArray() : array |
|
| 120 | - { |
|
| 121 | - return $this->data; |
|
| 122 | - } |
|
| 123 | - /** |
|
| 124 | - * Get the cookie name. |
|
| 125 | - * |
|
| 126 | - * @return string |
|
| 127 | - */ |
|
| 128 | - public function getName() |
|
| 129 | - { |
|
| 130 | - return $this->data['Name']; |
|
| 131 | - } |
|
| 132 | - /** |
|
| 133 | - * Set the cookie name. |
|
| 134 | - * |
|
| 135 | - * @param string $name Cookie name |
|
| 136 | - */ |
|
| 137 | - public function setName($name) : void |
|
| 138 | - { |
|
| 139 | - if (!\is_string($name)) { |
|
| 140 | - trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
| 141 | - } |
|
| 142 | - $this->data['Name'] = (string) $name; |
|
| 143 | - } |
|
| 144 | - /** |
|
| 145 | - * Get the cookie value. |
|
| 146 | - * |
|
| 147 | - * @return string|null |
|
| 148 | - */ |
|
| 149 | - public function getValue() |
|
| 150 | - { |
|
| 151 | - return $this->data['Value']; |
|
| 152 | - } |
|
| 153 | - /** |
|
| 154 | - * Set the cookie value. |
|
| 155 | - * |
|
| 156 | - * @param string $value Cookie value |
|
| 157 | - */ |
|
| 158 | - public function setValue($value) : void |
|
| 159 | - { |
|
| 160 | - if (!\is_string($value)) { |
|
| 161 | - trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
| 162 | - } |
|
| 163 | - $this->data['Value'] = (string) $value; |
|
| 164 | - } |
|
| 165 | - /** |
|
| 166 | - * Get the domain. |
|
| 167 | - * |
|
| 168 | - * @return string|null |
|
| 169 | - */ |
|
| 170 | - public function getDomain() |
|
| 171 | - { |
|
| 172 | - return $this->data['Domain']; |
|
| 173 | - } |
|
| 174 | - /** |
|
| 175 | - * Set the domain of the cookie. |
|
| 176 | - * |
|
| 177 | - * @param string|null $domain |
|
| 178 | - */ |
|
| 179 | - public function setDomain($domain) : void |
|
| 180 | - { |
|
| 181 | - if (!\is_string($domain) && null !== $domain) { |
|
| 182 | - trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string or null to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
| 183 | - } |
|
| 184 | - $this->data['Domain'] = null === $domain ? null : (string) $domain; |
|
| 185 | - } |
|
| 186 | - /** |
|
| 187 | - * Get the path. |
|
| 188 | - * |
|
| 189 | - * @return string |
|
| 190 | - */ |
|
| 191 | - public function getPath() |
|
| 192 | - { |
|
| 193 | - return $this->data['Path']; |
|
| 194 | - } |
|
| 195 | - /** |
|
| 196 | - * Set the path of the cookie. |
|
| 197 | - * |
|
| 198 | - * @param string $path Path of the cookie |
|
| 199 | - */ |
|
| 200 | - public function setPath($path) : void |
|
| 201 | - { |
|
| 202 | - if (!\is_string($path)) { |
|
| 203 | - trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
| 204 | - } |
|
| 205 | - $this->data['Path'] = (string) $path; |
|
| 206 | - } |
|
| 207 | - /** |
|
| 208 | - * Maximum lifetime of the cookie in seconds. |
|
| 209 | - * |
|
| 210 | - * @return int|null |
|
| 211 | - */ |
|
| 212 | - public function getMaxAge() |
|
| 213 | - { |
|
| 214 | - return null === $this->data['Max-Age'] ? null : (int) $this->data['Max-Age']; |
|
| 215 | - } |
|
| 216 | - /** |
|
| 217 | - * Set the max-age of the cookie. |
|
| 218 | - * |
|
| 219 | - * @param int|null $maxAge Max age of the cookie in seconds |
|
| 220 | - */ |
|
| 221 | - public function setMaxAge($maxAge) : void |
|
| 222 | - { |
|
| 223 | - if (!\is_int($maxAge) && null !== $maxAge) { |
|
| 224 | - trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing an int or null to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
| 225 | - } |
|
| 226 | - $this->data['Max-Age'] = $maxAge === null ? null : (int) $maxAge; |
|
| 227 | - } |
|
| 228 | - /** |
|
| 229 | - * The UNIX timestamp when the cookie Expires. |
|
| 230 | - * |
|
| 231 | - * @return string|int|null |
|
| 232 | - */ |
|
| 233 | - public function getExpires() |
|
| 234 | - { |
|
| 235 | - return $this->data['Expires']; |
|
| 236 | - } |
|
| 237 | - /** |
|
| 238 | - * Set the unix timestamp for which the cookie will expire. |
|
| 239 | - * |
|
| 240 | - * @param int|string|null $timestamp Unix timestamp or any English textual datetime description. |
|
| 241 | - */ |
|
| 242 | - public function setExpires($timestamp) : void |
|
| 243 | - { |
|
| 244 | - if (!\is_int($timestamp) && !\is_string($timestamp) && null !== $timestamp) { |
|
| 245 | - trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing an int, string or null to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
| 246 | - } |
|
| 247 | - $this->data['Expires'] = null === $timestamp ? null : (\is_numeric($timestamp) ? (int) $timestamp : \strtotime((string) $timestamp)); |
|
| 248 | - } |
|
| 249 | - /** |
|
| 250 | - * Get whether or not this is a secure cookie. |
|
| 251 | - * |
|
| 252 | - * @return bool |
|
| 253 | - */ |
|
| 254 | - public function getSecure() |
|
| 255 | - { |
|
| 256 | - return $this->data['Secure']; |
|
| 257 | - } |
|
| 258 | - /** |
|
| 259 | - * Set whether or not the cookie is secure. |
|
| 260 | - * |
|
| 261 | - * @param bool $secure Set to true or false if secure |
|
| 262 | - */ |
|
| 263 | - public function setSecure($secure) : void |
|
| 264 | - { |
|
| 265 | - if (!\is_bool($secure)) { |
|
| 266 | - trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a bool to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
| 267 | - } |
|
| 268 | - $this->data['Secure'] = (bool) $secure; |
|
| 269 | - } |
|
| 270 | - /** |
|
| 271 | - * Get whether or not this is a session cookie. |
|
| 272 | - * |
|
| 273 | - * @return bool|null |
|
| 274 | - */ |
|
| 275 | - public function getDiscard() |
|
| 276 | - { |
|
| 277 | - return $this->data['Discard']; |
|
| 278 | - } |
|
| 279 | - /** |
|
| 280 | - * Set whether or not this is a session cookie. |
|
| 281 | - * |
|
| 282 | - * @param bool $discard Set to true or false if this is a session cookie |
|
| 283 | - */ |
|
| 284 | - public function setDiscard($discard) : void |
|
| 285 | - { |
|
| 286 | - if (!\is_bool($discard)) { |
|
| 287 | - trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a bool to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
| 288 | - } |
|
| 289 | - $this->data['Discard'] = (bool) $discard; |
|
| 290 | - } |
|
| 291 | - /** |
|
| 292 | - * Get whether or not this is an HTTP only cookie. |
|
| 293 | - * |
|
| 294 | - * @return bool |
|
| 295 | - */ |
|
| 296 | - public function getHttpOnly() |
|
| 297 | - { |
|
| 298 | - return $this->data['HttpOnly']; |
|
| 299 | - } |
|
| 300 | - /** |
|
| 301 | - * Set whether or not this is an HTTP only cookie. |
|
| 302 | - * |
|
| 303 | - * @param bool $httpOnly Set to true or false if this is HTTP only |
|
| 304 | - */ |
|
| 305 | - public function setHttpOnly($httpOnly) : void |
|
| 306 | - { |
|
| 307 | - if (!\is_bool($httpOnly)) { |
|
| 308 | - trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a bool to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
| 309 | - } |
|
| 310 | - $this->data['HttpOnly'] = (bool) $httpOnly; |
|
| 311 | - } |
|
| 312 | - /** |
|
| 313 | - * Check if the cookie matches a path value. |
|
| 314 | - * |
|
| 315 | - * A request-path path-matches a given cookie-path if at least one of |
|
| 316 | - * the following conditions holds: |
|
| 317 | - * |
|
| 318 | - * - The cookie-path and the request-path are identical. |
|
| 319 | - * - The cookie-path is a prefix of the request-path, and the last |
|
| 320 | - * character of the cookie-path is %x2F ("/"). |
|
| 321 | - * - The cookie-path is a prefix of the request-path, and the first |
|
| 322 | - * character of the request-path that is not included in the cookie- |
|
| 323 | - * path is a %x2F ("/") character. |
|
| 324 | - * |
|
| 325 | - * @param string $requestPath Path to check against |
|
| 326 | - */ |
|
| 327 | - public function matchesPath(string $requestPath) : bool |
|
| 328 | - { |
|
| 329 | - $cookiePath = $this->getPath(); |
|
| 330 | - // Match on exact matches or when path is the default empty "/" |
|
| 331 | - if ($cookiePath === '/' || $cookiePath == $requestPath) { |
|
| 332 | - return \true; |
|
| 333 | - } |
|
| 334 | - // Ensure that the cookie-path is a prefix of the request path. |
|
| 335 | - if (0 !== \strpos($requestPath, $cookiePath)) { |
|
| 336 | - return \false; |
|
| 337 | - } |
|
| 338 | - // Match if the last character of the cookie-path is "/" |
|
| 339 | - if (\substr($cookiePath, -1, 1) === '/') { |
|
| 340 | - return \true; |
|
| 341 | - } |
|
| 342 | - // Match if the first character not included in cookie path is "/" |
|
| 343 | - return \substr($requestPath, \strlen($cookiePath), 1) === '/'; |
|
| 344 | - } |
|
| 345 | - /** |
|
| 346 | - * Check if the cookie matches a domain value. |
|
| 347 | - * |
|
| 348 | - * @param string $domain Domain to check against |
|
| 349 | - */ |
|
| 350 | - public function matchesDomain(string $domain) : bool |
|
| 351 | - { |
|
| 352 | - $cookieDomain = $this->getDomain(); |
|
| 353 | - if (null === $cookieDomain) { |
|
| 354 | - return \true; |
|
| 355 | - } |
|
| 356 | - // Remove the leading '.' as per spec in RFC 6265. |
|
| 357 | - // https://datatracker.ietf.org/doc/html/rfc6265#section-5.2.3 |
|
| 358 | - $cookieDomain = \ltrim(\strtolower($cookieDomain), '.'); |
|
| 359 | - $domain = \strtolower($domain); |
|
| 360 | - // Domain not set or exact match. |
|
| 361 | - if ('' === $cookieDomain || $domain === $cookieDomain) { |
|
| 362 | - return \true; |
|
| 363 | - } |
|
| 364 | - // Matching the subdomain according to RFC 6265. |
|
| 365 | - // https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.3 |
|
| 366 | - if (\filter_var($domain, \FILTER_VALIDATE_IP)) { |
|
| 367 | - return \false; |
|
| 368 | - } |
|
| 369 | - return (bool) \preg_match('/\\.' . \preg_quote($cookieDomain, '/') . '$/', $domain); |
|
| 370 | - } |
|
| 371 | - /** |
|
| 372 | - * Check if the cookie is expired. |
|
| 373 | - */ |
|
| 374 | - public function isExpired() : bool |
|
| 375 | - { |
|
| 376 | - return $this->getExpires() !== null && \time() > $this->getExpires(); |
|
| 377 | - } |
|
| 378 | - /** |
|
| 379 | - * Check if the cookie is valid according to RFC 6265. |
|
| 380 | - * |
|
| 381 | - * @return bool|string Returns true if valid or an error message if invalid |
|
| 382 | - */ |
|
| 383 | - public function validate() |
|
| 384 | - { |
|
| 385 | - $name = $this->getName(); |
|
| 386 | - if ($name === '') { |
|
| 387 | - return 'The cookie name must not be empty'; |
|
| 388 | - } |
|
| 389 | - // Check if any of the invalid characters are present in the cookie name |
|
| 390 | - if (\preg_match('/[\\x00-\\x20\\x22\\x28-\\x29\\x2c\\x2f\\x3a-\\x40\\x5c\\x7b\\x7d\\x7f]/', $name)) { |
|
| 391 | - return 'Cookie name must not contain invalid characters: ASCII ' . 'Control characters (0-31;127), space, tab and the ' . 'following characters: ()<>@,;:\\"/?={}'; |
|
| 392 | - } |
|
| 393 | - // Value must not be null. 0 and empty string are valid. Empty strings |
|
| 394 | - // are technically against RFC 6265, but known to happen in the wild. |
|
| 395 | - $value = $this->getValue(); |
|
| 396 | - if ($value === null) { |
|
| 397 | - return 'The cookie value must not be empty'; |
|
| 398 | - } |
|
| 399 | - // Domains must not be empty, but can be 0. "0" is not a valid internet |
|
| 400 | - // domain, but may be used as server name in a private network. |
|
| 401 | - $domain = $this->getDomain(); |
|
| 402 | - if ($domain === null || $domain === '') { |
|
| 403 | - return 'The cookie domain must not be empty'; |
|
| 404 | - } |
|
| 405 | - return \true; |
|
| 406 | - } |
|
| 10 | + /** |
|
| 11 | + * @var array |
|
| 12 | + */ |
|
| 13 | + private static $defaults = ['Name' => null, 'Value' => null, 'Domain' => null, 'Path' => '/', 'Max-Age' => null, 'Expires' => null, 'Secure' => \false, 'Discard' => \false, 'HttpOnly' => \false]; |
|
| 14 | + /** |
|
| 15 | + * @var array Cookie data |
|
| 16 | + */ |
|
| 17 | + private $data; |
|
| 18 | + /** |
|
| 19 | + * Create a new SetCookie object from a string. |
|
| 20 | + * |
|
| 21 | + * @param string $cookie Set-Cookie header string |
|
| 22 | + */ |
|
| 23 | + public static function fromString(string $cookie) : self |
|
| 24 | + { |
|
| 25 | + // Create the default return array |
|
| 26 | + $data = self::$defaults; |
|
| 27 | + // Explode the cookie string using a series of semicolons |
|
| 28 | + $pieces = \array_filter(\array_map('trim', \explode(';', $cookie))); |
|
| 29 | + // The name of the cookie (first kvp) must exist and include an equal sign. |
|
| 30 | + if (!isset($pieces[0]) || \strpos($pieces[0], '=') === \false) { |
|
| 31 | + return new self($data); |
|
| 32 | + } |
|
| 33 | + // Add the cookie pieces into the parsed data array |
|
| 34 | + foreach ($pieces as $part) { |
|
| 35 | + $cookieParts = \explode('=', $part, 2); |
|
| 36 | + $key = \trim($cookieParts[0]); |
|
| 37 | + $value = isset($cookieParts[1]) ? \trim($cookieParts[1], " \n\r\t\x00\v") : \true; |
|
| 38 | + // Only check for non-cookies when cookies have been found |
|
| 39 | + if (!isset($data['Name'])) { |
|
| 40 | + $data['Name'] = $key; |
|
| 41 | + $data['Value'] = $value; |
|
| 42 | + } else { |
|
| 43 | + foreach (\array_keys(self::$defaults) as $search) { |
|
| 44 | + if (!\strcasecmp($search, $key)) { |
|
| 45 | + if ($search === 'Max-Age') { |
|
| 46 | + if (\is_numeric($value)) { |
|
| 47 | + $data[$search] = (int) $value; |
|
| 48 | + } |
|
| 49 | + } else { |
|
| 50 | + $data[$search] = $value; |
|
| 51 | + } |
|
| 52 | + continue 2; |
|
| 53 | + } |
|
| 54 | + } |
|
| 55 | + $data[$key] = $value; |
|
| 56 | + } |
|
| 57 | + } |
|
| 58 | + return new self($data); |
|
| 59 | + } |
|
| 60 | + /** |
|
| 61 | + * @param array $data Array of cookie data provided by a Cookie parser |
|
| 62 | + */ |
|
| 63 | + public function __construct(array $data = []) |
|
| 64 | + { |
|
| 65 | + $this->data = self::$defaults; |
|
| 66 | + if (isset($data['Name'])) { |
|
| 67 | + $this->setName($data['Name']); |
|
| 68 | + } |
|
| 69 | + if (isset($data['Value'])) { |
|
| 70 | + $this->setValue($data['Value']); |
|
| 71 | + } |
|
| 72 | + if (isset($data['Domain'])) { |
|
| 73 | + $this->setDomain($data['Domain']); |
|
| 74 | + } |
|
| 75 | + if (isset($data['Path'])) { |
|
| 76 | + $this->setPath($data['Path']); |
|
| 77 | + } |
|
| 78 | + if (isset($data['Max-Age'])) { |
|
| 79 | + $this->setMaxAge($data['Max-Age']); |
|
| 80 | + } |
|
| 81 | + if (isset($data['Expires'])) { |
|
| 82 | + $this->setExpires($data['Expires']); |
|
| 83 | + } |
|
| 84 | + if (isset($data['Secure'])) { |
|
| 85 | + $this->setSecure($data['Secure']); |
|
| 86 | + } |
|
| 87 | + if (isset($data['Discard'])) { |
|
| 88 | + $this->setDiscard($data['Discard']); |
|
| 89 | + } |
|
| 90 | + if (isset($data['HttpOnly'])) { |
|
| 91 | + $this->setHttpOnly($data['HttpOnly']); |
|
| 92 | + } |
|
| 93 | + // Set the remaining values that don't have extra validation logic |
|
| 94 | + foreach (\array_diff(\array_keys($data), \array_keys(self::$defaults)) as $key) { |
|
| 95 | + $this->data[$key] = $data[$key]; |
|
| 96 | + } |
|
| 97 | + // Extract the Expires value and turn it into a UNIX timestamp if needed |
|
| 98 | + if (!$this->getExpires() && $this->getMaxAge()) { |
|
| 99 | + // Calculate the Expires date |
|
| 100 | + $this->setExpires(\time() + $this->getMaxAge()); |
|
| 101 | + } elseif (null !== ($expires = $this->getExpires()) && !\is_numeric($expires)) { |
|
| 102 | + $this->setExpires($expires); |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | + public function __toString() |
|
| 106 | + { |
|
| 107 | + $str = $this->data['Name'] . '=' . ($this->data['Value'] ?? '') . '; '; |
|
| 108 | + foreach ($this->data as $k => $v) { |
|
| 109 | + if ($k !== 'Name' && $k !== 'Value' && $v !== null && $v !== \false) { |
|
| 110 | + if ($k === 'Expires') { |
|
| 111 | + $str .= 'Expires=' . \gmdate('D, d M Y H:i:s \\G\\M\\T', $v) . '; '; |
|
| 112 | + } else { |
|
| 113 | + $str .= ($v === \true ? $k : "{$k}={$v}") . '; '; |
|
| 114 | + } |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | + return \rtrim($str, '; '); |
|
| 118 | + } |
|
| 119 | + public function toArray() : array |
|
| 120 | + { |
|
| 121 | + return $this->data; |
|
| 122 | + } |
|
| 123 | + /** |
|
| 124 | + * Get the cookie name. |
|
| 125 | + * |
|
| 126 | + * @return string |
|
| 127 | + */ |
|
| 128 | + public function getName() |
|
| 129 | + { |
|
| 130 | + return $this->data['Name']; |
|
| 131 | + } |
|
| 132 | + /** |
|
| 133 | + * Set the cookie name. |
|
| 134 | + * |
|
| 135 | + * @param string $name Cookie name |
|
| 136 | + */ |
|
| 137 | + public function setName($name) : void |
|
| 138 | + { |
|
| 139 | + if (!\is_string($name)) { |
|
| 140 | + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
| 141 | + } |
|
| 142 | + $this->data['Name'] = (string) $name; |
|
| 143 | + } |
|
| 144 | + /** |
|
| 145 | + * Get the cookie value. |
|
| 146 | + * |
|
| 147 | + * @return string|null |
|
| 148 | + */ |
|
| 149 | + public function getValue() |
|
| 150 | + { |
|
| 151 | + return $this->data['Value']; |
|
| 152 | + } |
|
| 153 | + /** |
|
| 154 | + * Set the cookie value. |
|
| 155 | + * |
|
| 156 | + * @param string $value Cookie value |
|
| 157 | + */ |
|
| 158 | + public function setValue($value) : void |
|
| 159 | + { |
|
| 160 | + if (!\is_string($value)) { |
|
| 161 | + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
| 162 | + } |
|
| 163 | + $this->data['Value'] = (string) $value; |
|
| 164 | + } |
|
| 165 | + /** |
|
| 166 | + * Get the domain. |
|
| 167 | + * |
|
| 168 | + * @return string|null |
|
| 169 | + */ |
|
| 170 | + public function getDomain() |
|
| 171 | + { |
|
| 172 | + return $this->data['Domain']; |
|
| 173 | + } |
|
| 174 | + /** |
|
| 175 | + * Set the domain of the cookie. |
|
| 176 | + * |
|
| 177 | + * @param string|null $domain |
|
| 178 | + */ |
|
| 179 | + public function setDomain($domain) : void |
|
| 180 | + { |
|
| 181 | + if (!\is_string($domain) && null !== $domain) { |
|
| 182 | + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string or null to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
| 183 | + } |
|
| 184 | + $this->data['Domain'] = null === $domain ? null : (string) $domain; |
|
| 185 | + } |
|
| 186 | + /** |
|
| 187 | + * Get the path. |
|
| 188 | + * |
|
| 189 | + * @return string |
|
| 190 | + */ |
|
| 191 | + public function getPath() |
|
| 192 | + { |
|
| 193 | + return $this->data['Path']; |
|
| 194 | + } |
|
| 195 | + /** |
|
| 196 | + * Set the path of the cookie. |
|
| 197 | + * |
|
| 198 | + * @param string $path Path of the cookie |
|
| 199 | + */ |
|
| 200 | + public function setPath($path) : void |
|
| 201 | + { |
|
| 202 | + if (!\is_string($path)) { |
|
| 203 | + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
| 204 | + } |
|
| 205 | + $this->data['Path'] = (string) $path; |
|
| 206 | + } |
|
| 207 | + /** |
|
| 208 | + * Maximum lifetime of the cookie in seconds. |
|
| 209 | + * |
|
| 210 | + * @return int|null |
|
| 211 | + */ |
|
| 212 | + public function getMaxAge() |
|
| 213 | + { |
|
| 214 | + return null === $this->data['Max-Age'] ? null : (int) $this->data['Max-Age']; |
|
| 215 | + } |
|
| 216 | + /** |
|
| 217 | + * Set the max-age of the cookie. |
|
| 218 | + * |
|
| 219 | + * @param int|null $maxAge Max age of the cookie in seconds |
|
| 220 | + */ |
|
| 221 | + public function setMaxAge($maxAge) : void |
|
| 222 | + { |
|
| 223 | + if (!\is_int($maxAge) && null !== $maxAge) { |
|
| 224 | + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing an int or null to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
| 225 | + } |
|
| 226 | + $this->data['Max-Age'] = $maxAge === null ? null : (int) $maxAge; |
|
| 227 | + } |
|
| 228 | + /** |
|
| 229 | + * The UNIX timestamp when the cookie Expires. |
|
| 230 | + * |
|
| 231 | + * @return string|int|null |
|
| 232 | + */ |
|
| 233 | + public function getExpires() |
|
| 234 | + { |
|
| 235 | + return $this->data['Expires']; |
|
| 236 | + } |
|
| 237 | + /** |
|
| 238 | + * Set the unix timestamp for which the cookie will expire. |
|
| 239 | + * |
|
| 240 | + * @param int|string|null $timestamp Unix timestamp or any English textual datetime description. |
|
| 241 | + */ |
|
| 242 | + public function setExpires($timestamp) : void |
|
| 243 | + { |
|
| 244 | + if (!\is_int($timestamp) && !\is_string($timestamp) && null !== $timestamp) { |
|
| 245 | + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing an int, string or null to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
| 246 | + } |
|
| 247 | + $this->data['Expires'] = null === $timestamp ? null : (\is_numeric($timestamp) ? (int) $timestamp : \strtotime((string) $timestamp)); |
|
| 248 | + } |
|
| 249 | + /** |
|
| 250 | + * Get whether or not this is a secure cookie. |
|
| 251 | + * |
|
| 252 | + * @return bool |
|
| 253 | + */ |
|
| 254 | + public function getSecure() |
|
| 255 | + { |
|
| 256 | + return $this->data['Secure']; |
|
| 257 | + } |
|
| 258 | + /** |
|
| 259 | + * Set whether or not the cookie is secure. |
|
| 260 | + * |
|
| 261 | + * @param bool $secure Set to true or false if secure |
|
| 262 | + */ |
|
| 263 | + public function setSecure($secure) : void |
|
| 264 | + { |
|
| 265 | + if (!\is_bool($secure)) { |
|
| 266 | + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a bool to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
| 267 | + } |
|
| 268 | + $this->data['Secure'] = (bool) $secure; |
|
| 269 | + } |
|
| 270 | + /** |
|
| 271 | + * Get whether or not this is a session cookie. |
|
| 272 | + * |
|
| 273 | + * @return bool|null |
|
| 274 | + */ |
|
| 275 | + public function getDiscard() |
|
| 276 | + { |
|
| 277 | + return $this->data['Discard']; |
|
| 278 | + } |
|
| 279 | + /** |
|
| 280 | + * Set whether or not this is a session cookie. |
|
| 281 | + * |
|
| 282 | + * @param bool $discard Set to true or false if this is a session cookie |
|
| 283 | + */ |
|
| 284 | + public function setDiscard($discard) : void |
|
| 285 | + { |
|
| 286 | + if (!\is_bool($discard)) { |
|
| 287 | + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a bool to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
| 288 | + } |
|
| 289 | + $this->data['Discard'] = (bool) $discard; |
|
| 290 | + } |
|
| 291 | + /** |
|
| 292 | + * Get whether or not this is an HTTP only cookie. |
|
| 293 | + * |
|
| 294 | + * @return bool |
|
| 295 | + */ |
|
| 296 | + public function getHttpOnly() |
|
| 297 | + { |
|
| 298 | + return $this->data['HttpOnly']; |
|
| 299 | + } |
|
| 300 | + /** |
|
| 301 | + * Set whether or not this is an HTTP only cookie. |
|
| 302 | + * |
|
| 303 | + * @param bool $httpOnly Set to true or false if this is HTTP only |
|
| 304 | + */ |
|
| 305 | + public function setHttpOnly($httpOnly) : void |
|
| 306 | + { |
|
| 307 | + if (!\is_bool($httpOnly)) { |
|
| 308 | + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a bool to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
| 309 | + } |
|
| 310 | + $this->data['HttpOnly'] = (bool) $httpOnly; |
|
| 311 | + } |
|
| 312 | + /** |
|
| 313 | + * Check if the cookie matches a path value. |
|
| 314 | + * |
|
| 315 | + * A request-path path-matches a given cookie-path if at least one of |
|
| 316 | + * the following conditions holds: |
|
| 317 | + * |
|
| 318 | + * - The cookie-path and the request-path are identical. |
|
| 319 | + * - The cookie-path is a prefix of the request-path, and the last |
|
| 320 | + * character of the cookie-path is %x2F ("/"). |
|
| 321 | + * - The cookie-path is a prefix of the request-path, and the first |
|
| 322 | + * character of the request-path that is not included in the cookie- |
|
| 323 | + * path is a %x2F ("/") character. |
|
| 324 | + * |
|
| 325 | + * @param string $requestPath Path to check against |
|
| 326 | + */ |
|
| 327 | + public function matchesPath(string $requestPath) : bool |
|
| 328 | + { |
|
| 329 | + $cookiePath = $this->getPath(); |
|
| 330 | + // Match on exact matches or when path is the default empty "/" |
|
| 331 | + if ($cookiePath === '/' || $cookiePath == $requestPath) { |
|
| 332 | + return \true; |
|
| 333 | + } |
|
| 334 | + // Ensure that the cookie-path is a prefix of the request path. |
|
| 335 | + if (0 !== \strpos($requestPath, $cookiePath)) { |
|
| 336 | + return \false; |
|
| 337 | + } |
|
| 338 | + // Match if the last character of the cookie-path is "/" |
|
| 339 | + if (\substr($cookiePath, -1, 1) === '/') { |
|
| 340 | + return \true; |
|
| 341 | + } |
|
| 342 | + // Match if the first character not included in cookie path is "/" |
|
| 343 | + return \substr($requestPath, \strlen($cookiePath), 1) === '/'; |
|
| 344 | + } |
|
| 345 | + /** |
|
| 346 | + * Check if the cookie matches a domain value. |
|
| 347 | + * |
|
| 348 | + * @param string $domain Domain to check against |
|
| 349 | + */ |
|
| 350 | + public function matchesDomain(string $domain) : bool |
|
| 351 | + { |
|
| 352 | + $cookieDomain = $this->getDomain(); |
|
| 353 | + if (null === $cookieDomain) { |
|
| 354 | + return \true; |
|
| 355 | + } |
|
| 356 | + // Remove the leading '.' as per spec in RFC 6265. |
|
| 357 | + // https://datatracker.ietf.org/doc/html/rfc6265#section-5.2.3 |
|
| 358 | + $cookieDomain = \ltrim(\strtolower($cookieDomain), '.'); |
|
| 359 | + $domain = \strtolower($domain); |
|
| 360 | + // Domain not set or exact match. |
|
| 361 | + if ('' === $cookieDomain || $domain === $cookieDomain) { |
|
| 362 | + return \true; |
|
| 363 | + } |
|
| 364 | + // Matching the subdomain according to RFC 6265. |
|
| 365 | + // https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.3 |
|
| 366 | + if (\filter_var($domain, \FILTER_VALIDATE_IP)) { |
|
| 367 | + return \false; |
|
| 368 | + } |
|
| 369 | + return (bool) \preg_match('/\\.' . \preg_quote($cookieDomain, '/') . '$/', $domain); |
|
| 370 | + } |
|
| 371 | + /** |
|
| 372 | + * Check if the cookie is expired. |
|
| 373 | + */ |
|
| 374 | + public function isExpired() : bool |
|
| 375 | + { |
|
| 376 | + return $this->getExpires() !== null && \time() > $this->getExpires(); |
|
| 377 | + } |
|
| 378 | + /** |
|
| 379 | + * Check if the cookie is valid according to RFC 6265. |
|
| 380 | + * |
|
| 381 | + * @return bool|string Returns true if valid or an error message if invalid |
|
| 382 | + */ |
|
| 383 | + public function validate() |
|
| 384 | + { |
|
| 385 | + $name = $this->getName(); |
|
| 386 | + if ($name === '') { |
|
| 387 | + return 'The cookie name must not be empty'; |
|
| 388 | + } |
|
| 389 | + // Check if any of the invalid characters are present in the cookie name |
|
| 390 | + if (\preg_match('/[\\x00-\\x20\\x22\\x28-\\x29\\x2c\\x2f\\x3a-\\x40\\x5c\\x7b\\x7d\\x7f]/', $name)) { |
|
| 391 | + return 'Cookie name must not contain invalid characters: ASCII ' . 'Control characters (0-31;127), space, tab and the ' . 'following characters: ()<>@,;:\\"/?={}'; |
|
| 392 | + } |
|
| 393 | + // Value must not be null. 0 and empty string are valid. Empty strings |
|
| 394 | + // are technically against RFC 6265, but known to happen in the wild. |
|
| 395 | + $value = $this->getValue(); |
|
| 396 | + if ($value === null) { |
|
| 397 | + return 'The cookie value must not be empty'; |
|
| 398 | + } |
|
| 399 | + // Domains must not be empty, but can be 0. "0" is not a valid internet |
|
| 400 | + // domain, but may be used as server name in a private network. |
|
| 401 | + $domain = $this->getDomain(); |
|
| 402 | + if ($domain === null || $domain === '') { |
|
| 403 | + return 'The cookie domain must not be empty'; |
|
| 404 | + } |
|
| 405 | + return \true; |
|
| 406 | + } |
|
| 407 | 407 | } |
@@ -44,7 +44,7 @@ discard block |
||
| 44 | 44 | if (!\strcasecmp($search, $key)) { |
| 45 | 45 | if ($search === 'Max-Age') { |
| 46 | 46 | if (\is_numeric($value)) { |
| 47 | - $data[$search] = (int) $value; |
|
| 47 | + $data[$search] = (int)$value; |
|
| 48 | 48 | } |
| 49 | 49 | } else { |
| 50 | 50 | $data[$search] = $value; |
@@ -104,13 +104,13 @@ discard block |
||
| 104 | 104 | } |
| 105 | 105 | public function __toString() |
| 106 | 106 | { |
| 107 | - $str = $this->data['Name'] . '=' . ($this->data['Value'] ?? '') . '; '; |
|
| 107 | + $str = $this->data['Name'].'='.($this->data['Value'] ?? '').'; '; |
|
| 108 | 108 | foreach ($this->data as $k => $v) { |
| 109 | 109 | if ($k !== 'Name' && $k !== 'Value' && $v !== null && $v !== \false) { |
| 110 | 110 | if ($k === 'Expires') { |
| 111 | - $str .= 'Expires=' . \gmdate('D, d M Y H:i:s \\G\\M\\T', $v) . '; '; |
|
| 111 | + $str .= 'Expires='.\gmdate('D, d M Y H:i:s \\G\\M\\T', $v).'; '; |
|
| 112 | 112 | } else { |
| 113 | - $str .= ($v === \true ? $k : "{$k}={$v}") . '; '; |
|
| 113 | + $str .= ($v === \true ? $k : "{$k}={$v}").'; '; |
|
| 114 | 114 | } |
| 115 | 115 | } |
| 116 | 116 | } |
@@ -139,7 +139,7 @@ discard block |
||
| 139 | 139 | if (!\is_string($name)) { |
| 140 | 140 | trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
| 141 | 141 | } |
| 142 | - $this->data['Name'] = (string) $name; |
|
| 142 | + $this->data['Name'] = (string)$name; |
|
| 143 | 143 | } |
| 144 | 144 | /** |
| 145 | 145 | * Get the cookie value. |
@@ -160,7 +160,7 @@ discard block |
||
| 160 | 160 | if (!\is_string($value)) { |
| 161 | 161 | trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
| 162 | 162 | } |
| 163 | - $this->data['Value'] = (string) $value; |
|
| 163 | + $this->data['Value'] = (string)$value; |
|
| 164 | 164 | } |
| 165 | 165 | /** |
| 166 | 166 | * Get the domain. |
@@ -181,7 +181,7 @@ discard block |
||
| 181 | 181 | if (!\is_string($domain) && null !== $domain) { |
| 182 | 182 | trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string or null to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
| 183 | 183 | } |
| 184 | - $this->data['Domain'] = null === $domain ? null : (string) $domain; |
|
| 184 | + $this->data['Domain'] = null === $domain ? null : (string)$domain; |
|
| 185 | 185 | } |
| 186 | 186 | /** |
| 187 | 187 | * Get the path. |
@@ -202,7 +202,7 @@ discard block |
||
| 202 | 202 | if (!\is_string($path)) { |
| 203 | 203 | trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
| 204 | 204 | } |
| 205 | - $this->data['Path'] = (string) $path; |
|
| 205 | + $this->data['Path'] = (string)$path; |
|
| 206 | 206 | } |
| 207 | 207 | /** |
| 208 | 208 | * Maximum lifetime of the cookie in seconds. |
@@ -211,7 +211,7 @@ discard block |
||
| 211 | 211 | */ |
| 212 | 212 | public function getMaxAge() |
| 213 | 213 | { |
| 214 | - return null === $this->data['Max-Age'] ? null : (int) $this->data['Max-Age']; |
|
| 214 | + return null === $this->data['Max-Age'] ? null : (int)$this->data['Max-Age']; |
|
| 215 | 215 | } |
| 216 | 216 | /** |
| 217 | 217 | * Set the max-age of the cookie. |
@@ -223,7 +223,7 @@ discard block |
||
| 223 | 223 | if (!\is_int($maxAge) && null !== $maxAge) { |
| 224 | 224 | trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing an int or null to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
| 225 | 225 | } |
| 226 | - $this->data['Max-Age'] = $maxAge === null ? null : (int) $maxAge; |
|
| 226 | + $this->data['Max-Age'] = $maxAge === null ? null : (int)$maxAge; |
|
| 227 | 227 | } |
| 228 | 228 | /** |
| 229 | 229 | * The UNIX timestamp when the cookie Expires. |
@@ -244,7 +244,7 @@ discard block |
||
| 244 | 244 | if (!\is_int($timestamp) && !\is_string($timestamp) && null !== $timestamp) { |
| 245 | 245 | trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing an int, string or null to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
| 246 | 246 | } |
| 247 | - $this->data['Expires'] = null === $timestamp ? null : (\is_numeric($timestamp) ? (int) $timestamp : \strtotime((string) $timestamp)); |
|
| 247 | + $this->data['Expires'] = null === $timestamp ? null : (\is_numeric($timestamp) ? (int)$timestamp : \strtotime((string)$timestamp)); |
|
| 248 | 248 | } |
| 249 | 249 | /** |
| 250 | 250 | * Get whether or not this is a secure cookie. |
@@ -265,7 +265,7 @@ discard block |
||
| 265 | 265 | if (!\is_bool($secure)) { |
| 266 | 266 | trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a bool to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
| 267 | 267 | } |
| 268 | - $this->data['Secure'] = (bool) $secure; |
|
| 268 | + $this->data['Secure'] = (bool)$secure; |
|
| 269 | 269 | } |
| 270 | 270 | /** |
| 271 | 271 | * Get whether or not this is a session cookie. |
@@ -286,7 +286,7 @@ discard block |
||
| 286 | 286 | if (!\is_bool($discard)) { |
| 287 | 287 | trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a bool to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
| 288 | 288 | } |
| 289 | - $this->data['Discard'] = (bool) $discard; |
|
| 289 | + $this->data['Discard'] = (bool)$discard; |
|
| 290 | 290 | } |
| 291 | 291 | /** |
| 292 | 292 | * Get whether or not this is an HTTP only cookie. |
@@ -307,7 +307,7 @@ discard block |
||
| 307 | 307 | if (!\is_bool($httpOnly)) { |
| 308 | 308 | trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a bool to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
| 309 | 309 | } |
| 310 | - $this->data['HttpOnly'] = (bool) $httpOnly; |
|
| 310 | + $this->data['HttpOnly'] = (bool)$httpOnly; |
|
| 311 | 311 | } |
| 312 | 312 | /** |
| 313 | 313 | * Check if the cookie matches a path value. |
@@ -366,7 +366,7 @@ discard block |
||
| 366 | 366 | if (\filter_var($domain, \FILTER_VALIDATE_IP)) { |
| 367 | 367 | return \false; |
| 368 | 368 | } |
| 369 | - return (bool) \preg_match('/\\.' . \preg_quote($cookieDomain, '/') . '$/', $domain); |
|
| 369 | + return (bool)\preg_match('/\\.'.\preg_quote($cookieDomain, '/').'$/', $domain); |
|
| 370 | 370 | } |
| 371 | 371 | /** |
| 372 | 372 | * Check if the cookie is expired. |
@@ -388,7 +388,7 @@ discard block |
||
| 388 | 388 | } |
| 389 | 389 | // Check if any of the invalid characters are present in the cookie name |
| 390 | 390 | if (\preg_match('/[\\x00-\\x20\\x22\\x28-\\x29\\x2c\\x2f\\x3a-\\x40\\x5c\\x7b\\x7d\\x7f]/', $name)) { |
| 391 | - return 'Cookie name must not contain invalid characters: ASCII ' . 'Control characters (0-31;127), space, tab and the ' . 'following characters: ()<>@,;:\\"/?={}'; |
|
| 391 | + return 'Cookie name must not contain invalid characters: ASCII '.'Control characters (0-31;127), space, tab and the '.'following characters: ()<>@,;:\\"/?={}'; |
|
| 392 | 392 | } |
| 393 | 393 | // Value must not be null. 0 and empty string are valid. Empty strings |
| 394 | 394 | // are technically against RFC 6265, but known to happen in the wild. |
@@ -5,8 +5,7 @@ |
||
| 5 | 5 | /** |
| 6 | 6 | * Persists cookies in the client session |
| 7 | 7 | */ |
| 8 | -class SessionCookieJar extends CookieJar |
|
| 9 | -{ |
|
| 8 | +class SessionCookieJar extends CookieJar { |
|
| 10 | 9 | /** |
| 11 | 10 | * @var string session key |
| 12 | 11 | */ |
@@ -7,65 +7,65 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class SessionCookieJar extends CookieJar |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * @var string session key |
|
| 12 | - */ |
|
| 13 | - private $sessionKey; |
|
| 14 | - /** |
|
| 15 | - * @var bool Control whether to persist session cookies or not. |
|
| 16 | - */ |
|
| 17 | - private $storeSessionCookies; |
|
| 18 | - /** |
|
| 19 | - * Create a new SessionCookieJar object |
|
| 20 | - * |
|
| 21 | - * @param string $sessionKey Session key name to store the cookie |
|
| 22 | - * data in session |
|
| 23 | - * @param bool $storeSessionCookies Set to true to store session cookies |
|
| 24 | - * in the cookie jar. |
|
| 25 | - */ |
|
| 26 | - public function __construct(string $sessionKey, bool $storeSessionCookies = \false) |
|
| 27 | - { |
|
| 28 | - parent::__construct(); |
|
| 29 | - $this->sessionKey = $sessionKey; |
|
| 30 | - $this->storeSessionCookies = $storeSessionCookies; |
|
| 31 | - $this->load(); |
|
| 32 | - } |
|
| 33 | - /** |
|
| 34 | - * Saves cookies to session when shutting down |
|
| 35 | - */ |
|
| 36 | - public function __destruct() |
|
| 37 | - { |
|
| 38 | - $this->save(); |
|
| 39 | - } |
|
| 40 | - /** |
|
| 41 | - * Save cookies to the client session |
|
| 42 | - */ |
|
| 43 | - public function save() : void |
|
| 44 | - { |
|
| 45 | - $json = []; |
|
| 46 | - /** @var SetCookie $cookie */ |
|
| 47 | - foreach ($this as $cookie) { |
|
| 48 | - if (CookieJar::shouldPersist($cookie, $this->storeSessionCookies)) { |
|
| 49 | - $json[] = $cookie->toArray(); |
|
| 50 | - } |
|
| 51 | - } |
|
| 52 | - $_SESSION[$this->sessionKey] = \json_encode($json); |
|
| 53 | - } |
|
| 54 | - /** |
|
| 55 | - * Load the contents of the client session into the data array |
|
| 56 | - */ |
|
| 57 | - protected function load() : void |
|
| 58 | - { |
|
| 59 | - if (!isset($_SESSION[$this->sessionKey])) { |
|
| 60 | - return; |
|
| 61 | - } |
|
| 62 | - $data = \json_decode($_SESSION[$this->sessionKey], \true); |
|
| 63 | - if (\is_array($data)) { |
|
| 64 | - foreach ($data as $cookie) { |
|
| 65 | - $this->setCookie(new SetCookie($cookie)); |
|
| 66 | - } |
|
| 67 | - } elseif (\strlen($data)) { |
|
| 68 | - throw new \RuntimeException('Invalid cookie data'); |
|
| 69 | - } |
|
| 70 | - } |
|
| 10 | + /** |
|
| 11 | + * @var string session key |
|
| 12 | + */ |
|
| 13 | + private $sessionKey; |
|
| 14 | + /** |
|
| 15 | + * @var bool Control whether to persist session cookies or not. |
|
| 16 | + */ |
|
| 17 | + private $storeSessionCookies; |
|
| 18 | + /** |
|
| 19 | + * Create a new SessionCookieJar object |
|
| 20 | + * |
|
| 21 | + * @param string $sessionKey Session key name to store the cookie |
|
| 22 | + * data in session |
|
| 23 | + * @param bool $storeSessionCookies Set to true to store session cookies |
|
| 24 | + * in the cookie jar. |
|
| 25 | + */ |
|
| 26 | + public function __construct(string $sessionKey, bool $storeSessionCookies = \false) |
|
| 27 | + { |
|
| 28 | + parent::__construct(); |
|
| 29 | + $this->sessionKey = $sessionKey; |
|
| 30 | + $this->storeSessionCookies = $storeSessionCookies; |
|
| 31 | + $this->load(); |
|
| 32 | + } |
|
| 33 | + /** |
|
| 34 | + * Saves cookies to session when shutting down |
|
| 35 | + */ |
|
| 36 | + public function __destruct() |
|
| 37 | + { |
|
| 38 | + $this->save(); |
|
| 39 | + } |
|
| 40 | + /** |
|
| 41 | + * Save cookies to the client session |
|
| 42 | + */ |
|
| 43 | + public function save() : void |
|
| 44 | + { |
|
| 45 | + $json = []; |
|
| 46 | + /** @var SetCookie $cookie */ |
|
| 47 | + foreach ($this as $cookie) { |
|
| 48 | + if (CookieJar::shouldPersist($cookie, $this->storeSessionCookies)) { |
|
| 49 | + $json[] = $cookie->toArray(); |
|
| 50 | + } |
|
| 51 | + } |
|
| 52 | + $_SESSION[$this->sessionKey] = \json_encode($json); |
|
| 53 | + } |
|
| 54 | + /** |
|
| 55 | + * Load the contents of the client session into the data array |
|
| 56 | + */ |
|
| 57 | + protected function load() : void |
|
| 58 | + { |
|
| 59 | + if (!isset($_SESSION[$this->sessionKey])) { |
|
| 60 | + return; |
|
| 61 | + } |
|
| 62 | + $data = \json_decode($_SESSION[$this->sessionKey], \true); |
|
| 63 | + if (\is_array($data)) { |
|
| 64 | + foreach ($data as $cookie) { |
|
| 65 | + $this->setCookie(new SetCookie($cookie)); |
|
| 66 | + } |
|
| 67 | + } elseif (\strlen($data)) { |
|
| 68 | + throw new \RuntimeException('Invalid cookie data'); |
|
| 69 | + } |
|
| 70 | + } |
|
| 71 | 71 | } |
@@ -7,8 +7,7 @@ |
||
| 7 | 7 | /** |
| 8 | 8 | * Persists non-session cookies using a JSON formatted file |
| 9 | 9 | */ |
| 10 | -class FileCookieJar extends CookieJar |
|
| 11 | -{ |
|
| 10 | +class FileCookieJar extends CookieJar { |
|
| 12 | 11 | /** |
| 13 | 12 | * @var string filename |
| 14 | 13 | */ |
@@ -8,85 +8,85 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | class FileCookieJar extends CookieJar |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * @var string filename |
|
| 13 | - */ |
|
| 14 | - private $filename; |
|
| 15 | - /** |
|
| 16 | - * @var bool Control whether to persist session cookies or not. |
|
| 17 | - */ |
|
| 18 | - private $storeSessionCookies; |
|
| 19 | - /** |
|
| 20 | - * Create a new FileCookieJar object |
|
| 21 | - * |
|
| 22 | - * @param string $cookieFile File to store the cookie data |
|
| 23 | - * @param bool $storeSessionCookies Set to true to store session cookies |
|
| 24 | - * in the cookie jar. |
|
| 25 | - * |
|
| 26 | - * @throws \RuntimeException if the file cannot be found or created |
|
| 27 | - */ |
|
| 28 | - public function __construct(string $cookieFile, bool $storeSessionCookies = \false) |
|
| 29 | - { |
|
| 30 | - parent::__construct(); |
|
| 31 | - $this->filename = $cookieFile; |
|
| 32 | - $this->storeSessionCookies = $storeSessionCookies; |
|
| 33 | - if (\file_exists($cookieFile)) { |
|
| 34 | - $this->load($cookieFile); |
|
| 35 | - } |
|
| 36 | - } |
|
| 37 | - /** |
|
| 38 | - * Saves the file when shutting down |
|
| 39 | - */ |
|
| 40 | - public function __destruct() |
|
| 41 | - { |
|
| 42 | - $this->save($this->filename); |
|
| 43 | - } |
|
| 44 | - /** |
|
| 45 | - * Saves the cookies to a file. |
|
| 46 | - * |
|
| 47 | - * @param string $filename File to save |
|
| 48 | - * |
|
| 49 | - * @throws \RuntimeException if the file cannot be found or created |
|
| 50 | - */ |
|
| 51 | - public function save(string $filename) : void |
|
| 52 | - { |
|
| 53 | - $json = []; |
|
| 54 | - /** @var SetCookie $cookie */ |
|
| 55 | - foreach ($this as $cookie) { |
|
| 56 | - if (CookieJar::shouldPersist($cookie, $this->storeSessionCookies)) { |
|
| 57 | - $json[] = $cookie->toArray(); |
|
| 58 | - } |
|
| 59 | - } |
|
| 60 | - $jsonStr = Utils::jsonEncode($json); |
|
| 61 | - if (\false === \file_put_contents($filename, $jsonStr, \LOCK_EX)) { |
|
| 62 | - throw new \RuntimeException("Unable to save file {$filename}"); |
|
| 63 | - } |
|
| 64 | - } |
|
| 65 | - /** |
|
| 66 | - * Load cookies from a JSON formatted file. |
|
| 67 | - * |
|
| 68 | - * Old cookies are kept unless overwritten by newly loaded ones. |
|
| 69 | - * |
|
| 70 | - * @param string $filename Cookie file to load. |
|
| 71 | - * |
|
| 72 | - * @throws \RuntimeException if the file cannot be loaded. |
|
| 73 | - */ |
|
| 74 | - public function load(string $filename) : void |
|
| 75 | - { |
|
| 76 | - $json = \file_get_contents($filename); |
|
| 77 | - if (\false === $json) { |
|
| 78 | - throw new \RuntimeException("Unable to load file {$filename}"); |
|
| 79 | - } |
|
| 80 | - if ($json === '') { |
|
| 81 | - return; |
|
| 82 | - } |
|
| 83 | - $data = Utils::jsonDecode($json, \true); |
|
| 84 | - if (\is_array($data)) { |
|
| 85 | - foreach ($data as $cookie) { |
|
| 86 | - $this->setCookie(new SetCookie($cookie)); |
|
| 87 | - } |
|
| 88 | - } elseif (\is_scalar($data) && !empty($data)) { |
|
| 89 | - throw new \RuntimeException("Invalid cookie file: {$filename}"); |
|
| 90 | - } |
|
| 91 | - } |
|
| 11 | + /** |
|
| 12 | + * @var string filename |
|
| 13 | + */ |
|
| 14 | + private $filename; |
|
| 15 | + /** |
|
| 16 | + * @var bool Control whether to persist session cookies or not. |
|
| 17 | + */ |
|
| 18 | + private $storeSessionCookies; |
|
| 19 | + /** |
|
| 20 | + * Create a new FileCookieJar object |
|
| 21 | + * |
|
| 22 | + * @param string $cookieFile File to store the cookie data |
|
| 23 | + * @param bool $storeSessionCookies Set to true to store session cookies |
|
| 24 | + * in the cookie jar. |
|
| 25 | + * |
|
| 26 | + * @throws \RuntimeException if the file cannot be found or created |
|
| 27 | + */ |
|
| 28 | + public function __construct(string $cookieFile, bool $storeSessionCookies = \false) |
|
| 29 | + { |
|
| 30 | + parent::__construct(); |
|
| 31 | + $this->filename = $cookieFile; |
|
| 32 | + $this->storeSessionCookies = $storeSessionCookies; |
|
| 33 | + if (\file_exists($cookieFile)) { |
|
| 34 | + $this->load($cookieFile); |
|
| 35 | + } |
|
| 36 | + } |
|
| 37 | + /** |
|
| 38 | + * Saves the file when shutting down |
|
| 39 | + */ |
|
| 40 | + public function __destruct() |
|
| 41 | + { |
|
| 42 | + $this->save($this->filename); |
|
| 43 | + } |
|
| 44 | + /** |
|
| 45 | + * Saves the cookies to a file. |
|
| 46 | + * |
|
| 47 | + * @param string $filename File to save |
|
| 48 | + * |
|
| 49 | + * @throws \RuntimeException if the file cannot be found or created |
|
| 50 | + */ |
|
| 51 | + public function save(string $filename) : void |
|
| 52 | + { |
|
| 53 | + $json = []; |
|
| 54 | + /** @var SetCookie $cookie */ |
|
| 55 | + foreach ($this as $cookie) { |
|
| 56 | + if (CookieJar::shouldPersist($cookie, $this->storeSessionCookies)) { |
|
| 57 | + $json[] = $cookie->toArray(); |
|
| 58 | + } |
|
| 59 | + } |
|
| 60 | + $jsonStr = Utils::jsonEncode($json); |
|
| 61 | + if (\false === \file_put_contents($filename, $jsonStr, \LOCK_EX)) { |
|
| 62 | + throw new \RuntimeException("Unable to save file {$filename}"); |
|
| 63 | + } |
|
| 64 | + } |
|
| 65 | + /** |
|
| 66 | + * Load cookies from a JSON formatted file. |
|
| 67 | + * |
|
| 68 | + * Old cookies are kept unless overwritten by newly loaded ones. |
|
| 69 | + * |
|
| 70 | + * @param string $filename Cookie file to load. |
|
| 71 | + * |
|
| 72 | + * @throws \RuntimeException if the file cannot be loaded. |
|
| 73 | + */ |
|
| 74 | + public function load(string $filename) : void |
|
| 75 | + { |
|
| 76 | + $json = \file_get_contents($filename); |
|
| 77 | + if (\false === $json) { |
|
| 78 | + throw new \RuntimeException("Unable to load file {$filename}"); |
|
| 79 | + } |
|
| 80 | + if ($json === '') { |
|
| 81 | + return; |
|
| 82 | + } |
|
| 83 | + $data = Utils::jsonDecode($json, \true); |
|
| 84 | + if (\is_array($data)) { |
|
| 85 | + foreach ($data as $cookie) { |
|
| 86 | + $this->setCookie(new SetCookie($cookie)); |
|
| 87 | + } |
|
| 88 | + } elseif (\is_scalar($data) && !empty($data)) { |
|
| 89 | + throw new \RuntimeException("Invalid cookie file: {$filename}"); |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | 92 | } |
@@ -8,8 +8,7 @@ |
||
| 8 | 8 | /** |
| 9 | 9 | * Cookie jar that stores cookies as an array |
| 10 | 10 | */ |
| 11 | -class CookieJar implements CookieJarInterface |
|
| 12 | -{ |
|
| 11 | +class CookieJar implements CookieJarInterface { |
|
| 13 | 12 | /** |
| 14 | 13 | * @var SetCookie[] Loaded cookie data |
| 15 | 14 | */ |
@@ -82,7 +82,7 @@ discard block |
||
| 82 | 82 | } |
| 83 | 83 | public function toArray() : array |
| 84 | 84 | { |
| 85 | - return \array_map(static function (SetCookie $cookie) : array { |
|
| 85 | + return \array_map(static function(SetCookie $cookie) : array { |
|
| 86 | 86 | return $cookie->toArray(); |
| 87 | 87 | }, $this->getIterator()->getArrayCopy()); |
| 88 | 88 | } |
@@ -92,22 +92,22 @@ discard block |
||
| 92 | 92 | $this->cookies = []; |
| 93 | 93 | return; |
| 94 | 94 | } elseif (!$path) { |
| 95 | - $this->cookies = \array_filter($this->cookies, static function (SetCookie $cookie) use($domain) : bool { |
|
| 95 | + $this->cookies = \array_filter($this->cookies, static function(SetCookie $cookie) use($domain) : bool { |
|
| 96 | 96 | return !$cookie->matchesDomain($domain); |
| 97 | 97 | }); |
| 98 | 98 | } elseif (!$name) { |
| 99 | - $this->cookies = \array_filter($this->cookies, static function (SetCookie $cookie) use($path, $domain) : bool { |
|
| 99 | + $this->cookies = \array_filter($this->cookies, static function(SetCookie $cookie) use($path, $domain) : bool { |
|
| 100 | 100 | return !($cookie->matchesPath($path) && $cookie->matchesDomain($domain)); |
| 101 | 101 | }); |
| 102 | 102 | } else { |
| 103 | - $this->cookies = \array_filter($this->cookies, static function (SetCookie $cookie) use($path, $domain, $name) { |
|
| 103 | + $this->cookies = \array_filter($this->cookies, static function(SetCookie $cookie) use($path, $domain, $name) { |
|
| 104 | 104 | return !($cookie->getName() == $name && $cookie->matchesPath($path) && $cookie->matchesDomain($domain)); |
| 105 | 105 | }); |
| 106 | 106 | } |
| 107 | 107 | } |
| 108 | 108 | public function clearSessionCookies() : void |
| 109 | 109 | { |
| 110 | - $this->cookies = \array_filter($this->cookies, static function (SetCookie $cookie) : bool { |
|
| 110 | + $this->cookies = \array_filter($this->cookies, static function(SetCookie $cookie) : bool { |
|
| 111 | 111 | return !$cookie->getDiscard() && $cookie->getExpires(); |
| 112 | 112 | }); |
| 113 | 113 | } |
@@ -123,7 +123,7 @@ discard block |
||
| 123 | 123 | $result = $cookie->validate(); |
| 124 | 124 | if ($result !== \true) { |
| 125 | 125 | if ($this->strictMode) { |
| 126 | - throw new \RuntimeException('Invalid cookie: ' . $result); |
|
| 126 | + throw new \RuntimeException('Invalid cookie: '.$result); |
|
| 127 | 127 | } |
| 128 | 128 | $this->removeCookieIfEmpty($cookie); |
| 129 | 129 | return \false; |
@@ -221,7 +221,7 @@ discard block |
||
| 221 | 221 | $path = $uri->getPath() ?: '/'; |
| 222 | 222 | foreach ($this->cookies as $cookie) { |
| 223 | 223 | if ($cookie->matchesPath($path) && $cookie->matchesDomain($host) && !$cookie->isExpired() && (!$cookie->getSecure() || $scheme === 'https')) { |
| 224 | - $values[] = $cookie->getName() . '=' . $cookie->getValue(); |
|
| 224 | + $values[] = $cookie->getName().'='.$cookie->getValue(); |
|
| 225 | 225 | } |
| 226 | 226 | } |
| 227 | 227 | return $values ? $request->withHeader('Cookie', \implode('; ', $values)) : $request; |
@@ -9,232 +9,232 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class CookieJar implements CookieJarInterface |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * @var SetCookie[] Loaded cookie data |
|
| 14 | - */ |
|
| 15 | - private $cookies = []; |
|
| 16 | - /** |
|
| 17 | - * @var bool |
|
| 18 | - */ |
|
| 19 | - private $strictMode; |
|
| 20 | - /** |
|
| 21 | - * @param bool $strictMode Set to true to throw exceptions when invalid |
|
| 22 | - * cookies are added to the cookie jar. |
|
| 23 | - * @param array $cookieArray Array of SetCookie objects or a hash of |
|
| 24 | - * arrays that can be used with the SetCookie |
|
| 25 | - * constructor |
|
| 26 | - */ |
|
| 27 | - public function __construct(bool $strictMode = \false, array $cookieArray = []) |
|
| 28 | - { |
|
| 29 | - $this->strictMode = $strictMode; |
|
| 30 | - foreach ($cookieArray as $cookie) { |
|
| 31 | - if (!$cookie instanceof SetCookie) { |
|
| 32 | - $cookie = new SetCookie($cookie); |
|
| 33 | - } |
|
| 34 | - $this->setCookie($cookie); |
|
| 35 | - } |
|
| 36 | - } |
|
| 37 | - /** |
|
| 38 | - * Create a new Cookie jar from an associative array and domain. |
|
| 39 | - * |
|
| 40 | - * @param array $cookies Cookies to create the jar from |
|
| 41 | - * @param string $domain Domain to set the cookies to |
|
| 42 | - */ |
|
| 43 | - public static function fromArray(array $cookies, string $domain) : self |
|
| 44 | - { |
|
| 45 | - $cookieJar = new self(); |
|
| 46 | - foreach ($cookies as $name => $value) { |
|
| 47 | - $cookieJar->setCookie(new SetCookie(['Domain' => $domain, 'Name' => $name, 'Value' => $value, 'Discard' => \true])); |
|
| 48 | - } |
|
| 49 | - return $cookieJar; |
|
| 50 | - } |
|
| 51 | - /** |
|
| 52 | - * Evaluate if this cookie should be persisted to storage |
|
| 53 | - * that survives between requests. |
|
| 54 | - * |
|
| 55 | - * @param SetCookie $cookie Being evaluated. |
|
| 56 | - * @param bool $allowSessionCookies If we should persist session cookies |
|
| 57 | - */ |
|
| 58 | - public static function shouldPersist(SetCookie $cookie, bool $allowSessionCookies = \false) : bool |
|
| 59 | - { |
|
| 60 | - if ($cookie->getExpires() || $allowSessionCookies) { |
|
| 61 | - if (!$cookie->getDiscard()) { |
|
| 62 | - return \true; |
|
| 63 | - } |
|
| 64 | - } |
|
| 65 | - return \false; |
|
| 66 | - } |
|
| 67 | - /** |
|
| 68 | - * Finds and returns the cookie based on the name |
|
| 69 | - * |
|
| 70 | - * @param string $name cookie name to search for |
|
| 71 | - * |
|
| 72 | - * @return SetCookie|null cookie that was found or null if not found |
|
| 73 | - */ |
|
| 74 | - public function getCookieByName(string $name) : ?SetCookie |
|
| 75 | - { |
|
| 76 | - foreach ($this->cookies as $cookie) { |
|
| 77 | - if ($cookie->getName() !== null && \strcasecmp($cookie->getName(), $name) === 0) { |
|
| 78 | - return $cookie; |
|
| 79 | - } |
|
| 80 | - } |
|
| 81 | - return null; |
|
| 82 | - } |
|
| 83 | - public function toArray() : array |
|
| 84 | - { |
|
| 85 | - return \array_map(static function (SetCookie $cookie) : array { |
|
| 86 | - return $cookie->toArray(); |
|
| 87 | - }, $this->getIterator()->getArrayCopy()); |
|
| 88 | - } |
|
| 89 | - public function clear(?string $domain = null, ?string $path = null, ?string $name = null) : void |
|
| 90 | - { |
|
| 91 | - if (!$domain) { |
|
| 92 | - $this->cookies = []; |
|
| 93 | - return; |
|
| 94 | - } elseif (!$path) { |
|
| 95 | - $this->cookies = \array_filter($this->cookies, static function (SetCookie $cookie) use($domain) : bool { |
|
| 96 | - return !$cookie->matchesDomain($domain); |
|
| 97 | - }); |
|
| 98 | - } elseif (!$name) { |
|
| 99 | - $this->cookies = \array_filter($this->cookies, static function (SetCookie $cookie) use($path, $domain) : bool { |
|
| 100 | - return !($cookie->matchesPath($path) && $cookie->matchesDomain($domain)); |
|
| 101 | - }); |
|
| 102 | - } else { |
|
| 103 | - $this->cookies = \array_filter($this->cookies, static function (SetCookie $cookie) use($path, $domain, $name) { |
|
| 104 | - return !($cookie->getName() == $name && $cookie->matchesPath($path) && $cookie->matchesDomain($domain)); |
|
| 105 | - }); |
|
| 106 | - } |
|
| 107 | - } |
|
| 108 | - public function clearSessionCookies() : void |
|
| 109 | - { |
|
| 110 | - $this->cookies = \array_filter($this->cookies, static function (SetCookie $cookie) : bool { |
|
| 111 | - return !$cookie->getDiscard() && $cookie->getExpires(); |
|
| 112 | - }); |
|
| 113 | - } |
|
| 114 | - public function setCookie(SetCookie $cookie) : bool |
|
| 115 | - { |
|
| 116 | - // If the name string is empty (but not 0), ignore the set-cookie |
|
| 117 | - // string entirely. |
|
| 118 | - $name = $cookie->getName(); |
|
| 119 | - if (!$name && $name !== '0') { |
|
| 120 | - return \false; |
|
| 121 | - } |
|
| 122 | - // Only allow cookies with set and valid domain, name, value |
|
| 123 | - $result = $cookie->validate(); |
|
| 124 | - if ($result !== \true) { |
|
| 125 | - if ($this->strictMode) { |
|
| 126 | - throw new \RuntimeException('Invalid cookie: ' . $result); |
|
| 127 | - } |
|
| 128 | - $this->removeCookieIfEmpty($cookie); |
|
| 129 | - return \false; |
|
| 130 | - } |
|
| 131 | - // Resolve conflicts with previously set cookies |
|
| 132 | - foreach ($this->cookies as $i => $c) { |
|
| 133 | - // Two cookies are identical, when their path, and domain are |
|
| 134 | - // identical. |
|
| 135 | - if ($c->getPath() != $cookie->getPath() || $c->getDomain() != $cookie->getDomain() || $c->getName() != $cookie->getName()) { |
|
| 136 | - continue; |
|
| 137 | - } |
|
| 138 | - // The previously set cookie is a discard cookie and this one is |
|
| 139 | - // not so allow the new cookie to be set |
|
| 140 | - if (!$cookie->getDiscard() && $c->getDiscard()) { |
|
| 141 | - unset($this->cookies[$i]); |
|
| 142 | - continue; |
|
| 143 | - } |
|
| 144 | - // If the new cookie's expiration is further into the future, then |
|
| 145 | - // replace the old cookie |
|
| 146 | - if ($cookie->getExpires() > $c->getExpires()) { |
|
| 147 | - unset($this->cookies[$i]); |
|
| 148 | - continue; |
|
| 149 | - } |
|
| 150 | - // If the value has changed, we better change it |
|
| 151 | - if ($cookie->getValue() !== $c->getValue()) { |
|
| 152 | - unset($this->cookies[$i]); |
|
| 153 | - continue; |
|
| 154 | - } |
|
| 155 | - // The cookie exists, so no need to continue |
|
| 156 | - return \false; |
|
| 157 | - } |
|
| 158 | - $this->cookies[] = $cookie; |
|
| 159 | - return \true; |
|
| 160 | - } |
|
| 161 | - public function count() : int |
|
| 162 | - { |
|
| 163 | - return \count($this->cookies); |
|
| 164 | - } |
|
| 165 | - /** |
|
| 166 | - * @return \ArrayIterator<int, SetCookie> |
|
| 167 | - */ |
|
| 168 | - public function getIterator() : \ArrayIterator |
|
| 169 | - { |
|
| 170 | - return new \ArrayIterator(\array_values($this->cookies)); |
|
| 171 | - } |
|
| 172 | - public function extractCookies(RequestInterface $request, ResponseInterface $response) : void |
|
| 173 | - { |
|
| 174 | - if ($cookieHeader = $response->getHeader('Set-Cookie')) { |
|
| 175 | - foreach ($cookieHeader as $cookie) { |
|
| 176 | - $sc = SetCookie::fromString($cookie); |
|
| 177 | - if (!$sc->getDomain()) { |
|
| 178 | - $sc->setDomain($request->getUri()->getHost()); |
|
| 179 | - } |
|
| 180 | - if (0 !== \strpos($sc->getPath(), '/')) { |
|
| 181 | - $sc->setPath($this->getCookiePathFromRequest($request)); |
|
| 182 | - } |
|
| 183 | - if (!$sc->matchesDomain($request->getUri()->getHost())) { |
|
| 184 | - continue; |
|
| 185 | - } |
|
| 186 | - // Note: At this point `$sc->getDomain()` being a public suffix should |
|
| 187 | - // be rejected, but we don't want to pull in the full PSL dependency. |
|
| 188 | - $this->setCookie($sc); |
|
| 189 | - } |
|
| 190 | - } |
|
| 191 | - } |
|
| 192 | - /** |
|
| 193 | - * Computes cookie path following RFC 6265 section 5.1.4 |
|
| 194 | - * |
|
| 195 | - * @see https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.4 |
|
| 196 | - */ |
|
| 197 | - private function getCookiePathFromRequest(RequestInterface $request) : string |
|
| 198 | - { |
|
| 199 | - $uriPath = $request->getUri()->getPath(); |
|
| 200 | - if ('' === $uriPath) { |
|
| 201 | - return '/'; |
|
| 202 | - } |
|
| 203 | - if (0 !== \strpos($uriPath, '/')) { |
|
| 204 | - return '/'; |
|
| 205 | - } |
|
| 206 | - if ('/' === $uriPath) { |
|
| 207 | - return '/'; |
|
| 208 | - } |
|
| 209 | - $lastSlashPos = \strrpos($uriPath, '/'); |
|
| 210 | - if (0 === $lastSlashPos || \false === $lastSlashPos) { |
|
| 211 | - return '/'; |
|
| 212 | - } |
|
| 213 | - return \substr($uriPath, 0, $lastSlashPos); |
|
| 214 | - } |
|
| 215 | - public function withCookieHeader(RequestInterface $request) : RequestInterface |
|
| 216 | - { |
|
| 217 | - $values = []; |
|
| 218 | - $uri = $request->getUri(); |
|
| 219 | - $scheme = $uri->getScheme(); |
|
| 220 | - $host = $uri->getHost(); |
|
| 221 | - $path = $uri->getPath() ?: '/'; |
|
| 222 | - foreach ($this->cookies as $cookie) { |
|
| 223 | - if ($cookie->matchesPath($path) && $cookie->matchesDomain($host) && !$cookie->isExpired() && (!$cookie->getSecure() || $scheme === 'https')) { |
|
| 224 | - $values[] = $cookie->getName() . '=' . $cookie->getValue(); |
|
| 225 | - } |
|
| 226 | - } |
|
| 227 | - return $values ? $request->withHeader('Cookie', \implode('; ', $values)) : $request; |
|
| 228 | - } |
|
| 229 | - /** |
|
| 230 | - * If a cookie already exists and the server asks to set it again with a |
|
| 231 | - * null value, the cookie must be deleted. |
|
| 232 | - */ |
|
| 233 | - private function removeCookieIfEmpty(SetCookie $cookie) : void |
|
| 234 | - { |
|
| 235 | - $cookieValue = $cookie->getValue(); |
|
| 236 | - if ($cookieValue === null || $cookieValue === '') { |
|
| 237 | - $this->clear($cookie->getDomain(), $cookie->getPath(), $cookie->getName()); |
|
| 238 | - } |
|
| 239 | - } |
|
| 12 | + /** |
|
| 13 | + * @var SetCookie[] Loaded cookie data |
|
| 14 | + */ |
|
| 15 | + private $cookies = []; |
|
| 16 | + /** |
|
| 17 | + * @var bool |
|
| 18 | + */ |
|
| 19 | + private $strictMode; |
|
| 20 | + /** |
|
| 21 | + * @param bool $strictMode Set to true to throw exceptions when invalid |
|
| 22 | + * cookies are added to the cookie jar. |
|
| 23 | + * @param array $cookieArray Array of SetCookie objects or a hash of |
|
| 24 | + * arrays that can be used with the SetCookie |
|
| 25 | + * constructor |
|
| 26 | + */ |
|
| 27 | + public function __construct(bool $strictMode = \false, array $cookieArray = []) |
|
| 28 | + { |
|
| 29 | + $this->strictMode = $strictMode; |
|
| 30 | + foreach ($cookieArray as $cookie) { |
|
| 31 | + if (!$cookie instanceof SetCookie) { |
|
| 32 | + $cookie = new SetCookie($cookie); |
|
| 33 | + } |
|
| 34 | + $this->setCookie($cookie); |
|
| 35 | + } |
|
| 36 | + } |
|
| 37 | + /** |
|
| 38 | + * Create a new Cookie jar from an associative array and domain. |
|
| 39 | + * |
|
| 40 | + * @param array $cookies Cookies to create the jar from |
|
| 41 | + * @param string $domain Domain to set the cookies to |
|
| 42 | + */ |
|
| 43 | + public static function fromArray(array $cookies, string $domain) : self |
|
| 44 | + { |
|
| 45 | + $cookieJar = new self(); |
|
| 46 | + foreach ($cookies as $name => $value) { |
|
| 47 | + $cookieJar->setCookie(new SetCookie(['Domain' => $domain, 'Name' => $name, 'Value' => $value, 'Discard' => \true])); |
|
| 48 | + } |
|
| 49 | + return $cookieJar; |
|
| 50 | + } |
|
| 51 | + /** |
|
| 52 | + * Evaluate if this cookie should be persisted to storage |
|
| 53 | + * that survives between requests. |
|
| 54 | + * |
|
| 55 | + * @param SetCookie $cookie Being evaluated. |
|
| 56 | + * @param bool $allowSessionCookies If we should persist session cookies |
|
| 57 | + */ |
|
| 58 | + public static function shouldPersist(SetCookie $cookie, bool $allowSessionCookies = \false) : bool |
|
| 59 | + { |
|
| 60 | + if ($cookie->getExpires() || $allowSessionCookies) { |
|
| 61 | + if (!$cookie->getDiscard()) { |
|
| 62 | + return \true; |
|
| 63 | + } |
|
| 64 | + } |
|
| 65 | + return \false; |
|
| 66 | + } |
|
| 67 | + /** |
|
| 68 | + * Finds and returns the cookie based on the name |
|
| 69 | + * |
|
| 70 | + * @param string $name cookie name to search for |
|
| 71 | + * |
|
| 72 | + * @return SetCookie|null cookie that was found or null if not found |
|
| 73 | + */ |
|
| 74 | + public function getCookieByName(string $name) : ?SetCookie |
|
| 75 | + { |
|
| 76 | + foreach ($this->cookies as $cookie) { |
|
| 77 | + if ($cookie->getName() !== null && \strcasecmp($cookie->getName(), $name) === 0) { |
|
| 78 | + return $cookie; |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | + return null; |
|
| 82 | + } |
|
| 83 | + public function toArray() : array |
|
| 84 | + { |
|
| 85 | + return \array_map(static function (SetCookie $cookie) : array { |
|
| 86 | + return $cookie->toArray(); |
|
| 87 | + }, $this->getIterator()->getArrayCopy()); |
|
| 88 | + } |
|
| 89 | + public function clear(?string $domain = null, ?string $path = null, ?string $name = null) : void |
|
| 90 | + { |
|
| 91 | + if (!$domain) { |
|
| 92 | + $this->cookies = []; |
|
| 93 | + return; |
|
| 94 | + } elseif (!$path) { |
|
| 95 | + $this->cookies = \array_filter($this->cookies, static function (SetCookie $cookie) use($domain) : bool { |
|
| 96 | + return !$cookie->matchesDomain($domain); |
|
| 97 | + }); |
|
| 98 | + } elseif (!$name) { |
|
| 99 | + $this->cookies = \array_filter($this->cookies, static function (SetCookie $cookie) use($path, $domain) : bool { |
|
| 100 | + return !($cookie->matchesPath($path) && $cookie->matchesDomain($domain)); |
|
| 101 | + }); |
|
| 102 | + } else { |
|
| 103 | + $this->cookies = \array_filter($this->cookies, static function (SetCookie $cookie) use($path, $domain, $name) { |
|
| 104 | + return !($cookie->getName() == $name && $cookie->matchesPath($path) && $cookie->matchesDomain($domain)); |
|
| 105 | + }); |
|
| 106 | + } |
|
| 107 | + } |
|
| 108 | + public function clearSessionCookies() : void |
|
| 109 | + { |
|
| 110 | + $this->cookies = \array_filter($this->cookies, static function (SetCookie $cookie) : bool { |
|
| 111 | + return !$cookie->getDiscard() && $cookie->getExpires(); |
|
| 112 | + }); |
|
| 113 | + } |
|
| 114 | + public function setCookie(SetCookie $cookie) : bool |
|
| 115 | + { |
|
| 116 | + // If the name string is empty (but not 0), ignore the set-cookie |
|
| 117 | + // string entirely. |
|
| 118 | + $name = $cookie->getName(); |
|
| 119 | + if (!$name && $name !== '0') { |
|
| 120 | + return \false; |
|
| 121 | + } |
|
| 122 | + // Only allow cookies with set and valid domain, name, value |
|
| 123 | + $result = $cookie->validate(); |
|
| 124 | + if ($result !== \true) { |
|
| 125 | + if ($this->strictMode) { |
|
| 126 | + throw new \RuntimeException('Invalid cookie: ' . $result); |
|
| 127 | + } |
|
| 128 | + $this->removeCookieIfEmpty($cookie); |
|
| 129 | + return \false; |
|
| 130 | + } |
|
| 131 | + // Resolve conflicts with previously set cookies |
|
| 132 | + foreach ($this->cookies as $i => $c) { |
|
| 133 | + // Two cookies are identical, when their path, and domain are |
|
| 134 | + // identical. |
|
| 135 | + if ($c->getPath() != $cookie->getPath() || $c->getDomain() != $cookie->getDomain() || $c->getName() != $cookie->getName()) { |
|
| 136 | + continue; |
|
| 137 | + } |
|
| 138 | + // The previously set cookie is a discard cookie and this one is |
|
| 139 | + // not so allow the new cookie to be set |
|
| 140 | + if (!$cookie->getDiscard() && $c->getDiscard()) { |
|
| 141 | + unset($this->cookies[$i]); |
|
| 142 | + continue; |
|
| 143 | + } |
|
| 144 | + // If the new cookie's expiration is further into the future, then |
|
| 145 | + // replace the old cookie |
|
| 146 | + if ($cookie->getExpires() > $c->getExpires()) { |
|
| 147 | + unset($this->cookies[$i]); |
|
| 148 | + continue; |
|
| 149 | + } |
|
| 150 | + // If the value has changed, we better change it |
|
| 151 | + if ($cookie->getValue() !== $c->getValue()) { |
|
| 152 | + unset($this->cookies[$i]); |
|
| 153 | + continue; |
|
| 154 | + } |
|
| 155 | + // The cookie exists, so no need to continue |
|
| 156 | + return \false; |
|
| 157 | + } |
|
| 158 | + $this->cookies[] = $cookie; |
|
| 159 | + return \true; |
|
| 160 | + } |
|
| 161 | + public function count() : int |
|
| 162 | + { |
|
| 163 | + return \count($this->cookies); |
|
| 164 | + } |
|
| 165 | + /** |
|
| 166 | + * @return \ArrayIterator<int, SetCookie> |
|
| 167 | + */ |
|
| 168 | + public function getIterator() : \ArrayIterator |
|
| 169 | + { |
|
| 170 | + return new \ArrayIterator(\array_values($this->cookies)); |
|
| 171 | + } |
|
| 172 | + public function extractCookies(RequestInterface $request, ResponseInterface $response) : void |
|
| 173 | + { |
|
| 174 | + if ($cookieHeader = $response->getHeader('Set-Cookie')) { |
|
| 175 | + foreach ($cookieHeader as $cookie) { |
|
| 176 | + $sc = SetCookie::fromString($cookie); |
|
| 177 | + if (!$sc->getDomain()) { |
|
| 178 | + $sc->setDomain($request->getUri()->getHost()); |
|
| 179 | + } |
|
| 180 | + if (0 !== \strpos($sc->getPath(), '/')) { |
|
| 181 | + $sc->setPath($this->getCookiePathFromRequest($request)); |
|
| 182 | + } |
|
| 183 | + if (!$sc->matchesDomain($request->getUri()->getHost())) { |
|
| 184 | + continue; |
|
| 185 | + } |
|
| 186 | + // Note: At this point `$sc->getDomain()` being a public suffix should |
|
| 187 | + // be rejected, but we don't want to pull in the full PSL dependency. |
|
| 188 | + $this->setCookie($sc); |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | + } |
|
| 192 | + /** |
|
| 193 | + * Computes cookie path following RFC 6265 section 5.1.4 |
|
| 194 | + * |
|
| 195 | + * @see https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.4 |
|
| 196 | + */ |
|
| 197 | + private function getCookiePathFromRequest(RequestInterface $request) : string |
|
| 198 | + { |
|
| 199 | + $uriPath = $request->getUri()->getPath(); |
|
| 200 | + if ('' === $uriPath) { |
|
| 201 | + return '/'; |
|
| 202 | + } |
|
| 203 | + if (0 !== \strpos($uriPath, '/')) { |
|
| 204 | + return '/'; |
|
| 205 | + } |
|
| 206 | + if ('/' === $uriPath) { |
|
| 207 | + return '/'; |
|
| 208 | + } |
|
| 209 | + $lastSlashPos = \strrpos($uriPath, '/'); |
|
| 210 | + if (0 === $lastSlashPos || \false === $lastSlashPos) { |
|
| 211 | + return '/'; |
|
| 212 | + } |
|
| 213 | + return \substr($uriPath, 0, $lastSlashPos); |
|
| 214 | + } |
|
| 215 | + public function withCookieHeader(RequestInterface $request) : RequestInterface |
|
| 216 | + { |
|
| 217 | + $values = []; |
|
| 218 | + $uri = $request->getUri(); |
|
| 219 | + $scheme = $uri->getScheme(); |
|
| 220 | + $host = $uri->getHost(); |
|
| 221 | + $path = $uri->getPath() ?: '/'; |
|
| 222 | + foreach ($this->cookies as $cookie) { |
|
| 223 | + if ($cookie->matchesPath($path) && $cookie->matchesDomain($host) && !$cookie->isExpired() && (!$cookie->getSecure() || $scheme === 'https')) { |
|
| 224 | + $values[] = $cookie->getName() . '=' . $cookie->getValue(); |
|
| 225 | + } |
|
| 226 | + } |
|
| 227 | + return $values ? $request->withHeader('Cookie', \implode('; ', $values)) : $request; |
|
| 228 | + } |
|
| 229 | + /** |
|
| 230 | + * If a cookie already exists and the server asks to set it again with a |
|
| 231 | + * null value, the cookie must be deleted. |
|
| 232 | + */ |
|
| 233 | + private function removeCookieIfEmpty(SetCookie $cookie) : void |
|
| 234 | + { |
|
| 235 | + $cookieValue = $cookie->getValue(); |
|
| 236 | + if ($cookieValue === null || $cookieValue === '') { |
|
| 237 | + $this->clear($cookie->getDomain(), $cookie->getPath(), $cookie->getName()); |
|
| 238 | + } |
|
| 239 | + } |
|
| 240 | 240 | } |
@@ -2,6 +2,5 @@ |
||
| 2 | 2 | |
| 3 | 3 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Exception; |
| 4 | 4 | |
| 5 | -class TooManyRedirectsException extends RequestException |
|
| 6 | -{ |
|
| 5 | +class TooManyRedirectsException extends RequestException { |
|
| 7 | 6 | } |
@@ -5,6 +5,5 @@ |
||
| 5 | 5 | /** |
| 6 | 6 | * Exception when a client error is encountered (4xx codes) |
| 7 | 7 | */ |
| 8 | -class ClientException extends BadResponseException |
|
| 9 | -{ |
|
| 8 | +class ClientException extends BadResponseException { |
|
| 10 | 9 | } |
@@ -5,6 +5,5 @@ |
||
| 5 | 5 | /** |
| 6 | 6 | * Exception when a server error is encountered (5xx codes) |
| 7 | 7 | */ |
| 8 | -class ServerException extends BadResponseException |
|
| 9 | -{ |
|
| 8 | +class ServerException extends BadResponseException { |
|
| 10 | 9 | } |
@@ -12,8 +12,7 @@ |
||
| 12 | 12 | /** |
| 13 | 13 | * HTTP Request exception |
| 14 | 14 | */ |
| 15 | -class RequestException extends TransferException implements RequestExceptionInterface |
|
| 16 | -{ |
|
| 15 | +class RequestException extends TransferException implements RequestExceptionInterface { |
|
| 17 | 16 | /** |
| 18 | 17 | * @var RequestInterface |
| 19 | 18 | */ |
@@ -55,7 +55,7 @@ |
||
| 55 | 55 | if (!$response) { |
| 56 | 56 | return new self('Error completing request', $request, null, $previous, $handlerContext); |
| 57 | 57 | } |
| 58 | - $level = (int) \floor($response->getStatusCode() / 100); |
|
| 58 | + $level = (int)\floor($response->getStatusCode() / 100); |
|
| 59 | 59 | if ($level === 4) { |
| 60 | 60 | $label = 'Client error'; |
| 61 | 61 | $className = ClientException::class; |
@@ -12,100 +12,100 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class RequestException extends TransferException implements RequestExceptionInterface |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * @var RequestInterface |
|
| 17 | - */ |
|
| 18 | - private $request; |
|
| 19 | - /** |
|
| 20 | - * @var ResponseInterface|null |
|
| 21 | - */ |
|
| 22 | - private $response; |
|
| 23 | - /** |
|
| 24 | - * @var array |
|
| 25 | - */ |
|
| 26 | - private $handlerContext; |
|
| 27 | - public function __construct(string $message, RequestInterface $request, ?ResponseInterface $response = null, ?\Throwable $previous = null, array $handlerContext = []) |
|
| 28 | - { |
|
| 29 | - // Set the code of the exception if the response is set and not future. |
|
| 30 | - $code = $response ? $response->getStatusCode() : 0; |
|
| 31 | - parent::__construct($message, $code, $previous); |
|
| 32 | - $this->request = $request; |
|
| 33 | - $this->response = $response; |
|
| 34 | - $this->handlerContext = $handlerContext; |
|
| 35 | - } |
|
| 36 | - /** |
|
| 37 | - * Wrap non-RequestExceptions with a RequestException |
|
| 38 | - */ |
|
| 39 | - public static function wrapException(RequestInterface $request, \Throwable $e) : RequestException |
|
| 40 | - { |
|
| 41 | - return $e instanceof RequestException ? $e : new RequestException($e->getMessage(), $request, null, $e); |
|
| 42 | - } |
|
| 43 | - /** |
|
| 44 | - * Factory method to create a new exception with a normalized error message |
|
| 45 | - * |
|
| 46 | - * @param RequestInterface $request Request sent |
|
| 47 | - * @param ResponseInterface $response Response received |
|
| 48 | - * @param \Throwable|null $previous Previous exception |
|
| 49 | - * @param array $handlerContext Optional handler context |
|
| 50 | - * @param BodySummarizerInterface|null $bodySummarizer Optional body summarizer |
|
| 51 | - */ |
|
| 52 | - public static function create(RequestInterface $request, ?ResponseInterface $response = null, ?\Throwable $previous = null, array $handlerContext = [], ?BodySummarizerInterface $bodySummarizer = null) : self |
|
| 53 | - { |
|
| 54 | - if (!$response) { |
|
| 55 | - return new self('Error completing request', $request, null, $previous, $handlerContext); |
|
| 56 | - } |
|
| 57 | - $level = (int) \floor($response->getStatusCode() / 100); |
|
| 58 | - if ($level === 4) { |
|
| 59 | - $label = 'Client error'; |
|
| 60 | - $className = ClientException::class; |
|
| 61 | - } elseif ($level === 5) { |
|
| 62 | - $label = 'Server error'; |
|
| 63 | - $className = ServerException::class; |
|
| 64 | - } else { |
|
| 65 | - $label = 'Unsuccessful request'; |
|
| 66 | - $className = __CLASS__; |
|
| 67 | - } |
|
| 68 | - $uri = \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Psr7\Utils::redactUserInfo($request->getUri()); |
|
| 69 | - // Client Error: `GET /` resulted in a `404 Not Found` response: |
|
| 70 | - // <html> ... (truncated) |
|
| 71 | - $message = \sprintf('%s: `%s %s` resulted in a `%s %s` response', $label, $request->getMethod(), $uri->__toString(), $response->getStatusCode(), $response->getReasonPhrase()); |
|
| 72 | - $summary = ($bodySummarizer ?? new BodySummarizer())->summarize($response); |
|
| 73 | - if ($summary !== null) { |
|
| 74 | - $message .= ":\n{$summary}\n"; |
|
| 75 | - } |
|
| 76 | - return new $className($message, $request, $response, $previous, $handlerContext); |
|
| 77 | - } |
|
| 78 | - /** |
|
| 79 | - * Get the request that caused the exception |
|
| 80 | - */ |
|
| 81 | - public function getRequest() : RequestInterface |
|
| 82 | - { |
|
| 83 | - return $this->request; |
|
| 84 | - } |
|
| 85 | - /** |
|
| 86 | - * Get the associated response |
|
| 87 | - */ |
|
| 88 | - public function getResponse() : ?ResponseInterface |
|
| 89 | - { |
|
| 90 | - return $this->response; |
|
| 91 | - } |
|
| 92 | - /** |
|
| 93 | - * Check if a response was received |
|
| 94 | - */ |
|
| 95 | - public function hasResponse() : bool |
|
| 96 | - { |
|
| 97 | - return $this->response !== null; |
|
| 98 | - } |
|
| 99 | - /** |
|
| 100 | - * Get contextual information about the error from the underlying handler. |
|
| 101 | - * |
|
| 102 | - * The contents of this array will vary depending on which handler you are |
|
| 103 | - * using. It may also be just an empty array. Relying on this data will |
|
| 104 | - * couple you to a specific handler, but can give more debug information |
|
| 105 | - * when needed. |
|
| 106 | - */ |
|
| 107 | - public function getHandlerContext() : array |
|
| 108 | - { |
|
| 109 | - return $this->handlerContext; |
|
| 110 | - } |
|
| 15 | + /** |
|
| 16 | + * @var RequestInterface |
|
| 17 | + */ |
|
| 18 | + private $request; |
|
| 19 | + /** |
|
| 20 | + * @var ResponseInterface|null |
|
| 21 | + */ |
|
| 22 | + private $response; |
|
| 23 | + /** |
|
| 24 | + * @var array |
|
| 25 | + */ |
|
| 26 | + private $handlerContext; |
|
| 27 | + public function __construct(string $message, RequestInterface $request, ?ResponseInterface $response = null, ?\Throwable $previous = null, array $handlerContext = []) |
|
| 28 | + { |
|
| 29 | + // Set the code of the exception if the response is set and not future. |
|
| 30 | + $code = $response ? $response->getStatusCode() : 0; |
|
| 31 | + parent::__construct($message, $code, $previous); |
|
| 32 | + $this->request = $request; |
|
| 33 | + $this->response = $response; |
|
| 34 | + $this->handlerContext = $handlerContext; |
|
| 35 | + } |
|
| 36 | + /** |
|
| 37 | + * Wrap non-RequestExceptions with a RequestException |
|
| 38 | + */ |
|
| 39 | + public static function wrapException(RequestInterface $request, \Throwable $e) : RequestException |
|
| 40 | + { |
|
| 41 | + return $e instanceof RequestException ? $e : new RequestException($e->getMessage(), $request, null, $e); |
|
| 42 | + } |
|
| 43 | + /** |
|
| 44 | + * Factory method to create a new exception with a normalized error message |
|
| 45 | + * |
|
| 46 | + * @param RequestInterface $request Request sent |
|
| 47 | + * @param ResponseInterface $response Response received |
|
| 48 | + * @param \Throwable|null $previous Previous exception |
|
| 49 | + * @param array $handlerContext Optional handler context |
|
| 50 | + * @param BodySummarizerInterface|null $bodySummarizer Optional body summarizer |
|
| 51 | + */ |
|
| 52 | + public static function create(RequestInterface $request, ?ResponseInterface $response = null, ?\Throwable $previous = null, array $handlerContext = [], ?BodySummarizerInterface $bodySummarizer = null) : self |
|
| 53 | + { |
|
| 54 | + if (!$response) { |
|
| 55 | + return new self('Error completing request', $request, null, $previous, $handlerContext); |
|
| 56 | + } |
|
| 57 | + $level = (int) \floor($response->getStatusCode() / 100); |
|
| 58 | + if ($level === 4) { |
|
| 59 | + $label = 'Client error'; |
|
| 60 | + $className = ClientException::class; |
|
| 61 | + } elseif ($level === 5) { |
|
| 62 | + $label = 'Server error'; |
|
| 63 | + $className = ServerException::class; |
|
| 64 | + } else { |
|
| 65 | + $label = 'Unsuccessful request'; |
|
| 66 | + $className = __CLASS__; |
|
| 67 | + } |
|
| 68 | + $uri = \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Psr7\Utils::redactUserInfo($request->getUri()); |
|
| 69 | + // Client Error: `GET /` resulted in a `404 Not Found` response: |
|
| 70 | + // <html> ... (truncated) |
|
| 71 | + $message = \sprintf('%s: `%s %s` resulted in a `%s %s` response', $label, $request->getMethod(), $uri->__toString(), $response->getStatusCode(), $response->getReasonPhrase()); |
|
| 72 | + $summary = ($bodySummarizer ?? new BodySummarizer())->summarize($response); |
|
| 73 | + if ($summary !== null) { |
|
| 74 | + $message .= ":\n{$summary}\n"; |
|
| 75 | + } |
|
| 76 | + return new $className($message, $request, $response, $previous, $handlerContext); |
|
| 77 | + } |
|
| 78 | + /** |
|
| 79 | + * Get the request that caused the exception |
|
| 80 | + */ |
|
| 81 | + public function getRequest() : RequestInterface |
|
| 82 | + { |
|
| 83 | + return $this->request; |
|
| 84 | + } |
|
| 85 | + /** |
|
| 86 | + * Get the associated response |
|
| 87 | + */ |
|
| 88 | + public function getResponse() : ?ResponseInterface |
|
| 89 | + { |
|
| 90 | + return $this->response; |
|
| 91 | + } |
|
| 92 | + /** |
|
| 93 | + * Check if a response was received |
|
| 94 | + */ |
|
| 95 | + public function hasResponse() : bool |
|
| 96 | + { |
|
| 97 | + return $this->response !== null; |
|
| 98 | + } |
|
| 99 | + /** |
|
| 100 | + * Get contextual information about the error from the underlying handler. |
|
| 101 | + * |
|
| 102 | + * The contents of this array will vary depending on which handler you are |
|
| 103 | + * using. It may also be just an empty array. Relying on this data will |
|
| 104 | + * couple you to a specific handler, but can give more debug information |
|
| 105 | + * when needed. |
|
| 106 | + */ |
|
| 107 | + public function getHandlerContext() : array |
|
| 108 | + { |
|
| 109 | + return $this->handlerContext; |
|
| 110 | + } |
|
| 111 | 111 | } |
@@ -10,8 +10,7 @@ |
||
| 10 | 10 | * |
| 11 | 11 | * Note that no response is present for a ConnectException |
| 12 | 12 | */ |
| 13 | -class ConnectException extends TransferException implements NetworkExceptionInterface |
|
| 14 | -{ |
|
| 13 | +class ConnectException extends TransferException implements NetworkExceptionInterface { |
|
| 15 | 14 | /** |
| 16 | 15 | * @var RequestInterface |
| 17 | 16 | */ |
@@ -11,37 +11,37 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class ConnectException extends TransferException implements NetworkExceptionInterface |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * @var RequestInterface |
|
| 16 | - */ |
|
| 17 | - private $request; |
|
| 18 | - /** |
|
| 19 | - * @var array |
|
| 20 | - */ |
|
| 21 | - private $handlerContext; |
|
| 22 | - public function __construct(string $message, RequestInterface $request, ?\Throwable $previous = null, array $handlerContext = []) |
|
| 23 | - { |
|
| 24 | - parent::__construct($message, 0, $previous); |
|
| 25 | - $this->request = $request; |
|
| 26 | - $this->handlerContext = $handlerContext; |
|
| 27 | - } |
|
| 28 | - /** |
|
| 29 | - * Get the request that caused the exception |
|
| 30 | - */ |
|
| 31 | - public function getRequest() : RequestInterface |
|
| 32 | - { |
|
| 33 | - return $this->request; |
|
| 34 | - } |
|
| 35 | - /** |
|
| 36 | - * Get contextual information about the error from the underlying handler. |
|
| 37 | - * |
|
| 38 | - * The contents of this array will vary depending on which handler you are |
|
| 39 | - * using. It may also be just an empty array. Relying on this data will |
|
| 40 | - * couple you to a specific handler, but can give more debug information |
|
| 41 | - * when needed. |
|
| 42 | - */ |
|
| 43 | - public function getHandlerContext() : array |
|
| 44 | - { |
|
| 45 | - return $this->handlerContext; |
|
| 46 | - } |
|
| 14 | + /** |
|
| 15 | + * @var RequestInterface |
|
| 16 | + */ |
|
| 17 | + private $request; |
|
| 18 | + /** |
|
| 19 | + * @var array |
|
| 20 | + */ |
|
| 21 | + private $handlerContext; |
|
| 22 | + public function __construct(string $message, RequestInterface $request, ?\Throwable $previous = null, array $handlerContext = []) |
|
| 23 | + { |
|
| 24 | + parent::__construct($message, 0, $previous); |
|
| 25 | + $this->request = $request; |
|
| 26 | + $this->handlerContext = $handlerContext; |
|
| 27 | + } |
|
| 28 | + /** |
|
| 29 | + * Get the request that caused the exception |
|
| 30 | + */ |
|
| 31 | + public function getRequest() : RequestInterface |
|
| 32 | + { |
|
| 33 | + return $this->request; |
|
| 34 | + } |
|
| 35 | + /** |
|
| 36 | + * Get contextual information about the error from the underlying handler. |
|
| 37 | + * |
|
| 38 | + * The contents of this array will vary depending on which handler you are |
|
| 39 | + * using. It may also be just an empty array. Relying on this data will |
|
| 40 | + * couple you to a specific handler, but can give more debug information |
|
| 41 | + * when needed. |
|
| 42 | + */ |
|
| 43 | + public function getHandlerContext() : array |
|
| 44 | + { |
|
| 45 | + return $this->handlerContext; |
|
| 46 | + } |
|
| 47 | 47 | } |