Skip to content

Tls version #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 27, 2024
Merged
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
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Modern, non-blocking and exception free, header-only HTTP Client library for C++
* [POST request with JSON data](#post-request-with-json-data)
* [What about others? (PUT, DELETE, PATCH)](#what-about-others-put-delete-patch)
* [How to ignore SSL certificate errors?](#how-to-ignore-ssl-certificate-errors)
* [Setting the TLS version](#setting-the-tls-version)
* [How to set timeout?](#how-to-set-timeout)
* [How can I limit download and upload bandwidth?](#how-can-i-limit-download-and-upload-bandwidth)
* [Semantic Versioning](#semantic-versioning)
Expand Down Expand Up @@ -388,6 +389,31 @@ int main() {
}
```


## Setting the TLS version

You can set the TLS version used during the request with the setTLSVersion method

```cpp
#include <fstream>
#include "libcpp-http-client.hpp"

using namespace lklibs;

int main() {
HttpRequest httpRequest("https://api.myproject.com");

// You can set the TLS version to be used for the request with setTLSVersion method
auto response = httpRequest
.setTLSVersion(TLSVersion::TLSv1_3)
.send()
.get();

return 0;
}
```


## How to set timeout?

You can use the setTimeout method to set the timeout duration in seconds during requests.
Expand Down
17 changes: 17 additions & 0 deletions examples/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,21 @@ void ignoreSslErrors()
std::cout << "Data: " << response.textData << std::endl;
}

void setTLSVersion()
{
HttpRequest httpRequest("https://httpbun.com/get");

// You can set the TLS version to be used for the request with setTLSVersion method
auto response = httpRequest
.setTLSVersion(TLSVersion::TLSv1_3)
.send()
.get();

std::cout << "Succeed: " << response.succeed << std::endl;
std::cout << "Http Status Code: " << response.statusCode << std::endl;
std::cout << "Data: " << response.textData << std::endl;
}

void setTimeout()
{
HttpRequest httpRequest("https://httpstat.us/504?sleep=10000");
Expand Down Expand Up @@ -243,6 +258,8 @@ int main()

ignoreSslErrors();

setTLSVersion();

setTimeout();

setDownloadAndUploadBandwidthLimit();
Expand Down
29 changes: 29 additions & 0 deletions src/libcpp-http-client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,21 @@ namespace lklibs
PATCH
};

/**
* @brief TLS Version options for the request
*/
enum class TLSVersion
{
DEFAULT,
TLSv1, /* TLS 1.x */
SSLv2,
SSLv3,
TLSv1_0,
TLSv1_1,
TLSv1_2,
TLSv1_3
};

/**
* @brief Class to initialize and cleanup the curl library
*/
Expand Down Expand Up @@ -215,6 +230,18 @@ namespace lklibs
return *this;
}

/**
* @brief Set the TLS version for the request
*
* @param version: TLS version to be used for the request
*/
HttpRequest& setTLSVersion(TLSVersion version) noexcept
{
this->tlsVersion = version;

return *this;
}

/**
* @brief Add a HTTP header to the request
*
Expand Down Expand Up @@ -301,6 +328,7 @@ namespace lklibs
int timeout = 0;
int uploadBandwidthLimit = 0;
int downloadBandwidthLimit = 0;
TLSVersion tlsVersion = TLSVersion::DEFAULT;

struct CurlDeleter
{
Expand Down Expand Up @@ -353,6 +381,7 @@ namespace lklibs
curl_easy_setopt(curl.get(), CURLOPT_CUSTOMREQUEST, this->method.c_str());
curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYPEER, this->sslErrorsWillBeIgnored ? 0L : 1L);
curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYHOST, this->sslErrorsWillBeIgnored ? 0L : 1L);
curl_easy_setopt(curl.get(), CURLOPT_SSLVERSION, static_cast<int>(this->tlsVersion));
curl_easy_setopt(curl.get(), CURLOPT_TIMEOUT, this->timeout);
curl_easy_setopt(curl.get(), CURLOPT_MAX_SEND_SPEED_LARGE, this->uploadBandwidthLimit);
curl_easy_setopt(curl.get(), CURLOPT_MAX_RECV_SPEED_LARGE, this->downloadBandwidthLimit);
Expand Down
20 changes: 18 additions & 2 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,22 @@ TEST(InvalidSSLTest, HttpGetRequestMustBeCompletedSuccessfullyForAnInvalidSslIfI
ASSERT_TRUE(response.errorMessage.empty()) << "HTTP Error Message is not empty";
}

TEST(TLSVersionTest, TLSVersionCanBeSet)
{
HttpRequest httpRequest("https://httpbun.com/get");

auto response = httpRequest
.setTLSVersion(TLSVersion::TLSv1_3)
.send()
.get();

ASSERT_TRUE(response.succeed) << "HTTP Request failed";
ASSERT_EQ(response.statusCode, 200) << "HTTP Status Code is not 200";
ASSERT_FALSE(response.textData.empty()) << "HTTP Response is empty";
ASSERT_TRUE(response.binaryData.empty()) << "Binary data is not empty";
ASSERT_TRUE(response.errorMessage.empty()) << "HTTP Error Message is not empty";
}

TEST(TimeoutTest, TimeoutCanBeSet)
{
HttpRequest httpRequest("https://httpstat.us/504?sleep=10000");
Expand All @@ -757,7 +773,7 @@ TEST(TimeoutTest, TimeoutCanBeSet)
ASSERT_FALSE(response.errorMessage.empty()) << "HTTP Error Message is empty";
}

TEST(BandwidthLimit, DownloadBandwidthLimitCanBeSet)
TEST(BandwidthLimitTest, DownloadBandwidthLimitCanBeSet)
{
HttpRequest httpRequest("https://httpbun.com/get");

Expand All @@ -773,7 +789,7 @@ TEST(BandwidthLimit, DownloadBandwidthLimitCanBeSet)
ASSERT_TRUE(response.errorMessage.empty()) << "HTTP Error Message is not empty";
}

TEST(BandwidthLimit, UploadBandwidthLimitCanBeSet)
TEST(BandwidthLimitTest, UploadBandwidthLimitCanBeSet)
{
HttpRequest httpRequest("https://httpbun.com/get");

Expand Down
Loading