S3ControlEndpointMiddleware.php
3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
namespace Aws\S3Control;
use Aws\CommandInterface;
use Psr\Http\Message\RequestInterface;
/**
* Used to update the URL used for S3 Control requests to support S3 Control
* DualStack. It will build to host style paths, including for S3 Control
* DualStack.
*
* IMPORTANT: this middleware must be added after the "build" step.
*
* @internal
*/
class S3ControlEndpointMiddleware
{
const NO_PATTERN = 0;
const DUALSTACK = 1;
/** @var bool */
private $dualStackByDefault;
/** @var string */
private $region;
/** @var callable */
private $nextHandler;
/**
* Create a middleware wrapper function
*
* @param string $region
* @param array $options
*
* @return callable
*/
public static function wrap($region, array $options)
{
return function (callable $handler) use ($region, $options) {
return new self($handler, $region, $options);
};
}
public function __construct(
callable $nextHandler,
$region,
array $options
) {
$this->dualStackByDefault = isset($options['dual_stack'])
? (bool) $options['dual_stack'] : false;
$this->region = (string) $region;
$this->nextHandler = $nextHandler;
}
public function __invoke(CommandInterface $command, RequestInterface $request)
{
if ($this->isDualStackRequest($command, $request)) {
$request = $this->applyDualStackEndpoint($command, $request);
}
$request = $this->applyHostStyleEndpoint($command, $request)
->withoutHeader('x-amz-account-id');
unset($command['AccountId']);
$nextHandler = $this->nextHandler;
return $nextHandler($command, $request);
}
private function isDualStackRequest(
CommandInterface $command,
RequestInterface $request
) {
return isset($command['@use_dual_stack_endpoint'])
? $command['@use_dual_stack_endpoint'] : $this->dualStackByDefault;
}
private function getDualStackHost($host)
{
$parts = explode(".{$this->region}.", $host);
return $parts[0] . ".dualstack.{$this->region}." . $parts[1];
}
private function applyDualStackEndpoint(
CommandInterface $command,
RequestInterface $request
) {
$uri = $request->getUri();
return $request->withUri(
$uri->withHost($this->getDualStackHost(
$uri->getHost()
))
);
}
private function getAccountIdStyleHost(CommandInterface $command, $host)
{
return "{$command['AccountId']}.{$host}";
}
private function getAccountIdlessPath($path, CommandInterface $command)
{
$pattern = '/^\\/' . preg_quote($command['AccountId'], '/') . '/';
return preg_replace($pattern, '', $path) ?: '/';
}
private function applyHostStyleEndpoint(
CommandInterface $command,
RequestInterface $request
) {
$uri = $request->getUri();
$request = $request->withUri(
$uri->withHost($this->getAccountIdStyleHost(
$command,
$uri->getHost()
))
->withPath($this->getAccountIdlessPath(
$uri->getPath(),
$command
))
);
return $request;
}
}