Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions core/include/tencentcloud/core/http/HttpClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ namespace TencentCloud
void SetCaInfo(std::string caInfo);
void SetCaPath(std::string caPath);

void SetTargetIp(std::string targetIp);

HttpResponseOutcome SendRequest(const HttpRequest &request);

void SetProxy(const NetworkProxy &proxy);
Expand All @@ -54,6 +56,7 @@ namespace TencentCloud
NetworkProxy m_proxy;
std::string m_caInfo;
std::string m_caPath;
std::string m_targetIp;
#ifdef ENABLE_COMPRESS_MODULE
int GzipDecompress(const char *src, int srcLen, const char *dst, int* dstLen);
bool TryDecompress(const char *src, int srcLen, std::string &decompressData);
Expand Down
6 changes: 5 additions & 1 deletion core/include/tencentcloud/core/profile/HttpProfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ namespace TencentCloud

void SetCaInfo(std::string caInfo);
std::string GetCaInfo() const;
void SetTargetIp(std::string targetIp);
std::string GetTargetIp() const;
void SetCaPath(std::string caPath);
std::string GetCaPath() const;

Expand All @@ -62,7 +64,8 @@ namespace TencentCloud
m_connectTimeout(o.m_connectTimeout),
m_keepAlive(o.m_keepAlive),
m_caInfo(o.m_caInfo),
m_caPath(o.m_caPath)
m_caPath(o.m_caPath),
m_targetIp(o.m_targetIp)
{
}

Expand Down Expand Up @@ -91,6 +94,7 @@ namespace TencentCloud
bool m_keepAlive;
std::string m_caInfo;
std::string m_caPath;
std::string m_targetIp;
};
}

Expand Down
3 changes: 2 additions & 1 deletion core/src/AbstractClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ HttpClient::HttpResponseOutcome AbstractClient::DoRequest(const std::string &act
m_httpClient->SetCaInfo(httpProfile.GetCaInfo());
m_httpClient->SetCaPath(httpProfile.GetCaPath());

m_httpClient->SetTargetIp(httpProfile.GetTargetIp());

return m_httpClient->SendRequest(httpRequest);
}

Expand Down Expand Up @@ -212,4 +214,3 @@ void AbstractClient::GenerateSignature(HttpRequest &request)
+ ", SignedHeaders=content-type;host" + ", Signature=" + signature;
request.AddHeader("Authorization", authorization);
}

22 changes: 21 additions & 1 deletion core/src/http/HttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ void HttpClient::SetCaInfo(std::string caInfo)
m_caInfo = caInfo;
}

void HttpClient::SetTargetIp(std::string targetIp)
{
m_targetIp = targetIp;
}

void HttpClient::SetCaPath(std::string caPath)
{
m_caPath = caPath;
Expand Down Expand Up @@ -174,6 +179,16 @@ HttpClient::HttpResponseOutcome HttpClient::SendRequest(const HttpRequest &reque
curl_easy_setopt(m_curlHandle, CURLOPT_CONNECTTIMEOUT_MS, m_connectTimeout);

curl_easy_setopt(m_curlHandle, CURLOPT_URL, url.c_str());
std::string domain = request.GetUrl().GetHost();

// 创建解析规则链表(用于CURLOPT_RESOLVE)
struct curl_slist *resolveList = nullptr;

if (!m_targetIp.empty() && !domain.empty()) {
std::string resolveEntry = domain + ":443:" + m_targetIp;
resolveList = curl_slist_append(resolveList, resolveEntry.c_str());
curl_easy_setopt(m_curlHandle, CURLOPT_RESOLVE, resolveList);
}
curl_easy_setopt(m_curlHandle, CURLOPT_SSL_VERIFYPEER, 1L);
curl_easy_setopt(m_curlHandle, CURLOPT_SSL_VERIFYHOST, 2L);

Expand Down Expand Up @@ -205,6 +220,11 @@ HttpClient::HttpResponseOutcome HttpClient::SendRequest(const HttpRequest &reque
curl_easy_setopt(m_curlHandle, CURLOPT_ERRORBUFFER, errbuf);

CURLcode res = curl_easy_perform(m_curlHandle);
if (resolveList)
{
curl_slist_free_all(resolveList);
}

if (header_list)
{
curl_slist_free_all(header_list);
Expand Down Expand Up @@ -279,4 +299,4 @@ int HttpClient::GzipDecompress(const char *src, int srcLen, const char *dst, int
inflateEnd(&stream);
return err;
}
#endif // ENABLE_COMPRESS_MODULE
#endif // ENABLE_COMPRESS_MODULE
13 changes: 12 additions & 1 deletion core/src/profile/HttpProfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ HttpProfile::HttpProfile() :
m_connectTimeout(TM_MINUTE_SECOND),
m_keepAlive(false),
m_caInfo(""),
m_caPath("")
m_caPath(""),
m_targetIp("")
{
}

Expand Down Expand Up @@ -95,6 +96,16 @@ std::string HttpProfile::GetCaInfo() const
return m_caInfo;
}

void HttpProfile::SetTargetIp(std::string targetIp)
{
m_targetIp = targetIp;
}

std::string HttpProfile::GetTargetIp() const
{
return m_targetIp;
}

void HttpProfile::SetCaPath(std::string caPath)
{
m_caPath = caPath;
Expand Down
5 changes: 3 additions & 2 deletions test/function_test/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ add_executable(core_ft
Core_Http_HttpResponse_Ft.cpp
Core_Profile_HttpProfile_Ft.cpp
Core_Profile_ClientProfile_Ft.cpp
Core_Http_TargetIp_Ft.cpp
Main.cpp)
target_link_libraries(core_ft tencentcloud-sdk-cpp-cbs tencentcloud-sdk-cpp-core)
target_link_libraries(core_ft tencentcloud-sdk-cpp-cbs tencentcloud-sdk-cpp-cvm tencentcloud-sdk-cpp-core)
target_link_libraries(core_ft gtest gmock_main -lpthread -lm)

set_target_properties(core_ft
PROPERTIES
OUTPUT_NAME ${TARGET_OUTPUT_NAME_PREFIX}core_ft
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

add_test(NAME core_ft COMMAND core_ft)
add_test(NAME core_ft COMMAND core_ft)
119 changes: 119 additions & 0 deletions test/function_test/core/Core_Http_TargetIp_Ft.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#include <tencentcloud/core/TencentCloud.h>
#include <tencentcloud/core/profile/HttpProfile.h>
#include <tencentcloud/core/profile/ClientProfile.h>
#include <tencentcloud/core/Credential.h>
#include <tencentcloud/cvm/v20170312/CvmClient.h>
#include <tencentcloud/cvm/v20170312/model/DescribeInstancesRequest.h>

#include <iostream>
#include <string>
#include "../utils.h"
#include "gtest/gtest.h"

using namespace TencentCloud;
using namespace TencentCloud::Cvm::V20170312;
using namespace TencentCloud::Cvm::V20170312::Model;
using namespace std;

namespace
{
TEST(cvm, DescribeInstances_UseValidTargetIp)
{
TencentCloud::InitAPI();

string secretId = CUtils::GetEnv("TENCENTCLOUD_SECRET_ID");
string secretKey = CUtils::GetEnv("TENCENTCLOUD_SECRET_KEY");
Credential cred = Credential(secretId, secretKey);

HttpProfile httpProfile = HttpProfile();
httpProfile.SetEndpoint("cvm.ap-guangzhou.tencentcloudapi.com");
httpProfile.SetReqTimeout(10);
httpProfile.SetTargetIp("106.55.122.199");

ClientProfile clientProfile = ClientProfile(httpProfile);

DescribeInstancesRequest req = DescribeInstancesRequest();
req.SetOffset(0);
req.SetLimit(5);

CvmClient cvm_client = CvmClient(cred, "ap-guangzhou", clientProfile);

auto outcome = cvm_client.DescribeInstances(req);
EXPECT_TRUE(outcome.IsSuccess());
EXPECT_TRUE(outcome.GetError().GetErrorCode().empty());
EXPECT_FALSE(outcome.GetResult().GetRequestId().empty());

TencentCloud::ShutdownAPI();
}

TEST(cvm, DescribeInstances_UseInvalidTargetIp)
{
TencentCloud::InitAPI();

string secretId = CUtils::GetEnv("TENCENTCLOUD_SECRET_ID");
string secretKey = CUtils::GetEnv("TENCENTCLOUD_SECRET_KEY");
Credential cred = Credential(secretId, secretKey);

HttpProfile httpProfile = HttpProfile();
httpProfile.SetEndpoint("cvm.ap-guangzhou.tencentcloudapi.com");
httpProfile.SetReqTimeout(5);
httpProfile.SetTargetIp("192.168.1.1");

ClientProfile clientProfile = ClientProfile(httpProfile);

DescribeInstancesRequest req = DescribeInstancesRequest();
req.SetOffset(0);
req.SetLimit(5);

CvmClient cvm_client = CvmClient(cred, "ap-guangzhou", clientProfile);

auto outcome = cvm_client.DescribeInstances(req);
EXPECT_FALSE(outcome.IsSuccess());
EXPECT_FALSE(outcome.GetError().GetErrorCode().empty());

TencentCloud::ShutdownAPI();
}

TEST(cvm, DescribeInstances_WithoutTargetIp)
{
TencentCloud::InitAPI();

string secretId = CUtils::GetEnv("TENCENTCLOUD_SECRET_ID");
string secretKey = CUtils::GetEnv("TENCENTCLOUD_SECRET_KEY");
Credential cred = Credential(secretId, secretKey);

HttpProfile httpProfile = HttpProfile();
httpProfile.SetEndpoint("cvm.ap-guangzhou.tencentcloudapi.com");
httpProfile.SetReqTimeout(5);

ClientProfile clientProfile = ClientProfile(httpProfile);

DescribeInstancesRequest req = DescribeInstancesRequest();
req.SetOffset(0);
req.SetLimit(5);

CvmClient cvm_client = CvmClient(cred, "ap-guangzhou", clientProfile);

auto outcome = cvm_client.DescribeInstances(req);
EXPECT_TRUE(outcome.IsSuccess());
EXPECT_TRUE(outcome.GetError().GetErrorCode().empty());
EXPECT_FALSE(outcome.GetResult().GetRequestId().empty());

TencentCloud::ShutdownAPI();
}

TEST(cvm, TargetIp_ParameterPassing)
{
HttpProfile httpProfile;
string testIp = "106.55.122.199";
httpProfile.SetTargetIp(testIp);

EXPECT_EQ(httpProfile.GetTargetIp(), testIp);

ClientProfile clientProfile(httpProfile);
EXPECT_EQ(clientProfile.GetHttpProfile().GetTargetIp(), testIp);

httpProfile.SetTargetIp("");
EXPECT_TRUE(httpProfile.GetTargetIp().empty());
}
}