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