From db365340bf54e708fd2a0568ff0bd2e6731bc010 Mon Sep 17 00:00:00 2001 From: eidheim Date: Sat, 1 Nov 2014 18:18:00 +0100 Subject: [PATCH] Added option for verify_file in Server-constructor, and certification/key file and verify_file for Client-constructor (Warning: not tested). Also moved set_timeout_on_socket to the ServerBase. --- client_https.hpp | 14 ++++++++++++-- server_http.hpp | 24 +++++++++++------------- server_https.hpp | 18 +++++------------- 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/client_https.hpp b/client_https.hpp index 522d396..2099416 100644 --- a/client_https.hpp +++ b/client_https.hpp @@ -10,13 +10,23 @@ namespace SimpleWeb { template<> class Client : public ClientBase { public: - Client(const std::string& server_port_path, bool verify_certificate=true) : ClientBase::ClientBase(server_port_path, 443), - asio_context(boost::asio::ssl::context::sslv23) { + Client(const std::string& server_port_path, bool verify_certificate=true, + const std::string& cert_file=std::string(), const std::string& private_key_file=std::string(), + const std::string& verify_file=std::string()) : + ClientBase::ClientBase(server_port_path, 443), asio_context(boost::asio::ssl::context::sslv23) { if(verify_certificate) asio_context.set_verify_mode(boost::asio::ssl::verify_peer); else asio_context.set_verify_mode(boost::asio::ssl::verify_none); + if(cert_file.size()>0 && private_key_file.size()>0) { + asio_context.use_certificate_chain_file(cert_file); + asio_context.use_private_key_file(private_key_file, boost::asio::ssl::context::pem); + } + + if(verify_file.size()>0) + asio_context.load_verify_file(verify_file); + socket=std::make_shared(asio_io_service, asio_context); }; diff --git a/server_http.hpp b/server_http.hpp index fa160eb..5168784 100644 --- a/server_http.hpp +++ b/server_http.hpp @@ -87,7 +87,17 @@ namespace SimpleWeb { virtual void accept()=0; - virtual std::shared_ptr set_timeout_on_socket(std::shared_ptr socket, size_t seconds)=0; + std::shared_ptr set_timeout_on_socket(std::shared_ptr socket, size_t seconds) { + std::shared_ptr timer(new boost::asio::deadline_timer(m_io_service)); + timer->expires_from_now(boost::posix_time::seconds(seconds)); + timer->async_wait([socket](const boost::system::error_code& ec){ + if(!ec) { + socket->lowest_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both); + socket->lowest_layer().close(); + } + }); + return timer; + } void read_request_and_content(std::shared_ptr socket) { //Create new streambuf (Request::streambuf) for async_read_until() @@ -226,18 +236,6 @@ namespace SimpleWeb { } }); } - - std::shared_ptr set_timeout_on_socket(std::shared_ptr socket, size_t seconds) { - std::shared_ptr timer(new boost::asio::deadline_timer(m_io_service)); - timer->expires_from_now(boost::posix_time::seconds(seconds)); - timer->async_wait([socket](const boost::system::error_code& ec){ - if(!ec) { - socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both); - socket->close(); - } - }); - return timer; - } }; } #endif /* SERVER_HTTP_HPP */ \ No newline at end of file diff --git a/server_https.hpp b/server_https.hpp index faf2fcb..a3fd67e 100644 --- a/server_https.hpp +++ b/server_https.hpp @@ -11,11 +11,15 @@ namespace SimpleWeb { class Server : public ServerBase { public: Server(unsigned short port, size_t num_threads, const std::string& cert_file, const std::string& private_key_file, - size_t timeout_request=5, size_t timeout_content=300) : + size_t timeout_request=5, size_t timeout_content=300, + const std::string& verify_file=std::string()) : ServerBase::ServerBase(port, num_threads, timeout_request, timeout_content), context(boost::asio::ssl::context::sslv23) { context.use_certificate_chain_file(cert_file); context.use_private_key_file(private_key_file, boost::asio::ssl::context::pem); + + if(verify_file.size()>0) + context.load_verify_file(verify_file); } private: @@ -45,18 +49,6 @@ namespace SimpleWeb { } }); } - - std::shared_ptr set_timeout_on_socket(std::shared_ptr socket, size_t seconds) { - std::shared_ptr timer(new boost::asio::deadline_timer(m_io_service)); - timer->expires_from_now(boost::posix_time::seconds(seconds)); - timer->async_wait([socket](const boost::system::error_code& ec){ - if(!ec) { - socket->lowest_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both); - socket->lowest_layer().close(); - } - }); - return timer; - } }; }