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

Commit db36534

Browse files
committed
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.
1 parent 8bd90d2 commit db36534

3 files changed

Lines changed: 28 additions & 28 deletions

File tree

client_https.hpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,23 @@ namespace SimpleWeb {
1010
template<>
1111
class Client<HTTPS> : public ClientBase<HTTPS> {
1212
public:
13-
Client(const std::string& server_port_path, bool verify_certificate=true) : ClientBase<HTTPS>::ClientBase(server_port_path, 443),
14-
asio_context(boost::asio::ssl::context::sslv23) {
13+
Client(const std::string& server_port_path, bool verify_certificate=true,
14+
const std::string& cert_file=std::string(), const std::string& private_key_file=std::string(),
15+
const std::string& verify_file=std::string()) :
16+
ClientBase<HTTPS>::ClientBase(server_port_path, 443), asio_context(boost::asio::ssl::context::sslv23) {
1517
if(verify_certificate)
1618
asio_context.set_verify_mode(boost::asio::ssl::verify_peer);
1719
else
1820
asio_context.set_verify_mode(boost::asio::ssl::verify_none);
1921

22+
if(cert_file.size()>0 && private_key_file.size()>0) {
23+
asio_context.use_certificate_chain_file(cert_file);
24+
asio_context.use_private_key_file(private_key_file, boost::asio::ssl::context::pem);
25+
}
26+
27+
if(verify_file.size()>0)
28+
asio_context.load_verify_file(verify_file);
29+
2030
socket=std::make_shared<HTTPS>(asio_io_service, asio_context);
2131
};
2232

server_http.hpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,17 @@ namespace SimpleWeb {
8787

8888
virtual void accept()=0;
8989

90-
virtual std::shared_ptr<boost::asio::deadline_timer> set_timeout_on_socket(std::shared_ptr<socket_type> socket, size_t seconds)=0;
90+
std::shared_ptr<boost::asio::deadline_timer> set_timeout_on_socket(std::shared_ptr<socket_type> socket, size_t seconds) {
91+
std::shared_ptr<boost::asio::deadline_timer> timer(new boost::asio::deadline_timer(m_io_service));
92+
timer->expires_from_now(boost::posix_time::seconds(seconds));
93+
timer->async_wait([socket](const boost::system::error_code& ec){
94+
if(!ec) {
95+
socket->lowest_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both);
96+
socket->lowest_layer().close();
97+
}
98+
});
99+
return timer;
100+
}
91101

92102
void read_request_and_content(std::shared_ptr<socket_type> socket) {
93103
//Create new streambuf (Request::streambuf) for async_read_until()
@@ -226,18 +236,6 @@ namespace SimpleWeb {
226236
}
227237
});
228238
}
229-
230-
std::shared_ptr<boost::asio::deadline_timer> set_timeout_on_socket(std::shared_ptr<HTTP> socket, size_t seconds) {
231-
std::shared_ptr<boost::asio::deadline_timer> timer(new boost::asio::deadline_timer(m_io_service));
232-
timer->expires_from_now(boost::posix_time::seconds(seconds));
233-
timer->async_wait([socket](const boost::system::error_code& ec){
234-
if(!ec) {
235-
socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both);
236-
socket->close();
237-
}
238-
});
239-
return timer;
240-
}
241239
};
242240
}
243241
#endif /* SERVER_HTTP_HPP */

server_https.hpp

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@ namespace SimpleWeb {
1111
class Server<HTTPS> : public ServerBase<HTTPS> {
1212
public:
1313
Server(unsigned short port, size_t num_threads, const std::string& cert_file, const std::string& private_key_file,
14-
size_t timeout_request=5, size_t timeout_content=300) :
14+
size_t timeout_request=5, size_t timeout_content=300,
15+
const std::string& verify_file=std::string()) :
1516
ServerBase<HTTPS>::ServerBase(port, num_threads, timeout_request, timeout_content),
1617
context(boost::asio::ssl::context::sslv23) {
1718
context.use_certificate_chain_file(cert_file);
1819
context.use_private_key_file(private_key_file, boost::asio::ssl::context::pem);
20+
21+
if(verify_file.size()>0)
22+
context.load_verify_file(verify_file);
1923
}
2024

2125
private:
@@ -45,18 +49,6 @@ namespace SimpleWeb {
4549
}
4650
});
4751
}
48-
49-
std::shared_ptr<boost::asio::deadline_timer> set_timeout_on_socket(std::shared_ptr<HTTPS> socket, size_t seconds) {
50-
std::shared_ptr<boost::asio::deadline_timer> timer(new boost::asio::deadline_timer(m_io_service));
51-
timer->expires_from_now(boost::posix_time::seconds(seconds));
52-
timer->async_wait([socket](const boost::system::error_code& ec){
53-
if(!ec) {
54-
socket->lowest_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both);
55-
socket->lowest_layer().close();
56-
}
57-
});
58-
return timer;
59-
}
6052
};
6153
}
6254

0 commit comments

Comments
 (0)