PluginProbe
Media Cloud Sync / 1.3.11
Media Cloud Sync v1.3.11
1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 All 34 releases
media-cloud-sync / includes / sdk / s3 / GuzzleHttp / PrepareBodyMiddleware.php

PrepareBodyMiddleware.php in Media Cloud Sync 1.3.11, at includes/sdk/s3/GuzzleHttp/PrepareBodyMiddleware.php

87 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Dudlewebs\WPMCS\s3\GuzzleHttp;
4
5 use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\PromiseInterface;
6 use Dudlewebs\WPMCS\s3\Psr\Http\Message\RequestInterface;
7 /**
8 * Prepares requests that contain a body, adding the Content-Length,
9 * Content-Type, and Expect headers.
10 *
11 * @final
12 */
13 class PrepareBodyMiddleware
14 {
15 /**
16 * @var callable(RequestInterface, array): PromiseInterface
17 */
18 private $nextHandler;
19 /**
20 * @param callable(RequestInterface, array): PromiseInterface $nextHandler Next handler to invoke.
21 */
22 public function __construct(callable $nextHandler)
23 {
24 $this->nextHandler = $nextHandler;
25 }
26 public function __invoke(RequestInterface $request, array $options) : PromiseInterface
27 {
28 $fn = $this->nextHandler;
29 // Don't do anything if the request has no body.
30 if ($request->getBody()->getSize() === 0) {
31 return $fn($request, $options);
32 }
33 $modify = [];
34 // Add a default content-type if possible.
35 if (!$request->hasHeader('Content-Type')) {
36 if ($uri = $request->getBody()->getMetadata('uri')) {
37 if (\is_string($uri) && ($type = Psr7\MimeType::fromFilename($uri))) {
38 $modify['set_headers']['Content-Type'] = $type;
39 }
40 }
41 }
42 // Add a default content-length or transfer-encoding header.
43 if (!$request->hasHeader('Content-Length') && !$request->hasHeader('Transfer-Encoding')) {
44 $size = $request->getBody()->getSize();
45 if ($size !== null) {
46 $modify['set_headers']['Content-Length'] = $size;
47 } else {
48 $modify['set_headers']['Transfer-Encoding'] = 'chunked';
49 }
50 }
51 // Add the expect header if needed.
52 $this->addExpectHeader($request, $options, $modify);
53 return $fn(Psr7\Utils::modifyRequest($request, $modify), $options);
54 }
55 /**
56 * Add expect header
57 */
58 private function addExpectHeader(RequestInterface $request, array $options, array &$modify) : void
59 {
60 // Determine if the Expect header should be used
61 if ($request->hasHeader('Expect')) {
62 return;
63 }
64 $expect = $options['expect'] ?? null;
65 // Return if disabled or using HTTP/1.0
66 if ($expect === \false || $request->getProtocolVersion() === '1.0') {
67 return;
68 }
69 // The expect header is unconditionally enabled
70 if ($expect === \true) {
71 $modify['set_headers']['Expect'] = '100-Continue';
72 return;
73 }
74 // By default, send the expect header when the payload is > 1mb
75 if ($expect === null) {
76 $expect = 1048576;
77 }
78 // Always add if the body cannot be rewound, the size cannot be
79 // determined, or the size is greater than the cutoff threshold
80 $body = $request->getBody();
81 $size = $body->getSize();
82 if ($size === null || $size >= (int) $expect || !$body->isSeekable()) {
83 $modify['set_headers']['Expect'] = '100-Continue';
84 }
85 }
86 }
87