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.

This commit is contained in:
eidheim 2014-11-01 18:18:00 +01:00
commit db365340bf
3 changed files with 28 additions and 28 deletions

View file

@ -10,13 +10,23 @@ namespace SimpleWeb {
template<>
class Client<HTTPS> : public ClientBase<HTTPS> {
public:
Client(const std::string& server_port_path, bool verify_certificate=true) : ClientBase<HTTPS>::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<HTTPS>::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<HTTPS>(asio_io_service, asio_context);
};

View file

@ -87,7 +87,17 @@ namespace SimpleWeb {
virtual void accept()=0;
virtual std::shared_ptr<boost::asio::deadline_timer> set_timeout_on_socket(std::shared_ptr<socket_type> socket, size_t seconds)=0;
std::shared_ptr<boost::asio::deadline_timer> set_timeout_on_socket(std::shared_ptr<socket_type> socket, size_t seconds) {
std::shared_ptr<boost::asio::deadline_timer> 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_type> socket) {
//Create new streambuf (Request::streambuf) for async_read_until()
@ -226,18 +236,6 @@ namespace SimpleWeb {
}
});
}
std::shared_ptr<boost::asio::deadline_timer> set_timeout_on_socket(std::shared_ptr<HTTP> socket, size_t seconds) {
std::shared_ptr<boost::asio::deadline_timer> 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 */

View file

@ -11,11 +11,15 @@ namespace SimpleWeb {
class Server<HTTPS> : public ServerBase<HTTPS> {
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<HTTPS>::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<boost::asio::deadline_timer> set_timeout_on_socket(std::shared_ptr<HTTPS> socket, size_t seconds) {
std::shared_ptr<boost::asio::deadline_timer> 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;
}
};
}