@@ -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 | */ |
@@ -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 | } |
@@ -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; |
@@ -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 | */ |
@@ -13,112 +13,112 @@ |
||
13 | 13 | */ |
14 | 14 | class RequestException extends TransferException implements RequestExceptionInterface |
15 | 15 | { |
16 | - /** |
|
17 | - * @var RequestInterface |
|
18 | - */ |
|
19 | - private $request; |
|
20 | - /** |
|
21 | - * @var ResponseInterface|null |
|
22 | - */ |
|
23 | - private $response; |
|
24 | - /** |
|
25 | - * @var array |
|
26 | - */ |
|
27 | - private $handlerContext; |
|
28 | - public function __construct(string $message, RequestInterface $request, ResponseInterface $response = null, \Throwable $previous = null, array $handlerContext = []) |
|
29 | - { |
|
30 | - // Set the code of the exception if the response is set and not future. |
|
31 | - $code = $response ? $response->getStatusCode() : 0; |
|
32 | - parent::__construct($message, $code, $previous); |
|
33 | - $this->request = $request; |
|
34 | - $this->response = $response; |
|
35 | - $this->handlerContext = $handlerContext; |
|
36 | - } |
|
37 | - /** |
|
38 | - * Wrap non-RequestExceptions with a RequestException |
|
39 | - */ |
|
40 | - public static function wrapException(RequestInterface $request, \Throwable $e) : RequestException |
|
41 | - { |
|
42 | - return $e instanceof RequestException ? $e : new RequestException($e->getMessage(), $request, null, $e); |
|
43 | - } |
|
44 | - /** |
|
45 | - * Factory method to create a new exception with a normalized error message |
|
46 | - * |
|
47 | - * @param RequestInterface $request Request sent |
|
48 | - * @param ResponseInterface $response Response received |
|
49 | - * @param \Throwable|null $previous Previous exception |
|
50 | - * @param array $handlerContext Optional handler context |
|
51 | - * @param BodySummarizerInterface|null $bodySummarizer Optional body summarizer |
|
52 | - */ |
|
53 | - public static function create(RequestInterface $request, ResponseInterface $response = null, \Throwable $previous = null, array $handlerContext = [], BodySummarizerInterface $bodySummarizer = null) : self |
|
54 | - { |
|
55 | - if (!$response) { |
|
56 | - return new self('Error completing request', $request, null, $previous, $handlerContext); |
|
57 | - } |
|
58 | - $level = (int) \floor($response->getStatusCode() / 100); |
|
59 | - if ($level === 4) { |
|
60 | - $label = 'Client error'; |
|
61 | - $className = ClientException::class; |
|
62 | - } elseif ($level === 5) { |
|
63 | - $label = 'Server error'; |
|
64 | - $className = ServerException::class; |
|
65 | - } else { |
|
66 | - $label = 'Unsuccessful request'; |
|
67 | - $className = __CLASS__; |
|
68 | - } |
|
69 | - $uri = $request->getUri(); |
|
70 | - $uri = static::obfuscateUri($uri); |
|
71 | - // Client Error: `GET /` resulted in a `404 Not Found` response: |
|
72 | - // <html> ... (truncated) |
|
73 | - $message = \sprintf('%s: `%s %s` resulted in a `%s %s` response', $label, $request->getMethod(), $uri->__toString(), $response->getStatusCode(), $response->getReasonPhrase()); |
|
74 | - $summary = ($bodySummarizer ?? new BodySummarizer())->summarize($response); |
|
75 | - if ($summary !== null) { |
|
76 | - $message .= ":\n{$summary}\n"; |
|
77 | - } |
|
78 | - return new $className($message, $request, $response, $previous, $handlerContext); |
|
79 | - } |
|
80 | - /** |
|
81 | - * Obfuscates URI if there is a username and a password present |
|
82 | - */ |
|
83 | - private static function obfuscateUri(UriInterface $uri) : UriInterface |
|
84 | - { |
|
85 | - $userInfo = $uri->getUserInfo(); |
|
86 | - if (\false !== ($pos = \strpos($userInfo, ':'))) { |
|
87 | - return $uri->withUserInfo(\substr($userInfo, 0, $pos), '***'); |
|
88 | - } |
|
89 | - return $uri; |
|
90 | - } |
|
91 | - /** |
|
92 | - * Get the request that caused the exception |
|
93 | - */ |
|
94 | - public function getRequest() : RequestInterface |
|
95 | - { |
|
96 | - return $this->request; |
|
97 | - } |
|
98 | - /** |
|
99 | - * Get the associated response |
|
100 | - */ |
|
101 | - public function getResponse() : ?ResponseInterface |
|
102 | - { |
|
103 | - return $this->response; |
|
104 | - } |
|
105 | - /** |
|
106 | - * Check if a response was received |
|
107 | - */ |
|
108 | - public function hasResponse() : bool |
|
109 | - { |
|
110 | - return $this->response !== null; |
|
111 | - } |
|
112 | - /** |
|
113 | - * Get contextual information about the error from the underlying handler. |
|
114 | - * |
|
115 | - * The contents of this array will vary depending on which handler you are |
|
116 | - * using. It may also be just an empty array. Relying on this data will |
|
117 | - * couple you to a specific handler, but can give more debug information |
|
118 | - * when needed. |
|
119 | - */ |
|
120 | - public function getHandlerContext() : array |
|
121 | - { |
|
122 | - return $this->handlerContext; |
|
123 | - } |
|
16 | + /** |
|
17 | + * @var RequestInterface |
|
18 | + */ |
|
19 | + private $request; |
|
20 | + /** |
|
21 | + * @var ResponseInterface|null |
|
22 | + */ |
|
23 | + private $response; |
|
24 | + /** |
|
25 | + * @var array |
|
26 | + */ |
|
27 | + private $handlerContext; |
|
28 | + public function __construct(string $message, RequestInterface $request, ResponseInterface $response = null, \Throwable $previous = null, array $handlerContext = []) |
|
29 | + { |
|
30 | + // Set the code of the exception if the response is set and not future. |
|
31 | + $code = $response ? $response->getStatusCode() : 0; |
|
32 | + parent::__construct($message, $code, $previous); |
|
33 | + $this->request = $request; |
|
34 | + $this->response = $response; |
|
35 | + $this->handlerContext = $handlerContext; |
|
36 | + } |
|
37 | + /** |
|
38 | + * Wrap non-RequestExceptions with a RequestException |
|
39 | + */ |
|
40 | + public static function wrapException(RequestInterface $request, \Throwable $e) : RequestException |
|
41 | + { |
|
42 | + return $e instanceof RequestException ? $e : new RequestException($e->getMessage(), $request, null, $e); |
|
43 | + } |
|
44 | + /** |
|
45 | + * Factory method to create a new exception with a normalized error message |
|
46 | + * |
|
47 | + * @param RequestInterface $request Request sent |
|
48 | + * @param ResponseInterface $response Response received |
|
49 | + * @param \Throwable|null $previous Previous exception |
|
50 | + * @param array $handlerContext Optional handler context |
|
51 | + * @param BodySummarizerInterface|null $bodySummarizer Optional body summarizer |
|
52 | + */ |
|
53 | + public static function create(RequestInterface $request, ResponseInterface $response = null, \Throwable $previous = null, array $handlerContext = [], BodySummarizerInterface $bodySummarizer = null) : self |
|
54 | + { |
|
55 | + if (!$response) { |
|
56 | + return new self('Error completing request', $request, null, $previous, $handlerContext); |
|
57 | + } |
|
58 | + $level = (int) \floor($response->getStatusCode() / 100); |
|
59 | + if ($level === 4) { |
|
60 | + $label = 'Client error'; |
|
61 | + $className = ClientException::class; |
|
62 | + } elseif ($level === 5) { |
|
63 | + $label = 'Server error'; |
|
64 | + $className = ServerException::class; |
|
65 | + } else { |
|
66 | + $label = 'Unsuccessful request'; |
|
67 | + $className = __CLASS__; |
|
68 | + } |
|
69 | + $uri = $request->getUri(); |
|
70 | + $uri = static::obfuscateUri($uri); |
|
71 | + // Client Error: `GET /` resulted in a `404 Not Found` response: |
|
72 | + // <html> ... (truncated) |
|
73 | + $message = \sprintf('%s: `%s %s` resulted in a `%s %s` response', $label, $request->getMethod(), $uri->__toString(), $response->getStatusCode(), $response->getReasonPhrase()); |
|
74 | + $summary = ($bodySummarizer ?? new BodySummarizer())->summarize($response); |
|
75 | + if ($summary !== null) { |
|
76 | + $message .= ":\n{$summary}\n"; |
|
77 | + } |
|
78 | + return new $className($message, $request, $response, $previous, $handlerContext); |
|
79 | + } |
|
80 | + /** |
|
81 | + * Obfuscates URI if there is a username and a password present |
|
82 | + */ |
|
83 | + private static function obfuscateUri(UriInterface $uri) : UriInterface |
|
84 | + { |
|
85 | + $userInfo = $uri->getUserInfo(); |
|
86 | + if (\false !== ($pos = \strpos($userInfo, ':'))) { |
|
87 | + return $uri->withUserInfo(\substr($userInfo, 0, $pos), '***'); |
|
88 | + } |
|
89 | + return $uri; |
|
90 | + } |
|
91 | + /** |
|
92 | + * Get the request that caused the exception |
|
93 | + */ |
|
94 | + public function getRequest() : RequestInterface |
|
95 | + { |
|
96 | + return $this->request; |
|
97 | + } |
|
98 | + /** |
|
99 | + * Get the associated response |
|
100 | + */ |
|
101 | + public function getResponse() : ?ResponseInterface |
|
102 | + { |
|
103 | + return $this->response; |
|
104 | + } |
|
105 | + /** |
|
106 | + * Check if a response was received |
|
107 | + */ |
|
108 | + public function hasResponse() : bool |
|
109 | + { |
|
110 | + return $this->response !== null; |
|
111 | + } |
|
112 | + /** |
|
113 | + * Get contextual information about the error from the underlying handler. |
|
114 | + * |
|
115 | + * The contents of this array will vary depending on which handler you are |
|
116 | + * using. It may also be just an empty array. Relying on this data will |
|
117 | + * couple you to a specific handler, but can give more debug information |
|
118 | + * when needed. |
|
119 | + */ |
|
120 | + public function getHandlerContext() : array |
|
121 | + { |
|
122 | + return $this->handlerContext; |
|
123 | + } |
|
124 | 124 | } |
@@ -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; |
@@ -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 | } |
@@ -11,8 +11,7 @@ |
||
11 | 11 | * |
12 | 12 | * @property StreamInterface $stream |
13 | 13 | */ |
14 | -trait StreamDecoratorTrait |
|
15 | -{ |
|
14 | +trait StreamDecoratorTrait { |
|
16 | 15 | /** |
17 | 16 | * @param StreamInterface $stream Stream to decorate |
18 | 17 | */ |
@@ -11,121 +11,121 @@ |
||
11 | 11 | */ |
12 | 12 | trait StreamDecoratorTrait |
13 | 13 | { |
14 | - /** |
|
15 | - * @param StreamInterface $stream Stream to decorate |
|
16 | - */ |
|
17 | - public function __construct(StreamInterface $stream) |
|
18 | - { |
|
19 | - $this->stream = $stream; |
|
20 | - } |
|
21 | - /** |
|
22 | - * Magic method used to create a new stream if streams are not added in |
|
23 | - * the constructor of a decorator (e.g., LazyOpenStream). |
|
24 | - * |
|
25 | - * @return StreamInterface |
|
26 | - */ |
|
27 | - public function __get(string $name) |
|
28 | - { |
|
29 | - if ($name === 'stream') { |
|
30 | - $this->stream = $this->createStream(); |
|
31 | - return $this->stream; |
|
32 | - } |
|
33 | - throw new \UnexpectedValueException("{$name} not found on class"); |
|
34 | - } |
|
35 | - public function __toString() : string |
|
36 | - { |
|
37 | - try { |
|
38 | - if ($this->isSeekable()) { |
|
39 | - $this->seek(0); |
|
40 | - } |
|
41 | - return $this->getContents(); |
|
42 | - } catch (\Throwable $e) { |
|
43 | - if (\PHP_VERSION_ID >= 70400) { |
|
44 | - throw $e; |
|
45 | - } |
|
46 | - \trigger_error(\sprintf('%s::__toString exception: %s', self::class, (string) $e), \E_USER_ERROR); |
|
47 | - return ''; |
|
48 | - } |
|
49 | - } |
|
50 | - public function getContents() : string |
|
51 | - { |
|
52 | - return Utils::copyToString($this); |
|
53 | - } |
|
54 | - /** |
|
55 | - * Allow decorators to implement custom methods |
|
56 | - * |
|
57 | - * @return mixed |
|
58 | - */ |
|
59 | - public function __call(string $method, array $args) |
|
60 | - { |
|
61 | - /** @var callable $callable */ |
|
62 | - $callable = [$this->stream, $method]; |
|
63 | - $result = $callable(...$args); |
|
64 | - // Always return the wrapped object if the result is a return $this |
|
65 | - return $result === $this->stream ? $this : $result; |
|
66 | - } |
|
67 | - public function close() : void |
|
68 | - { |
|
69 | - $this->stream->close(); |
|
70 | - } |
|
71 | - /** |
|
72 | - * @return mixed |
|
73 | - */ |
|
74 | - public function getMetadata($key = null) |
|
75 | - { |
|
76 | - return $this->stream->getMetadata($key); |
|
77 | - } |
|
78 | - public function detach() |
|
79 | - { |
|
80 | - return $this->stream->detach(); |
|
81 | - } |
|
82 | - public function getSize() : ?int |
|
83 | - { |
|
84 | - return $this->stream->getSize(); |
|
85 | - } |
|
86 | - public function eof() : bool |
|
87 | - { |
|
88 | - return $this->stream->eof(); |
|
89 | - } |
|
90 | - public function tell() : int |
|
91 | - { |
|
92 | - return $this->stream->tell(); |
|
93 | - } |
|
94 | - public function isReadable() : bool |
|
95 | - { |
|
96 | - return $this->stream->isReadable(); |
|
97 | - } |
|
98 | - public function isWritable() : bool |
|
99 | - { |
|
100 | - return $this->stream->isWritable(); |
|
101 | - } |
|
102 | - public function isSeekable() : bool |
|
103 | - { |
|
104 | - return $this->stream->isSeekable(); |
|
105 | - } |
|
106 | - public function rewind() : void |
|
107 | - { |
|
108 | - $this->seek(0); |
|
109 | - } |
|
110 | - public function seek($offset, $whence = \SEEK_SET) : void |
|
111 | - { |
|
112 | - $this->stream->seek($offset, $whence); |
|
113 | - } |
|
114 | - public function read($length) : string |
|
115 | - { |
|
116 | - return $this->stream->read($length); |
|
117 | - } |
|
118 | - public function write($string) : int |
|
119 | - { |
|
120 | - return $this->stream->write($string); |
|
121 | - } |
|
122 | - /** |
|
123 | - * Implement in subclasses to dynamically create streams when requested. |
|
124 | - * |
|
125 | - * @throws \BadMethodCallException |
|
126 | - */ |
|
127 | - protected function createStream() : StreamInterface |
|
128 | - { |
|
129 | - throw new \BadMethodCallException('Not implemented'); |
|
130 | - } |
|
14 | + /** |
|
15 | + * @param StreamInterface $stream Stream to decorate |
|
16 | + */ |
|
17 | + public function __construct(StreamInterface $stream) |
|
18 | + { |
|
19 | + $this->stream = $stream; |
|
20 | + } |
|
21 | + /** |
|
22 | + * Magic method used to create a new stream if streams are not added in |
|
23 | + * the constructor of a decorator (e.g., LazyOpenStream). |
|
24 | + * |
|
25 | + * @return StreamInterface |
|
26 | + */ |
|
27 | + public function __get(string $name) |
|
28 | + { |
|
29 | + if ($name === 'stream') { |
|
30 | + $this->stream = $this->createStream(); |
|
31 | + return $this->stream; |
|
32 | + } |
|
33 | + throw new \UnexpectedValueException("{$name} not found on class"); |
|
34 | + } |
|
35 | + public function __toString() : string |
|
36 | + { |
|
37 | + try { |
|
38 | + if ($this->isSeekable()) { |
|
39 | + $this->seek(0); |
|
40 | + } |
|
41 | + return $this->getContents(); |
|
42 | + } catch (\Throwable $e) { |
|
43 | + if (\PHP_VERSION_ID >= 70400) { |
|
44 | + throw $e; |
|
45 | + } |
|
46 | + \trigger_error(\sprintf('%s::__toString exception: %s', self::class, (string) $e), \E_USER_ERROR); |
|
47 | + return ''; |
|
48 | + } |
|
49 | + } |
|
50 | + public function getContents() : string |
|
51 | + { |
|
52 | + return Utils::copyToString($this); |
|
53 | + } |
|
54 | + /** |
|
55 | + * Allow decorators to implement custom methods |
|
56 | + * |
|
57 | + * @return mixed |
|
58 | + */ |
|
59 | + public function __call(string $method, array $args) |
|
60 | + { |
|
61 | + /** @var callable $callable */ |
|
62 | + $callable = [$this->stream, $method]; |
|
63 | + $result = $callable(...$args); |
|
64 | + // Always return the wrapped object if the result is a return $this |
|
65 | + return $result === $this->stream ? $this : $result; |
|
66 | + } |
|
67 | + public function close() : void |
|
68 | + { |
|
69 | + $this->stream->close(); |
|
70 | + } |
|
71 | + /** |
|
72 | + * @return mixed |
|
73 | + */ |
|
74 | + public function getMetadata($key = null) |
|
75 | + { |
|
76 | + return $this->stream->getMetadata($key); |
|
77 | + } |
|
78 | + public function detach() |
|
79 | + { |
|
80 | + return $this->stream->detach(); |
|
81 | + } |
|
82 | + public function getSize() : ?int |
|
83 | + { |
|
84 | + return $this->stream->getSize(); |
|
85 | + } |
|
86 | + public function eof() : bool |
|
87 | + { |
|
88 | + return $this->stream->eof(); |
|
89 | + } |
|
90 | + public function tell() : int |
|
91 | + { |
|
92 | + return $this->stream->tell(); |
|
93 | + } |
|
94 | + public function isReadable() : bool |
|
95 | + { |
|
96 | + return $this->stream->isReadable(); |
|
97 | + } |
|
98 | + public function isWritable() : bool |
|
99 | + { |
|
100 | + return $this->stream->isWritable(); |
|
101 | + } |
|
102 | + public function isSeekable() : bool |
|
103 | + { |
|
104 | + return $this->stream->isSeekable(); |
|
105 | + } |
|
106 | + public function rewind() : void |
|
107 | + { |
|
108 | + $this->seek(0); |
|
109 | + } |
|
110 | + public function seek($offset, $whence = \SEEK_SET) : void |
|
111 | + { |
|
112 | + $this->stream->seek($offset, $whence); |
|
113 | + } |
|
114 | + public function read($length) : string |
|
115 | + { |
|
116 | + return $this->stream->read($length); |
|
117 | + } |
|
118 | + public function write($string) : int |
|
119 | + { |
|
120 | + return $this->stream->write($string); |
|
121 | + } |
|
122 | + /** |
|
123 | + * Implement in subclasses to dynamically create streams when requested. |
|
124 | + * |
|
125 | + * @throws \BadMethodCallException |
|
126 | + */ |
|
127 | + protected function createStream() : StreamInterface |
|
128 | + { |
|
129 | + throw new \BadMethodCallException('Not implemented'); |
|
130 | + } |
|
131 | 131 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare (strict_types=1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Psr7; |
5 | 5 | |
6 | 6 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\StreamInterface; |
@@ -51,7 +51,7 @@ discard block |
||
51 | 51 | if (\PHP_VERSION_ID >= 70400) { |
52 | 52 | throw $e; |
53 | 53 | } |
54 | - \trigger_error(\sprintf('%s::__toString exception: %s', self::class, (string) $e), \E_USER_ERROR); |
|
54 | + \trigger_error(\sprintf('%s::__toString exception: %s', self::class, (string)$e), \E_USER_ERROR); |
|
55 | 55 | return ''; |
56 | 56 | } |
57 | 57 | } |