PluginProbe
Media Cloud Sync / 1.2.6
Media Cloud Sync v1.2.6
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.2.6, at includes/sdk/s3/GuzzleHttp/PrepareBodyMiddleware.php

92 lines 3.2 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\GuzzleHttp\Psr7;
7 use Dudlewebs\WPMCS\s3\Psr\Http\Message\RequestInterface;
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 * @param callable $nextHandler Next handler to invoke.
18 */
19 public function __construct(callable $nextHandler)
20 {
21 $this->nextHandler = $nextHandler;
22 }
23 /**
24 * @param RequestInterface $request
25 * @param array $options
26 *
27 * @return PromiseInterface
28 */
29 public function __invoke(RequestInterface $request, array $options)
30 {
31 $fn = $this->nextHandler;
32 // Don't do anything if the request has no body.
33 if ($request->getBody()->getSize() === 0) {
34 return $fn($request, $options);
35 }
36 $modify = [];
37 // Add a default content-type if possible.
38 if (!$request->hasHeader('Content-Type')) {
39 if ($uri = $request->getBody()->getMetadata('uri')) {
40 if ($type = Psr7\mimetype_from_filename($uri)) {
41 $modify['set_headers']['Content-Type'] = $type;
42 }
43 }
44 }
45 // Add a default content-length or transfer-encoding header.
46 if (!$request->hasHeader('Content-Length') && !$request->hasHeader('Transfer-Encoding')) {
47 $size = $request->getBody()->getSize();
48 if ($size !== null) {
49 $modify['set_headers']['Content-Length'] = $size;
50 } else {
51 $modify['set_headers']['Transfer-Encoding'] = 'chunked';
52 }
53 }
54 // Add the expect header if needed.
55 $this->addExpectHeader($request, $options, $modify);
56 return $fn(Psr7\modify_request($request, $modify), $options);
57 }
58 /**
59 * Add expect header
60 *
61 * @return void
62 */
63 private function addExpectHeader(RequestInterface $request, array $options, array &$modify)
64 {
65 // Determine if the Expect header should be used
66 if ($request->hasHeader('Expect')) {
67 return;
68 }
69 $expect = isset($options['expect']) ? $options['expect'] : null;
70 // Return if disabled or if you're not using HTTP/1.1 or HTTP/2.0
71 if ($expect === \false || $request->getProtocolVersion() < 1.1) {
72 return;
73 }
74 // The expect header is unconditionally enabled
75 if ($expect === \true) {
76 $modify['set_headers']['Expect'] = '100-Continue';
77 return;
78 }
79 // By default, send the expect header when the payload is > 1mb
80 if ($expect === null) {
81 $expect = 1048576;
82 }
83 // Always add if the body cannot be rewound, the size cannot be
84 // determined, or the size is greater than the cutoff threshold
85 $body = $request->getBody();
86 $size = $body->getSize();
87 if ($size === null || $size >= (int) $expect || !$body->isSeekable()) {
88 $modify['set_headers']['Expect'] = '100-Continue';
89 }
90 }
91 }
92