| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7; |
| 6 |
use Dudlewebs\WPMCS\s3\Psr\Http\Message\RequestInterface; |
| 7 |
/** |
| 8 |
* Used to compress request payloads if the service/operation support it. |
| 9 |
* |
| 10 |
* IMPORTANT: this middleware must be added after the "build" step. |
| 11 |
* |
| 12 |
* @internal |
| 13 |
*/ |
| 14 |
class RequestCompressionMiddleware |
| 15 |
{ |
| 16 |
private $api; |
| 17 |
private $minimumCompressionSize; |
| 18 |
private $nextHandler; |
| 19 |
private $encodings; |
| 20 |
private $encoding; |
| 21 |
private $encodingMap = ['gzip' => 'gzencode']; |
| 22 |
/** |
| 23 |
* Create a middleware wrapper function. |
| 24 |
* |
| 25 |
* @return callable |
| 26 |
*/ |
| 27 |
public static function wrap(array $config) |
| 28 |
{ |
| 29 |
return function (callable $handler) use($config) { |
| 30 |
return new self($handler, $config); |
| 31 |
}; |
| 32 |
} |
| 33 |
public function __construct(callable $nextHandler, $config) |
| 34 |
{ |
| 35 |
$this->minimumCompressionSize = $this->determineMinimumCompressionSize($config); |
| 36 |
$this->api = $config['api']; |
| 37 |
$this->nextHandler = $nextHandler; |
| 38 |
} |
| 39 |
public function __invoke(CommandInterface $command, RequestInterface $request) |
| 40 |
{ |
| 41 |
if (isset($command['@request_min_compression_size_bytes']) && \is_int($command['@request_min_compression_size_bytes']) && $this->isValidCompressionSize($command['@request_min_compression_size_bytes'])) { |
| 42 |
$this->minimumCompressionSize = $command['@request_min_compression_size_bytes']; |
| 43 |
} |
| 44 |
$nextHandler = $this->nextHandler; |
| 45 |
$operation = $this->api->getOperation($command->getName()); |
| 46 |
$compressionInfo = $operation['requestcompression'] ?? null; |
| 47 |
if (!$this->shouldCompressRequestBody($compressionInfo, $command, $operation, $request)) { |
| 48 |
return $nextHandler($command, $request); |
| 49 |
} |
| 50 |
$this->encodings = $compressionInfo['encodings']; |
| 51 |
$request = $this->compressRequestBody($request); |
| 52 |
// Capture request compression metric |
| 53 |
$command->getMetricsBuilder()->identifyMetricByValueAndAppend('request_compression', $request->getHeaderLine('content-encoding')); |
| 54 |
return $nextHandler($command, $request); |
| 55 |
} |
| 56 |
private function compressRequestBody(RequestInterface $request) |
| 57 |
{ |
| 58 |
$fn = $this->determineEncoding(); |
| 59 |
if (\is_null($fn)) { |
| 60 |
return $request; |
| 61 |
} |
| 62 |
$body = $request->getBody()->getContents(); |
| 63 |
$compressedBody = $fn($body); |
| 64 |
$request = $request->withBody(Psr7\Utils::streamFor($compressedBody)); |
| 65 |
if ($request->hasHeader('Content-Encoding')) { |
| 66 |
return $request->withAddedHeader('Content-Encoding', $this->encoding); |
| 67 |
} |
| 68 |
return $request->withHeader('Content-Encoding', $this->encoding); |
| 69 |
} |
| 70 |
private function determineEncoding() |
| 71 |
{ |
| 72 |
foreach ($this->encodings as $encoding) { |
| 73 |
if (isset($this->encodingMap[$encoding])) { |
| 74 |
$this->encoding = $encoding; |
| 75 |
return $this->encodingMap[$encoding]; |
| 76 |
} |
| 77 |
} |
| 78 |
return null; |
| 79 |
} |
| 80 |
private function shouldCompressRequestBody($compressionInfo, $command, $operation, $request) |
| 81 |
{ |
| 82 |
if ($compressionInfo) { |
| 83 |
if (isset($command['@disable_request_compression']) && $command['@disable_request_compression'] === \true) { |
| 84 |
return \false; |
| 85 |
} elseif ($this->hasStreamingTraitWithoutRequiresLength($command, $operation)) { |
| 86 |
return \true; |
| 87 |
} |
| 88 |
$requestBodySize = $request->hasHeader('content-length') ? (int) $request->getHeaderLine('content-length') : $request->getBody()->getSize(); |
| 89 |
if ($requestBodySize >= $this->minimumCompressionSize) { |
| 90 |
return \true; |
| 91 |
} |
| 92 |
} |
| 93 |
return \false; |
| 94 |
} |
| 95 |
private function hasStreamingTraitWithoutRequiresLength($command, $operation) |
| 96 |
{ |
| 97 |
foreach ($operation->getInput()->getMembers() as $name => $member) { |
| 98 |
if (isset($command[$name]) && !empty($member['streaming']) && empty($member['requiresLength'])) { |
| 99 |
return \true; |
| 100 |
} |
| 101 |
} |
| 102 |
return \false; |
| 103 |
} |
| 104 |
private function determineMinimumCompressionSize($config) |
| 105 |
{ |
| 106 |
if (\is_callable($config['request_min_compression_size_bytes'])) { |
| 107 |
$minCompressionSz = $config['request_min_compression_size_bytes'](); |
| 108 |
} else { |
| 109 |
$minCompressionSz = $config['request_min_compression_size_bytes']; |
| 110 |
} |
| 111 |
if ($this->isValidCompressionSize($minCompressionSz)) { |
| 112 |
return $minCompressionSz; |
| 113 |
} |
| 114 |
} |
| 115 |
private function isValidCompressionSize($compressionSize) |
| 116 |
{ |
| 117 |
if (\is_numeric($compressionSize) && ($compressionSize >= 0 && $compressionSize <= 10485760)) { |
| 118 |
return \true; |
| 119 |
} |
| 120 |
throw new \InvalidArgumentException('The minimum request compression size must be a ' . 'non-negative integer value between 0 and 10485760 bytes, inclusive.'); |
| 121 |
} |
| 122 |
} |
| 123 |
|