RoaServiceClient.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright 2009-2017 Alibaba Cloud All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <alibabacloud/core/RoaServiceClient.h>
  17. #include <algorithm>
  18. #include <iomanip>
  19. #include <sstream>
  20. #include <alibabacloud/core/HmacSha1Signer.h>
  21. //#include <alibabacloud/core/RoaErrorMarshaller.h>
  22. #include "Utils.h"
  23. using namespace AlibabaCloud;
  24. RoaServiceClient::RoaServiceClient(const std::string & servicename, const std::shared_ptr<CredentialsProvider> &credentialsProvider,
  25. const ClientConfiguration &configuration,
  26. const std::shared_ptr<Signer> &signer) :
  27. CoreClient(servicename, configuration),
  28. credentialsProvider_(credentialsProvider),
  29. signer_(signer)
  30. {
  31. }
  32. RoaServiceClient::~RoaServiceClient()
  33. {
  34. }
  35. std::string RoaServiceClient::canonicalizedResource(const std::string &path, std::map <std::string, std::string> &params)const
  36. {
  37. if (params.empty())
  38. return path;
  39. std::stringstream ss;
  40. for (const auto &p : params)
  41. {
  42. if (p.second.empty())
  43. ss << "&" << p.first;
  44. else
  45. ss << "&" << p.first << "=" << p.second;
  46. }
  47. std::string str = path;
  48. str.append("?").append(ss.str().substr(1));
  49. return str;
  50. }
  51. std::string RoaServiceClient::canonicalizedHeaders(const HttpMessage::HeaderCollection &headers)const
  52. {
  53. std::map <std::string, std::string> materials;
  54. for (const auto &p : headers)
  55. {
  56. std::string key = p.first;
  57. std::transform(key.begin(), key.end(), key.begin(), ::tolower);
  58. if (key.find("x-acs-") != 0)
  59. continue;
  60. std::string value = p.second;
  61. StringReplace(value, "\t", " ");
  62. StringReplace(value, "\n", " ");
  63. StringReplace(value, "\r", " ");
  64. StringReplace(value, "\f", " ");
  65. materials[key] = value;
  66. }
  67. if (materials.empty())
  68. return std::string();
  69. std::stringstream ss;
  70. for (const auto &p : materials)
  71. ss << p.first << ":" << p.second << "\n";
  72. return ss.str();
  73. }
  74. RoaServiceClient::JsonOutcome RoaServiceClient::makeRequest(const std::string &endpoint, const RoaServiceRequest &msg, HttpRequest::Method method)const
  75. {
  76. auto outcome = AttemptRequest(endpoint, msg, method);
  77. if (outcome.isSuccess())
  78. return JsonOutcome(std::string(outcome.result().body(),
  79. outcome.result().bodySize()));
  80. else
  81. return JsonOutcome(outcome.error());
  82. }
  83. HttpRequest RoaServiceClient::buildHttpRequest(const std::string & endpoint, const ServiceRequest &msg, HttpRequest::Method method)const
  84. {
  85. return buildHttpRequest(endpoint, dynamic_cast<const RoaServiceRequest& >(msg), method);
  86. }
  87. HttpRequest RoaServiceClient::buildHttpRequest(const std::string & endpoint, const RoaServiceRequest &msg, HttpRequest::Method method) const
  88. {
  89. const Credentials credentials = credentialsProvider_->getCredentials();
  90. Url url;
  91. url.setScheme("https");
  92. url.setHost(endpoint);
  93. url.setPath(msg.resourcePath());
  94. auto params = msg.parameters();
  95. std::map <std::string, std::string> queryParams;
  96. for (const auto &p : params){
  97. if (!p.second.empty())
  98. queryParams[p.first] = p.second;
  99. }
  100. if (!queryParams.empty()) {
  101. std::stringstream queryString;
  102. for (const auto &p : queryParams)
  103. {
  104. if (p.second.empty())
  105. queryString << "&" << p.first;
  106. else
  107. queryString << "&" << p.first << "=" << p.second;
  108. }
  109. url.setQuery(queryString.str().substr(1));
  110. }
  111. HttpRequest request(url);
  112. request.setMethod(method);
  113. request.setHeader("Accept", "application/json");
  114. if (msg.hasContent()) {
  115. std::stringstream ss;
  116. ss << msg.contentSize();
  117. request.setHeader("Content-Length", ss.str());
  118. request.setHeader("Content-Type", "application/octet-stream");
  119. request.setHeader("Content-MD5", ComputeContentMD5(msg.content(),msg.contentSize()));
  120. }
  121. std::time_t t = std::time(nullptr);
  122. std::stringstream date;
  123. #if defined(__GNUG__) && __GNUC__ < 5
  124. char tmbuff[26];
  125. strftime(tmbuff, 26, "%a, %d %b %Y %T", std::gmtime(&t));
  126. date << tmbuff << " GMT";
  127. #else
  128. date << std::put_time(std::gmtime(&t), "%a, %d %b %Y %T GMT");
  129. #endif
  130. request.setHeader("Date", date.str());
  131. request.setHeader("Host", url.host());
  132. request.setHeader("x-sdk-client", std::string("CPP/").append(ALIBABACLOUD_VERSION_STR));
  133. request.setHeader("x-acs-region-id", configuration().regionId());
  134. if(!credentials.sessionToken().empty())
  135. request.setHeader("x-acs-security-token", credentials.sessionToken());
  136. request.setHeader("x-acs-signature-method", signer_->name());
  137. request.setHeader("x-acs-signature-nonce", GenerateUuid());
  138. request.setHeader("x-acs-signature-version", signer_->version());
  139. request.setHeader("x-acs-version", msg.version());
  140. std::stringstream plaintext;
  141. plaintext << HttpMethodToString(method) << "\n"
  142. << request.header("Accept") << "\n"
  143. << request.header("Content-MD5") << "\n"
  144. << request.header("Content-Type") << "\n"
  145. << request.header("Date") << "\n"
  146. << canonicalizedHeaders(request.headers());
  147. if (!url.hasQuery())
  148. plaintext << url.path();
  149. else
  150. plaintext << url.path() << "?" << url.query();
  151. std::stringstream sign;
  152. sign << "acs "
  153. << credentials.accessKeyId()
  154. << ":"
  155. << signer_->generate(plaintext.str(), credentials.accessKeySecret());
  156. request.setHeader("Authorization", sign.str());
  157. return request;
  158. }