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