| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\S3; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\Aws\CommandInterface; |
| 6 |
use Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7\Uri; |
| 7 |
use Dudlewebs\WPMCS\s3\Psr\Http\Message\RequestInterface; |
| 8 |
/** |
| 9 |
* Used to update the host used for S3 requests in the case of using a |
| 10 |
* "bucket endpoint" or CNAME bucket. |
| 11 |
* |
| 12 |
* IMPORTANT: this middleware must be added after the "build" step. |
| 13 |
* |
| 14 |
* @internal |
| 15 |
*/ |
| 16 |
class BucketEndpointMiddleware |
| 17 |
{ |
| 18 |
private static $exclusions = ['GetBucketLocation' => \true]; |
| 19 |
private $nextHandler; |
| 20 |
private bool $useEndpointV2; |
| 21 |
private ?string $endpoint; |
| 22 |
/** |
| 23 |
* Create a middleware wrapper function. |
| 24 |
* |
| 25 |
* @param bool $useEndpointV2 |
| 26 |
* @param string|null $endpoint |
| 27 |
* |
| 28 |
* @return callable |
| 29 |
*/ |
| 30 |
public static function wrap(bool $useEndpointV2 = \false, ?string $endpoint = null) : callable |
| 31 |
{ |
| 32 |
return function (callable $handler) use($useEndpointV2, $endpoint) { |
| 33 |
return new self($handler, $useEndpointV2, $endpoint); |
| 34 |
}; |
| 35 |
} |
| 36 |
public function __construct(callable $nextHandler, bool $useEndpointV2, ?string $endpoint = null) |
| 37 |
{ |
| 38 |
$this->nextHandler = $nextHandler; |
| 39 |
$this->useEndpointV2 = $useEndpointV2; |
| 40 |
$this->endpoint = $endpoint; |
| 41 |
} |
| 42 |
public function __invoke(CommandInterface $command, RequestInterface $request) |
| 43 |
{ |
| 44 |
$nextHandler = $this->nextHandler; |
| 45 |
$bucket = $command['Bucket']; |
| 46 |
if ($bucket && !isset(self::$exclusions[$command->getName()])) { |
| 47 |
$request = $this->modifyRequest($request, $command); |
| 48 |
} |
| 49 |
return $nextHandler($command, $request); |
| 50 |
} |
| 51 |
/** |
| 52 |
* @param string $path |
| 53 |
* @param string $bucket |
| 54 |
* |
| 55 |
* @return string |
| 56 |
*/ |
| 57 |
private function removeBucketFromPath(string $path, string $bucket) : string |
| 58 |
{ |
| 59 |
$len = \strlen($bucket) + 1; |
| 60 |
if (\str_starts_with($path, "/{$bucket}")) { |
| 61 |
$path = \substr($path, $len); |
| 62 |
} |
| 63 |
return $path ?: '/'; |
| 64 |
} |
| 65 |
/** |
| 66 |
* @param RequestInterface $request |
| 67 |
* @param CommandInterface $command |
| 68 |
* |
| 69 |
* @return RequestInterface |
| 70 |
*/ |
| 71 |
private function modifyRequest(RequestInterface $request, CommandInterface $command) : RequestInterface |
| 72 |
{ |
| 73 |
$uri = $request->getUri(); |
| 74 |
$path = $uri->getPath(); |
| 75 |
$host = $uri->getHost(); |
| 76 |
$bucket = $command['Bucket']; |
| 77 |
if ($this->useEndpointV2 && !empty($this->endpoint)) { |
| 78 |
// V2 provider adds bucket name to host by default |
| 79 |
// preserve original host |
| 80 |
$host = (new Uri($this->endpoint))->getHost(); |
| 81 |
} |
| 82 |
$path = $this->removeBucketFromPath($path, $bucket); |
| 83 |
// Modify the Key to make sure the key is encoded, but slashes are not. |
| 84 |
if ($command['Key']) { |
| 85 |
$path = S3Client::encodeKey(\rawurldecode($path)); |
| 86 |
} |
| 87 |
return $request->withUri($uri->withPath($path)->withHost($host)); |
| 88 |
} |
| 89 |
} |
| 90 |
|