RoaAcsRequest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. }
  83. private function replaceOccupiedParameters()
  84. {
  85. $result = $this->uriPattern;
  86. foreach ($this->pathParameters as $pathParameterKey => $apiParameterValue)
  87. {
  88. $target = "[".$pathParameterKey."]";
  89. $result = str_replace($target,$apiParameterValue,$result);
  90. }
  91. return $result;
  92. }
  93. private function buildCanonicalHeaders()
  94. {
  95. $sortMap = array();
  96. foreach ($this->headers as $headerKey => $headerValue)
  97. {
  98. $key = strtolower($headerKey);
  99. if(strpos($key, "x-acs-") === 0)
  100. {
  101. $sortMap[$key] = $headerValue;
  102. }
  103. }
  104. ksort($sortMap);
  105. $headerString = "";
  106. foreach ($sortMap as $sortMapKey => $sortMapValue)
  107. {
  108. $headerString = $headerString.$sortMapKey.":".$sortMapValue.self::$headerSeparator;
  109. }
  110. return $headerString;
  111. }
  112. private function splitSubResource($uri)
  113. {
  114. $queIndex = strpos($uri, "?");
  115. $uriParts = array();
  116. if(null != $queIndex)
  117. {
  118. array_push($uriParts, substr($uri,0,$queIndex));
  119. array_push($uriParts, substr($uri,$queIndex+1));
  120. }
  121. else
  122. {
  123. array_push($uriParts,$uri);
  124. }
  125. return $uriParts;
  126. }
  127. private function buildQueryString($uri)
  128. {
  129. $uriParts = $this->splitSubResource($uri);
  130. $sortMap = $this->queryParameters;
  131. if(isset($uriParts[1]))
  132. {
  133. $sortMap[$uriParts[1]] = null;
  134. }
  135. $queryString = $uriParts[0];
  136. if(count($uriParts))
  137. {
  138. $queryString = $queryString."?";
  139. }
  140. ksort($sortMap);
  141. foreach ($sortMap as $sortMapKey => $sortMapValue)
  142. {
  143. $queryString = $queryString.$sortMapKey;
  144. if(isset($sortMapValue))
  145. {
  146. $queryString = $queryString."=".$sortMapValue;
  147. }
  148. $queryString = $queryString.$querySeprator;
  149. }
  150. if(null==count($sortMap))
  151. {
  152. $queryString = substr($queryString, 0, strlen($queryString)-1);
  153. }
  154. return $queryString;
  155. }
  156. private function formatToAccept($acceptFormat)
  157. {
  158. if($acceptFormat == "JSON")
  159. {
  160. return "application/json";
  161. }
  162. elseif ($acceptFormat == "XML") {
  163. return "application/xml";
  164. }
  165. return "application/octet-stream";
  166. }
  167. public function getPathParameters()
  168. {
  169. return $this->pathParameters;
  170. }
  171. public function putPathParameter($name, $value)
  172. {
  173. $this->pathParameters[$name] = $value;
  174. }
  175. public function getDomainParameter()
  176. {
  177. return $this->domainParameters;
  178. }
  179. public function putDomainParameters($name, $value)
  180. {
  181. $this->domainParameters[$name] = $value;
  182. }
  183. public function getUriPattern()
  184. {
  185. return $this->uriPattern;
  186. }
  187. public function setUriPattern($uriPattern)
  188. {
  189. return $this->uriPattern = $uriPattern;
  190. }
  191. public function setVersion($version)
  192. {
  193. $this->version = $version;
  194. $this->headers["x-acs-version"] = $version;
  195. }
  196. }