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