Added timeouts

This commit is contained in:
eidheim 2014-08-04 23:12:23 +02:00
commit b1a91fb923
3 changed files with 70 additions and 19 deletions

View file

@ -11,6 +11,7 @@ See also https://github.com/eidheim/Simple-WebSocket-Server for an easy way to m
* Platform independent * Platform independent
* HTTPS support * HTTPS support
* HTTP persistent connection (for HTTP/1.1) * HTTP persistent connection (for HTTP/1.1)
* Server timeouts
* Simple way to add REST resources using regex for path, and anonymous functions * Simple way to add REST resources using regex for path, and anonymous functions
###Usage ###Usage

View file

@ -28,9 +28,6 @@ namespace SimpleWeb {
resource_type default_resource; resource_type default_resource;
ServerBase(unsigned short port, size_t num_threads=1) : endpoint(boost::asio::ip::tcp::v4(), port),
acceptor(m_io_service, endpoint), num_threads(num_threads) {}
void start() { void start() {
//All resources with default_resource at the end of vector //All resources with default_resource at the end of vector
//Used in the respond-method //Used in the respond-method
@ -65,20 +62,33 @@ namespace SimpleWeb {
boost::asio::ip::tcp::acceptor acceptor; boost::asio::ip::tcp::acceptor acceptor;
size_t num_threads; size_t num_threads;
std::vector<std::thread> threads; std::vector<std::thread> threads;
size_t timeout_request;
size_t timeout_content;
//All resources with default_resource at the end of vector //All resources with default_resource at the end of vector
//Created in start() //Created in start()
std::vector<resource_type::iterator> all_resources; std::vector<resource_type::iterator> all_resources;
virtual void accept() {} ServerBase(unsigned short port, size_t num_threads, size_t timeout_request, size_t timeout_send_or_receive) : endpoint(boost::asio::ip::tcp::v4(), port),
acceptor(m_io_service, endpoint), num_threads(num_threads), timeout_request(timeout_request),
void process_request_and_respond(std::shared_ptr<socket_type> socket) const { timeout_content(timeout_send_or_receive) {}
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;
void process_request_and_respond(std::shared_ptr<socket_type> socket) {
//Create new read_buffer for async_read_until() //Create new read_buffer for async_read_until()
//Shared_ptr is used to pass temporary objects to the asynchronous functions //Shared_ptr is used to pass temporary objects to the asynchronous functions
std::shared_ptr<boost::asio::streambuf> read_buffer(new boost::asio::streambuf); std::shared_ptr<boost::asio::streambuf> read_buffer(new boost::asio::streambuf);
//Set timeout on the following boost::asio::async-read or write function
auto timer=set_timeout_on_socket(socket, timeout_request);
boost::asio::async_read_until(*socket, *read_buffer, "\r\n\r\n", boost::asio::async_read_until(*socket, *read_buffer, "\r\n\r\n",
[this, socket, read_buffer](const boost::system::error_code& ec, size_t bytes_transferred) { [this, socket, read_buffer, timer](const boost::system::error_code& ec, size_t bytes_transferred) {
timer->cancel();
if(!ec) { if(!ec) {
//read_buffer->size() is not necessarily the same as bytes_transferred, from Boost-docs: //read_buffer->size() is not necessarily the same as bytes_transferred, from Boost-docs:
//"After a successful async_read_until operation, the streambuf may contain additional data beyond the delimiter" //"After a successful async_read_until operation, the streambuf may contain additional data beyond the delimiter"
@ -88,17 +98,22 @@ namespace SimpleWeb {
//Convert to istream to extract string-lines //Convert to istream to extract string-lines
std::istream stream(read_buffer.get()); std::istream stream(read_buffer.get());
std::shared_ptr<Request> request(new Request()); std::shared_ptr<Request> request(new Request());
*request=parse_request(stream); *request=parse_request(stream);
size_t num_additional_bytes=total-bytes_transferred; size_t num_additional_bytes=total-bytes_transferred;
//If content, read that as well //If content, read that as well
if(request->header.count("Content-Length")>0) { if(request->header.count("Content-Length")>0) {
//Set timeout on the following boost::asio::async-read or write function
auto timer=set_timeout_on_socket(socket, timeout_content);
boost::asio::async_read(*socket, *read_buffer, boost::asio::async_read(*socket, *read_buffer,
boost::asio::transfer_exactly(stoull(request->header["Content-Length"])-num_additional_bytes), boost::asio::transfer_exactly(stoull(request->header["Content-Length"])-num_additional_bytes),
[this, socket, read_buffer, request](const boost::system::error_code& ec, size_t bytes_transferred) { [this, socket, read_buffer, request, timer](const boost::system::error_code& ec,
size_t bytes_transferred) {
timer->cancel();
if(!ec) { if(!ec) {
//Store pointer to read_buffer as istream object //Store pointer to read_buffer as istream object
request->content=std::shared_ptr<std::istream>(new std::istream(read_buffer.get())); request->content=std::shared_ptr<std::istream>(new std::istream(read_buffer.get()));
@ -147,7 +162,7 @@ namespace SimpleWeb {
return request; return request;
} }
void respond(std::shared_ptr<socket_type> socket, std::shared_ptr<Request> request) const { void respond(std::shared_ptr<socket_type> socket, std::shared_ptr<Request> request) {
//Find path- and method-match, and generate response //Find path- and method-match, and generate response
for(auto res_it: all_resources) { for(auto res_it: all_resources) {
std::regex e(res_it->first); std::regex e(res_it->first);
@ -160,9 +175,14 @@ namespace SimpleWeb {
std::ostream response(write_buffer.get()); std::ostream response(write_buffer.get());
res_it->second[request->method](response, *request); res_it->second[request->method](response, *request);
//Set timeout on the following boost::asio::async-read or write function
auto timer=set_timeout_on_socket(socket, timeout_content);
//Capture write_buffer in lambda so it is not destroyed before async_write is finished //Capture write_buffer in lambda so it is not destroyed before async_write is finished
boost::asio::async_write(*socket, *write_buffer, boost::asio::async_write(*socket, *write_buffer,
[this, socket, request, write_buffer](const boost::system::error_code& ec, size_t bytes_transferred) { [this, socket, request, write_buffer, timer](const boost::system::error_code& ec,
size_t bytes_transferred) {
timer->cancel();
//HTTP persistent connection (HTTP 1.1): //HTTP persistent connection (HTTP 1.1):
if(!ec && stof(request->http_version)>1.05) if(!ec && stof(request->http_version)>1.05)
process_request_and_respond(socket); process_request_and_respond(socket);
@ -182,7 +202,8 @@ namespace SimpleWeb {
template<> template<>
class Server<HTTP> : public ServerBase<HTTP> { class Server<HTTP> : public ServerBase<HTTP> {
public: public:
Server(unsigned short port, size_t num_threads=1) : ServerBase<HTTP>::ServerBase(port, num_threads) {}; Server(unsigned short port, size_t num_threads=1, size_t timeout_request=5, size_t timeout_content=300) :
ServerBase<HTTP>::ServerBase(port, num_threads, timeout_request, timeout_content) {};
private: private:
void accept() { void accept() {
@ -199,6 +220,18 @@ 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> timeout(new boost::asio::deadline_timer(m_io_service));
timeout->expires_from_now(boost::posix_time::seconds(seconds));
timeout->async_wait([socket](const boost::system::error_code& ec){
if(!ec) {
socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both);
socket->close();
}
});
return timeout;
}
}; };
} }
#endif /* SERVER_HTTP_HPP */ #endif /* SERVER_HTTP_HPP */

View file

@ -10,8 +10,10 @@ namespace SimpleWeb {
template<> template<>
class Server<HTTPS> : public ServerBase<HTTPS> { class Server<HTTPS> : public ServerBase<HTTPS> {
public: public:
Server(unsigned short port, size_t num_threads, const std::string& cert_file, const std::string& private_key_file) : Server(unsigned short port, size_t num_threads, const std::string& cert_file, const std::string& private_key_file,
ServerBase<HTTPS>::ServerBase(port, num_threads), context(boost::asio::ssl::context::sslv23) { size_t timeout_request=5, size_t timeout_content=300) :
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_certificate_chain_file(cert_file);
context.use_private_key_file(private_key_file, boost::asio::ssl::context::pem); context.use_private_key_file(private_key_file, boost::asio::ssl::context::pem);
} }
@ -29,14 +31,29 @@ namespace SimpleWeb {
accept(); accept();
if(!ec) { if(!ec) {
(*socket).async_handshake(boost::asio::ssl::stream_base::server, [this, socket](const boost::system::error_code& ec) { //Set timeout on the following boost::asio::ssl::stream::async_handshake
if(!ec) { auto 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) {
timer->cancel();
if(!ec)
process_request_and_respond(socket); process_request_and_respond(socket);
}
}); });
} }
}); });
} }
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> timeout(new boost::asio::deadline_timer(m_io_service));
timeout->expires_from_now(boost::posix_time::seconds(seconds));
timeout->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 timeout;
}
}; };
} }