Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/RequestCompressionMiddleware.php

raw symbols SCIP
1 <?php
2 namespace Aws;
3
4 use GuzzleHttp\Psr7;
5 use Psr\Http\Message\RequestInterface;
6
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 = [
22 'gzip' => 'gzencode'
23 ];
24
25 /**
26 * Create a middleware wrapper function.
27 *
28 * @return callable
29 */
30 public static function wrap(array $config)
31 {
32 return function (callable $handler) use ($config) {
33 return new self($handler, $config);
34 };
35 }
36
37 public function __construct(callable $nextHandler, $config)
38 {
39 $this->minimumCompressionSize = $this->determineMinimumCompressionSize($config);
40 $this->api = $config['api'];
41 $this->nextHandler = $nextHandler;
42 }
43
44 public function __invoke(CommandInterface $command, RequestInterface $request)
45 {
46 if (isset($command['@request_min_compression_size_bytes'])
47 && is_int($command['@request_min_compression_size_bytes'])
48 && $this->isValidCompressionSize($command['@request_min_compression_size_bytes'])
49 ) {
50 $this->minimumCompressionSize = $command['@request_min_compression_size_bytes'];
51 }
52 $nextHandler = $this->nextHandler;
53 $operation = $this->api->getOperation($command->getName());
54 $compressionInfo = $operation['requestcompression'] ?? null;
55
56 if (!$this->shouldCompressRequestBody(
57 $compressionInfo,
58 $command,
59 $operation,
60 $request
61 )) {
62 return $nextHandler($command, $request);
63 }
64
65 $this->encodings = $compressionInfo['encodings'];
66 $request = $this->compressRequestBody($request);
67
68 // Capture request compression metric
69 $command->getMetricsBuilder()->identifyMetricByValueAndAppend(
70 'request_compression',
71 $request->getHeaderLine('content-encoding')
72 );
73
74 return $nextHandler($command, $request);
75 }
76
77 private function compressRequestBody(
78 RequestInterface $request
79 ) {
80 $fn = $this->determineEncoding();
81 if (is_null($fn)) {
82 return $request;
83 }
84
85 $body = $request->getBody()->getContents();
86 $compressedBody = $fn($body);
87
88 $request = $request->withBody(Psr7\Utils::streamFor($compressedBody));
89 if ($request->hasHeader('Content-Encoding')) {
90 return $request->withAddedHeader('Content-Encoding', $this->encoding);
91 }
92
93 return $request->withHeader('Content-Encoding', $this->encoding);
94 }
95
96 private function determineEncoding()
97 {
98 foreach ($this->encodings as $encoding) {
99 if (isset($this->encodingMap[$encoding])) {
100 $this->encoding = $encoding;
101 return $this->encodingMap[$encoding];
102 }
103 }
104 return null;
105 }
106
107 private function shouldCompressRequestBody(
108 $compressionInfo,
109 $command,
110 $operation,
111 $request
112 ){
113 if ($compressionInfo) {
114 if (isset($command['@disable_request_compression'])
115 && $command['@disable_request_compression'] === true
116 ) {
117 return false;
118 } elseif ($this->hasStreamingTraitWithoutRequiresLength($command, $operation)
119 ) {
120 return true;
121 }
122
123 $requestBodySize = $request->hasHeader('content-length')
124 ? (int) $request->getHeaderLine('content-length')
125 : $request->getBody()->getSize();
126
127 if ($requestBodySize >= $this->minimumCompressionSize) {
128 return true;
129 }
130 }
131 return false;
132 }
133
134 private function hasStreamingTraitWithoutRequiresLength($command, $operation)
135 {
136 foreach ($operation->getInput()->getMembers() as $name => $member) {
137 if (isset($command[$name])
138 && !empty($member['streaming'])
139 && empty($member['requiresLength'])
140 ){
141 return true;
142 }
143 }
144 return false;
145 }
146
147 private function determineMinimumCompressionSize($config) {
148 if (is_callable($config['request_min_compression_size_bytes'])) {
149 $minCompressionSz = $config['request_min_compression_size_bytes']();
150 } else {
151 $minCompressionSz = $config['request_min_compression_size_bytes'];
152 }
153
154 if ($this->isValidCompressionSize($minCompressionSz)) {
155 return $minCompressionSz;
156 }
157 }
158
159 private function isValidCompressionSize($compressionSize)
160 {
161 if (is_numeric($compressionSize)
162 && ($compressionSize >= 0 && $compressionSize <= 10485760)
163 ) {
164 return true;
165 }
166
167 throw new \InvalidArgumentException(
168 'The minimum request compression size must be a '
169 . 'non-negative integer value between 0 and 10485760 bytes, inclusive.'
170 );
171 }
172 }