Skip to content

Commit

Permalink
add HTTPS support - enabled if certiface pair is set (PEM)
Browse files Browse the repository at this point in the history
Signed-off-by: astib <[email protected]>
  • Loading branch information
astibal committed Oct 15, 2022
1 parent fea67c1 commit 49a5a99
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions include/lmhttpd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ namespace lmh {
struct MHD_Daemon* daemon_;
struct options_t {
bool bind_loopback = false;
std::optional<std::pair<std::string, std::string>> certificate;

// optional handlers
std::optional<std::function<bool()>> handler_should_terminate;
Expand Down Expand Up @@ -227,13 +228,34 @@ namespace lmh {
bind_addr.sin_port = htons(port_);
if(options().bind_loopback) bind_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);

daemon_ = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION,
port_, nullptr, nullptr,
reinterpret_cast<MHD_AccessHandlerCallback>(&request_handler),
this,
MHD_OPTION_SOCK_ADDR, &bind_addr,
MHD_OPTION_NOTIFY_COMPLETED, reinterpret_cast<MHD_RequestCompletedCallback>(request_complete_handler), nullptr,
MHD_OPTION_END);
if(options().certificate.has_value()) {

auto key_src = options().certificate->first;
auto cert_src = options().certificate->second;

daemon_ = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_SSL,
port_, nullptr, nullptr,
reinterpret_cast<MHD_AccessHandlerCallback>(&request_handler),
this,
MHD_OPTION_SOCK_ADDR, &bind_addr,
MHD_OPTION_NOTIFY_COMPLETED,
reinterpret_cast<MHD_RequestCompletedCallback>(request_complete_handler),
nullptr,
MHD_OPTION_HTTPS_MEM_KEY, key_src.c_str(),
MHD_OPTION_HTTPS_MEM_CERT, cert_src.c_str(),
MHD_OPTION_END);

} else {
daemon_ = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION,
port_, nullptr, nullptr,
reinterpret_cast<MHD_AccessHandlerCallback>(&request_handler),
this,
MHD_OPTION_SOCK_ADDR, &bind_addr,
MHD_OPTION_NOTIFY_COMPLETED,
reinterpret_cast<MHD_RequestCompletedCallback>(request_complete_handler),
nullptr,
MHD_OPTION_END);
}

if(!daemon_)
return false;
Expand Down

0 comments on commit 49a5a99

Please sign in to comment.