diff --git a/README.md b/README.md index 784afad..c6b77d5 100644 --- a/README.md +++ b/README.md @@ -128,6 +128,9 @@ public: ##### 1. Initialize the request framework: ```c++ Request::init(); //Must be callable on Windows. +HttpsHelper::configContext = [](SSLContextPtr& ptr) { + /* Configure your SSL context if needed */ +}; ``` ##### 2. Create a RequestInfo object: @@ -165,7 +168,15 @@ Request::clear(); //Must be callable on Windows. ##### 7. example [example.cpp](example.cpp) -**If you prefer not to use HTTPS, set DISABLE_HTTPS to ON in the [CMake file](src/CMakeLists.txt).** +### Notes + +* **If you prefer not to use HTTPS, set DISABLE_HTTPS to ON in the [CMake file](src/CMakeLists.txt).** + + +* **If you are using Windows, please remember to call `Request::init()` before making a request, +and ensure that the system variables `OPENSSL_ROOT_DIR`, `OPENSSL_INCLUDE_DIR`, and `OPENSSL_CRYPTO_LIBRARY` are set.** + + ### Contributing Feel free to contribute by opening issues or submitting pull requests. Please follow the code of conduct. diff --git a/example.cpp b/example.cpp index d53ae97..537f2e3 100644 --- a/example.cpp +++ b/example.cpp @@ -15,6 +15,11 @@ bool isFinished = false; int main() { Request::init(); +#if ENABLE_HTTPS + HttpsHelper::configContext = [](SSLContextPtr& ptr) { + //Configure your SSL context if needed + }; +#endif RequestInfo info; info.url = "https://httpbin.org/get"; info.methodType = HttpMethodType::Get; diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6c1f3af..5781ea3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -21,7 +21,7 @@ option(DISABLE_HTTPS "disable https" OFF) if (NOT DISABLE_HTTPS) message(STATUS "enable https") - add_definitions(-DENABLE_HTTPS) + target_compile_definitions(${PROJECT_NAME} PUBLIC ENABLE_HTTPS) find_package(OpenSSL REQUIRED) target_link_libraries(${PROJECT_NAME} OpenSSL::SSL OpenSSL::Crypto) else() diff --git a/src/SSLManager.cpp b/src/SSLManager.cpp index c5f9443..ddcf811 100644 --- a/src/SSLManager.cpp +++ b/src/SSLManager.cpp @@ -25,6 +25,7 @@ namespace http { #pragma clang diagnostic push #pragma ide diagnostic ignored "modernize-use-equals-default" #endif + SSLManager::SSLManager() { #if OPENSSL_VERSION_NUMBER < 0x10100000L SSL_library_init(); @@ -43,7 +44,8 @@ SSLManager::~SSLManager() { #ifdef __clang__ #pragma clang diagnostic pop #endif -std::function SSLManager::configContext = nullptr; + +std::function HttpsHelper::configContext = nullptr; SSLContextPtr& SSLManager::shareContext() { static std::once_flag flag; @@ -54,8 +56,8 @@ SSLContextPtr& SSLManager::shareContext() { SSL_CTX* context = SSL_CTX_new(sslMethod); if (context) { contextPtr.reset(context); - if (configContext) { - configContext(contextPtr); + if (HttpsHelper::configContext) { + HttpsHelper::configContext(contextPtr); } } else { throw std::runtime_error("create SSL Context error."); diff --git a/src/include/SSLManager.h b/src/include/SSLManager.h index b0e271f..936be4a 100644 --- a/src/include/SSLManager.h +++ b/src/include/SSLManager.h @@ -7,19 +7,15 @@ #if ENABLE_HTTPS #include -#include #include #include "PlainSocket.h" +#include "HttpsHelper.h" namespace http { -using SSLPtr = std::unique_ptr; -using SSLContextPtr = std::unique_ptr; - class SSLManager { public: - static std::function configContext; static SSLContextPtr& shareContext(); static SSLPtr create(Socket) noexcept; diff --git a/src/include/public/HttpsHelper.h b/src/include/public/HttpsHelper.h new file mode 100644 index 0000000..e71c978 --- /dev/null +++ b/src/include/public/HttpsHelper.h @@ -0,0 +1,21 @@ +// +// Created by Nevermore on 2024/7/6. +// example HttpsHelper +// Copyright (c) 2024 Nevermore All rights reserved. +// +#if ENABLE_HTTPS +#pragma once +#include + +namespace http { + +using SSLPtr = std::unique_ptr; +using SSLContextPtr = std::unique_ptr; + +class HttpsHelper { +public: + static std::function configContext; +}; + +} //end of namespace http +#endif //end ENABLE_HTTPS diff --git a/src/include/public/Request.h b/src/include/public/Request.h index 9252107..2a99686 100644 --- a/src/include/public/Request.h +++ b/src/include/public/Request.h @@ -9,6 +9,10 @@ #include "Data.hpp" #include "Type.h" +#if ENABLE_HTTPS +#include "HttpsHelper.h" +#endif + namespace http { class ISocket;