RoaAcsRequest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. abstract class RoaAcsRequest extends AcsRequest
  21. {
  22. /**
  23. * @var string
  24. */
  25. protected $uriPattern;
  26. /**
  27. * @var array
  28. */
  29. protected $pathParameters = array();
  30. /**
  31. * @var array
  32. */
  33. private $domainParameters = array();
  34. /**
  35. * @var string
  36. */
  37. private $dateTimeFormat = "D, d M Y H:i:s \G\M\T";
  38. /**
  39. * @var string
  40. */
  41. private static $headerSeparator = "\n";
  42. /**
  43. * @var string
  44. */
  45. private static $querySeparator = '&';
  46. /**
  47. * @var string
  48. */
  49. protected $method = 'RAW';
  50. /**
  51. * @var string
  52. */
  53. protected $acceptFormat = 'JSON';
  54. /**
  55. * @param $iSigner
  56. * @param $credential
  57. * @param $domain
  58. *
  59. * @return mixed|string
  60. */
  61. public function composeUrl($iSigner, $credential, $domain)
  62. {
  63. $this->headers['x-acs-version'] = &$this->version;
  64. $this->prepareHeader($iSigner, $credential);
  65. $signString = $this->getMethod() . self::$headerSeparator;
  66. if (isset($this->headers['Accept'])) {
  67. $signString .= $this->headers['Accept'];
  68. }
  69. $signString .= self::$headerSeparator;
  70. if (isset($this->headers['Content-MD5'])) {
  71. $signString .= $this->headers['Content-MD5'];
  72. }
  73. $signString .= self::$headerSeparator;
  74. if (isset($this->headers['Content-Type'])) {
  75. $signString .= $this->headers['Content-Type'];
  76. }
  77. $signString .= self::$headerSeparator;
  78. if (isset($this->headers['Date'])) {
  79. $signString .= $this->headers['Date'];
  80. }
  81. $signString .= self::$headerSeparator;
  82. $uri = $this->replaceOccupiedParameters();
  83. $signString .= $this->buildCanonicalHeaders();
  84. $queryString = $this->buildQueryString($uri);
  85. if (substr($queryString, -1) === '?') {
  86. $queryString = substr($queryString, 0, -1);
  87. }
  88. $signString .= $queryString;
  89. $this->stringToBeSigned = $signString;
  90. $this->headers['Authorization'] = 'acs ' . $credential->getAccessKeyId() . ':'
  91. . $iSigner->signString($signString, $credential->getAccessSecret());
  92. $requestUrl = $this->getProtocol() . '://' . $domain . $queryString;
  93. return $requestUrl;
  94. }
  95. /**
  96. * @return string
  97. */
  98. private function concatQueryString()
  99. {
  100. $sortMap = $this->queryParameters;
  101. if (null == $sortMap || count($sortMap) == 0) {
  102. return '';
  103. }
  104. $queryString = '';
  105. ksort($sortMap);
  106. foreach ($sortMap as $sortMapKey => $sortMapValue) {
  107. $queryString .= $sortMapKey;
  108. if (isset($sortMapValue)) {
  109. $queryString = $queryString . '=' . urlencode($sortMapValue);
  110. }
  111. $queryString .= self::$querySeparator;
  112. }
  113. if (count($sortMap) > 0) {
  114. $queryString = substr($queryString, 0, -1);
  115. }
  116. return '?' . $queryString;
  117. }
  118. /**
  119. * @param $iSigner
  120. * @param $credential
  121. */
  122. private function prepareHeader($iSigner, $credential)
  123. {
  124. $this->headers['Date'] = gmdate($this->dateTimeFormat);
  125. if (null == $this->acceptFormat) {
  126. $this->acceptFormat = 'RAW';
  127. }
  128. $this->headers['Accept'] = $this->formatToAccept($this->getAcceptFormat());
  129. $this->headers['x-acs-signature-method'] = $iSigner->getSignatureMethod();
  130. $this->headers['x-acs-signature-version'] = $iSigner->getSignatureVersion();
  131. if ($iSigner->getSignatureType() != null) {
  132. $this->headers['x-acs-signature-type'] = $iSigner->getSignatureType();
  133. }
  134. $this->headers['x-acs-region-id'] = $this->regionId;
  135. $content = $this->getDomainParameter();
  136. if ($content != null) {
  137. $this->headers['Content-MD5'] = base64_encode(md5(json_encode($content), true));
  138. }
  139. if ($this->acceptFormat === 'JSON') {
  140. $this->headers['Content-Type'] = 'application/json;charset=utf-8';
  141. } else {
  142. $this->headers['Content-Type'] = 'application/octet-stream;charset=utf-8';
  143. }
  144. if ($credential->getSecurityToken() != null) {
  145. $this->headers['x-acs-security-token'] = $credential->getSecurityToken();
  146. }
  147. if ($credential instanceof BearerTokenCredential) {
  148. $this->headers['x-acs-bearer-token'] = $credential->getBearerToken();
  149. }
  150. }
  151. /**
  152. * @return mixed|string
  153. */
  154. private function replaceOccupiedParameters()
  155. {
  156. $result = $this->uriPattern;
  157. foreach ($this->pathParameters as $pathParameterKey => $apiParameterValue) {
  158. $target = '[' . $pathParameterKey . ']';
  159. $result = str_replace($target, $apiParameterValue, $result);
  160. }
  161. return $result;
  162. }
  163. /**
  164. * @return string
  165. */
  166. private function buildCanonicalHeaders()
  167. {
  168. $sortMap = array();
  169. foreach ($this->headers as $headerKey => $headerValue) {
  170. $key = strtolower($headerKey);
  171. if (strpos($key, 'x-acs-') === 0) {
  172. $sortMap[$key] = $headerValue;
  173. }
  174. }
  175. ksort($sortMap);
  176. $headerString = '';
  177. foreach ($sortMap as $sortMapKey => $sortMapValue) {
  178. $headerString = $headerString . $sortMapKey . ':' . $sortMapValue . self::$headerSeparator;
  179. }
  180. return $headerString;
  181. }
  182. /**
  183. * @param $uri
  184. *
  185. * @return array
  186. */
  187. private function splitSubResource($uri)
  188. {
  189. $queIndex = strpos($uri, '?');
  190. $uriParts = array();
  191. if (null != $queIndex) {
  192. $uriParts[] = substr($uri, 0, $queIndex);
  193. $uriParts[] = substr($uri, $queIndex + 1);
  194. } else {
  195. $uriParts[] = $uri;
  196. }
  197. return $uriParts;
  198. }
  199. /**
  200. * @param $uri
  201. *
  202. * @return bool|mixed|string
  203. */
  204. private function buildQueryString($uri)
  205. {
  206. $uriParts = $this->splitSubResource($uri);
  207. $sortMap = $this->queryParameters;
  208. if (isset($uriParts[1])) {
  209. $sortMap[$uriParts[1]] = null;
  210. }
  211. $queryString = $uriParts[0];
  212. if (count($uriParts)) {
  213. $queryString .= '?';
  214. }
  215. ksort($sortMap);
  216. foreach ($sortMap as $sortMapKey => $sortMapValue) {
  217. $queryString .= $sortMapKey;
  218. if (isset($sortMapValue)) {
  219. $queryString = $queryString . '=' . $sortMapValue;
  220. }
  221. $queryString .= self::$querySeparator;
  222. }
  223. if (0 < count($sortMap)) {
  224. $queryString = substr($queryString, 0, -1);
  225. }
  226. return $queryString;
  227. }
  228. /**
  229. * @param $acceptFormat
  230. *
  231. * @return string
  232. */
  233. private function formatToAccept($acceptFormat)
  234. {
  235. if ($acceptFormat === 'JSON') {
  236. return 'application/json';
  237. }
  238. if ($acceptFormat === 'XML') {
  239. return 'application/xml';
  240. }
  241. return 'application/octet-stream';
  242. }
  243. /**
  244. * @return array
  245. */
  246. public function getPathParameters()
  247. {
  248. return $this->pathParameters;
  249. }
  250. /**
  251. * @param $name
  252. * @param $value
  253. */
  254. public function putPathParameter($name, $value)
  255. {
  256. $this->pathParameters[$name] = $value;
  257. }
  258. /**
  259. * @return array
  260. */
  261. public function getDomainParameter()
  262. {
  263. return $this->domainParameters;
  264. }
  265. /**
  266. * @param $name
  267. * @param $value
  268. */
  269. public function putDomainParameters($name, $value)
  270. {
  271. $this->domainParameters[$name] = $value;
  272. }
  273. /**
  274. * @return string
  275. */
  276. public function getUriPattern()
  277. {
  278. return $this->uriPattern;
  279. }
  280. /**
  281. * @param $uriPattern
  282. *
  283. * @return mixed
  284. */
  285. public function setUriPattern($uriPattern)
  286. {
  287. return $this->uriPattern = $uriPattern;
  288. }
  289. }