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