Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
namespace Aws;
4
 
5
use Aws\EndpointDiscovery\Configuration;
6
use Closure;
7
use Psr\Http\Message\RequestInterface;
8
 
9
/**
10
 * Builds and injects the user agent header values.
11
 * This middleware must be appended into step where all the
12
 * metrics to be gathered are already resolved. As of now it should be
13
 * after the signing step.
14
 */
15
class UserAgentMiddleware
16
{
17
    const AGENT_VERSION = 2.1;
18
 
19
    static $userAgentFnList = [
20
        'getSdkVersion',
21
        'getUserAgentVersion',
22
        'getHhvmVersion',
23
        'getOsName',
24
        'getLangVersion',
25
        'getExecEnv',
26
        'getEndpointDiscovery',
27
        'getAppId',
28
        'getMetrics'
29
    ];
30
 
31
    /** @var callable  */
32
    private $nextHandler;
33
 
34
    /** @var array */
35
    private $args;
36
 
37
    /** @var MetricsBuilder */
38
    private $metricsBuilder;
39
 
40
    /**
41
     * Returns a middleware wrapper function.
42
     *
43
     * @param array $args
44
     *
45
     * @return Closure
46
     */
47
    public static function wrap(
48
        array $args
49
    ) : Closure
50
    {
51
        return function (callable $handler) use ($args) {
52
            return new self($handler, $args);
53
        };
54
    }
55
 
56
    /**
57
     * @param callable $nextHandler
58
     * @param array $args
59
     */
60
    public function __construct(callable $nextHandler, array $args=[])
61
    {
62
        $this->nextHandler = $nextHandler;
63
        $this->args = $args;
64
    }
65
 
66
    /**
67
     * When invoked, its injects the user agent header into the
68
     * request headers.
69
     *
70
     * @param CommandInterface $command
71
     * @param RequestInterface $request
72
     *
73
     * @return mixed
74
     */
75
    public function __invoke(CommandInterface $command, RequestInterface $request)
76
    {
77
        $handler = $this->nextHandler;
78
        $this->metricsBuilder = MetricsBuilder::fromCommand($command);
79
        $request = $this->requestWithUserAgentHeader($request);
80
 
81
        return $handler($command, $request);
82
    }
83
 
84
    /**
85
     * Builds the user agent header value, and injects it into the request
86
     * headers. Then, it returns the mutated request.
87
     *
88
     * @param RequestInterface $request
89
     *
90
     * @return RequestInterface
91
     */
92
    private function requestWithUserAgentHeader(RequestInterface $request): RequestInterface
93
    {
94
        $uaAppend = $this->args['ua_append'] ?? [];
95
        $userAgentValue = array_merge(
96
            $this->buildUserAgentValue(),
97
            $uaAppend
98
        );
99
        // It includes the user agent values just for the User-Agent header.
100
        // The reason is that the SEP does not mention appending the
101
        // metrics into the X-Amz-User-Agent header.
102
        return $request->withHeader(
103
            'User-Agent',
104
            implode(' ', array_merge(
105
                $userAgentValue,
106
                $request->getHeader('User-Agent')
107
            ))
108
        );
109
    }
110
 
111
    /**
112
     * Builds the different user agent values.
113
     *
114
     * @return array
115
     */
116
    private function buildUserAgentValue(): array
117
    {
118
        $userAgentValue = [];
119
        foreach (self::$userAgentFnList as $fn) {
120
            $val = $this->{$fn}();
121
            if (!empty($val)) {
122
                $userAgentValue[] = $val;
123
            }
124
        }
125
 
126
        return $userAgentValue;
127
    }
128
 
129
    /**
130
     * Returns the user agent value for SDK version.
131
     *
132
     * @return string
133
     */
134
    private function getSdkVersion(): string
135
    {
136
        return 'aws-sdk-php/' . Sdk::VERSION;
137
    }
138
 
139
    /**
140
     * Returns the user agent value for the agent version.
141
     *
142
     * @return string
143
     */
144
    private function getUserAgentVersion(): string
145
    {
146
        return 'ua/' . self::AGENT_VERSION;
147
    }
148
 
149
    /**
150
     * Returns the user agent value for the hhvm version, but just
151
     * when it is defined.
152
     *
153
     * @return string
154
     */
155
    private function getHhvmVersion(): string
156
    {
157
        if (defined('HHVM_VERSION')) {
158
            return 'HHVM/' . HHVM_VERSION;
159
        }
160
 
161
        return "";
162
    }
163
 
164
    /**
165
     * Returns the user agent value for the os version.
166
     *
167
     * @return string
168
     */
169
    private function getOsName(): string
170
    {
171
        $disabledFunctions = explode(',', ini_get('disable_functions'));
172
        if (function_exists('php_uname')
173
            && !in_array('php_uname', $disabledFunctions, true)
174
        ) {
175
            $osName = "OS/" . php_uname('s') . '#' . php_uname('r');
176
            if (!empty($osName)) {
177
                return $osName;
178
            }
179
        }
180
 
181
        return "";
182
    }
183
 
184
    /**
185
     * Returns the user agent value for the php language used.
186
     *
187
     * @return string
188
     */
189
    private function getLangVersion(): string
190
    {
191
        return 'lang/php#' . phpversion();
192
    }
193
 
194
    /**
195
     * Returns the user agent value for the execution env.
196
     *
197
     * @return string
198
     */
199
    private function getExecEnv(): string
200
    {
201
        if ($executionEnvironment = getenv('AWS_EXECUTION_ENV')) {
202
            return $executionEnvironment;
203
        }
204
 
205
        return "";
206
    }
207
 
208
    /**
209
     * Returns the user agent value for endpoint discovery as cfg.
210
     * This feature is deprecated.
211
     *
212
     * @return string
213
     */
214
    private function getEndpointDiscovery(): string
215
    {
216
        $args = $this->args;
217
        if (isset($args['endpoint_discovery'])) {
218
            if (($args['endpoint_discovery'] instanceof Configuration
219
                && $args['endpoint_discovery']->isEnabled())
220
            ) {
221
                return 'cfg/endpoint-discovery';
222
            } elseif (is_array($args['endpoint_discovery'])
223
                && isset($args['endpoint_discovery']['enabled'])
224
                && $args['endpoint_discovery']['enabled']
225
            ) {
226
                return 'cfg/endpoint-discovery';
227
            }
228
        }
229
 
230
        return "";
231
    }
232
 
233
    /**
234
     * Returns the user agent value for app id, but just when an
235
     * app id was provided as a client argument.
236
     *
237
     * @return string
238
     */
239
    private function getAppId(): string
240
    {
241
        if (empty($this->args['app_id'])) {
242
            return "";
243
        }
244
 
245
        return 'app/' . $this->args['app_id'];
246
    }
247
 
248
    /**
249
     * Returns the user agent value for metrics.
250
     *
251
     * @return string
252
     */
253
    private function getMetrics(): string
254
    {
255
        // Resolve first metrics related to client arguments.
256
        $this->metricsBuilder->resolveAndAppendFromArgs($this->args);
257
        // Build the metrics.
258
        $metricsEncoded = $this->metricsBuilder->build();
259
        if (empty($metricsEncoded)) {
260
            return "";
261
        }
262
 
263
        return "m/" . $metricsEncoded;
264
    }
265
}