1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlibabaCloud\Client\Request; |
4
|
|
|
|
5
|
|
|
use AlibabaCloud\Client\Credentials\AccessKeyCredential; |
6
|
|
|
use AlibabaCloud\Client\Credentials\BearerTokenCredential; |
7
|
|
|
use AlibabaCloud\Client\Credentials\CredentialsInterface; |
8
|
|
|
use AlibabaCloud\Client\Credentials\StsCredential; |
9
|
|
|
use AlibabaCloud\Client\Exception\ClientException; |
10
|
|
|
use AlibabaCloud\Client\Filter\ApiFilter; |
11
|
|
|
use AlibabaCloud\Client\Filter\Filter; |
12
|
|
|
use AlibabaCloud\Client\Request\Traits\DeprecatedRoaTrait; |
13
|
|
|
use AlibabaCloud\Client\SDK; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* RESTful ROA Request. |
17
|
|
|
* |
18
|
|
|
* @package AlibabaCloud\Client\Request |
19
|
|
|
*/ |
20
|
|
|
class RoaRequest extends Request |
21
|
|
|
{ |
22
|
|
|
use DeprecatedRoaTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private static $headerSeparator = "\n"; |
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private static $querySeparator = '&'; |
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
public $pathPattern = '/'; |
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
public $pathParameters = []; |
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $dateTimeFormat = "D, d M Y H:i:s \G\M\T"; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Resolve request parameter. |
47
|
|
|
* |
48
|
|
|
* @param AccessKeyCredential|BearerTokenCredential|StsCredential|CredentialsInterface $credential |
49
|
|
|
* |
50
|
|
|
* @throws ClientException |
51
|
|
|
*/ |
52
|
16 |
|
public function resolveParameters($credential) |
53
|
|
|
{ |
54
|
16 |
|
$this->options['query']['Version'] = $this->version; |
55
|
16 |
|
$this->options['headers']['x-acs-version'] = $this->version; |
56
|
16 |
|
$signature = $this->httpClient()->getSignature(); |
57
|
16 |
|
$this->options['headers']['Date'] = gmdate($this->dateTimeFormat); |
58
|
16 |
|
$this->options['headers']['Accept'] = self::formatToAccept($this->format); |
59
|
16 |
|
$this->options['headers']['x-acs-signature-method'] = $signature->getMethod(); |
60
|
16 |
|
$this->options['headers']['x-acs-signature-version'] = $signature->getVersion(); |
61
|
16 |
|
if ($signature->getType()) { |
62
|
3 |
|
$this->options['headers']['x-acs-signature-type'] = $signature->getType(); |
63
|
3 |
|
} |
64
|
16 |
|
$this->options['headers']['x-acs-region-id'] = $this->realRegionId(); |
65
|
16 |
|
if (isset($this->options['form_params'])) { |
66
|
2 |
|
$this->options['headers']['Content-MD5'] = $this->contentMD5(); |
67
|
2 |
|
} |
68
|
16 |
|
$this->options['headers']['Content-Type'] = "{$this->options['headers']['Accept']};chrset=utf-8"; |
69
|
|
|
|
70
|
16 |
|
$this->resolveSecurityToken($credential); |
71
|
16 |
|
$this->resolveBearerToken($credential); |
72
|
|
|
|
73
|
16 |
|
$this->sign($credential); |
74
|
16 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns the accept header according to format. |
78
|
|
|
* |
79
|
|
|
* @param string $format |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
20 |
|
private static function formatToAccept($format) |
84
|
|
|
{ |
85
|
20 |
|
switch (\strtoupper($format)) { |
86
|
20 |
|
case 'JSON': |
87
|
17 |
|
return 'application/json'; |
88
|
3 |
|
case 'XML': |
89
|
1 |
|
return 'application/xml'; |
90
|
2 |
|
default: |
91
|
2 |
|
return 'application/octet-stream'; |
92
|
2 |
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Calculate the md5 value of the content. |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
3 |
|
private function contentMD5() |
101
|
|
|
{ |
102
|
3 |
|
return base64_encode( |
103
|
3 |
|
md5(json_encode($this->options['form_params']), true) |
104
|
3 |
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param CredentialsInterface $credential |
109
|
|
|
*/ |
110
|
16 |
|
private function resolveSecurityToken(CredentialsInterface $credential) |
111
|
|
|
{ |
112
|
16 |
|
if ($credential instanceof StsCredential && $credential->getSecurityToken()) { |
113
|
1 |
|
$this->options['headers']['x-acs-security-token'] = $credential->getSecurityToken(); |
114
|
1 |
|
} |
115
|
16 |
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param CredentialsInterface $credential |
119
|
|
|
*/ |
120
|
16 |
|
private function resolveBearerToken(CredentialsInterface $credential) |
121
|
|
|
{ |
122
|
16 |
|
if ($credential instanceof BearerTokenCredential) { |
123
|
2 |
|
$this->options['headers']['x-acs-bearer-token'] = $credential->getBearerToken(); |
124
|
2 |
|
} |
125
|
16 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Sign the request message. |
129
|
|
|
* |
130
|
|
|
* @param AccessKeyCredential|BearerTokenCredential|StsCredential $credential |
131
|
|
|
* |
132
|
|
|
* @throws ClientException |
133
|
|
|
*/ |
134
|
16 |
|
private function sign($credential) |
135
|
|
|
{ |
136
|
16 |
|
$stringToBeSigned = $this->method . self::$headerSeparator; |
137
|
16 |
|
if (isset($this->options['headers']['Accept'])) { |
138
|
16 |
|
$stringToBeSigned .= $this->options['headers']['Accept']; |
139
|
16 |
|
} |
140
|
16 |
|
$stringToBeSigned .= self::$headerSeparator; |
141
|
|
|
|
142
|
16 |
|
if (isset($this->options['headers']['Content-MD5'])) { |
143
|
2 |
|
$stringToBeSigned .= $this->options['headers']['Content-MD5']; |
144
|
2 |
|
} |
145
|
16 |
|
$stringToBeSigned .= self::$headerSeparator; |
146
|
|
|
|
147
|
16 |
|
if (isset($this->options['headers']['Content-Type'])) { |
148
|
16 |
|
$stringToBeSigned .= $this->options['headers']['Content-Type']; |
149
|
16 |
|
} |
150
|
16 |
|
$stringToBeSigned .= self::$headerSeparator; |
151
|
|
|
|
152
|
16 |
|
if (isset($this->options['headers']['Date'])) { |
153
|
16 |
|
$stringToBeSigned .= $this->options['headers']['Date']; |
154
|
16 |
|
} |
155
|
16 |
|
$stringToBeSigned .= self::$headerSeparator; |
156
|
|
|
|
157
|
16 |
|
$stringToBeSigned .= $this->constructAcsHeader(); |
158
|
|
|
|
159
|
16 |
|
$this->uri = $this->uri->withPath($this->assignPathParameters()) |
160
|
16 |
|
->withQuery($this->queryString()); |
161
|
|
|
|
162
|
16 |
|
$stringToBeSigned .= $this->uri->getPath() . '?' . $this->uri->getQuery(); |
163
|
|
|
|
164
|
16 |
|
$this->stringToBeSigned = $stringToBeSigned; |
165
|
|
|
|
166
|
16 |
|
$this->options['headers']['Authorization'] = 'acs ' |
167
|
16 |
|
. $credential->getAccessKeyId() |
168
|
16 |
|
. ':' |
169
|
16 |
|
. $this->httpClient() |
170
|
16 |
|
->getSignature() |
171
|
16 |
|
->sign( |
172
|
16 |
|
$this->stringToBeSigned, |
173
|
16 |
|
$credential->getAccessKeySecret() |
174
|
16 |
|
); |
175
|
16 |
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Construct standard Header for Alibaba Cloud. |
179
|
|
|
* |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
16 |
|
private function constructAcsHeader() |
183
|
|
|
{ |
184
|
16 |
|
$sortMap = []; |
185
|
16 |
|
foreach ($this->options['headers'] as $headerKey => $headerValue) { |
186
|
16 |
|
$key = strtolower($headerKey); |
187
|
16 |
|
if (strpos($key, 'x-acs-') === 0) { |
188
|
16 |
|
$sortMap[$key] = $headerValue; |
189
|
16 |
|
} |
190
|
16 |
|
} |
191
|
16 |
|
ksort($sortMap); |
192
|
16 |
|
$headerString = ''; |
193
|
16 |
|
foreach ($sortMap as $sortMapKey => $sortMapValue) { |
194
|
16 |
|
$headerString .= $sortMapKey . ':' . $sortMapValue . self::$headerSeparator; |
195
|
16 |
|
} |
196
|
|
|
|
197
|
16 |
|
return $headerString; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Assign path parameters to the url. |
202
|
|
|
* |
203
|
|
|
* @return string |
204
|
|
|
*/ |
205
|
18 |
|
private function assignPathParameters() |
206
|
|
|
{ |
207
|
18 |
|
$result = $this->pathPattern; |
208
|
18 |
|
foreach ($this->pathParameters as $pathParameterKey => $apiParameterValue) { |
209
|
11 |
|
$target = '[' . $pathParameterKey . ']'; |
210
|
11 |
|
$result = str_replace($target, $apiParameterValue, $result); |
211
|
18 |
|
} |
212
|
|
|
|
213
|
18 |
|
return $result; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Get the query string. |
218
|
|
|
* |
219
|
|
|
* @return bool|mixed|string |
220
|
|
|
*/ |
221
|
16 |
|
public function queryString() |
222
|
|
|
{ |
223
|
16 |
|
$query = isset($this->options['query']) |
224
|
16 |
|
? $this->options['query'] |
225
|
16 |
|
: []; |
226
|
|
|
|
227
|
16 |
|
$queryString = $this->ksort($queryString, $query); |
228
|
|
|
|
229
|
16 |
|
if (0 < count($query)) { |
230
|
16 |
|
$queryString = substr($queryString, 0, -1); |
231
|
16 |
|
} |
232
|
|
|
|
233
|
16 |
|
return $queryString; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Sort the entries by key. |
238
|
|
|
* |
239
|
|
|
* @param string $queryString |
240
|
|
|
* @param array $map |
241
|
|
|
* |
242
|
|
|
* @return string |
243
|
|
|
*/ |
244
|
16 |
|
private function ksort(&$queryString, array $map) |
245
|
|
|
{ |
246
|
16 |
|
ksort($map); |
247
|
16 |
|
foreach ($map as $sortMapKey => $sortMapValue) { |
248
|
16 |
|
$queryString .= $sortMapKey; |
249
|
16 |
|
if ($sortMapValue !== null) { |
250
|
16 |
|
$queryString .= '=' . $sortMapValue; |
251
|
16 |
|
} |
252
|
16 |
|
$queryString .= self::$querySeparator; |
253
|
16 |
|
} |
254
|
|
|
|
255
|
16 |
|
return $queryString; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Set path parameter by name. |
260
|
|
|
* |
261
|
|
|
* @param string $name |
262
|
|
|
* @param string $value |
263
|
|
|
* |
264
|
|
|
* @return RoaRequest |
265
|
|
|
* @throws ClientException |
266
|
|
|
*/ |
267
|
16 |
|
public function pathParameter($name, $value) |
268
|
|
|
{ |
269
|
16 |
|
Filter::name($name); |
270
|
|
|
|
271
|
14 |
|
if ($value === '') { |
272
|
1 |
|
throw new ClientException( |
273
|
1 |
|
'Value cannot be empty', |
274
|
|
|
SDK::INVALID_ARGUMENT |
275
|
1 |
|
); |
276
|
|
|
} |
277
|
|
|
|
278
|
13 |
|
$this->pathParameters[$name] = $value; |
279
|
|
|
|
280
|
13 |
|
return $this; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Set path pattern. |
285
|
|
|
* |
286
|
|
|
* @param string $pattern |
287
|
|
|
* |
288
|
|
|
* @return self |
289
|
|
|
* @throws ClientException |
290
|
|
|
*/ |
291
|
10 |
|
public function pathPattern($pattern) |
292
|
|
|
{ |
293
|
10 |
|
ApiFilter::pattern($pattern); |
294
|
|
|
|
295
|
8 |
|
$this->pathPattern = $pattern; |
296
|
|
|
|
297
|
8 |
|
return $this; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Magic method for set or get request parameters. |
302
|
|
|
* |
303
|
|
|
* @param string $name |
304
|
|
|
* @param mixed $arguments |
305
|
|
|
* |
306
|
|
|
* @return $this |
307
|
|
|
*/ |
308
|
11 |
|
public function __call($name, $arguments) |
309
|
|
|
{ |
310
|
11 |
|
if (\strpos($name, 'get') !== false) { |
311
|
2 |
|
$parameterName = $this->propertyNameByMethodName($name); |
312
|
|
|
|
313
|
2 |
|
return $this->__get($parameterName); |
314
|
|
|
} |
315
|
|
|
|
316
|
11 |
|
if (\strpos($name, 'with') !== false) { |
317
|
11 |
|
$parameterName = $this->propertyNameByMethodName($name, 4); |
318
|
11 |
|
$this->__set($parameterName, $arguments[0]); |
319
|
11 |
|
$this->pathParameters[$parameterName] = $arguments[0]; |
320
|
11 |
|
} |
321
|
|
|
|
322
|
11 |
|
return $this; |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|