-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathAbstractClient.cpp
More file actions
216 lines (187 loc) · 7.42 KB
/
AbstractClient.cpp
File metadata and controls
216 lines (187 loc) · 7.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* Copyright (c) 2017-2019 THL A29 Limited, a Tencent company. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <tencentcloud/core/AbstractClient.h>
#include <tencentcloud/core/utils/Utils.h>
#include <tencentcloud/core/Sign.h>
#include <tencentcloud/core/Config.h>
using namespace TencentCloud;
using std::string;
namespace
{
const string SDK_VERSION_PREFIX = "SDK_CPP_";
}
AbstractClient::AbstractClient(const string &endpoint, const string &version, const Credential &credential, const string ®ion) :
AbstractClient(endpoint, version, credential, region, ClientProfile())
{
}
AbstractClient::AbstractClient(const string &endpoint, const string &version, const Credential &credential, const string ®ion, const ClientProfile &profile) :
m_credential(credential),
m_clientProfile(profile),
m_endpoint(endpoint),
m_region(region),
m_sdkVersion(SDK_VERSION_PREFIX + TENCENTCLOUD_VERSION_STR),
m_apiVersion(version),
m_httpClient(new HttpClient()),
m_service("")
{
}
AbstractClient::~AbstractClient()
{
delete m_httpClient;
}
void AbstractClient::SetNetworkProxy(const NetworkProxy &proxy)
{
m_httpClient->SetProxy(proxy);
}
void AbstractClient::SetRegion(const string ®ion)
{
m_region = region;
}
string AbstractClient::GetRegion() const
{
return m_region;
}
void AbstractClient::SetClientProfile(const ClientProfile &profile)
{
m_clientProfile = profile;
}
ClientProfile AbstractClient::GetClientProfile() const
{
return m_clientProfile;
}
void AbstractClient::SetCredential(const Credential &credential)
{
m_credential = credential;
}
Credential AbstractClient::GetCredential() const
{
return m_credential;
}
void AbstractClient::SetHeader(const std::map<std::string, std::string> &headers)
{
m_headers = headers;
}
HttpClient::HttpResponseOutcome AbstractClient::MakeRequest(const AbstractModel& request, const std::string &actionName)
{
const string body = request.ToJsonString();
std::map<std::string, std::string> headers;
headers.insert(std::make_pair("Content-Type", "application/json"));
return DoRequest(actionName, body, headers);
}
HttpClient::HttpResponseOutcome AbstractClient::MakeRequestJson(const std::string &actionName, const std::string ¶ms)
{
std::map<std::string, std::string> headers;
headers.insert(std::make_pair("Content-Type", "application/json"));
return DoRequest(actionName, params, headers);
}
HttpClient::HttpResponseOutcome AbstractClient::MakeRequestOctetStream(const std::string &actionName, std::map<std::string, std::string> &headers, const std::string &body)
{
headers.insert(std::make_pair("Content-Type", "application/octet-stream"));
return DoRequest(actionName, body, headers);
}
typedef std::map<std::string, std::string> OctetStreamHeadersMap;
HttpClient::HttpResponseOutcome AbstractClient::DoRequest(const std::string &actionName, const std::string &body, std::map<std::string, std::string> &headers)
{
HttpProfile httpProfile = m_clientProfile.GetHttpProfile();
string endpoint = httpProfile.GetEndpoint();
if (endpoint == "")
endpoint = m_endpoint;
string::size_type pos = endpoint.find_first_of(".");
if (pos != string::npos)
m_service = endpoint.substr(0, pos);
else
{
m_service = "unknown";
return HttpClient::HttpResponseOutcome(Core::Error("ClientError", "endpoint `"+ endpoint + "` is not valid"));
}
Url url;
url.SetHost(endpoint);
HttpProfile::Scheme scheme = httpProfile.GetProtocol();
if (scheme == HttpProfile::Scheme::HTTP)
url.SetScheme("http");
HttpRequest httpRequest(url);
httpRequest.SetMethod(HttpRequest::Method::POST);
httpRequest.SetBody(body);
httpRequest.AddHeader("Host", url.GetHost());
httpRequest.AddHeader("X-TC-Action", actionName);
httpRequest.AddHeader("X-TC-Version", m_apiVersion);
if (m_region != "")
httpRequest.AddHeader("X-TC-Region", m_region);
string token = m_credential.GetToken();
if (token != "")
httpRequest.AddHeader("X-TC-Token", token);
httpRequest.AddHeader("X-TC-RequestClient", m_sdkVersion);
if (httpProfile.IsKeepAlive())
httpRequest.AddHeader("Connection", "Keep-Alive");
else
httpRequest.AddHeader("Connection", "Close");
if (headers.size() > 0)
{
for(std::map<std::string, std::string>::iterator iter = headers.begin(); iter != headers.end(); iter++)
{
httpRequest.AddHeader(iter->first, iter->second);
}
}
if (m_headers.size() > 0)
{
for(std::map<std::string, std::string>::iterator iter = m_headers.begin(); iter != m_headers.end(); iter++)
{
httpRequest.AddHeader(iter->first, iter->second);
}
}
GenerateSignature(httpRequest);
m_httpClient->SetReqTimeout(httpProfile.GetReqTimeout()*1000);
m_httpClient->SetConnectTimeout(httpProfile.GetConnectTimeout()*1000);
m_httpClient->SetCaInfo(httpProfile.GetCaInfo());
m_httpClient->SetCaPath(httpProfile.GetCaPath());
m_httpClient->SetTargetIp(httpProfile.GetTargetIp());
return m_httpClient->SendRequest(httpRequest);
}
void AbstractClient::GenerateSignature(HttpRequest &request)
{
int64_t currentTime;
string credDate;
Utils::GetCurrentTimeAndUtcDate(currentTime, credDate);
request.AddHeader("X-TC-Timestamp", Utils::int2str(currentTime));
string credentialScope = credDate + "/" + m_service + "/tc3_request";
string serviceName = m_service;
string canonicalUri = request.GetUrl().GetPath();
string canonicalQueryString = "";
string payloadHash;
string method;
if (request.GetMethod() == HttpRequest::Method::POST)
{
method = "POST";
payloadHash = Utils::HashSha256(string(request.Body(), request.BodySize()));
}
Http::HeaderCollection headers = request.Headers();
string canonicalHeaders = "content-type:" + headers[string("Content-Type")] + "\nhost:"
+ headers["Host"] + "\n";
string signedHeaders = "content-type;host";
string canonicalRequest = method + "\n" + canonicalUri + "\n" + canonicalQueryString
+ "\n" + canonicalHeaders + "\n" + signedHeaders + "\n" + payloadHash;
string algorithm = "TC3-HMAC-SHA256";
string cannonicalRequestHash = Utils::HashSha256(canonicalRequest);
string stringToSign = algorithm + "\n" + headers["X-TC-Timestamp"] +
"\n" + credentialScope + "\n" + cannonicalRequestHash;
string secretId = m_credential.GetSecretId();
string secretKey = m_credential.GetSecretKey();
Sign signHandler(secretId, secretKey);
string signature = signHandler.Tc3Sign(credDate, serviceName, stringToSign);
string authorization = "TC3-HMAC-SHA256 Credential=" + secretId + "/" + credentialScope
+ ", SignedHeaders=content-type;host" + ", Signature=" + signature;
request.AddHeader("Authorization", authorization);
}