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