Utils.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 "Utils.h"
  17. #include <sstream>
  18. #include <algorithm>
  19. #include <stdlib.h>
  20. #ifdef _WIN32
  21. #include <Windows.h>
  22. #else
  23. #include <openssl/hmac.h>
  24. #include <openssl/md5.h>
  25. #include <uuid/uuid.h>
  26. #endif
  27. #include <curl/curl.h>
  28. std::string AlibabaCloud::GenerateUuid() {
  29. #ifdef _WIN32
  30. char *data;
  31. UUID uuidhandle;
  32. UuidCreate(&uuidhandle);
  33. UuidToString(&uuidhandle, reinterpret_cast<RPC_CSTR*>(&data));
  34. std::string uuid(data);
  35. RpcStringFree(reinterpret_cast<RPC_CSTR*>(&data));
  36. return uuid;
  37. #else
  38. uuid_t uu;
  39. uuid_generate(uu);
  40. char buf[36];
  41. uuid_unparse(uu, buf);
  42. return buf;
  43. #endif
  44. }
  45. std::string AlibabaCloud::UrlEncode(const std::string & src) {
  46. CURL *curl = curl_easy_init();
  47. char *output = curl_easy_escape(curl, src.c_str(), src.size());
  48. std::string result(output);
  49. curl_free(output);
  50. curl_easy_cleanup(curl);
  51. return result;
  52. }
  53. std::string AlibabaCloud::UrlDecode(const std::string & src) {
  54. CURL *curl = curl_easy_init();
  55. int outlength = 0;
  56. char *output = curl_easy_unescape(curl, src.c_str(), src.size(), &outlength);
  57. std::string result(output, outlength);
  58. curl_free(output);
  59. curl_easy_cleanup(curl);
  60. return result;
  61. }
  62. std::string AlibabaCloud::ComputeContentMD5(const char * data, size_t size) {
  63. #ifdef _WIN32
  64. HCRYPTPROV hProv = 0;
  65. HCRYPTHASH hHash = 0;
  66. BYTE pbHash[16];
  67. DWORD dwDataLen = 16;
  68. CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
  69. CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash);
  70. CryptHashData(hHash, (BYTE*)(data), size, 0);
  71. CryptGetHashParam(hHash, HP_HASHVAL, pbHash, &dwDataLen, 0);
  72. CryptDestroyHash(hHash);
  73. CryptReleaseContext(hProv, 0);
  74. DWORD dlen = 0;
  75. CryptBinaryToString(pbHash, dwDataLen,
  76. CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, NULL, &dlen);
  77. char* dest = new char[dlen];
  78. CryptBinaryToString(pbHash,
  79. dwDataLen, CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, dest, &dlen);
  80. std::string ret = std::string(dest, dlen);
  81. delete dest;
  82. return ret;
  83. #else
  84. unsigned char md[MD5_DIGEST_LENGTH];
  85. MD5(reinterpret_cast<const unsigned char*>(data), size, (unsigned char*)&md);
  86. char encodedData[100];
  87. EVP_EncodeBlock(reinterpret_cast<unsigned char*>(encodedData),
  88. md, MD5_DIGEST_LENGTH);
  89. return encodedData;
  90. #endif
  91. }
  92. void AlibabaCloud::StringReplace(std::string & src,
  93. const std::string & s1, const std::string & s2) {
  94. std::string::size_type pos = 0;
  95. while ((pos = src.find(s1, pos)) != std::string::npos) {
  96. src.replace(pos, s1.length(), s2);
  97. pos += s2.length();
  98. }
  99. }
  100. std::string AlibabaCloud::HttpMethodToString(HttpRequest::Method method) {
  101. switch (method) {
  102. case HttpRequest::Method::Head:
  103. return "HEAD";
  104. break;
  105. case HttpRequest::Method::Post:
  106. return "POST";
  107. break;
  108. case HttpRequest::Method::Put:
  109. return "PUT";
  110. break;
  111. case HttpRequest::Method::Delete:
  112. return "DELETE";
  113. break;
  114. case HttpRequest::Method::Connect:
  115. return "CONNECT";
  116. break;
  117. case HttpRequest::Method::Options:
  118. return "OPTIONS";
  119. break;
  120. case HttpRequest::Method::Patch:
  121. return "PATCH";
  122. break;
  123. case HttpRequest::Method::Trace:
  124. return "TRACE";
  125. break;
  126. case HttpRequest::Method::Get:
  127. default:
  128. return "GET";
  129. break;
  130. }
  131. }
  132. std::string AlibabaCloud::canonicalizedQuery(const std::map<std::string,
  133. std::string>& params) {
  134. if (params.empty())
  135. return std::string();
  136. std::stringstream ss;
  137. for (const auto &p : params) {
  138. std::string key = UrlEncode(p.first);
  139. StringReplace(key, "+", "%20");
  140. StringReplace(key, "*", "%2A");
  141. StringReplace(key, "%7E", "~");
  142. std::string value = UrlEncode(p.second);
  143. StringReplace(value, "+", "%20");
  144. StringReplace(value, "*", "%2A");
  145. StringReplace(value, "%7E", "~");
  146. ss << "&" << key << "=" << value;
  147. }
  148. return ss.str().substr(1);
  149. }
  150. std::string AlibabaCloud::canonicalizedHeaders(
  151. const HttpMessage::HeaderCollection &headers) {
  152. std::map <std::string, std::string> materials;
  153. for (const auto &p : headers) {
  154. std::string key = p.first;
  155. std::transform(key.begin(), key.end(), key.begin(), ::tolower);
  156. if (key.find("x-acs-") != 0)
  157. continue;
  158. std::string value = p.second;
  159. StringReplace(value, "\t", " ");
  160. StringReplace(value, "\n", " ");
  161. StringReplace(value, "\r", " ");
  162. StringReplace(value, "\f", " ");
  163. materials[key] = value;
  164. }
  165. if (materials.empty())
  166. return std::string();
  167. std::stringstream ss;
  168. for (const auto &p : materials)
  169. ss << p.first << ":" << p.second << "\n";
  170. return ss.str();
  171. }
  172. std::string AlibabaCloud::GetEnv(const std::string env) {
  173. #ifdef _WIN32
  174. char* buf = nullptr;
  175. size_t sz = 0;
  176. if (_dupenv_s(&buf, &sz, env.c_str()) == 0 && buf != nullptr) {
  177. std::string var(buf);
  178. free(buf);
  179. return var;
  180. } else {
  181. if (buf) {
  182. free(buf);
  183. }
  184. return std::string();
  185. }
  186. #else
  187. char* var = getenv(env.c_str());
  188. if (var) {
  189. return std::string(var);
  190. }
  191. return std::string();
  192. #endif
  193. }