RpcServiceClient.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright 1999-2019 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/AlibabaCloud.h>
  17. #include <alibabacloud/core/RpcServiceClient.h>
  18. #include <alibabacloud/core/HmacSha1Signer.h>
  19. #include <algorithm>
  20. #include <iomanip>
  21. #include <sstream>
  22. #include <alibabacloud/core/Utils.h>
  23. namespace AlibabaCloud {
  24. RpcServiceClient::RpcServiceClient(const std::string & servicename,
  25. const std::shared_ptr<CredentialsProvider> &credentialsProvider,
  26. const ClientConfiguration &configuration,
  27. const std::shared_ptr<Signer> &signer) :
  28. CoreClient(servicename, configuration),
  29. credentialsProvider_(credentialsProvider),
  30. signer_(signer) {
  31. }
  32. RpcServiceClient::~RpcServiceClient() {
  33. }
  34. RpcServiceClient::JsonOutcome RpcServiceClient::makeRequest(
  35. const std::string &endpoint,
  36. const RpcServiceRequest &request, HttpRequest::Method method) const
  37. {
  38. if (method == HttpRequest::Method::Get)
  39. {
  40. method = request.method();
  41. }
  42. auto outcome = AttemptRequest(endpoint, request, method);
  43. if (outcome.isSuccess())
  44. return JsonOutcome(std::string(outcome.result().body(),
  45. outcome.result().bodySize()));
  46. else
  47. return JsonOutcome(outcome.error());
  48. }
  49. HttpRequest RpcServiceClient::buildHttpRequest(const std::string & endpoint,
  50. const ServiceRequest &msg, HttpRequest::Method method)const {
  51. return buildHttpRequest(endpoint,
  52. dynamic_cast<const RpcServiceRequest& >(msg), method);
  53. }
  54. HttpRequest RpcServiceClient::buildHttpRequest(const std::string & endpoint,
  55. const RpcServiceRequest &msg, HttpRequest::Method method) const {
  56. const Credentials credentials = credentialsProvider_->getCredentials();
  57. Url url;
  58. if (msg.scheme().empty())
  59. {
  60. url.setScheme("https");
  61. }
  62. else
  63. {
  64. url.setScheme(msg.scheme());
  65. }
  66. url.setHost(endpoint);
  67. url.setPath(msg.resourcePath());
  68. std::map<std::string, std::string> signParams;
  69. auto params = msg.parameters();
  70. for (const auto &p : params)
  71. {
  72. if (!p.second.empty())
  73. signParams[p.first] = p.second;
  74. }
  75. signParams["AccessKeyId"] = credentials.accessKeyId();
  76. signParams["Format"] = "JSON";
  77. signParams["RegionId"] = configuration().regionId();
  78. signParams["SecurityToken"] = credentials.sessionToken();
  79. signParams["SignatureMethod"] = signer_->name();
  80. signParams["SignatureNonce"] = GenerateUuid();
  81. signParams["SignatureVersion"] = signer_->version();
  82. std::time_t t = std::time(nullptr);
  83. std::stringstream ss;
  84. #if defined(__GNUG__) && __GNUC__ < 5
  85. char tmbuff[26];
  86. strftime(tmbuff, 26, "%FT%TZ", std::gmtime(&t));
  87. ss << tmbuff;
  88. #else
  89. ss << std::put_time(std::gmtime(&t), "%FT%TZ");
  90. #endif
  91. signParams["Timestamp"] = ss.str();
  92. signParams["Version"] = msg.version();
  93. std::map<std::string, std::string> query;
  94. for (const auto &p : signParams)
  95. {
  96. query[p.first] = p.second;
  97. }
  98. auto body_params = msg.bodyParameters();
  99. for (const auto &p : body_params)
  100. {
  101. signParams[p.first] = p.second;
  102. }
  103. std::stringstream plaintext;
  104. plaintext << HttpMethodToString(method)
  105. << "&"
  106. << UrlEncode(url.path())
  107. << "&"
  108. << UrlEncode(canonicalizedQuery(signParams));
  109. query["Signature"] = signer_->generate(plaintext.str(),
  110. credentials.accessKeySecret() + "&");
  111. std::stringstream queryString;
  112. for (const auto &p : query)
  113. queryString << "&" << p.first << "=" << UrlEncode(p.second);
  114. url.setQuery(queryString.str().substr(1));
  115. HttpRequest request(url);
  116. if (msg.connectTimeout() != kInvalidTimeout)
  117. {
  118. request.setConnectTimeout(msg.connectTimeout());
  119. }
  120. else
  121. {
  122. request.setConnectTimeout(configuration().connectTimeout());
  123. }
  124. for (const auto &h : msg.headers())
  125. {
  126. if (!h.second.empty())
  127. {
  128. request.setHeader(h.first, h.second);
  129. }
  130. }
  131. if (msg.readTimeout() != kInvalidTimeout)
  132. {
  133. request.setReadTimeout(msg.readTimeout());
  134. }
  135. else
  136. {
  137. request.setReadTimeout(configuration().readTimeout());
  138. }
  139. request.setMethod(method);
  140. request.setHeader("Host", url.host());
  141. request.setHeader("x-sdk-client",
  142. std::string("CPP/").append(ALIBABACLOUD_VERSION_STR));
  143. std::stringstream tmp;
  144. for (const auto &p : body_params)
  145. tmp << "&" << p.first << "=" << UrlEncode(p.second);
  146. if(tmp.str().length() > 0){
  147. std::string body = tmp.str().substr(1);
  148. request.setBody(body.c_str(), body.length());
  149. }
  150. return request;
  151. }
  152. } // namespace AlibabaCloud