RpcServiceClient.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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/RpcServiceClient.h>
  17. #include <algorithm>
  18. #include <iomanip>
  19. #include <sstream>
  20. #include <alibabacloud/core/HmacSha1Signer.h>
  21. #include "Utils.h"
  22. using namespace AlibabaCloud;
  23. RpcServiceClient::RpcServiceClient(const std::string & servicename, const std::shared_ptr<CredentialsProvider> &credentialsProvider,
  24. const ClientConfiguration &configuration,
  25. const std::shared_ptr<Signer> &signer) :
  26. CoreClient(servicename, configuration),
  27. credentialsProvider_(credentialsProvider),
  28. signer_(signer)
  29. {
  30. }
  31. RpcServiceClient::~RpcServiceClient()
  32. {
  33. }
  34. std::string RpcServiceClient::canonicalizedQuery(const std::map<std::string, std::string>& params) const
  35. {
  36. if (params.empty())
  37. return std::string();
  38. std::stringstream ss;
  39. for (const auto &p : params)
  40. {
  41. std::string key = UrlEncode(p.first);
  42. StringReplace(key, "+", "%20");
  43. StringReplace(key, "*", "%2A");
  44. StringReplace(key, "%7E", "~");
  45. std::string value = UrlEncode(p.second);
  46. StringReplace(value, "+", "%20");
  47. StringReplace(value, "*", "%2A");
  48. StringReplace(value, "%7E", "~");
  49. ss << "&" << key << "=" << value;
  50. }
  51. return ss.str().substr(1);
  52. }
  53. RpcServiceClient::JsonOutcome RpcServiceClient::makeRequest(const std::string &endpoint, const RpcServiceRequest &msg, HttpRequest::Method method)const
  54. {
  55. auto outcome = AttemptRequest(endpoint, msg, method);
  56. if (outcome.isSuccess())
  57. return JsonOutcome(std::string(outcome.result().body(),
  58. outcome.result().bodySize()));
  59. else
  60. return JsonOutcome(outcome.error());
  61. }
  62. HttpRequest RpcServiceClient::buildHttpRequest(const std::string & endpoint, const ServiceRequest &msg, HttpRequest::Method method )const
  63. {
  64. return buildHttpRequest(endpoint, dynamic_cast<const RpcServiceRequest& >(msg), method);
  65. }
  66. HttpRequest RpcServiceClient::buildHttpRequest(const std::string & endpoint, const RpcServiceRequest &msg, HttpRequest::Method method) const
  67. {
  68. const Credentials credentials = credentialsProvider_->getCredentials();
  69. Url url;
  70. url.setScheme("https");
  71. url.setHost(endpoint);
  72. url.setPath(msg.resourcePath());
  73. auto params = msg.parameters();
  74. std::map <std::string, std::string> queryParams;
  75. for (const auto &p : params) {
  76. if (!p.second.empty())
  77. queryParams[p.first] = p.second;
  78. }
  79. queryParams["AccessKeyId"] = credentials.accessKeyId();
  80. queryParams["Format"] = "JSON";
  81. queryParams["RegionId"] = configuration().regionId();
  82. queryParams["SecurityToken"] = credentials.sessionToken();
  83. queryParams["SignatureMethod"] = signer_->name();
  84. queryParams["SignatureNonce"] = GenerateUuid();
  85. queryParams["SignatureVersion"] = signer_->version();
  86. std::time_t t = std::time(nullptr);
  87. std::stringstream ss;
  88. #if defined(__GNUG__) && __GNUC__ < 5
  89. char tmbuff[26];
  90. strftime(tmbuff, 26, "%FT%TZ" , std::gmtime(&t));
  91. ss << tmbuff;
  92. #else
  93. ss << std::put_time(std::gmtime(&t), "%FT%TZ");
  94. #endif
  95. queryParams["Timestamp"] = ss.str();
  96. queryParams["Version"] = msg.version();
  97. std::stringstream plaintext;
  98. plaintext << HttpMethodToString(method)
  99. << "&"
  100. << UrlEncode(url.path())
  101. << "&"
  102. << UrlEncode(canonicalizedQuery(queryParams));
  103. queryParams["Signature"] = signer_->generate(plaintext.str(), credentials.accessKeySecret() + "&");
  104. std::stringstream queryString;
  105. for (const auto &p : queryParams)
  106. queryString << "&" << p.first << "=" << UrlEncode(p.second);
  107. url.setQuery(queryString.str().substr(1));
  108. HttpRequest request(url);
  109. request.setMethod(method);
  110. request.setHeader("Host", url.host());
  111. request.setHeader("x-sdk-client", std::string("CPP/").append(ALIBABACLOUD_VERSION_STR));
  112. return request;
  113. }