summaryrefslogtreecommitdiff
path: root/vendor/aws/aws-sdk-php/src/RequestCompressionMiddleware.php
blob: a83e593fdc3148e3fe078a5a2f4adafe75dc28cd (plain)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
namespace Aws;

use GuzzleHttp\Psr7;
use Psr\Http\Message\RequestInterface;

/**
 * Used to compress request payloads if the service/operation support it.
 *
 * IMPORTANT: this middleware must be added after the "build" step.
 *
 * @internal
 */
class RequestCompressionMiddleware
{
    private $api;
    private $minimumCompressionSize;
    private $nextHandler;
    private $encodings;
    private $encoding;
    private $encodingMap = [
        'gzip' => 'gzencode'
    ];

    /**
     * Create a middleware wrapper function.
     *
     * @return callable
     */
    public static function wrap(array $config)
    {
        return function (callable $handler) use ($config) {
            return new self($handler, $config);
        };
    }

    public function __construct(callable $nextHandler, $config)
    {
        $this->minimumCompressionSize = $this->determineMinimumCompressionSize($config);
        $this->api = $config['api'];
        $this->nextHandler = $nextHandler;
    }

    public function __invoke(CommandInterface $command, RequestInterface $request)
    {
        if (isset($command['@request_min_compression_size_bytes'])
            && is_int($command['@request_min_compression_size_bytes'])
            && $this->isValidCompressionSize($command['@request_min_compression_size_bytes'])
        ) {
            $this->minimumCompressionSize = $command['@request_min_compression_size_bytes'];
        }
        $nextHandler = $this->nextHandler;
        $operation = $this->api->getOperation($command->getName());
        $compressionInfo = isset($operation['requestcompression'])
            ? $operation['requestcompression']
            : null;

        if (!$this->shouldCompressRequestBody(
            $compressionInfo,
            $command,
            $operation,
            $request
        )) {
            return $nextHandler($command, $request);
        }

        $this->encodings = $compressionInfo['encodings'];
        $request = $this->compressRequestBody($request);

        return $nextHandler($command, $request);
    }

    private function compressRequestBody(
        RequestInterface $request
    ) {
        $fn = $this->determineEncoding();
        if (is_null($fn)) {
            return $request;
        }

        $body = $request->getBody()->getContents();
        $compressedBody = $fn($body);

        return $request->withBody(Psr7\Utils::streamFor($compressedBody))
            ->withHeader('content-encoding', $this->encoding);
    }

    private function determineEncoding()
    {
        foreach ($this->encodings as $encoding) {
            if (isset($this->encodingMap[$encoding])) {
                $this->encoding = $encoding;
                return $this->encodingMap[$encoding];
            }
        }
        return null;
    }

    private function shouldCompressRequestBody(
        $compressionInfo,
        $command,
        $operation,
        $request
    ){
        if ($compressionInfo) {
            if (isset($command['@disable_request_compression'])
                && $command['@disable_request_compression'] === true
            ) {
                return false;
            } elseif ($this->hasStreamingTraitWithoutRequiresLength($command, $operation)
            ) {
                return true;
            }

            $requestBodySize = $request->hasHeader('content-length')
                ? (int) $request->getHeaderLine('content-length')
                : $request->getBody()->getSize();

            if ($requestBodySize >= $this->minimumCompressionSize) {
                return true;
            }
        }
        return false;
    }

    private function hasStreamingTraitWithoutRequiresLength($command, $operation)
    {
        foreach ($operation->getInput()->getMembers() as $name => $member) {
            if (isset($command[$name])
                && !empty($member['streaming'])
                && empty($member['requiresLength'])
            ){
                return true;
            }
        }
        return false;
    }

    private function determineMinimumCompressionSize($config) {
        if (is_callable($config['request_min_compression_size_bytes'])) {
            $minCompressionSz = $config['request_min_compression_size_bytes']();
        } else {
            $minCompressionSz = $config['request_min_compression_size_bytes'];
        }

        if ($this->isValidCompressionSize($minCompressionSz)) {
            return $minCompressionSz;
        }
    }

    private function isValidCompressionSize($compressionSize)
    {
        if (is_numeric($compressionSize)
            && ($compressionSize >= 0 && $compressionSize <= 10485760)
        ) {
            return true;
        }

        throw new \InvalidArgumentException(
            'The minimum request compression size must be a '
            . 'non-negative integer value between 0 and 10485760 bytes, inclusive.'
        );
    }
}