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
|
|
|
use Exception; |
15
|
|
|
use Ramsey\Uuid\Uuid; |
16
|
|
|
use RuntimeException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* RESTful ROA Request. |
20
|
|
|
* |
21
|
|
|
* @package AlibabaCloud\Client\Request |
22
|
|
|
*/ |
23
|
|
|
class RoaRequest extends Request |
24
|
|
|
{ |
25
|
|
|
use DeprecatedRoaTrait; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private static $headerSeparator = "\n"; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private static $querySeparator = '&'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
public $pathPattern = '/'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
public $pathParameters = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
private $dateTimeFormat = "D, d M Y H:i:s \G\M\T"; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Resolve request parameter. |
54
|
|
|
* |
55
|
|
|
* @param AccessKeyCredential|BearerTokenCredential|StsCredential|CredentialsInterface $credential |
56
|
|
|
* |
57
|
|
|
* @throws ClientException |
58
|
|
|
* @throws Exception |
59
|
|
|
*/ |
60
|
17 |
|
public function resolveParameters($credential) |
61
|
|
|
{ |
62
|
17 |
|
$this->resolveCommonParameters($credential); |
63
|
17 |
|
$this->options['headers']['Authorization'] = $this->signature($credential); |
64
|
17 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Resolve Common Parameters. |
68
|
|
|
* |
69
|
|
|
* @param AccessKeyCredential|BearerTokenCredential|StsCredential|CredentialsInterface $credential |
70
|
|
|
* |
71
|
|
|
* @throws ClientException |
72
|
|
|
* @throws Exception |
73
|
|
|
*/ |
74
|
17 |
|
private function resolveCommonParameters($credential) |
75
|
|
|
{ |
76
|
17 |
|
$signature = $this->httpClient()->getSignature(); |
77
|
|
|
|
78
|
17 |
|
if (!isset($this->options['query']['Version'])) { |
79
|
17 |
|
$this->options['query']['Version'] = $this->version; |
80
|
17 |
|
} |
81
|
|
|
|
82
|
17 |
|
if (!isset($this->options['headers']['x-acs-version'])) { |
83
|
17 |
|
$this->options['headers']['x-acs-version'] = $this->version; |
84
|
17 |
|
} |
85
|
|
|
|
86
|
17 |
|
if (!isset($this->options['headers']['Date'])) { |
87
|
17 |
|
$this->options['headers']['Date'] = gmdate($this->dateTimeFormat); |
88
|
17 |
|
} |
89
|
|
|
|
90
|
17 |
|
if (!isset($this->options['headers']['Accept'])) { |
91
|
17 |
|
$this->options['headers']['Accept'] = self::formatToAccept($this->format); |
92
|
17 |
|
} |
93
|
|
|
|
94
|
17 |
|
if (!isset($this->options['headers']['x-acs-signature-method'])) { |
95
|
17 |
|
$this->options['headers']['x-acs-signature-method'] = $signature->getMethod(); |
96
|
17 |
|
} |
97
|
|
|
|
98
|
17 |
|
if (!isset($this->options['headers']['x-acs-signature-nonce'])) { |
99
|
17 |
|
$this->options['headers']['x-acs-signature-nonce'] = Uuid::uuid1()->toString(); |
100
|
17 |
|
} |
101
|
|
|
|
102
|
17 |
|
if (!isset($this->options['headers']['x-acs-signature-version'])) { |
103
|
17 |
|
$this->options['headers']['x-acs-signature-version'] = $signature->getVersion(); |
104
|
17 |
|
} |
105
|
|
|
|
106
|
17 |
|
if (!isset($this->options['headers']['x-acs-signature-type']) && $signature->getType()) { |
107
|
4 |
|
$this->options['headers']['x-acs-signature-type'] = $signature->getType(); |
108
|
4 |
|
} |
109
|
|
|
|
110
|
17 |
|
if (!isset($this->options['headers']['x-acs-region-id'])) { |
111
|
17 |
|
$this->options['headers']['x-acs-region-id'] = $this->realRegionId(); |
112
|
17 |
|
} |
113
|
|
|
|
114
|
17 |
|
if (($this->method === 'POST' || $this->method === 'PUT') && !isset($this->options['body'])) { |
115
|
3 |
|
$this->options['body'] = $this->concatContent($this->data); |
116
|
3 |
|
$this->options['headers']['Content-MD5'] = base64_encode(md5($this->options['body'], true)); |
117
|
3 |
|
$this->options['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'; |
118
|
3 |
|
unset($this->options['form_params']); |
119
|
3 |
|
} |
120
|
|
|
|
121
|
17 |
|
if (!isset($this->options['headers']['Content-Type'])) { |
122
|
14 |
|
$this->options['headers']['Content-Type'] = "{$this->options['headers']['Accept']};chrset=utf-8"; |
123
|
14 |
|
} |
124
|
|
|
|
125
|
17 |
|
$this->resolveSecurityToken($credential); |
126
|
17 |
|
$this->resolveBearerToken($credential); |
127
|
17 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param array $sortMap |
131
|
|
|
* |
132
|
|
|
* @return bool|string |
133
|
|
|
*/ |
134
|
3 |
|
private function concatContent(array $sortMap) |
135
|
|
|
{ |
136
|
3 |
|
if (null === $sortMap || count($sortMap) === 0) { |
137
|
1 |
|
return ''; |
138
|
|
|
} |
139
|
2 |
|
$queryString = ''; |
140
|
2 |
|
ksort($sortMap); |
141
|
2 |
|
foreach ($sortMap as $sortMapKey => $sortMapValue) { |
142
|
2 |
|
$queryString .= $sortMapKey; |
143
|
2 |
|
if (isset($sortMapValue)) { |
144
|
2 |
|
$queryString = $queryString . '=' . urlencode($sortMapValue); |
145
|
2 |
|
} |
146
|
2 |
|
$queryString .= self::$querySeparator; |
147
|
2 |
|
} |
148
|
2 |
|
if (count($sortMap) > 0) { |
149
|
2 |
|
$queryString = substr($queryString, 0, -1); |
150
|
2 |
|
} |
151
|
|
|
|
152
|
2 |
|
return $queryString; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Returns the accept header according to format. |
157
|
|
|
* |
158
|
|
|
* @param string $format |
159
|
|
|
* |
160
|
|
|
* @return string |
161
|
|
|
*/ |
162
|
21 |
|
private static function formatToAccept($format) |
163
|
|
|
{ |
164
|
21 |
|
switch (\strtoupper($format)) { |
165
|
21 |
|
case 'JSON': |
166
|
18 |
|
return 'application/json'; |
167
|
3 |
|
case 'XML': |
168
|
1 |
|
return 'application/xml'; |
169
|
2 |
|
default: |
170
|
2 |
|
return 'application/octet-stream'; |
171
|
2 |
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param CredentialsInterface $credential |
176
|
|
|
*/ |
177
|
17 |
|
private function resolveSecurityToken(CredentialsInterface $credential) |
178
|
|
|
{ |
179
|
17 |
|
if ($credential instanceof StsCredential && $credential->getSecurityToken()) { |
180
|
1 |
|
$this->options['headers']['x-acs-security-token'] = $credential->getSecurityToken(); |
181
|
1 |
|
} |
182
|
17 |
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param CredentialsInterface $credential |
186
|
|
|
*/ |
187
|
17 |
|
private function resolveBearerToken(CredentialsInterface $credential) |
188
|
|
|
{ |
189
|
17 |
|
if ($credential instanceof BearerTokenCredential) { |
190
|
3 |
|
$this->options['headers']['x-acs-bearer-token'] = $credential->getBearerToken(); |
191
|
3 |
|
} |
192
|
17 |
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return string |
196
|
|
|
*/ |
197
|
17 |
|
private function headerStringToSign() |
198
|
|
|
{ |
199
|
17 |
|
$string = $this->method . self::$headerSeparator; |
200
|
17 |
|
if (isset($this->options['headers']['Accept'])) { |
201
|
17 |
|
$string .= $this->options['headers']['Accept']; |
202
|
17 |
|
} |
203
|
17 |
|
$string .= self::$headerSeparator; |
204
|
|
|
|
205
|
17 |
|
if (isset($this->options['headers']['Content-MD5'])) { |
206
|
3 |
|
$string .= $this->options['headers']['Content-MD5']; |
207
|
3 |
|
} |
208
|
17 |
|
$string .= self::$headerSeparator; |
209
|
|
|
|
210
|
17 |
|
if (isset($this->options['headers']['Content-Type'])) { |
211
|
17 |
|
$string .= $this->options['headers']['Content-Type']; |
212
|
17 |
|
} |
213
|
17 |
|
$string .= self::$headerSeparator; |
214
|
|
|
|
215
|
17 |
|
if (isset($this->options['headers']['Date'])) { |
216
|
17 |
|
$string .= $this->options['headers']['Date']; |
217
|
17 |
|
} |
218
|
17 |
|
$string .= self::$headerSeparator; |
219
|
|
|
|
220
|
17 |
|
$string .= $this->constructAcsHeader(); |
221
|
|
|
|
222
|
17 |
|
return $string; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @return string |
227
|
|
|
*/ |
228
|
17 |
|
private function resourceStringToSign() |
229
|
|
|
{ |
230
|
17 |
|
$this->uri = $this->uri->withPath($this->assignPathParameters()) |
231
|
17 |
|
->withQuery($this->queryString()); |
232
|
|
|
|
233
|
17 |
|
return $this->uri->getPath() . '?' . $this->uri->getQuery(); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @return string |
238
|
|
|
*/ |
239
|
17 |
|
public function stringToSign() |
240
|
|
|
{ |
241
|
17 |
|
return $this->headerStringToSign() . $this->resourceStringToSign(); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Sign the request message. |
246
|
|
|
* |
247
|
|
|
* @param AccessKeyCredential|BearerTokenCredential|StsCredential $credential |
248
|
|
|
* |
249
|
|
|
* @return string |
250
|
|
|
* @throws ClientException |
251
|
|
|
*/ |
252
|
17 |
|
private function signature($credential) |
253
|
|
|
{ |
254
|
17 |
|
$accessKeyId = $credential->getAccessKeyId(); |
255
|
17 |
|
$signature = $this->httpClient() |
256
|
17 |
|
->getSignature() |
257
|
17 |
|
->sign( |
258
|
17 |
|
$this->stringToSign(), |
259
|
17 |
|
$credential->getAccessKeySecret() |
260
|
17 |
|
); |
261
|
|
|
|
262
|
17 |
|
return "acs $accessKeyId:$signature"; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Construct standard Header for Alibaba Cloud. |
267
|
|
|
* |
268
|
|
|
* @return string |
269
|
|
|
*/ |
270
|
17 |
|
private function constructAcsHeader() |
271
|
|
|
{ |
272
|
17 |
|
$sortMap = []; |
273
|
17 |
|
foreach ($this->options['headers'] as $headerKey => $headerValue) { |
274
|
17 |
|
$key = strtolower($headerKey); |
275
|
17 |
|
if (strpos($key, 'x-acs-') === 0) { |
276
|
17 |
|
$sortMap[$key] = $headerValue; |
277
|
17 |
|
} |
278
|
17 |
|
} |
279
|
17 |
|
ksort($sortMap); |
280
|
17 |
|
$headerString = ''; |
281
|
17 |
|
foreach ($sortMap as $sortMapKey => $sortMapValue) { |
282
|
17 |
|
$headerString .= $sortMapKey . ':' . $sortMapValue . self::$headerSeparator; |
283
|
17 |
|
} |
284
|
|
|
|
285
|
17 |
|
return $headerString; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Assign path parameters to the url. |
290
|
|
|
* |
291
|
|
|
* @return string |
292
|
|
|
*/ |
293
|
19 |
|
private function assignPathParameters() |
294
|
|
|
{ |
295
|
19 |
|
$result = $this->pathPattern; |
296
|
19 |
|
foreach ($this->pathParameters as $pathParameterKey => $apiParameterValue) { |
297
|
12 |
|
$target = '[' . $pathParameterKey . ']'; |
298
|
12 |
|
$result = str_replace($target, $apiParameterValue, $result); |
299
|
19 |
|
} |
300
|
|
|
|
301
|
19 |
|
return $result; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Get the query string. |
306
|
|
|
* |
307
|
|
|
* @return bool|mixed|string |
308
|
|
|
*/ |
309
|
17 |
|
public function queryString() |
310
|
|
|
{ |
311
|
17 |
|
$query = isset($this->options['query']) |
312
|
17 |
|
? $this->options['query'] |
313
|
17 |
|
: []; |
314
|
|
|
|
315
|
17 |
|
$queryString = $this->ksort($queryString, $query); |
316
|
|
|
|
317
|
17 |
|
if (0 < count($query)) { |
318
|
17 |
|
$queryString = substr($queryString, 0, -1); |
319
|
17 |
|
} |
320
|
|
|
|
321
|
17 |
|
return $queryString; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Sort the entries by key. |
326
|
|
|
* |
327
|
|
|
* @param string $queryString |
328
|
|
|
* @param array $map |
329
|
|
|
* |
330
|
|
|
* @return string |
331
|
|
|
*/ |
332
|
17 |
|
private function ksort(&$queryString, array $map) |
333
|
|
|
{ |
334
|
17 |
|
ksort($map); |
335
|
17 |
|
foreach ($map as $sortMapKey => $sortMapValue) { |
336
|
17 |
|
$queryString .= $sortMapKey; |
337
|
17 |
|
if ($sortMapValue !== null) { |
338
|
17 |
|
$queryString .= '=' . $sortMapValue; |
339
|
17 |
|
} |
340
|
17 |
|
$queryString .= self::$querySeparator; |
341
|
17 |
|
} |
342
|
|
|
|
343
|
17 |
|
return $queryString; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Set path parameter by name. |
348
|
|
|
* |
349
|
|
|
* @param string $name |
350
|
|
|
* @param string $value |
351
|
|
|
* |
352
|
|
|
* @return RoaRequest |
353
|
|
|
* @throws ClientException |
354
|
|
|
*/ |
355
|
16 |
|
public function pathParameter($name, $value) |
356
|
|
|
{ |
357
|
16 |
|
Filter::name($name); |
358
|
|
|
|
359
|
14 |
|
if ($value === '') { |
360
|
1 |
|
throw new ClientException( |
361
|
1 |
|
'Value cannot be empty', |
362
|
|
|
SDK::INVALID_ARGUMENT |
363
|
1 |
|
); |
364
|
|
|
} |
365
|
|
|
|
366
|
13 |
|
$this->pathParameters[$name] = $value; |
367
|
|
|
|
368
|
13 |
|
return $this; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* Set path pattern. |
373
|
|
|
* |
374
|
|
|
* @param string $pattern |
375
|
|
|
* |
376
|
|
|
* @return self |
377
|
|
|
* @throws ClientException |
378
|
|
|
*/ |
379
|
10 |
|
public function pathPattern($pattern) |
380
|
|
|
{ |
381
|
10 |
|
ApiFilter::pattern($pattern); |
382
|
|
|
|
383
|
8 |
|
$this->pathPattern = $pattern; |
384
|
|
|
|
385
|
8 |
|
return $this; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* Magic method for set or get request parameters. |
390
|
|
|
* |
391
|
|
|
* @param string $name |
392
|
|
|
* @param mixed $arguments |
393
|
|
|
* |
394
|
|
|
* @return $this |
395
|
|
|
*/ |
396
|
11 |
|
public function __call($name, $arguments) |
397
|
|
|
{ |
398
|
11 |
|
if (\strpos($name, 'get') === 0) { |
399
|
1 |
|
$parameterName = $this->propertyNameByMethodName($name); |
400
|
|
|
|
401
|
1 |
|
return $this->__get($parameterName); |
402
|
|
|
} |
403
|
|
|
|
404
|
11 |
|
if (\strpos($name, 'with') === 0) { |
405
|
10 |
|
$parameterName = $this->propertyNameByMethodName($name, 4); |
406
|
10 |
|
$this->__set($parameterName, $arguments[0]); |
407
|
10 |
|
$this->pathParameters[$parameterName] = $arguments[0]; |
408
|
|
|
|
409
|
10 |
|
return $this; |
410
|
|
|
} |
411
|
|
|
|
412
|
2 |
|
if (\strpos($name, 'set') === 0) { |
413
|
1 |
|
$parameterName = $this->propertyNameByMethodName($name); |
414
|
1 |
|
$withMethod = "with$parameterName"; |
415
|
|
|
|
416
|
1 |
|
return $this->$withMethod($arguments[0]); |
417
|
|
|
} |
418
|
|
|
|
419
|
1 |
|
throw new RuntimeException('Call to undefined method ' . __CLASS__ . '::' . $name . '()'); |
420
|
|
|
} |
421
|
|
|
} |
422
|
|
|
|