#ifndef SERVER_HTTPS_HPP #define SERVER_HTTPS_HPP #include "server_http.hpp" #include namespace SimpleWeb { typedef boost::asio::ssl::stream HTTPS; template<> 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) : 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); } private: boost::asio::ssl::context context; void accept() { //Create new socket for this connection //Shared_ptr is used to pass temporary objects to the asynchronous functions std::shared_ptr socket(new HTTPS(m_io_service, context)); acceptor.async_accept((*socket).lowest_layer(), [this, socket](const boost::system::error_code& ec) { //Immediately start accepting a new connection accept(); if(!ec) { //Set timeout on the following boost::asio::ssl::stream::async_handshake std::shared_ptr timer; if(timeout_request>0) timer=set_timeout_on_socket(socket, timeout_request); (*socket).async_handshake(boost::asio::ssl::stream_base::server, [this, socket, timer] (const boost::system::error_code& ec) { if(timeout_request>0) timer->cancel(); if(!ec) process_request_and_respond(socket); }); } }); } 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; } }; } #endif /* SERVER_HTTPS_HPP */