Skip to content

Commit

Permalink
add function to configure SSL context
Browse files Browse the repository at this point in the history
  • Loading branch information
Nevermore1994 committed Jul 6, 2024
1 parent 7fb2c53 commit c987d98
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 10 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.

Expand Down
5 changes: 5 additions & 0 deletions example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 5 additions & 3 deletions src/SSLManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -43,7 +44,8 @@ SSLManager::~SSLManager() {
#ifdef __clang__
#pragma clang diagnostic pop
#endif
std::function<void(SSLContextPtr&)> SSLManager::configContext = nullptr;

std::function<void(SSLContextPtr&)> HttpsHelper::configContext = nullptr;

SSLContextPtr& SSLManager::shareContext() {
static std::once_flag flag;
Expand All @@ -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.");
Expand Down
6 changes: 1 addition & 5 deletions src/include/SSLManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,15 @@

#if ENABLE_HTTPS
#include <mutex>
#include <openssl/ssl.h>
#include <openssl/err.h>

#include "PlainSocket.h"
#include "HttpsHelper.h"

namespace http {

using SSLPtr = std::unique_ptr<SSL, decltype(&SSL_free)>;
using SSLContextPtr = std::unique_ptr<SSL_CTX, decltype(&SSL_CTX_free)>;

class SSLManager {
public:
static std::function<void(SSLContextPtr&)> configContext;
static SSLContextPtr& shareContext();

static SSLPtr create(Socket) noexcept;
Expand Down
21 changes: 21 additions & 0 deletions src/include/public/HttpsHelper.h
Original file line number Diff line number Diff line change
@@ -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 <openssl/ssl.h>

namespace http {

using SSLPtr = std::unique_ptr<SSL, decltype(&SSL_free)>;
using SSLContextPtr = std::unique_ptr<SSL_CTX, decltype(&SSL_CTX_free)>;

class HttpsHelper {
public:
static std::function<void(SSLContextPtr&)> configContext;
};

} //end of namespace http
#endif //end ENABLE_HTTPS
4 changes: 4 additions & 0 deletions src/include/public/Request.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#include "Data.hpp"
#include "Type.h"

#if ENABLE_HTTPS
#include "HttpsHelper.h"
#endif

namespace http {

class ISocket;
Expand Down

0 comments on commit c987d98

Please sign in to comment.