Minor dependency updates
[yaffs-website] / vendor / guzzlehttp / guzzle / src / PrepareBodyMiddleware.php
1 <?php
2 namespace GuzzleHttp;
3
4 use GuzzleHttp\Promise\PromiseInterface;
5 use GuzzleHttp\Psr7;
6 use Psr\Http\Message\RequestInterface;
7
8 /**
9  * Prepares requests that contain a body, adding the Content-Length,
10  * Content-Type, and Expect headers.
11  */
12 class PrepareBodyMiddleware
13 {
14     /** @var callable  */
15     private $nextHandler;
16
17     /**
18      * @param callable $nextHandler Next handler to invoke.
19      */
20     public function __construct(callable $nextHandler)
21     {
22         $this->nextHandler = $nextHandler;
23     }
24
25     /**
26      * @param RequestInterface $request
27      * @param array            $options
28      *
29      * @return PromiseInterface
30      */
31     public function __invoke(RequestInterface $request, array $options)
32     {
33         $fn = $this->nextHandler;
34
35         // Don't do anything if the request has no body.
36         if ($request->getBody()->getSize() === 0) {
37             return $fn($request, $options);
38         }
39
40         $modify = [];
41
42         // Add a default content-type if possible.
43         if (!$request->hasHeader('Content-Type')) {
44             if ($uri = $request->getBody()->getMetadata('uri')) {
45                 if ($type = Psr7\mimetype_from_filename($uri)) {
46                     $modify['set_headers']['Content-Type'] = $type;
47                 }
48             }
49         }
50
51         // Add a default content-length or transfer-encoding header.
52         if (!$request->hasHeader('Content-Length')
53             && !$request->hasHeader('Transfer-Encoding')
54         ) {
55             $size = $request->getBody()->getSize();
56             if ($size !== null) {
57                 $modify['set_headers']['Content-Length'] = $size;
58             } else {
59                 $modify['set_headers']['Transfer-Encoding'] = 'chunked';
60             }
61         }
62
63         // Add the expect header if needed.
64         $this->addExpectHeader($request, $options, $modify);
65
66         return $fn(Psr7\modify_request($request, $modify), $options);
67     }
68
69     private function addExpectHeader(
70         RequestInterface $request,
71         array $options,
72         array &$modify
73     ) {
74         // Determine if the Expect header should be used
75         if ($request->hasHeader('Expect')) {
76             return;
77         }
78
79         $expect = isset($options['expect']) ? $options['expect'] : null;
80
81         // Return if disabled or if you're not using HTTP/1.1 or HTTP/2.0
82         if ($expect === false || $request->getProtocolVersion() < 1.1) {
83             return;
84         }
85
86         // The expect header is unconditionally enabled
87         if ($expect === true) {
88             $modify['set_headers']['Expect'] = '100-Continue';
89             return;
90         }
91
92         // By default, send the expect header when the payload is > 1mb
93         if ($expect === null) {
94             $expect = 1048576;
95         }
96
97         // Always add if the body cannot be rewound, the size cannot be
98         // determined, or the size is greater than the cutoff threshold
99         $body = $request->getBody();
100         $size = $body->getSize();
101
102         if ($size === null || $size >= (int) $expect || !$body->isSeekable()) {
103             $modify['set_headers']['Expect'] = '100-Continue';
104         }
105     }
106 }