Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/UserAgentMiddleware.php

raw symbols SCIP
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 // Replace spaces with underscores to prevent breaking the user agent format
176 $os = str_replace(' ', '_', php_uname('s'));
177 $release = php_uname('r');
178 $osName = "OS/{$os}#{$release}";
179
180 if (!empty($osName)) {
181 return $osName;
182 }
183 }
184
185 return "";
186 }
187
188 /**
189 * Returns the user agent value for the php language used.
190 *
191 * @return string
192 */
193 private function getLangVersion(): string
194 {
195 return 'lang/php#' . phpversion();
196 }
197
198 /**
199 * Returns the user agent value for the execution env.
200 *
201 * @return string
202 */
203 private function getExecEnv(): string
204 {
205 if ($executionEnvironment = getenv('AWS_EXECUTION_ENV')) {
206 return $executionEnvironment;
207 }
208
209 return "";
210 }
211
212 /**
213 * Returns the user agent value for endpoint discovery as cfg.
214 * This feature is deprecated.
215 *
216 * @return string
217 */
218 private function getEndpointDiscovery(): string
219 {
220 $args = $this->args;
221 if (isset($args['endpoint_discovery'])) {
222 if (($args['endpoint_discovery'] instanceof Configuration
223 && $args['endpoint_discovery']->isEnabled())
224 ) {
225 return 'cfg/endpoint-discovery';
226 } elseif (is_array($args['endpoint_discovery'])
227 && isset($args['endpoint_discovery']['enabled'])
228 && $args['endpoint_discovery']['enabled']
229 ) {
230 return 'cfg/endpoint-discovery';
231 }
232 }
233
234 return "";
235 }
236
237 /**
238 * Returns the user agent value for app id, but just when an
239 * app id was provided as a client argument.
240 *
241 * @return string
242 */
243 private function getAppId(): string
244 {
245 if (empty($this->args['app_id'])) {
246 return "";
247 }
248
249 return 'app/' . $this->args['app_id'];
250 }
251
252 /**
253 * Returns the user agent value for metrics.
254 *
255 * @return string
256 */
257 private function getMetrics(): string
258 {
259 // Resolve first metrics related to client arguments.
260 $this->metricsBuilder->resolveAndAppendFromArgs($this->args);
261 // Build the metrics.
262 $metricsEncoded = $this->metricsBuilder->build();
263 if (empty($metricsEncoded)) {
264 return "";
265 }
266
267 return "m/" . $metricsEncoded;
268 }
269 }