RoaAcsRequest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. protected $uriPattern;
  23. private $pathParameters = array();
  24. private $domainParameters = array();
  25. private $dateTimeFormat ="D, d M Y H:i:s \G\M\T";
  26. private static $headerSeparator = "\n";
  27. private static $querySeprator = "&";
  28. function __construct($product, $version, $actionName)
  29. {
  30. parent::__construct($product, $version, $actionName);
  31. $this->setVersion($version);
  32. $this->initialize();
  33. }
  34. private function initialize()
  35. {
  36. $this->setMethod("RAW");
  37. }
  38. public function composeUrl($iSigner, $credential, $domain)
  39. {
  40. $this->prepareHeader($iSigner);
  41. $signString = $this->getMethod().self::$headerSeparator;
  42. if(isset($this->headers["Accept"]))
  43. {
  44. $signString = $signString.$this->headers["Accept"];
  45. }
  46. $signString = $signString.self::$headerSeparator;
  47. if(isset($this->headers["Content-MD5"]))
  48. {
  49. $signString = $signString.$this->headers["Content-MD5"];
  50. }
  51. $signString = $signString.self::$headerSeparator;
  52. if(isset($this->headers["Content-Type"]))
  53. {
  54. $signString = $signString.$this->headers["Content-Type"];
  55. }
  56. $signString = $signString.self::$headerSeparator;
  57. if(isset($this->headers["Date"]))
  58. {
  59. $signString = $signString.$this->headers["Date"];
  60. }
  61. $signString = $signString.self::$headerSeparator;
  62. $uri = $this->replaceOccupiedParameters();
  63. $signString = $signString.$this->buildCanonicalHeaders();
  64. $queryString = $this->buildQueryString($uri);
  65. $signString .= $queryString;
  66. $this->headers["Authorization"] = "acs ".$credential->getAccessKeyId().":"
  67. .$iSigner->signString($signString, $credential->getAccessSecret());
  68. $requestUrl = $this->getProtocol()."://".$domain.$queryString;
  69. return $requestUrl;
  70. }
  71. private function prepareHeader($iSigner)
  72. {
  73. date_default_timezone_set("GMT");
  74. $this->headers["Date"] = date($this->dateTimeFormat);
  75. if(null == $this->acceptFormat)
  76. {
  77. $this->acceptFormat = "RAW";
  78. }
  79. $this->headers["Accept"] = $this->formatToAccept($this->getAcceptFormat());
  80. $this->headers["x-acs-signature-method"] = $iSigner->getSignatureMethod();
  81. $this->headers["x-acs-signature-version"] = $iSigner->getSignatureVersion();
  82. $this->headers["x-acs-region-id"] = $this->regionId;
  83. $content = $this->getDomainParameter();
  84. if ($content != null) {
  85. $this->headers["Content-MD5"] = base64_encode(md5(json_encode($content),true));
  86. }
  87. $this->headers["Content-Type"] = "application/octet-stream;charset=utf-8";
  88. }
  89. private function replaceOccupiedParameters()
  90. {
  91. $result = $this->uriPattern;
  92. foreach ($this->pathParameters as $pathParameterKey => $apiParameterValue)
  93. {
  94. $target = "[".$pathParameterKey."]";
  95. $result = str_replace($target,$apiParameterValue,$result);
  96. }
  97. return $result;
  98. }
  99. private function buildCanonicalHeaders()
  100. {
  101. $sortMap = array();
  102. foreach ($this->headers as $headerKey => $headerValue)
  103. {
  104. $key = strtolower($headerKey);
  105. if(strpos($key, "x-acs-") === 0)
  106. {
  107. $sortMap[$key] = $headerValue;
  108. }
  109. }
  110. ksort($sortMap);
  111. $headerString = "";
  112. foreach ($sortMap as $sortMapKey => $sortMapValue)
  113. {
  114. $headerString = $headerString.$sortMapKey.":".$sortMapValue.self::$headerSeparator;
  115. }
  116. return $headerString;
  117. }
  118. private function splitSubResource($uri)
  119. {
  120. $queIndex = strpos($uri, "?");
  121. $uriParts = array();
  122. if(null != $queIndex)
  123. {
  124. array_push($uriParts, substr($uri,0,$queIndex));
  125. array_push($uriParts, substr($uri,$queIndex+1));
  126. }
  127. else
  128. {
  129. array_push($uriParts,$uri);
  130. }
  131. return $uriParts;
  132. }
  133. private function buildQueryString($uri)
  134. {
  135. $uriParts = $this->splitSubResource($uri);
  136. $sortMap = $this->queryParameters;
  137. if(isset($uriParts[1]))
  138. {
  139. $sortMap[$uriParts[1]] = null;
  140. }
  141. $queryString = $uriParts[0];
  142. if(count($uriParts))
  143. {
  144. $queryString = $queryString."?";
  145. }
  146. ksort($sortMap);
  147. foreach ($sortMap as $sortMapKey => $sortMapValue)
  148. {
  149. $queryString = $queryString.$sortMapKey;
  150. if(isset($sortMapValue))
  151. {
  152. $queryString = $queryString."=".$sortMapValue;
  153. }
  154. $queryString = $queryString.$querySeprator;
  155. }
  156. if(null==count($sortMap))
  157. {
  158. $queryString = substr($queryString, 0, strlen($queryString)-1);
  159. }
  160. return $queryString;
  161. }
  162. private function formatToAccept($acceptFormat)
  163. {
  164. if($acceptFormat == "JSON")
  165. {
  166. return "application/json";
  167. }
  168. elseif ($acceptFormat == "XML") {
  169. return "application/xml";
  170. }
  171. return "application/octet-stream";
  172. }
  173. public function getPathParameters()
  174. {
  175. return $this->pathParameters;
  176. }
  177. public function putPathParameter($name, $value)
  178. {
  179. $this->pathParameters[$name] = $value;
  180. }
  181. public function getDomainParameter()
  182. {
  183. return $this->domainParameters;
  184. }
  185. public function putDomainParameters($name, $value)
  186. {
  187. $this->domainParameters[$name] = $value;
  188. }
  189. public function getUriPattern()
  190. {
  191. return $this->uriPattern;
  192. }
  193. public function setUriPattern($uriPattern)
  194. {
  195. return $this->uriPattern = $uriPattern;
  196. }
  197. public function setVersion($version)
  198. {
  199. $this->version = $version;
  200. $this->headers["x-acs-version"] = $version;
  201. }
  202. }