Skip to content

Commit 54eb66b

Browse files
Merge pull request #30 from lk-libs/tls-version
Tls version
2 parents 49e2a79 + c67d701 commit 54eb66b

File tree

4 files changed

+90
-2
lines changed

4 files changed

+90
-2
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Modern, non-blocking and exception free, header-only HTTP Client library for C++
2424
* [POST request with JSON data](#post-request-with-json-data)
2525
* [What about others? (PUT, DELETE, PATCH)](#what-about-others-put-delete-patch)
2626
* [How to ignore SSL certificate errors?](#how-to-ignore-ssl-certificate-errors)
27+
* [Setting the TLS version](#setting-the-tls-version)
2728
* [How to set timeout?](#how-to-set-timeout)
2829
* [How can I limit download and upload bandwidth?](#how-can-i-limit-download-and-upload-bandwidth)
2930
* [Semantic Versioning](#semantic-versioning)
@@ -388,6 +389,31 @@ int main() {
388389
}
389390
```
390391

392+
393+
## Setting the TLS version
394+
395+
You can set the TLS version used during the request with the setTLSVersion method
396+
397+
```cpp
398+
#include <fstream>
399+
#include "libcpp-http-client.hpp"
400+
401+
using namespace lklibs;
402+
403+
int main() {
404+
HttpRequest httpRequest("https://api.myproject.com");
405+
406+
// You can set the TLS version to be used for the request with setTLSVersion method
407+
auto response = httpRequest
408+
.setTLSVersion(TLSVersion::TLSv1_3)
409+
.send()
410+
.get();
411+
412+
return 0;
413+
}
414+
```
415+
416+
391417
## How to set timeout?
392418

393419
You can use the setTimeout method to set the timeout duration in seconds during requests.

examples/main.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,21 @@ void ignoreSslErrors()
187187
std::cout << "Data: " << response.textData << std::endl;
188188
}
189189

190+
void setTLSVersion()
191+
{
192+
HttpRequest httpRequest("https://httpbun.com/get");
193+
194+
// You can set the TLS version to be used for the request with setTLSVersion method
195+
auto response = httpRequest
196+
.setTLSVersion(TLSVersion::TLSv1_3)
197+
.send()
198+
.get();
199+
200+
std::cout << "Succeed: " << response.succeed << std::endl;
201+
std::cout << "Http Status Code: " << response.statusCode << std::endl;
202+
std::cout << "Data: " << response.textData << std::endl;
203+
}
204+
190205
void setTimeout()
191206
{
192207
HttpRequest httpRequest("https://httpstat.us/504?sleep=10000");
@@ -243,6 +258,8 @@ int main()
243258

244259
ignoreSslErrors();
245260

261+
setTLSVersion();
262+
246263
setTimeout();
247264

248265
setDownloadAndUploadBandwidthLimit();

src/libcpp-http-client.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,21 @@ namespace lklibs
9494
PATCH
9595
};
9696

97+
/**
98+
* @brief TLS Version options for the request
99+
*/
100+
enum class TLSVersion
101+
{
102+
DEFAULT,
103+
TLSv1, /* TLS 1.x */
104+
SSLv2,
105+
SSLv3,
106+
TLSv1_0,
107+
TLSv1_1,
108+
TLSv1_2,
109+
TLSv1_3
110+
};
111+
97112
/**
98113
* @brief Class to initialize and cleanup the curl library
99114
*/
@@ -215,6 +230,18 @@ namespace lklibs
215230
return *this;
216231
}
217232

233+
/**
234+
* @brief Set the TLS version for the request
235+
*
236+
* @param version: TLS version to be used for the request
237+
*/
238+
HttpRequest& setTLSVersion(TLSVersion version) noexcept
239+
{
240+
this->tlsVersion = version;
241+
242+
return *this;
243+
}
244+
218245
/**
219246
* @brief Add a HTTP header to the request
220247
*
@@ -301,6 +328,7 @@ namespace lklibs
301328
int timeout = 0;
302329
int uploadBandwidthLimit = 0;
303330
int downloadBandwidthLimit = 0;
331+
TLSVersion tlsVersion = TLSVersion::DEFAULT;
304332

305333
struct CurlDeleter
306334
{
@@ -353,6 +381,7 @@ namespace lklibs
353381
curl_easy_setopt(curl.get(), CURLOPT_CUSTOMREQUEST, this->method.c_str());
354382
curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYPEER, this->sslErrorsWillBeIgnored ? 0L : 1L);
355383
curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYHOST, this->sslErrorsWillBeIgnored ? 0L : 1L);
384+
curl_easy_setopt(curl.get(), CURLOPT_SSLVERSION, static_cast<int>(this->tlsVersion));
356385
curl_easy_setopt(curl.get(), CURLOPT_TIMEOUT, this->timeout);
357386
curl_easy_setopt(curl.get(), CURLOPT_MAX_SEND_SPEED_LARGE, this->uploadBandwidthLimit);
358387
curl_easy_setopt(curl.get(), CURLOPT_MAX_RECV_SPEED_LARGE, this->downloadBandwidthLimit);

test/test.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,22 @@ TEST(InvalidSSLTest, HttpGetRequestMustBeCompletedSuccessfullyForAnInvalidSslIfI
746746
ASSERT_TRUE(response.errorMessage.empty()) << "HTTP Error Message is not empty";
747747
}
748748

749+
TEST(TLSVersionTest, TLSVersionCanBeSet)
750+
{
751+
HttpRequest httpRequest("https://httpbun.com/get");
752+
753+
auto response = httpRequest
754+
.setTLSVersion(TLSVersion::TLSv1_3)
755+
.send()
756+
.get();
757+
758+
ASSERT_TRUE(response.succeed) << "HTTP Request failed";
759+
ASSERT_EQ(response.statusCode, 200) << "HTTP Status Code is not 200";
760+
ASSERT_FALSE(response.textData.empty()) << "HTTP Response is empty";
761+
ASSERT_TRUE(response.binaryData.empty()) << "Binary data is not empty";
762+
ASSERT_TRUE(response.errorMessage.empty()) << "HTTP Error Message is not empty";
763+
}
764+
749765
TEST(TimeoutTest, TimeoutCanBeSet)
750766
{
751767
HttpRequest httpRequest("https://httpstat.us/504?sleep=10000");
@@ -757,7 +773,7 @@ TEST(TimeoutTest, TimeoutCanBeSet)
757773
ASSERT_FALSE(response.errorMessage.empty()) << "HTTP Error Message is empty";
758774
}
759775

760-
TEST(BandwidthLimit, DownloadBandwidthLimitCanBeSet)
776+
TEST(BandwidthLimitTest, DownloadBandwidthLimitCanBeSet)
761777
{
762778
HttpRequest httpRequest("https://httpbun.com/get");
763779

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

776-
TEST(BandwidthLimit, UploadBandwidthLimitCanBeSet)
792+
TEST(BandwidthLimitTest, UploadBandwidthLimitCanBeSet)
777793
{
778794
HttpRequest httpRequest("https://httpbun.com/get");
779795

0 commit comments

Comments
 (0)