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