From a75b0d95acf921cdb1b0fc5722b9f3ecb057cce7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Levent=20KARAG=C3=96L?= Date: Sat, 20 Apr 2024 19:31:03 +0300 Subject: [PATCH 1/2] ignoreSslErrors parameter has been added to functions --- README.md | 79 +++++++----- src/libcpp-http-client.hpp | 241 +++++++++++++++++++++---------------- 2 files changed, 187 insertions(+), 133 deletions(-) diff --git a/README.md b/README.md index 4a89c50..b2e0b52 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ Modern, non-blocking and exception free, header-only HTTP Client library for C++ * [POST request with form data](#post-request-with-form-data) * [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) * [Semantic Versioning](#semantic-versioning) * [Full function list](#full-function-list) * [License](#license) @@ -336,6 +337,30 @@ int main() { ``` +## How to ignore SSL certificate errors? + +If you need to ignore SSL certificate errors for any valid reason, you can continue working +by passing the **"true"** value to the **"ignoreSslErrors"** parameter at the end of the functions. + +```cpp +#include +#include "libcpp-http-client.hpp" + +using namespace lklibs; + +int main() { + + HttpClient httpClient; + + std::string payload = "param1=7¶m2=test"; + + auto response = httpClient.postRequest("https://api.myinvalidssl.com", payload, true).get(); + + return 0; +} +``` + + ## Semantic Versioning Versioning of the library is done using conventional semantic versioning. Accordingly, @@ -363,49 +388,49 @@ overloaded versions of 5 functions in total. ```cpp - getRequest - std::future getRequest(const std::string &url) - - std::future getRequest(const std::string &url, bool returnAsBinary) - - std::future getRequest(const std::string &url, const std::map &headers) - - std::future getRequest(const std::string &url, bool returnAsBinary, const std::map &headers) + - std::future getRequest(const std::string &url, bool returnAsBinary, bool ignoreSslErrors = false) + - std::future getRequest(const std::string &url, const std::map &headers, bool ignoreSslErrors = false) + - std::future getRequest(const std::string &url, bool returnAsBinary, const std::map &headers, bool ignoreSslErrors = false) - postRequest - std::future postRequest(const std::string &url) - - std::future postRequest(const std::string &url, const std::string &payload) - - std::future postRequest(const std::string &url, bool returnAsBinary) - - std::future postRequest(const std::string &url, const std::map &headers) - - std::future postRequest(const std::string &url, const std::string &payload, bool returnAsBinary) - - std::future postRequest(const std::string &url, const std::string &payload, const std::map &headers) - - std::future postRequest(const std::string &url, bool returnAsBinary, const std::map &headers) - - std::future postRequest(const std::string &url, const std::string &payload, bool returnAsBinary, const std::map &headers) + - std::future postRequest(const std::string &url, const std::string &payload, bool ignoreSslErrors = false) + - std::future postRequest(const std::string &url, bool returnAsBinary, bool ignoreSslErrors = false) + - std::future postRequest(const std::string &url, const std::map &headers, bool ignoreSslErrors = false) + - std::future postRequest(const std::string &url, const std::string &payload, bool returnAsBinary, bool ignoreSslErrors = false) + - std::future postRequest(const std::string &url, const std::string &payload, const std::map &headers, bool ignoreSslErrors = false) + - std::future postRequest(const std::string &url, bool returnAsBinary, const std::map &headers, bool ignoreSslErrors = false) + - std::future postRequest(const std::string &url, const std::string &payload, bool returnAsBinary, const std::map &headers, bool ignoreSslErrors = false) - putRequest - std::future putRequest(const std::string &url) - - std::future putRequest(const std::string &url, const std::string &payload) - - std::future putRequest(const std::string &url, bool returnAsBinary) - - std::future putRequest(const std::string &url, const std::map &headers) - - std::future putRequest(const std::string &url, const std::string &payload, bool returnAsBinary) - - std::future putRequest(const std::string &url, const std::string &payload, const std::map &headers) - - std::future putRequest(const std::string &url, bool returnAsBinary, const std::map &headers) - - std::future putRequest(const std::string &url, const std::string &payload, bool returnAsBinary, const std::map &headers) + - std::future putRequest(const std::string &url, const std::string &payload, bool ignoreSslErrors = false) + - std::future putRequest(const std::string &url, bool returnAsBinary, bool ignoreSslErrors = false) + - std::future putRequest(const std::string &url, const std::map &headers, bool ignoreSslErrors = false) + - std::future putRequest(const std::string &url, const std::string &payload, bool returnAsBinary, bool ignoreSslErrors = false) + - std::future putRequest(const std::string &url, const std::string &payload, const std::map &headers, bool ignoreSslErrors = false) + - std::future putRequest(const std::string &url, bool returnAsBinary, const std::map &headers, bool ignoreSslErrors = false) + - std::future putRequest(const std::string &url, const std::string &payload, bool returnAsBinary, const std::map &headers, bool ignoreSslErrors = false) - deleteRequest - std::future deleteRequest(const std::string &url) - - std::future deleteRequest(const std::string &url, const std::string &payload) - - std::future deleteRequest(const std::string &url, bool returnAsBinary) - - std::future deleteRequest(const std::string &url, const std::map &headers) - - std::future deleteRequest(const std::string &url, const std::string &payload, bool returnAsBinary) - - std::future deleteRequest(const std::string &url, const std::string &payload, const std::map &headers) - - std::future deleteRequest(const std::string &url, bool returnAsBinary, const std::map &headers) - - std::future deleteRequest(const std::string &url, const std::string &payload, bool returnAsBinary, const std::map &headers) + - std::future deleteRequest(const std::string &url, const std::string &payload, bool ignoreSslErrors = false) + - std::future deleteRequest(const std::string &url, bool returnAsBinary, bool ignoreSslErrors = false) + - std::future deleteRequest(const std::string &url, const std::map &headers, bool ignoreSslErrors = false) + - std::future deleteRequest(const std::string &url, const std::string &payload, bool returnAsBinary, bool ignoreSslErrors = false) + - std::future deleteRequest(const std::string &url, const std::string &payload, const std::map &headers, bool ignoreSslErrors = false) + - std::future deleteRequest(const std::string &url, bool returnAsBinary, const std::map &headers, bool ignoreSslErrors = false) + - std::future deleteRequest(const std::string &url, const std::string &payload, bool returnAsBinary, const std::map &headers, bool ignoreSslErrors = false) - patchRequest - std::future patchRequest(const std::string &url) - - std::future patchRequest(const std::string &url, bool returnAsBinary) - - std::future patchRequest(const std::string &url, const std::map &headers) - - std::future patchRequest(const std::string &url, bool returnAsBinary, const std::map &headers) + - std::future patchRequest(const std::string &url, bool returnAsBinary, bool ignoreSslErrors = false) + - std::future patchRequest(const std::string &url, const std::map &headers, bool ignoreSslErrors = false) + - std::future patchRequest(const std::string &url, bool returnAsBinary, const std::map &headers, bool ignoreSslErrors = false) ``` diff --git a/src/libcpp-http-client.hpp b/src/libcpp-http-client.hpp index a4b9e90..679f080 100644 --- a/src/libcpp-http-client.hpp +++ b/src/libcpp-http-client.hpp @@ -1,7 +1,7 @@ /* Modern non-blocking HTTP Client library for C++ (17+) -version 1.0.0 +version 1.1.0 https://github.com/lk-libs/libcpp-http-client If you encounter any issues, please submit a ticket at https://github.com/lk-libs/libcpp-http-client/issues @@ -99,44 +99,47 @@ namespace lklibs { */ std::future getRequest(const std::string &url) noexcept { - return request(url, "GET", "", false, {}); + return request(url, "GET", "", false, {}, false); } /** * @brief Makes an HTTP GET request for the given URL and returns the result * * @param url: Request URL - * @param returnAsBinary: Return result as binary instead of string (Optional) + * @param returnAsBinary: Return result as binary instead of string + * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future getRequest(const std::string &url, bool returnAsBinary) noexcept { + std::future getRequest(const std::string &url, bool returnAsBinary, bool ignoreSslErrors = false) noexcept { - return request(url, "GET", "", returnAsBinary, {}); + return request(url, "GET", "", returnAsBinary, {}, ignoreSslErrors); } /** * @brief Makes an HTTP GET request for the given URL and returns the result * * @param url: Request URL - * @param headers: HTTP Header information to be sent when making the request (Optional) + * @param headers: HTTP Header information to be sent when making the request + * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future getRequest(const std::string &url, const std::map &headers) noexcept { + std::future getRequest(const std::string &url, const std::map &headers, bool ignoreSslErrors = false) noexcept { - return request(url, "GET", "", false, headers); + return request(url, "GET", "", false, headers, ignoreSslErrors); } /** * @brief Makes an HTTP GET request for the given URL and returns the result * * @param url: Request URL - * @param returnAsBinary: Return result as binary instead of string (Optional) - * @param headers: HTTP Header information to be sent when making the request (Optional) + * @param returnAsBinary: Return result as binary instead of string + * @param headers: HTTP Header information to be sent when making the request + * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future getRequest(const std::string &url, bool returnAsBinary, const std::map &headers) noexcept { + std::future getRequest(const std::string &url, bool returnAsBinary, const std::map &headers, bool ignoreSslErrors = false) noexcept { - return request(url, "GET", "", returnAsBinary, headers); + return request(url, "GET", "", returnAsBinary, headers, ignoreSslErrors); } @@ -148,96 +151,103 @@ namespace lklibs { */ std::future postRequest(const std::string &url) noexcept { - return request(url, "POST", "", false, {}); + return request(url, "POST", "", false, {}, false); } /** * @brief Makes an HTTP POST request for the given URL and returns the result * * @param url: Request URL - * @param payload: Payload to be sent with the request (Optional) + * @param payload: Payload to be sent with the request + * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future postRequest(const std::string &url, const std::string &payload) noexcept { + std::future postRequest(const std::string &url, const std::string &payload, bool ignoreSslErrors = false) noexcept { - return request(url, "POST", payload, false, {}); + return request(url, "POST", payload, false, {}, ignoreSslErrors); } /** * @brief Makes an HTTP POST request for the given URL and returns the result * * @param url: Request URL - * @param returnAsBinary: Return result as binary instead of string (Optional) + * @param returnAsBinary: Return result as binary instead of string + * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future postRequest(const std::string &url, bool returnAsBinary) noexcept { + std::future postRequest(const std::string &url, bool returnAsBinary, bool ignoreSslErrors = false) noexcept { - return request(url, "POST", "", returnAsBinary, {}); + return request(url, "POST", "", returnAsBinary, {}, ignoreSslErrors); } /** * @brief Makes an HTTP POST request for the given URL and returns the result * * @param url: Request URL - * @param headers: HTTP Header information to be sent when making the request (Optional) + * @param headers: HTTP Header information to be sent when making the request + * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future postRequest(const std::string &url, const std::map &headers) noexcept { + std::future postRequest(const std::string &url, const std::map &headers, bool ignoreSslErrors = false) noexcept { - return request(url, "POST", "", false, headers); + return request(url, "POST", "", false, headers, ignoreSslErrors); } /** * @brief Makes an HTTP POST request for the given URL and returns the result * * @param url: Request URL - * @param payload: Payload to be sent with the request (Optional) - * @param returnAsBinary: Return result as binary instead of string (Optional) + * @param payload: Payload to be sent with the request + * @param returnAsBinary: Return result as binary instead of string + * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future postRequest(const std::string &url, const std::string &payload, bool returnAsBinary) noexcept { + std::future postRequest(const std::string &url, const std::string &payload, bool returnAsBinary, bool ignoreSslErrors = false) noexcept { - return request(url, "POST", payload, returnAsBinary, {}); + return request(url, "POST", payload, returnAsBinary, {}, ignoreSslErrors); } /** * @brief Makes an HTTP POST request for the given URL and returns the result * * @param url: Request URL - * @param payload: Payload to be sent with the request (Optional) - * @param headers: HTTP Header information to be sent when making the request (Optional) + * @param payload: Payload to be sent with the request + * @param headers: HTTP Header information to be sent when making the request + * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future postRequest(const std::string &url, const std::string &payload, const std::map &headers) noexcept { + std::future postRequest(const std::string &url, const std::string &payload, const std::map &headers, bool ignoreSslErrors = false) noexcept { - return request(url, "POST", payload, false, headers); + return request(url, "POST", payload, false, headers, ignoreSslErrors); } /** * @brief Makes an HTTP POST request for the given URL and returns the result * * @param url: Request URL - * @param returnAsBinary: Return result as binary instead of string (Optional) - * @param headers: HTTP Header information to be sent when making the request (Optional) + * @param returnAsBinary: Return result as binary instead of string + * @param headers: HTTP Header information to be sent when making the request + * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future postRequest(const std::string &url, bool returnAsBinary, const std::map &headers) noexcept { + std::future postRequest(const std::string &url, bool returnAsBinary, const std::map &headers, bool ignoreSslErrors = false) noexcept { - return request(url, "POST", "", returnAsBinary, headers); + return request(url, "POST", "", returnAsBinary, headers, ignoreSslErrors); } /** * @brief Makes an HTTP POST request for the given URL and returns the result * * @param url: Request URL - * @param payload: Payload to be sent with the request (Optional) - * @param returnAsBinary: Return result as binary instead of string (Optional) - * @param headers: HTTP Header information to be sent when making the request (Optional) + * @param payload: Payload to be sent with the request + * @param returnAsBinary: Return result as binary instead of string + * @param headers: HTTP Header information to be sent when making the request + * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future postRequest(const std::string &url, const std::string &payload, bool returnAsBinary, const std::map &headers) noexcept { + std::future postRequest(const std::string &url, const std::string &payload, bool returnAsBinary, const std::map &headers, bool ignoreSslErrors = false) noexcept { - return request(url, "POST", payload, returnAsBinary, headers); + return request(url, "POST", payload, returnAsBinary, headers, ignoreSslErrors); } @@ -249,96 +259,103 @@ namespace lklibs { */ std::future putRequest(const std::string &url) noexcept { - return request(url, "PUT", "", false, {}); + return request(url, "PUT", "", false, {}, false); } /** * @brief Makes an HTTP PUT request for the given URL and returns the result * * @param url: Request URL - * @param payload: Payload to be sent with the request (Optional) + * @param payload: Payload to be sent with the request + * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future putRequest(const std::string &url, const std::string &payload) noexcept { + std::future putRequest(const std::string &url, const std::string &payload, bool ignoreSslErrors = false) noexcept { - return request(url, "PUT", payload, false, {}); + return request(url, "PUT", payload, false, {}, ignoreSslErrors); } /** * @brief Makes an HTTP PUT request for the given URL and returns the result * * @param url: Request URL - * @param returnAsBinary: Return result as binary instead of string (Optional) + * @param returnAsBinary: Return result as binary instead of string + * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future putRequest(const std::string &url, bool returnAsBinary) noexcept { + std::future putRequest(const std::string &url, bool returnAsBinary, bool ignoreSslErrors = false) noexcept { - return request(url, "PUT", "", returnAsBinary, {}); + return request(url, "PUT", "", returnAsBinary, {}, ignoreSslErrors); } /** * @brief Makes an HTTP PUT request for the given URL and returns the result * * @param url: Request URL - * @param headers: HTTP Header information to be sent when making the request (Optional) + * @param headers: HTTP Header information to be sent when making the request + * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future putRequest(const std::string &url, const std::map &headers) noexcept { + std::future putRequest(const std::string &url, const std::map &headers, bool ignoreSslErrors = false) noexcept { - return request(url, "PUT", "", false, headers); + return request(url, "PUT", "", false, headers, ignoreSslErrors); } /** * @brief Makes an HTTP PUT request for the given URL and returns the result * * @param url: Request URL - * @param payload: Payload to be sent with the request (Optional) - * @param returnAsBinary: Return result as binary instead of string (Optional) + * @param payload: Payload to be sent with the request + * @param returnAsBinary: Return result as binary instead of string + * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future putRequest(const std::string &url, const std::string &payload, bool returnAsBinary) noexcept { + std::future putRequest(const std::string &url, const std::string &payload, bool returnAsBinary, bool ignoreSslErrors = false) noexcept { - return request(url, "PUT", payload, returnAsBinary, {}); + return request(url, "PUT", payload, returnAsBinary, {}, ignoreSslErrors); } /** * @brief Makes an HTTP PUT request for the given URL and returns the result * * @param url: Request URL - * @param payload: Payload to be sent with the request (Optional) - * @param headers: HTTP Header information to be sent when making the request (Optional) + * @param payload: Payload to be sent with the request + * @param headers: HTTP Header information to be sent when making the request + * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future putRequest(const std::string &url, const std::string &payload, const std::map &headers) noexcept { + std::future putRequest(const std::string &url, const std::string &payload, const std::map &headers, bool ignoreSslErrors = false) noexcept { - return request(url, "PUT", payload, false, headers); + return request(url, "PUT", payload, false, headers, ignoreSslErrors); } /** * @brief Makes an HTTP PUT request for the given URL and returns the result * * @param url: Request URL - * @param returnAsBinary: Return result as binary instead of string (Optional) - * @param headers: HTTP Header information to be sent when making the request (Optional) + * @param returnAsBinary: Return result as binary instead of string + * @param headers: HTTP Header information to be sent when making the request + * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future putRequest(const std::string &url, bool returnAsBinary, const std::map &headers) noexcept { + std::future putRequest(const std::string &url, bool returnAsBinary, const std::map &headers, bool ignoreSslErrors = false) noexcept { - return request(url, "PUT", "", returnAsBinary, headers); + return request(url, "PUT", "", returnAsBinary, headers, ignoreSslErrors); } /** * @brief Makes an HTTP PUT request for the given URL and returns the result * * @param url: Request URL - * @param payload: Payload to be sent with the request (Optional) - * @param returnAsBinary: Return result as binary instead of string (Optional) - * @param headers: HTTP Header information to be sent when making the request (Optional) + * @param payload: Payload to be sent with the request + * @param returnAsBinary: Return result as binary instead of string + * @param headers: HTTP Header information to be sent when making the request + * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future putRequest(const std::string &url, const std::string &payload, bool returnAsBinary, const std::map &headers) noexcept { + std::future putRequest(const std::string &url, const std::string &payload, bool returnAsBinary, const std::map &headers, bool ignoreSslErrors = false) noexcept { - return request(url, "PUT", payload, returnAsBinary, headers); + return request(url, "PUT", payload, returnAsBinary, headers, ignoreSslErrors); } @@ -350,96 +367,103 @@ namespace lklibs { */ std::future deleteRequest(const std::string &url) noexcept { - return request(url, "DELETE", "", false, {}); + return request(url, "DELETE", "", false, {}, false); } /** * @brief Makes an HTTP DELETE request for the given URL and returns the result * * @param url: Request URL - * @param payload: Payload to be sent with the request (Optional) + * @param payload: Payload to be sent with the request + * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future deleteRequest(const std::string &url, const std::string &payload) noexcept { + std::future deleteRequest(const std::string &url, const std::string &payload, bool ignoreSslErrors = false) noexcept { - return request(url, "DELETE", payload, false, {}); + return request(url, "DELETE", payload, false, {}, ignoreSslErrors); } /** * @brief Makes an HTTP DELETE request for the given URL and returns the result * * @param url: Request URL - * @param returnAsBinary: Return result as binary instead of string (Optional) + * @param returnAsBinary: Return result as binary instead of string + * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future deleteRequest(const std::string &url, bool returnAsBinary) noexcept { + std::future deleteRequest(const std::string &url, bool returnAsBinary, bool ignoreSslErrors = false) noexcept { - return request(url, "DELETE", "", returnAsBinary, {}); + return request(url, "DELETE", "", returnAsBinary, {}, ignoreSslErrors); } /** * @brief Makes an HTTP DELETE request for the given URL and returns the result * * @param url: Request URL - * @param headers: HTTP Header information to be sent when making the request (Optional) + * @param headers: HTTP Header information to be sent when making the request + * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future deleteRequest(const std::string &url, const std::map &headers) noexcept { + std::future deleteRequest(const std::string &url, const std::map &headers, bool ignoreSslErrors = false) noexcept { - return request(url, "DELETE", "", false, headers); + return request(url, "DELETE", "", false, headers, ignoreSslErrors); } /** * @brief Makes an HTTP DELETE request for the given URL and returns the result * * @param url: Request URL - * @param payload: Payload to be sent with the request (Optional) - * @param returnAsBinary: Return result as binary instead of string (Optional) + * @param payload: Payload to be sent with the request + * @param returnAsBinary: Return result as binary instead of string + * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future deleteRequest(const std::string &url, const std::string &payload, bool returnAsBinary) noexcept { + std::future deleteRequest(const std::string &url, const std::string &payload, bool returnAsBinary, bool ignoreSslErrors = false) noexcept { - return request(url, "DELETE", payload, returnAsBinary, {}); + return request(url, "DELETE", payload, returnAsBinary, {}, ignoreSslErrors); } /** * @brief Makes an HTTP DELETE request for the given URL and returns the result * * @param url: Request URL - * @param payload: Payload to be sent with the request (Optional) - * @param headers: HTTP Header information to be sent when making the request (Optional) + * @param payload: Payload to be sent with the request + * @param headers: HTTP Header information to be sent when making the request + * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future deleteRequest(const std::string &url, const std::string &payload, const std::map &headers) noexcept { + std::future deleteRequest(const std::string &url, const std::string &payload, const std::map &headers, bool ignoreSslErrors = false) noexcept { - return request(url, "DELETE", payload, false, headers); + return request(url, "DELETE", payload, false, headers, ignoreSslErrors); } /** * @brief Makes an HTTP DELETE request for the given URL and returns the result * * @param url: Request URL - * @param returnAsBinary: Return result as binary instead of string (Optional) - * @param headers: HTTP Header information to be sent when making the request (Optional) + * @param returnAsBinary: Return result as binary instead of string + * @param headers: HTTP Header information to be sent when making the request + * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future deleteRequest(const std::string &url, bool returnAsBinary, const std::map &headers) noexcept { + std::future deleteRequest(const std::string &url, bool returnAsBinary, const std::map &headers, bool ignoreSslErrors = false) noexcept { - return request(url, "DELETE", "", returnAsBinary, headers); + return request(url, "DELETE", "", returnAsBinary, headers, ignoreSslErrors); } /** * @brief Makes an HTTP DELETE request for the given URL and returns the result * * @param url: Request URL - * @param payload: Payload to be sent with the request (Optional) - * @param returnAsBinary: Return result as binary instead of string (Optional) - * @param headers: HTTP Header information to be sent when making the request (Optional) + * @param payload: Payload to be sent with the request + * @param returnAsBinary: Return result as binary instead of string + * @param headers: HTTP Header information to be sent when making the request + * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future deleteRequest(const std::string &url, const std::string &payload, bool returnAsBinary, const std::map &headers) noexcept { + std::future deleteRequest(const std::string &url, const std::string &payload, bool returnAsBinary, const std::map &headers, bool ignoreSslErrors = false) noexcept { - return request(url, "DELETE", payload, returnAsBinary, headers); + return request(url, "DELETE", payload, returnAsBinary, headers, ignoreSslErrors); } @@ -451,44 +475,47 @@ namespace lklibs { */ std::future patchRequest(const std::string &url) noexcept { - return request(url, "PATCH", "", false, {}); + return request(url, "PATCH", "", false, {}, false); } /** * @brief Makes an HTTP PATCH request for the given URL and returns the result * * @param url: Request URL - * @param returnAsBinary: Return result as binary instead of string (Optional) + * @param returnAsBinary: Return result as binary instead of string + * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future patchRequest(const std::string &url, bool returnAsBinary) noexcept { + std::future patchRequest(const std::string &url, bool returnAsBinary, bool ignoreSslErrors = false) noexcept { - return request(url, "PATCH", "", returnAsBinary, {}); + return request(url, "PATCH", "", returnAsBinary, {}, ignoreSslErrors); } /** * @brief Makes an HTTP PATCH request for the given URL and returns the result * * @param url: Request URL - * @param headers: HTTP Header information to be sent when making the request (Optional) + * @param headers: HTTP Header information to be sent when making the request + * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future patchRequest(const std::string &url, const std::map &headers) noexcept { + std::future patchRequest(const std::string &url, const std::map &headers, bool ignoreSslErrors = false) noexcept { - return request(url, "PATCH", "", false, headers); + return request(url, "PATCH", "", false, headers, ignoreSslErrors); } /** * @brief Makes an HTTP PATCH request for the given URL and returns the result * * @param url: Request URL - * @param returnAsBinary: Return result as binary instead of string (Optional) - * @param headers: HTTP Header information to be sent when making the request (Optional) + * @param returnAsBinary: Return result as binary instead of string + * @param headers: HTTP Header information to be sent when making the request + * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future patchRequest(const std::string &url, bool returnAsBinary, const std::map &headers) noexcept { + std::future patchRequest(const std::string &url, bool returnAsBinary, const std::map &headers, bool ignoreSslErrors = false) noexcept { - return request(url, "PATCH", "", returnAsBinary, headers); + return request(url, "PATCH", "", returnAsBinary, headers, ignoreSslErrors); } private: @@ -515,9 +542,9 @@ namespace lklibs { } }; - std::future request(const std::string &url, const std::string &method, const std::string &payload, bool returnAsBinary, const std::map &headers) { + std::future request(const std::string &url, const std::string &method, const std::string &payload, bool returnAsBinary, const std::map &headers, bool ignoreSslErrors) { - return std::async(std::launch::async, [url, method, payload, returnAsBinary, headers]() -> HttpResult { + return std::async(std::launch::async, [url, method, payload, returnAsBinary, headers, ignoreSslErrors]() -> HttpResult { std::unique_ptr curl(curl_easy_init()); @@ -542,6 +569,8 @@ namespace lklibs { curl_easy_setopt(curl.get(), CURLOPT_HTTPHEADER, headerList.get()); curl_easy_setopt(curl.get(), CURLOPT_URL, url.c_str()); curl_easy_setopt(curl.get(), CURLOPT_CUSTOMREQUEST, method.c_str()); + curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYPEER, ignoreSslErrors ? 0L : 1L); + curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYHOST, ignoreSslErrors ? 0L : 1L); if (!payload.empty()) { From b5680a5bc99cc3a32fcc9f05112ec3f22e0abc83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Levent=20KARAG=C3=96L?= Date: Sat, 20 Apr 2024 21:06:20 +0300 Subject: [PATCH 2/2] ignoreSslErrors option has been added --- README.md | 12 +-- examples/main.cpp | 16 ++++ src/libcpp-http-client.hpp | 158 ++++++++++++++++--------------------- test/test.cpp | 26 ++++++ 4 files changed, 117 insertions(+), 95 deletions(-) diff --git a/README.md b/README.md index b2e0b52..e60d2bd 100644 --- a/README.md +++ b/README.md @@ -339,8 +339,9 @@ int main() { ## How to ignore SSL certificate errors? -If you need to ignore SSL certificate errors for any valid reason, you can continue working -by passing the **"true"** value to the **"ignoreSslErrors"** parameter at the end of the functions. +If you need to ignore SSL certificate errors for any valid reason, you can continue +working by passing **"true"** value to the **"ignoreSslErrors"** variable of the +HttpClient class. ```cpp #include @@ -351,10 +352,11 @@ using namespace lklibs; int main() { HttpClient httpClient; + + // If you need to ignore SSL errors, you can set the "ignoreSslErrors" field to true + httpClient.ignoreSslErrors = true; - std::string payload = "param1=7¶m2=test"; - - auto response = httpClient.postRequest("https://api.myinvalidssl.com", payload, true).get(); + auto response = httpClient.getRequest("https://api.myinvalidssl.com").get(); return 0; } diff --git a/examples/main.cpp b/examples/main.cpp index fa524f4..b00272c 100644 --- a/examples/main.cpp +++ b/examples/main.cpp @@ -160,6 +160,20 @@ void simplePatch() { std::cout << "Data: " << response.textData << std::endl; } +void ignoreSslErrors() { + + HttpClient httpClient; + + // If you need to ignore SSL errors, you can set the "ignoreSslErrors" field to true + httpClient.ignoreSslErrors = true; + + auto response = httpClient.getRequest("https://self-signed-cert.httpbun.com").get(); + + std::cout << "Succeed: " << response.succeed << std::endl; + std::cout << "Http Status Code: " << response.statusCode << std::endl; + std::cout << "Data: " << response.textData << std::endl; +} + int main() { @@ -183,5 +197,7 @@ int main() { simplePatch(); + ignoreSslErrors(); + return 0; } \ No newline at end of file diff --git a/src/libcpp-http-client.hpp b/src/libcpp-http-client.hpp index 679f080..f2799df 100644 --- a/src/libcpp-http-client.hpp +++ b/src/libcpp-http-client.hpp @@ -91,6 +91,11 @@ namespace lklibs { curl_global_cleanup(); } + /** + * @brief Ignore SSL errors when making HTTP requests + */ + bool ignoreSslErrors = false; + /** * @brief Makes an HTTP GET request for the given URL and returns the result * @@ -99,7 +104,7 @@ namespace lklibs { */ std::future getRequest(const std::string &url) noexcept { - return request(url, "GET", "", false, {}, false); + return request(url, "GET", "", false, {}); } /** @@ -107,12 +112,11 @@ namespace lklibs { * * @param url: Request URL * @param returnAsBinary: Return result as binary instead of string - * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future getRequest(const std::string &url, bool returnAsBinary, bool ignoreSslErrors = false) noexcept { + std::future getRequest(const std::string &url, bool returnAsBinary) noexcept { - return request(url, "GET", "", returnAsBinary, {}, ignoreSslErrors); + return request(url, "GET", "", returnAsBinary, {}); } /** @@ -120,12 +124,11 @@ namespace lklibs { * * @param url: Request URL * @param headers: HTTP Header information to be sent when making the request - * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future getRequest(const std::string &url, const std::map &headers, bool ignoreSslErrors = false) noexcept { + std::future getRequest(const std::string &url, const std::map &headers) noexcept { - return request(url, "GET", "", false, headers, ignoreSslErrors); + return request(url, "GET", "", false, headers); } /** @@ -134,12 +137,11 @@ namespace lklibs { * @param url: Request URL * @param returnAsBinary: Return result as binary instead of string * @param headers: HTTP Header information to be sent when making the request - * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future getRequest(const std::string &url, bool returnAsBinary, const std::map &headers, bool ignoreSslErrors = false) noexcept { + std::future getRequest(const std::string &url, bool returnAsBinary, const std::map &headers) noexcept { - return request(url, "GET", "", returnAsBinary, headers, ignoreSslErrors); + return request(url, "GET", "", returnAsBinary, headers); } @@ -151,7 +153,7 @@ namespace lklibs { */ std::future postRequest(const std::string &url) noexcept { - return request(url, "POST", "", false, {}, false); + return request(url, "POST", "", false, {}); } /** @@ -159,12 +161,11 @@ namespace lklibs { * * @param url: Request URL * @param payload: Payload to be sent with the request - * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future postRequest(const std::string &url, const std::string &payload, bool ignoreSslErrors = false) noexcept { + std::future postRequest(const std::string &url, const std::string &payload) noexcept { - return request(url, "POST", payload, false, {}, ignoreSslErrors); + return request(url, "POST", payload, false, {}); } /** @@ -172,12 +173,11 @@ namespace lklibs { * * @param url: Request URL * @param returnAsBinary: Return result as binary instead of string - * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future postRequest(const std::string &url, bool returnAsBinary, bool ignoreSslErrors = false) noexcept { + std::future postRequest(const std::string &url, bool returnAsBinary) noexcept { - return request(url, "POST", "", returnAsBinary, {}, ignoreSslErrors); + return request(url, "POST", "", returnAsBinary, {}); } /** @@ -185,12 +185,11 @@ namespace lklibs { * * @param url: Request URL * @param headers: HTTP Header information to be sent when making the request - * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future postRequest(const std::string &url, const std::map &headers, bool ignoreSslErrors = false) noexcept { + std::future postRequest(const std::string &url, const std::map &headers) noexcept { - return request(url, "POST", "", false, headers, ignoreSslErrors); + return request(url, "POST", "", false, headers); } /** @@ -199,12 +198,11 @@ namespace lklibs { * @param url: Request URL * @param payload: Payload to be sent with the request * @param returnAsBinary: Return result as binary instead of string - * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future postRequest(const std::string &url, const std::string &payload, bool returnAsBinary, bool ignoreSslErrors = false) noexcept { + std::future postRequest(const std::string &url, const std::string &payload, bool returnAsBinary) noexcept { - return request(url, "POST", payload, returnAsBinary, {}, ignoreSslErrors); + return request(url, "POST", payload, returnAsBinary, {}); } /** @@ -213,12 +211,11 @@ namespace lklibs { * @param url: Request URL * @param payload: Payload to be sent with the request * @param headers: HTTP Header information to be sent when making the request - * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future postRequest(const std::string &url, const std::string &payload, const std::map &headers, bool ignoreSslErrors = false) noexcept { + std::future postRequest(const std::string &url, const std::string &payload, const std::map &headers) noexcept { - return request(url, "POST", payload, false, headers, ignoreSslErrors); + return request(url, "POST", payload, false, headers); } /** @@ -227,12 +224,11 @@ namespace lklibs { * @param url: Request URL * @param returnAsBinary: Return result as binary instead of string * @param headers: HTTP Header information to be sent when making the request - * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future postRequest(const std::string &url, bool returnAsBinary, const std::map &headers, bool ignoreSslErrors = false) noexcept { + std::future postRequest(const std::string &url, bool returnAsBinary, const std::map &headers) noexcept { - return request(url, "POST", "", returnAsBinary, headers, ignoreSslErrors); + return request(url, "POST", "", returnAsBinary, headers); } /** @@ -242,12 +238,11 @@ namespace lklibs { * @param payload: Payload to be sent with the request * @param returnAsBinary: Return result as binary instead of string * @param headers: HTTP Header information to be sent when making the request - * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future postRequest(const std::string &url, const std::string &payload, bool returnAsBinary, const std::map &headers, bool ignoreSslErrors = false) noexcept { + std::future postRequest(const std::string &url, const std::string &payload, bool returnAsBinary, const std::map &headers) noexcept { - return request(url, "POST", payload, returnAsBinary, headers, ignoreSslErrors); + return request(url, "POST", payload, returnAsBinary, headers); } @@ -259,7 +254,7 @@ namespace lklibs { */ std::future putRequest(const std::string &url) noexcept { - return request(url, "PUT", "", false, {}, false); + return request(url, "PUT", "", false, {}); } /** @@ -267,12 +262,11 @@ namespace lklibs { * * @param url: Request URL * @param payload: Payload to be sent with the request - * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future putRequest(const std::string &url, const std::string &payload, bool ignoreSslErrors = false) noexcept { + std::future putRequest(const std::string &url, const std::string &payload) noexcept { - return request(url, "PUT", payload, false, {}, ignoreSslErrors); + return request(url, "PUT", payload, false, {}); } /** @@ -280,12 +274,11 @@ namespace lklibs { * * @param url: Request URL * @param returnAsBinary: Return result as binary instead of string - * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future putRequest(const std::string &url, bool returnAsBinary, bool ignoreSslErrors = false) noexcept { + std::future putRequest(const std::string &url, bool returnAsBinary) noexcept { - return request(url, "PUT", "", returnAsBinary, {}, ignoreSslErrors); + return request(url, "PUT", "", returnAsBinary, {}); } /** @@ -293,12 +286,11 @@ namespace lklibs { * * @param url: Request URL * @param headers: HTTP Header information to be sent when making the request - * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future putRequest(const std::string &url, const std::map &headers, bool ignoreSslErrors = false) noexcept { + std::future putRequest(const std::string &url, const std::map &headers) noexcept { - return request(url, "PUT", "", false, headers, ignoreSslErrors); + return request(url, "PUT", "", false, headers); } /** @@ -307,12 +299,11 @@ namespace lklibs { * @param url: Request URL * @param payload: Payload to be sent with the request * @param returnAsBinary: Return result as binary instead of string - * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future putRequest(const std::string &url, const std::string &payload, bool returnAsBinary, bool ignoreSslErrors = false) noexcept { + std::future putRequest(const std::string &url, const std::string &payload, bool returnAsBinary) noexcept { - return request(url, "PUT", payload, returnAsBinary, {}, ignoreSslErrors); + return request(url, "PUT", payload, returnAsBinary, {}); } /** @@ -321,12 +312,11 @@ namespace lklibs { * @param url: Request URL * @param payload: Payload to be sent with the request * @param headers: HTTP Header information to be sent when making the request - * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future putRequest(const std::string &url, const std::string &payload, const std::map &headers, bool ignoreSslErrors = false) noexcept { + std::future putRequest(const std::string &url, const std::string &payload, const std::map &headers) noexcept { - return request(url, "PUT", payload, false, headers, ignoreSslErrors); + return request(url, "PUT", payload, false, headers); } /** @@ -335,12 +325,11 @@ namespace lklibs { * @param url: Request URL * @param returnAsBinary: Return result as binary instead of string * @param headers: HTTP Header information to be sent when making the request - * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future putRequest(const std::string &url, bool returnAsBinary, const std::map &headers, bool ignoreSslErrors = false) noexcept { + std::future putRequest(const std::string &url, bool returnAsBinary, const std::map &headers) noexcept { - return request(url, "PUT", "", returnAsBinary, headers, ignoreSslErrors); + return request(url, "PUT", "", returnAsBinary, headers); } /** @@ -350,12 +339,11 @@ namespace lklibs { * @param payload: Payload to be sent with the request * @param returnAsBinary: Return result as binary instead of string * @param headers: HTTP Header information to be sent when making the request - * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future putRequest(const std::string &url, const std::string &payload, bool returnAsBinary, const std::map &headers, bool ignoreSslErrors = false) noexcept { + std::future putRequest(const std::string &url, const std::string &payload, bool returnAsBinary, const std::map &headers) noexcept { - return request(url, "PUT", payload, returnAsBinary, headers, ignoreSslErrors); + return request(url, "PUT", payload, returnAsBinary, headers); } @@ -367,7 +355,7 @@ namespace lklibs { */ std::future deleteRequest(const std::string &url) noexcept { - return request(url, "DELETE", "", false, {}, false); + return request(url, "DELETE", "", false, {}); } /** @@ -375,12 +363,11 @@ namespace lklibs { * * @param url: Request URL * @param payload: Payload to be sent with the request - * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future deleteRequest(const std::string &url, const std::string &payload, bool ignoreSslErrors = false) noexcept { + std::future deleteRequest(const std::string &url, const std::string &payload) noexcept { - return request(url, "DELETE", payload, false, {}, ignoreSslErrors); + return request(url, "DELETE", payload, false, {}); } /** @@ -388,12 +375,11 @@ namespace lklibs { * * @param url: Request URL * @param returnAsBinary: Return result as binary instead of string - * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future deleteRequest(const std::string &url, bool returnAsBinary, bool ignoreSslErrors = false) noexcept { + std::future deleteRequest(const std::string &url, bool returnAsBinary) noexcept { - return request(url, "DELETE", "", returnAsBinary, {}, ignoreSslErrors); + return request(url, "DELETE", "", returnAsBinary, {}); } /** @@ -401,12 +387,11 @@ namespace lklibs { * * @param url: Request URL * @param headers: HTTP Header information to be sent when making the request - * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future deleteRequest(const std::string &url, const std::map &headers, bool ignoreSslErrors = false) noexcept { + std::future deleteRequest(const std::string &url, const std::map &headers) noexcept { - return request(url, "DELETE", "", false, headers, ignoreSslErrors); + return request(url, "DELETE", "", false, headers); } /** @@ -415,12 +400,11 @@ namespace lklibs { * @param url: Request URL * @param payload: Payload to be sent with the request * @param returnAsBinary: Return result as binary instead of string - * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future deleteRequest(const std::string &url, const std::string &payload, bool returnAsBinary, bool ignoreSslErrors = false) noexcept { + std::future deleteRequest(const std::string &url, const std::string &payload, bool returnAsBinary) noexcept { - return request(url, "DELETE", payload, returnAsBinary, {}, ignoreSslErrors); + return request(url, "DELETE", payload, returnAsBinary, {}); } /** @@ -429,12 +413,11 @@ namespace lklibs { * @param url: Request URL * @param payload: Payload to be sent with the request * @param headers: HTTP Header information to be sent when making the request - * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future deleteRequest(const std::string &url, const std::string &payload, const std::map &headers, bool ignoreSslErrors = false) noexcept { + std::future deleteRequest(const std::string &url, const std::string &payload, const std::map &headers) noexcept { - return request(url, "DELETE", payload, false, headers, ignoreSslErrors); + return request(url, "DELETE", payload, false, headers); } /** @@ -443,12 +426,11 @@ namespace lklibs { * @param url: Request URL * @param returnAsBinary: Return result as binary instead of string * @param headers: HTTP Header information to be sent when making the request - * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future deleteRequest(const std::string &url, bool returnAsBinary, const std::map &headers, bool ignoreSslErrors = false) noexcept { + std::future deleteRequest(const std::string &url, bool returnAsBinary, const std::map &headers) noexcept { - return request(url, "DELETE", "", returnAsBinary, headers, ignoreSslErrors); + return request(url, "DELETE", "", returnAsBinary, headers); } /** @@ -458,12 +440,11 @@ namespace lklibs { * @param payload: Payload to be sent with the request * @param returnAsBinary: Return result as binary instead of string * @param headers: HTTP Header information to be sent when making the request - * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future deleteRequest(const std::string &url, const std::string &payload, bool returnAsBinary, const std::map &headers, bool ignoreSslErrors = false) noexcept { + std::future deleteRequest(const std::string &url, const std::string &payload, bool returnAsBinary, const std::map &headers) noexcept { - return request(url, "DELETE", payload, returnAsBinary, headers, ignoreSslErrors); + return request(url, "DELETE", payload, returnAsBinary, headers); } @@ -475,7 +456,7 @@ namespace lklibs { */ std::future patchRequest(const std::string &url) noexcept { - return request(url, "PATCH", "", false, {}, false); + return request(url, "PATCH", "", false, {}); } /** @@ -483,12 +464,11 @@ namespace lklibs { * * @param url: Request URL * @param returnAsBinary: Return result as binary instead of string - * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future patchRequest(const std::string &url, bool returnAsBinary, bool ignoreSslErrors = false) noexcept { + std::future patchRequest(const std::string &url, bool returnAsBinary) noexcept { - return request(url, "PATCH", "", returnAsBinary, {}, ignoreSslErrors); + return request(url, "PATCH", "", returnAsBinary, {}); } /** @@ -496,12 +476,11 @@ namespace lklibs { * * @param url: Request URL * @param headers: HTTP Header information to be sent when making the request - * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future patchRequest(const std::string &url, const std::map &headers, bool ignoreSslErrors = false) noexcept { + std::future patchRequest(const std::string &url, const std::map &headers) noexcept { - return request(url, "PATCH", "", false, headers, ignoreSslErrors); + return request(url, "PATCH", "", false, headers); } /** @@ -510,12 +489,11 @@ namespace lklibs { * @param url: Request URL * @param returnAsBinary: Return result as binary instead of string * @param headers: HTTP Header information to be sent when making the request - * @param ignoreSslErrors: Ignore SSL errors (Optional) * @return Result of the request (see HttpResult object for details) */ - std::future patchRequest(const std::string &url, bool returnAsBinary, const std::map &headers, bool ignoreSslErrors = false) noexcept { + std::future patchRequest(const std::string &url, bool returnAsBinary, const std::map &headers) noexcept { - return request(url, "PATCH", "", returnAsBinary, headers, ignoreSslErrors); + return request(url, "PATCH", "", returnAsBinary, headers); } private: @@ -542,9 +520,9 @@ namespace lklibs { } }; - std::future request(const std::string &url, const std::string &method, const std::string &payload, bool returnAsBinary, const std::map &headers, bool ignoreSslErrors) { + std::future request(const std::string &url, const std::string &method, const std::string &payload, bool returnAsBinary, const std::map &headers) { - return std::async(std::launch::async, [url, method, payload, returnAsBinary, headers, ignoreSslErrors]() -> HttpResult { + return std::async(std::launch::async, [this, url, method, payload, returnAsBinary, headers]() -> HttpResult { std::unique_ptr curl(curl_easy_init()); @@ -569,8 +547,8 @@ namespace lklibs { curl_easy_setopt(curl.get(), CURLOPT_HTTPHEADER, headerList.get()); curl_easy_setopt(curl.get(), CURLOPT_URL, url.c_str()); curl_easy_setopt(curl.get(), CURLOPT_CUSTOMREQUEST, method.c_str()); - curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYPEER, ignoreSslErrors ? 0L : 1L); - curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYHOST, ignoreSslErrors ? 0L : 1L); + curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYPEER, this->ignoreSslErrors ? 0L : 1L); + curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYHOST, this->ignoreSslErrors ? 0L : 1L); if (!payload.empty()) { diff --git a/test/test.cpp b/test/test.cpp index b948912..bbc9823 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -677,6 +677,32 @@ TEST(HttpPatchTest, HttpHeadersCanBeSentWithTheHttpPatchRequest) { ASSERT_EQ(data["headers"]["Custom-Header2"], "value2") << "Custom-Header2 is invalid"; } +TEST(InvalidSSLTest, HttpGetRequestMustReturnErrorForAnInvalidSsl) { + + HttpClient httpClient; + + auto response = httpClient.getRequest("https://self-signed-cert.httpbun.com").get(); + + ASSERT_FALSE(response.succeed) << "HTTP Request failed"; + ASSERT_EQ(response.statusCode, 0) << "HTTP Status Code is not 0"; + ASSERT_EQ(response.errorMessage, "SSL peer certificate or SSH remote key was not OK") << "HTTP Error Message is invalid"; +} + +TEST(InvalidSSLTest, HttpGetRequestMustBeCompletedSuccessfullyForAnInvalidSslIfIgnoreSslErrorsFieldSetTrue) { + + HttpClient httpClient; + + httpClient.ignoreSslErrors = true; + + auto response = httpClient.getRequest("https://self-signed-cert.httpbun.com").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"; +} + int main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv);