upgrade sdk core:
supported json param; change the way of include Utils.h;
This commit is contained in:
@@ -14,10 +14,11 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "Utils.h"
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
#include <sstream>
|
||||
#include <stdlib.h>
|
||||
#include <alibabacloud/core/Utils.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <Windows.h>
|
||||
#else
|
||||
@@ -26,15 +27,17 @@
|
||||
#include <uuid/uuid.h>
|
||||
#endif
|
||||
#include <curl/curl.h>
|
||||
#include <json/json.h>
|
||||
|
||||
std::string AlibabaCloud::GenerateUuid() {
|
||||
std::string AlibabaCloud::GenerateUuid()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
char *data;
|
||||
UUID uuidhandle;
|
||||
UuidCreate(&uuidhandle);
|
||||
UuidToString(&uuidhandle, reinterpret_cast<RPC_CSTR*>(&data));
|
||||
UuidToString(&uuidhandle, reinterpret_cast<RPC_CSTR *>(&data));
|
||||
std::string uuid(data);
|
||||
RpcStringFree(reinterpret_cast<RPC_CSTR*>(&data));
|
||||
RpcStringFree(reinterpret_cast<RPC_CSTR *>(&data));
|
||||
return uuid;
|
||||
#else
|
||||
uuid_t uu;
|
||||
@@ -45,7 +48,8 @@ std::string AlibabaCloud::GenerateUuid() {
|
||||
#endif
|
||||
}
|
||||
|
||||
std::string AlibabaCloud::UrlEncode(const std::string & src) {
|
||||
std::string AlibabaCloud::UrlEncode(const std::string &src)
|
||||
{
|
||||
CURL *curl = curl_easy_init();
|
||||
char *output = curl_easy_escape(curl, src.c_str(), src.size());
|
||||
std::string result(output);
|
||||
@@ -54,7 +58,8 @@ std::string AlibabaCloud::UrlEncode(const std::string & src) {
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string AlibabaCloud::UrlDecode(const std::string & src) {
|
||||
std::string AlibabaCloud::UrlDecode(const std::string &src)
|
||||
{
|
||||
CURL *curl = curl_easy_init();
|
||||
int outlength = 0;
|
||||
char *output = curl_easy_unescape(curl, src.c_str(), src.size(), &outlength);
|
||||
@@ -64,7 +69,8 @@ std::string AlibabaCloud::UrlDecode(const std::string & src) {
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string AlibabaCloud::ComputeContentMD5(const char * data, size_t size) {
|
||||
std::string AlibabaCloud::ComputeContentMD5(const char *data, size_t size)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
HCRYPTPROV hProv = 0;
|
||||
HCRYPTHASH hHash = 0;
|
||||
@@ -73,7 +79,7 @@ std::string AlibabaCloud::ComputeContentMD5(const char * data, size_t size) {
|
||||
|
||||
CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
|
||||
CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash);
|
||||
CryptHashData(hHash, (BYTE*)(data), size, 0);
|
||||
CryptHashData(hHash, (BYTE *)(data), size, 0);
|
||||
CryptGetHashParam(hHash, HP_HASHVAL, pbHash, &dwDataLen, 0);
|
||||
|
||||
CryptDestroyHash(hHash);
|
||||
@@ -81,36 +87,41 @@ std::string AlibabaCloud::ComputeContentMD5(const char * data, size_t size) {
|
||||
|
||||
DWORD dlen = 0;
|
||||
CryptBinaryToString(pbHash, dwDataLen,
|
||||
CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, NULL, &dlen);
|
||||
char* dest = new char[dlen];
|
||||
CryptBinaryToString(pbHash,
|
||||
dwDataLen, CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, dest, &dlen);
|
||||
CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, NULL, &dlen);
|
||||
char *dest = new char[dlen];
|
||||
CryptBinaryToString(pbHash, dwDataLen,
|
||||
CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, dest, &dlen);
|
||||
|
||||
std::string ret = std::string(dest, dlen);
|
||||
delete dest;
|
||||
return ret;
|
||||
#else
|
||||
unsigned char md[MD5_DIGEST_LENGTH];
|
||||
MD5(reinterpret_cast<const unsigned char*>(data), size, (unsigned char*)&md);
|
||||
MD5(reinterpret_cast<const unsigned char *>(data), size,
|
||||
(unsigned char *)&md);
|
||||
|
||||
char encodedData[100];
|
||||
EVP_EncodeBlock(reinterpret_cast<unsigned char*>(encodedData),
|
||||
md, MD5_DIGEST_LENGTH);
|
||||
EVP_EncodeBlock(reinterpret_cast<unsigned char *>(encodedData), md,
|
||||
MD5_DIGEST_LENGTH);
|
||||
return encodedData;
|
||||
#endif
|
||||
}
|
||||
|
||||
void AlibabaCloud::StringReplace(std::string & src,
|
||||
const std::string & s1, const std::string & s2) {
|
||||
void AlibabaCloud::StringReplace(std::string &src, const std::string &s1,
|
||||
const std::string &s2)
|
||||
{
|
||||
std::string::size_type pos = 0;
|
||||
while ((pos = src.find(s1, pos)) != std::string::npos) {
|
||||
while ((pos = src.find(s1, pos)) != std::string::npos)
|
||||
{
|
||||
src.replace(pos, s1.length(), s2);
|
||||
pos += s2.length();
|
||||
}
|
||||
}
|
||||
|
||||
std::string AlibabaCloud::HttpMethodToString(HttpRequest::Method method) {
|
||||
switch (method) {
|
||||
std::string AlibabaCloud::HttpMethodToString(HttpRequest::Method method)
|
||||
{
|
||||
switch (method)
|
||||
{
|
||||
case HttpRequest::Method::Head:
|
||||
return "HEAD";
|
||||
break;
|
||||
@@ -142,13 +153,15 @@ std::string AlibabaCloud::HttpMethodToString(HttpRequest::Method method) {
|
||||
}
|
||||
}
|
||||
|
||||
std::string AlibabaCloud::canonicalizedQuery(const std::map<std::string,
|
||||
std::string>& params) {
|
||||
std::string AlibabaCloud::canonicalizedQuery(
|
||||
const std::map<std::string, std::string> ¶ms)
|
||||
{
|
||||
if (params.empty())
|
||||
return std::string();
|
||||
|
||||
std::stringstream ss;
|
||||
for (const auto &p : params) {
|
||||
for (const auto &p : params)
|
||||
{
|
||||
std::string key = UrlEncode(p.first);
|
||||
StringReplace(key, "+", "%20");
|
||||
StringReplace(key, "*", "%2A");
|
||||
@@ -163,9 +176,11 @@ std::string AlibabaCloud::canonicalizedQuery(const std::map<std::string,
|
||||
}
|
||||
|
||||
std::string AlibabaCloud::canonicalizedHeaders(
|
||||
const HttpMessage::HeaderCollection &headers) {
|
||||
std::map <std::string, std::string> materials;
|
||||
for (const auto &p : headers) {
|
||||
const HttpMessage::HeaderCollection &headers)
|
||||
{
|
||||
std::map<std::string, std::string> materials;
|
||||
for (const auto &p : headers)
|
||||
{
|
||||
std::string key = p.first;
|
||||
std::transform(key.begin(), key.end(), key.begin(), ::tolower);
|
||||
if (key.find("x-acs-") != 0)
|
||||
@@ -188,25 +203,111 @@ std::string AlibabaCloud::canonicalizedHeaders(
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
std::string AlibabaCloud::GetEnv(const std::string env) {
|
||||
Json::Value AlibabaCloud::ReadJson(const std::string str)
|
||||
{
|
||||
Json::CharReaderBuilder builder;
|
||||
Json::CharReader *reader = builder.newCharReader();
|
||||
Json::Value *val;
|
||||
Json::Value value;
|
||||
JSONCPP_STRING *errs;
|
||||
reader->parse(str.data(), str.data() + str.size(), val, errs);
|
||||
if (errs == NULL)
|
||||
{
|
||||
value = *val;
|
||||
return value;
|
||||
}
|
||||
throw errs;
|
||||
}
|
||||
|
||||
std::string AlibabaCloud::GetEnv(const std::string env)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
char* buf = nullptr;
|
||||
char *buf = nullptr;
|
||||
size_t sz = 0;
|
||||
if (_dupenv_s(&buf, &sz, env.c_str()) == 0 && buf != nullptr) {
|
||||
if (_dupenv_s(&buf, &sz, env.c_str()) == 0 && buf != nullptr)
|
||||
{
|
||||
std::string var(buf);
|
||||
free(buf);
|
||||
return var;
|
||||
} else {
|
||||
if (buf) {
|
||||
}
|
||||
else
|
||||
{
|
||||
if (buf)
|
||||
{
|
||||
free(buf);
|
||||
}
|
||||
return std::string();
|
||||
}
|
||||
#else
|
||||
char* var = getenv(env.c_str());
|
||||
if (var) {
|
||||
char *var = getenv(env.c_str());
|
||||
if (var)
|
||||
{
|
||||
return std::string(var);
|
||||
}
|
||||
return std::string();
|
||||
#endif
|
||||
}
|
||||
|
||||
std::string AlibabaCloud::MapToJson(const std::map<std::string, std::string> &maps)
|
||||
{
|
||||
Json::Value jsonObject;
|
||||
for (std::map<std::string, std::string>::const_iterator iter = maps.begin(); iter != maps.end(); ++iter)
|
||||
{
|
||||
jsonObject[iter->first] = iter->second;
|
||||
}
|
||||
return jsonObject.toStyledString();
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> AlibabaCloud::JsonToMap(const std::string &json)
|
||||
{
|
||||
Json::Reader reader;
|
||||
Json::Value value;
|
||||
std::map<std::string, std::string> maps;
|
||||
|
||||
if (json.length() > 0)
|
||||
{
|
||||
if (reader.parse(json, value))
|
||||
{
|
||||
Json::Value::Members members = value.getMemberNames();
|
||||
for (Json::Value::Members::iterator it = members.begin(); it != members.end(); it++)
|
||||
{
|
||||
Json::ValueType vt = value[*it].type();
|
||||
switch (vt)
|
||||
{
|
||||
case Json::stringValue:
|
||||
{
|
||||
maps.insert(std::pair<std::string, std::string>(*it, value[*it].asString()));
|
||||
break;
|
||||
}
|
||||
case Json::intValue:
|
||||
{
|
||||
int inttmp = value[*it].asInt();
|
||||
maps.insert(std::pair<std::string, std::string>(*it, std::to_string(inttmp)));
|
||||
break;
|
||||
}
|
||||
case Json::arrayValue:
|
||||
{
|
||||
std::string strid;
|
||||
for (unsigned int i = 0; i < value[*it].size(); i++)
|
||||
{
|
||||
strid += value[*it][i].asString();
|
||||
strid += ",";
|
||||
}
|
||||
if (!strid.empty())
|
||||
{
|
||||
strid = strid.substr(0, strid.size() - 1);
|
||||
}
|
||||
maps.insert(std::pair<std::string, std::string>(*it, strid));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return maps;
|
||||
}
|
||||
Reference in New Issue
Block a user