| 1 |
<?php |
| 2 |
|
| 3 |
namespace Aws; |
| 4 |
|
| 5 |
use Aws\EndpointDiscovery\Configuration; |
| 6 |
use Closure; |
| 7 |
use Psr\Http\Message\RequestInterface; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 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 |
|
| 32 |
private $nextHandler; |
| 33 |
|
| 34 |
|
| 35 |
private $args; |
| 36 |
|
| 37 |
|
| 38 |
private $metricsBuilder; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 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 |
|
| 58 |
|
| 59 |
|
| 60 |
public function __construct(callable $nextHandler, array $args=[]) |
| 61 |
{ |
| 62 |
$this->nextHandler = $nextHandler; |
| 63 |
$this->args = $args; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 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 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 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 |
|
| 100 |
|
| 101 |
|
| 102 |
return $request->withHeader( |
| 103 |
'User-Agent', |
| 104 |
implode(' ', array_merge( |
| 105 |
$userAgentValue, |
| 106 |
$request->getHeader('User-Agent') |
| 107 |
)) |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 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 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
private function getSdkVersion(): string |
| 135 |
{ |
| 136 |
return 'aws-sdk-php/' . Sdk::VERSION; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
private function getUserAgentVersion(): string |
| 145 |
{ |
| 146 |
return 'ua/' . self::AGENT_VERSION; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 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 |
|
| 166 |
|
| 167 |
|
| 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 |
|
| 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 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
private function getLangVersion(): string |
| 194 |
{ |
| 195 |
return 'lang/php#' . phpversion(); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 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 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 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 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 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 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
private function getMetrics(): string |
| 258 |
{ |
| 259 |
|
| 260 |
$this->metricsBuilder->resolveAndAppendFromArgs($this->args); |
| 261 |
|
| 262 |
$metricsEncoded = $this->metricsBuilder->build(); |
| 263 |
if (empty($metricsEncoded)) { |
| 264 |
return ""; |
| 265 |
} |
| 266 |
|
| 267 |
return "m/" . $metricsEncoded; |
| 268 |
} |
| 269 |
} |