S3ControlEndpointMiddleware.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace Aws\S3Control;
  3. use Aws\CommandInterface;
  4. use Psr\Http\Message\RequestInterface;
  5. /**
  6. * Used to update the URL used for S3 Control requests to support S3 Control
  7. * DualStack. It will build to host style paths, including for S3 Control
  8. * DualStack.
  9. *
  10. * IMPORTANT: this middleware must be added after the "build" step.
  11. *
  12. * @internal
  13. */
  14. class S3ControlEndpointMiddleware
  15. {
  16. /** @var bool */
  17. private $dualStackByDefault;
  18. /** @var string */
  19. private $region;
  20. /** @var callable */
  21. private $nextHandler;
  22. /** @var string|null */
  23. private $endpoint;
  24. /**
  25. * Create a middleware wrapper function
  26. *
  27. * @param string $region
  28. * @param array $options
  29. *
  30. * @return callable
  31. */
  32. public static function wrap($region, array $options)
  33. {
  34. return function (callable $handler) use ($region, $options) {
  35. return new self($handler, $region, $options);
  36. };
  37. }
  38. public function __construct(
  39. callable $nextHandler,
  40. $region,
  41. array $options
  42. ) {
  43. $this->dualStackByDefault = isset($options['dual_stack'])
  44. ? (bool) $options['dual_stack'] : false;
  45. $this->region = (string) $region;
  46. $this->nextHandler = $nextHandler;
  47. }
  48. public function __invoke(CommandInterface $command, RequestInterface $request)
  49. {
  50. if ($this->isDualStackRequest($command, $request)) {
  51. $request = $this->applyDualStackEndpoint($command, $request);
  52. }
  53. $nextHandler = $this->nextHandler;
  54. return $nextHandler($command, $request);
  55. }
  56. private function isDualStackRequest(
  57. CommandInterface $command,
  58. RequestInterface $request
  59. ) {
  60. return isset($command['@use_dual_stack_endpoint'])
  61. ? $command['@use_dual_stack_endpoint'] : $this->dualStackByDefault;
  62. }
  63. private function getDualStackHost($host)
  64. {
  65. $parts = explode(".{$this->region}.", $host);
  66. return $parts[0] . ".dualstack.{$this->region}." . $parts[1];
  67. }
  68. private function applyDualStackEndpoint(
  69. CommandInterface $command,
  70. RequestInterface $request
  71. ) {
  72. $uri = $request->getUri();
  73. return $request->withUri(
  74. $uri->withHost($this->getDualStackHost(
  75. $uri->getHost()
  76. ))
  77. );
  78. }
  79. }