1 |
efrain |
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 |
|
|
|
10 |
/**
|
|
|
11 |
* Apply required or optional checksums to requests before sending.
|
|
|
12 |
*
|
|
|
13 |
* IMPORTANT: This middleware must be added after the "build" step.
|
|
|
14 |
*
|
|
|
15 |
* @internal
|
|
|
16 |
*/
|
|
|
17 |
class ApplyChecksumMiddleware
|
|
|
18 |
{
|
|
|
19 |
use CalculatesChecksumTrait;
|
|
|
20 |
private static $sha256AndMd5 = [
|
|
|
21 |
'PutObject',
|
|
|
22 |
'UploadPart',
|
|
|
23 |
];
|
|
|
24 |
|
|
|
25 |
/** @var Service */
|
|
|
26 |
private $api;
|
|
|
27 |
|
|
|
28 |
private $nextHandler;
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Create a middleware wrapper function.
|
|
|
32 |
*
|
|
|
33 |
* @param Service $api
|
|
|
34 |
* @return callable
|
|
|
35 |
*/
|
|
|
36 |
public static function wrap(Service $api)
|
|
|
37 |
{
|
|
|
38 |
return function (callable $handler) use ($api) {
|
|
|
39 |
return new self($handler, $api);
|
|
|
40 |
};
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
public function __construct(callable $nextHandler, Service $api)
|
|
|
44 |
{
|
|
|
45 |
$this->api = $api;
|
|
|
46 |
$this->nextHandler = $nextHandler;
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
public function __invoke(
|
|
|
50 |
CommandInterface $command,
|
|
|
51 |
RequestInterface $request
|
|
|
52 |
) {
|
|
|
53 |
$next = $this->nextHandler;
|
|
|
54 |
$name = $command->getName();
|
|
|
55 |
$body = $request->getBody();
|
|
|
56 |
|
|
|
57 |
//Checks if AddContentMD5 has been specified for PutObject or UploadPart
|
|
|
58 |
$addContentMD5 = isset($command['AddContentMD5'])
|
|
|
59 |
? $command['AddContentMD5']
|
|
|
60 |
: null;
|
|
|
61 |
|
|
|
62 |
$op = $this->api->getOperation($command->getName());
|
|
|
63 |
|
|
|
64 |
$checksumInfo = isset($op['httpChecksum'])
|
|
|
65 |
? $op['httpChecksum']
|
|
|
66 |
: [];
|
|
|
67 |
$checksumMemberName = array_key_exists('requestAlgorithmMember', $checksumInfo)
|
|
|
68 |
? $checksumInfo['requestAlgorithmMember']
|
|
|
69 |
: "";
|
|
|
70 |
$requestedAlgorithm = isset($command[$checksumMemberName])
|
|
|
71 |
? $command[$checksumMemberName]
|
|
|
72 |
: null;
|
|
|
73 |
if (!empty($checksumMemberName) && !empty($requestedAlgorithm)) {
|
|
|
74 |
$requestedAlgorithm = strtolower($requestedAlgorithm);
|
|
|
75 |
$checksumMember = $op->getInput()->getMember($checksumMemberName);
|
|
|
76 |
$supportedAlgorithms = isset($checksumMember['enum'])
|
|
|
77 |
? array_map('strtolower', $checksumMember['enum'])
|
|
|
78 |
: null;
|
|
|
79 |
if (is_array($supportedAlgorithms)
|
|
|
80 |
&& in_array($requestedAlgorithm, $supportedAlgorithms)
|
|
|
81 |
) {
|
|
|
82 |
$headerName = "x-amz-checksum-{$requestedAlgorithm}";
|
|
|
83 |
$encoded = $this->getEncodedValue($requestedAlgorithm, $body);
|
|
|
84 |
if (!$request->hasHeader($headerName)) {
|
|
|
85 |
$request = $request->withHeader($headerName, $encoded);
|
|
|
86 |
}
|
|
|
87 |
} else {
|
|
|
88 |
throw new InvalidArgumentException(
|
|
|
89 |
"Unsupported algorithm supplied for input variable {$checksumMemberName}."
|
|
|
90 |
. " Supported checksums for this operation include: "
|
|
|
91 |
. implode(", ", $supportedAlgorithms) . "."
|
|
|
92 |
);
|
|
|
93 |
}
|
|
|
94 |
return $next($command, $request);
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
if (!empty($checksumInfo)) {
|
|
|
98 |
//if the checksum member is absent, check if it's required
|
|
|
99 |
$checksumRequired = isset($checksumInfo['requestChecksumRequired'])
|
|
|
100 |
? $checksumInfo['requestChecksumRequired']
|
|
|
101 |
: null;
|
|
|
102 |
if ((!empty($checksumRequired) && !$request->hasHeader('Content-MD5'))
|
|
|
103 |
|| (in_array($name, self::$sha256AndMd5) && $addContentMD5)
|
|
|
104 |
) {
|
|
|
105 |
// Set the content MD5 header for operations that require it.
|
|
|
106 |
$request = $request->withHeader(
|
|
|
107 |
'Content-MD5',
|
|
|
108 |
base64_encode(Psr7\Utils::hash($body, 'md5', true))
|
|
|
109 |
);
|
|
|
110 |
return $next($command, $request);
|
|
|
111 |
}
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
if (in_array($name, self::$sha256AndMd5) && $command['ContentSHA256']) {
|
|
|
115 |
// Set the content hash header if provided in the parameters.
|
|
|
116 |
$request = $request->withHeader(
|
|
|
117 |
'X-Amz-Content-Sha256',
|
|
|
118 |
$command['ContentSHA256']
|
|
|
119 |
);
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
return $next($command, $request);
|
|
|
123 |
}
|
|
|
124 |
}
|