Security update to Drupal 8.4.6
[yaffs-website] / vendor / guzzlehttp / guzzle / src / Middleware.php
1 <?php
2 namespace GuzzleHttp;
3
4 use GuzzleHttp\Cookie\CookieJarInterface;
5 use GuzzleHttp\Exception\RequestException;
6 use GuzzleHttp\Promise\RejectedPromise;
7 use GuzzleHttp\Psr7;
8 use Psr\Http\Message\ResponseInterface;
9 use Psr\Log\LoggerInterface;
10 use Psr\Log\LogLevel;
11
12 /**
13  * Functions used to create and wrap handlers with handler middleware.
14  */
15 final class Middleware
16 {
17     /**
18      * Middleware that adds cookies to requests.
19      *
20      * The options array must be set to a CookieJarInterface in order to use
21      * cookies. This is typically handled for you by a client.
22      *
23      * @return callable Returns a function that accepts the next handler.
24      */
25     public static function cookies()
26     {
27         return function (callable $handler) {
28             return function ($request, array $options) use ($handler) {
29                 if (empty($options['cookies'])) {
30                     return $handler($request, $options);
31                 } elseif (!($options['cookies'] instanceof CookieJarInterface)) {
32                     throw new \InvalidArgumentException('cookies must be an instance of GuzzleHttp\Cookie\CookieJarInterface');
33                 }
34                 $cookieJar = $options['cookies'];
35                 $request = $cookieJar->withCookieHeader($request);
36                 return $handler($request, $options)
37                     ->then(
38                         function ($response) use ($cookieJar, $request) {
39                             $cookieJar->extractCookies($request, $response);
40                             return $response;
41                         }
42                 );
43             };
44         };
45     }
46
47     /**
48      * Middleware that throws exceptions for 4xx or 5xx responses when the
49      * "http_error" request option is set to true.
50      *
51      * @return callable Returns a function that accepts the next handler.
52      */
53     public static function httpErrors()
54     {
55         return function (callable $handler) {
56             return function ($request, array $options) use ($handler) {
57                 if (empty($options['http_errors'])) {
58                     return $handler($request, $options);
59                 }
60                 return $handler($request, $options)->then(
61                     function (ResponseInterface $response) use ($request, $handler) {
62                         $code = $response->getStatusCode();
63                         if ($code < 400) {
64                             return $response;
65                         }
66                         throw RequestException::create($request, $response);
67                     }
68                 );
69             };
70         };
71     }
72
73     /**
74      * Middleware that pushes history data to an ArrayAccess container.
75      *
76      * @param array $container Container to hold the history (by reference).
77      *
78      * @return callable Returns a function that accepts the next handler.
79      * @throws \InvalidArgumentException if container is not an array or ArrayAccess.
80      */
81     public static function history(&$container)
82     {
83         if (!is_array($container) && !$container instanceof \ArrayAccess) {
84             throw new \InvalidArgumentException('history container must be an array or object implementing ArrayAccess');
85         }
86
87         return function (callable $handler) use (&$container) {
88             return function ($request, array $options) use ($handler, &$container) {
89                 return $handler($request, $options)->then(
90                     function ($value) use ($request, &$container, $options) {
91                         $container[] = [
92                             'request'  => $request,
93                             'response' => $value,
94                             'error'    => null,
95                             'options'  => $options
96                         ];
97                         return $value;
98                     },
99                     function ($reason) use ($request, &$container, $options) {
100                         $container[] = [
101                             'request'  => $request,
102                             'response' => null,
103                             'error'    => $reason,
104                             'options'  => $options
105                         ];
106                         return \GuzzleHttp\Promise\rejection_for($reason);
107                     }
108                 );
109             };
110         };
111     }
112
113     /**
114      * Middleware that invokes a callback before and after sending a request.
115      *
116      * The provided listener cannot modify or alter the response. It simply
117      * "taps" into the chain to be notified before returning the promise. The
118      * before listener accepts a request and options array, and the after
119      * listener accepts a request, options array, and response promise.
120      *
121      * @param callable $before Function to invoke before forwarding the request.
122      * @param callable $after  Function invoked after forwarding.
123      *
124      * @return callable Returns a function that accepts the next handler.
125      */
126     public static function tap(callable $before = null, callable $after = null)
127     {
128         return function (callable $handler) use ($before, $after) {
129             return function ($request, array $options) use ($handler, $before, $after) {
130                 if ($before) {
131                     $before($request, $options);
132                 }
133                 $response = $handler($request, $options);
134                 if ($after) {
135                     $after($request, $options, $response);
136                 }
137                 return $response;
138             };
139         };
140     }
141
142     /**
143      * Middleware that handles request redirects.
144      *
145      * @return callable Returns a function that accepts the next handler.
146      */
147     public static function redirect()
148     {
149         return function (callable $handler) {
150             return new RedirectMiddleware($handler);
151         };
152     }
153
154     /**
155      * Middleware that retries requests based on the boolean result of
156      * invoking the provided "decider" function.
157      *
158      * If no delay function is provided, a simple implementation of exponential
159      * backoff will be utilized.
160      *
161      * @param callable $decider Function that accepts the number of retries,
162      *                          a request, [response], and [exception] and
163      *                          returns true if the request is to be retried.
164      * @param callable $delay   Function that accepts the number of retries and
165      *                          returns the number of milliseconds to delay.
166      *
167      * @return callable Returns a function that accepts the next handler.
168      */
169     public static function retry(callable $decider, callable $delay = null)
170     {
171         return function (callable $handler) use ($decider, $delay) {
172             return new RetryMiddleware($decider, $handler, $delay);
173         };
174     }
175
176     /**
177      * Middleware that logs requests, responses, and errors using a message
178      * formatter.
179      *
180      * @param LoggerInterface  $logger Logs messages.
181      * @param MessageFormatter $formatter Formatter used to create message strings.
182      * @param string           $logLevel Level at which to log requests.
183      *
184      * @return callable Returns a function that accepts the next handler.
185      */
186     public static function log(LoggerInterface $logger, MessageFormatter $formatter, $logLevel = LogLevel::INFO)
187     {
188         return function (callable $handler) use ($logger, $formatter, $logLevel) {
189             return function ($request, array $options) use ($handler, $logger, $formatter, $logLevel) {
190                 return $handler($request, $options)->then(
191                     function ($response) use ($logger, $request, $formatter, $logLevel) {
192                         $message = $formatter->format($request, $response);
193                         $logger->log($logLevel, $message);
194                         return $response;
195                     },
196                     function ($reason) use ($logger, $request, $formatter) {
197                         $response = $reason instanceof RequestException
198                             ? $reason->getResponse()
199                             : null;
200                         $message = $formatter->format($request, $response, $reason);
201                         $logger->notice($message);
202                         return \GuzzleHttp\Promise\rejection_for($reason);
203                     }
204                 );
205             };
206         };
207     }
208
209     /**
210      * This middleware adds a default content-type if possible, a default
211      * content-length or transfer-encoding header, and the expect header.
212      *
213      * @return callable
214      */
215     public static function prepareBody()
216     {
217         return function (callable $handler) {
218             return new PrepareBodyMiddleware($handler);
219         };
220     }
221
222     /**
223      * Middleware that applies a map function to the request before passing to
224      * the next handler.
225      *
226      * @param callable $fn Function that accepts a RequestInterface and returns
227      *                     a RequestInterface.
228      * @return callable
229      */
230     public static function mapRequest(callable $fn)
231     {
232         return function (callable $handler) use ($fn) {
233             return function ($request, array $options) use ($handler, $fn) {
234                 return $handler($fn($request), $options);
235             };
236         };
237     }
238
239     /**
240      * Middleware that applies a map function to the resolved promise's
241      * response.
242      *
243      * @param callable $fn Function that accepts a ResponseInterface and
244      *                     returns a ResponseInterface.
245      * @return callable
246      */
247     public static function mapResponse(callable $fn)
248     {
249         return function (callable $handler) use ($fn) {
250             return function ($request, array $options) use ($handler, $fn) {
251                 return $handler($request, $options)->then($fn);
252             };
253         };
254     }
255 }