Skip to content
This repository has been archived by the owner on Jun 12, 2018. It is now read-only.

Commit

Permalink
Added option for verify_file in Server-constructor, and certification…
Browse files Browse the repository at this point in the history
…/key file and verify_file for Client-constructor (Warning: not tested). Also moved set_timeout_on_socket to the ServerBase.
  • Loading branch information
eidheim committed Nov 1, 2014
1 parent 8bd90d2 commit db36534
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
14 changes: 12 additions & 2 deletions client_https.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down
24 changes: 11 additions & 13 deletions server_http.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 */
18 changes: 5 additions & 13 deletions server_https.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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;
}
};
}

Expand Down

0 comments on commit db36534

Please sign in to comment.