The Integrate C SDK document describes the process of importing, configuring, parsing IP addresses, applying them to a network library, and validating the integration of the C SDK. This topic describes how to integrate the HTTPDNS C SDK with the Alibaba Cloud OSS C++ SDK.
1. Background
Alibaba Cloud Object Storage Service (OSS) is a general-purpose cloud storage service that allows developers to store and access files, such as profile pictures, images, audio, and video, for various applications. You can use the OSS C++ SDK on desktop platforms to interact with this service.
To improve the stability and speed of file transfers, you can combine the HTTPDNS C SDK with the OSS C++ SDK to resolve OSS domain names. This combination effectively prevents domain name hijacking and accelerates DNS resolution, which optimizes your application's network experience when accessing OSS.
2. Preparations
Version requirements:
HTTPDNS C SDK: >= 2.0.0
OSS C++ SDK: >= 1.10.1
2.1 Build HTTPDNS C SDK
git clone https://github.com/aliyun/alibabacloud-httpdns-c-sdk.git
cd alibabacloud-httpdns-c-sdk
mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make
sudo make install2.2 Build OSS C++ SDK
git clone https://github.com/aliyun/aliyun-oss-cpp-sdk.git
cd aliyun-oss-cpp-sdk
mkdir build && cd build
cmake ..
make
sudo make install2.3 Configure your project CMakeLists.txt
You can add both SDKs to your project's CMakeLists.txt file:
# Add HTTPDNS C SDK
find_library(HTTPDNS_LIBRARY httpdns_c_sdk_static)
include_directories(${CMAKE_INSTALL_PREFIX}/include/httpdns)
# Add OSS C++ SDK
find_library(OSS_LIBRARY alibabacloud-oss-cpp-sdk)
include_directories(${CMAKE_INSTALL_PREFIX}/include)
# Link libraries
target_link_libraries(your_target
${HTTPDNS_LIBRARY}
${OSS_LIBRARY}
curl
pthread
)3. Integration steps
Step 1: Add the HTTPDNS adapter
The OSS C++ SDK provides an HttpInterceptor extension point that lets you intercept and modify HTTP requests before they are sent. You can implement this interface to inject HTTPDNS resolution results into the request.
You can download the adapter file hdns_oss_interceptor.h and copy it into your project. This adapter implements the OSS SDK's HttpInterceptor interface. Its core logic is as follows:
void preSendRequest(void *handler,
const std::shared_ptr<OSS::HttpRequest> &request) override {
// Get the request domain name
std::string host = request->url().host();
// Resolve the domain name using HTTPDNS
hdns_list_head_t *results = nullptr;
hdns_status_t status = hdns_get_result_for_host_sync_with_cache(
hdnsClient_, host.c_str(), queryType_, nullptr, &results);
// Get the resolved IP address
char ip[HDNS_IP_ADDRESS_STRING_LENGTH] = {0};
hdns_select_ip_randomly(results, queryType_, ip);
// Inject the resolution result into the request
char resolveEntry[512];
snprintf(resolveEntry, sizeof(resolveEntry), "%s:%d:%s", host.c_str(), port, ip);
struct curl_slist *resolveList = curl_slist_append(nullptr, resolveEntry);
curl_easy_setopt(curl, CURLOPT_RESOLVE, resolveList);
}You can modify the adapter file as needed for your specific scenario, such as adjusting the resolution method, IP selection strategy, or query type.
Step 2: Integrate with OSS SDK
You can integrate the adapter into the OSS SDK to complete the HTTPDNS integration.
Integration code example:
#include <iostream>
#include <memory>
// HTTPDNS SDK
extern "C" {
#include "hdns_api.h"
}
// OSS SDK
#include <alibabacloud/oss/OssClient.h>
// HTTPDNS-OSS adapter
#include "hdns_oss_interceptor.h"
using namespace AlibabaCloud::OSS;
int main() {
std::string accessKeyId = "your_access_key_id";
std::string accessKeySecret = "your_access_key_secret";
std::string endpoint = "oss-cn-hangzhou.aliyuncs.com";
// ============ 1. Initialize HTTPDNS ============
hdns_sdk_init();
hdns_client_t *hdnsClient = hdns_client_create("your_httpdns_account_id", "your_secret_key");
// Configure the HTTPDNS client
hdns_client_set_timeout(hdnsClient, 2000);
hdns_client_set_using_cache(hdnsClient, true);
hdns_client_set_using_https(hdnsClient, true);
hdns_client_enable_expired_ip(hdnsClient, true);
hdns_client_enable_failover_localdns(hdnsClient, true);
hdns_client_add_pre_resolve_host(hdnsClient, endpoint.c_str());
hdns_client_start(hdnsClient);
// ============ 2. Initialize OSS and inject the adapter ============
InitializeSdk();
// Create the adapter
auto hdnsInterceptor = std::make_shared<AlibabaCloud::HDNS::HdnsOssInterceptor>(hdnsClient);
// Configure the OSS client
ClientConfiguration conf;
conf.httpInterceptor = hdnsInterceptor; // Key: inject the adapter
// Create the OSS client
OssClient ossClient("https://" + endpoint, accessKeyId, accessKeySecret, conf);
// ============ 3. Use the OSS client ============
auto outcome = ossClient.ListBuckets();
if (outcome.isSuccess()) {
std::cout << "Request succeeded. Bucket list:" << std::endl;
for (const auto &bucket : outcome.result().Buckets()) {
std::cout << " - " << bucket.Name() << std::endl;
}
} else {
std::cerr << "Request failed: " << outcome.error().Message() << std::endl;
}
// ============ 4. Release resources ============
// Note: Release the OSS client before releasing the HTTPDNS client
ShutdownSdk();
hdns_client_cleanup(hdnsClient);
hdns_sdk_cleanup();
return 0;
}You must initialize the HTTPDNS SDK before initializing the OSS SDK. When releasing resources, release the OSS client before releasing the HTTPDNS client.
4. Summary
Combining HTTPDNS with the OSS C++ SDK effectively prevents domain name hijacking and accelerates file transfer, which improves stability and security. The core implementation uses the HttpInterceptor mechanism to inject HTTPDNS resolution results into OSS requests before they are sent.
Key considerations:
Initialize the HTTPDNS SDK before initializing the OSS SDK.
When releasing resources, release the OSS client before releasing the HTTPDNS client.
If HTTPDNS resolution fails, the adapter automatically falls back to the system DNS without affecting normal operation.