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