PluginProbe ʕ •ᴥ•ʔ
Transferito: WP Migration / trunk
Transferito: WP Migration vtrunk
trunk 11.4.0 12.0.0 13.1.0 14.0.0 14.0.11 14.0.7 14.1.0 14.1.1 14.1.2 14.1.3 14.1.4
transferito / vendor / aws / aws-sdk-php / src / S3 / ApplyChecksumMiddleware.php
transferito / vendor / aws / aws-sdk-php / src / S3 Last commit date
Crypto 11 months ago Exception 11 months ago RegionalEndpoint 11 months ago UseArnRegion 11 months ago AmbiguousSuccessParser.php 11 months ago ApplyChecksumMiddleware.php 11 months ago BatchDelete.php 11 months ago BucketEndpointArnMiddleware.php 11 months ago BucketEndpointMiddleware.php 11 months ago CalculatesChecksumTrait.php 11 months ago EndpointRegionHelperTrait.php 11 months ago GetBucketLocationParser.php 11 months ago MultipartCopy.php 11 months ago MultipartUploader.php 11 months ago MultipartUploadingTrait.php 11 months ago ObjectCopier.php 11 months ago ObjectUploader.php 11 months ago PermanentRedirectMiddleware.php 11 months ago PostObject.php 11 months ago PostObjectV4.php 11 months ago PutObjectUrlMiddleware.php 11 months ago RetryableMalformedResponseParser.php 11 months ago S3Client.php 11 months ago S3ClientInterface.php 11 months ago S3ClientTrait.php 11 months ago S3EndpointMiddleware.php 11 months ago S3MultiRegionClient.php 11 months ago S3UriParser.php 11 months ago SSECMiddleware.php 11 months ago StreamWrapper.php 11 months ago Transfer.php 11 months ago ValidateResponseChecksumParser.php 11 months ago
ApplyChecksumMiddleware.php
156 lines
1 <?php
2 namespace Aws\S3;
3
4 use Aws\Api\Service;
5 use Aws\CommandInterface;
6 use GuzzleHttp\Psr7;
7 use InvalidArgumentException;
8 use Psr\Http\Message\RequestInterface;
9 use Psr\Http\Message\StreamInterface;
10
11 /**
12 * Apply required or optional checksums to requests before sending.
13 *
14 * IMPORTANT: This middleware must be added after the "build" step.
15 *
16 * @internal
17 */
18 class ApplyChecksumMiddleware
19 {
20 use CalculatesChecksumTrait;
21 private static $sha256AndMd5 = [
22 'PutObject',
23 'UploadPart',
24 ];
25
26 /** @var Service */
27 private $api;
28
29 private $nextHandler;
30
31 /**
32 * Create a middleware wrapper function.
33 *
34 * @param Service $api
35 * @return callable
36 */
37 public static function wrap(Service $api)
38 {
39 return function (callable $handler) use ($api) {
40 return new self($handler, $api);
41 };
42 }
43
44 public function __construct(callable $nextHandler, Service $api)
45 {
46 $this->api = $api;
47 $this->nextHandler = $nextHandler;
48 }
49
50 public function __invoke(
51 CommandInterface $command,
52 RequestInterface $request
53 ) {
54 $next = $this->nextHandler;
55 $name = $command->getName();
56 $body = $request->getBody();
57
58 //Checks if AddContentMD5 has been specified for PutObject or UploadPart
59 $addContentMD5 = isset($command['AddContentMD5'])
60 ? $command['AddContentMD5']
61 : null;
62
63 $op = $this->api->getOperation($command->getName());
64
65 $checksumInfo = isset($op['httpChecksum'])
66 ? $op['httpChecksum']
67 : [];
68 $checksumMemberName = array_key_exists('requestAlgorithmMember', $checksumInfo)
69 ? $checksumInfo['requestAlgorithmMember']
70 : "";
71 $requestedAlgorithm = isset($command[$checksumMemberName])
72 ? $command[$checksumMemberName]
73 : null;
74 if (!empty($checksumMemberName) && !empty($requestedAlgorithm)) {
75 $requestedAlgorithm = strtolower($requestedAlgorithm);
76 $checksumMember = $op->getInput()->getMember($checksumMemberName);
77 $supportedAlgorithms = isset($checksumMember['enum'])
78 ? array_map('strtolower', $checksumMember['enum'])
79 : null;
80 if (is_array($supportedAlgorithms)
81 && in_array($requestedAlgorithm, $supportedAlgorithms)
82 ) {
83 $request = $this->addAlgorithmHeader($requestedAlgorithm, $request, $body);
84 } else {
85 throw new InvalidArgumentException(
86 "Unsupported algorithm supplied for input variable {$checksumMemberName}."
87 . " Supported checksums for this operation include: "
88 . implode(", ", $supportedAlgorithms) . "."
89 );
90 }
91 return $next($command, $request);
92 }
93
94 if (!empty($checksumInfo)) {
95 //if the checksum member is absent, check if it's required
96 $checksumRequired = isset($checksumInfo['requestChecksumRequired'])
97 ? $checksumInfo['requestChecksumRequired']
98 : null;
99 if ((!empty($checksumRequired))
100 || (in_array($name, self::$sha256AndMd5) && $addContentMD5)
101 ) {
102 //S3Express doesn't support MD5; default to crc32 instead
103 if ($this->isS3Express($command)) {
104 $request = $this->addAlgorithmHeader('crc32', $request, $body);
105 } elseif (!$request->hasHeader('Content-MD5')) {
106 // Set the content MD5 header for operations that require it.
107 $request = $request->withHeader(
108 'Content-MD5',
109 base64_encode(Psr7\Utils::hash($body, 'md5', true))
110 );
111 }
112 return $next($command, $request);
113 }
114 }
115
116 if (in_array($name, self::$sha256AndMd5) && $command['ContentSHA256']) {
117 // Set the content hash header if provided in the parameters.
118 $request = $request->withHeader(
119 'X-Amz-Content-Sha256',
120 $command['ContentSHA256']
121 );
122 }
123
124 return $next($command, $request);
125 }
126
127 /**
128 * @param string $requestedAlgorithm
129 * @param RequestInterface $request
130 * @param StreamInterface $body
131 * @return RequestInterface
132 */
133 private function addAlgorithmHeader(
134 string $requestedAlgorithm,
135 RequestInterface $request,
136 StreamInterface $body
137 ) {
138 $headerName = "x-amz-checksum-{$requestedAlgorithm}";
139 if (!$request->hasHeader($headerName)) {
140 $encoded = $this->getEncodedValue($requestedAlgorithm, $body);
141 $request = $request->withHeader($headerName, $encoded);
142 }
143 return $request;
144 }
145
146 /**
147 * @param CommandInterface $command
148 * @return bool
149 */
150 private function isS3Express($command): bool
151 {
152 $authSchemes = $command->getAuthSchemes();
153 return isset($authSchemes['name']) && $authSchemes['name'] == 's3express';
154 }
155 }
156