From cdf7114f2d8ff3ebf083aafc631bfe3710c45adc Mon Sep 17 00:00:00 2001 From: eidheim Date: Tue, 5 Aug 2014 17:22:55 +0200 Subject: [PATCH] Added possibility of no timeout (=0) --- README.md | 2 +- server_http.hpp | 21 +++++++++++++++------ server_https.hpp | 7 +++++-- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 883dc7a..f0eafba 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ See also https://github.com/eidheim/Simple-WebSocket-Server for an easy way to m * Platform independent * HTTPS support * HTTP persistent connection (for HTTP/1.1) -* Server timeouts +* Timeouts, if any of Server::timeout_request and Server::timeout_content are >0 (default: Server::timeout_request=5 seconds, and Server::timeout_content=300 seconds) * Simple way to add REST resources using regex for path, and anonymous functions ###Usage diff --git a/server_http.hpp b/server_http.hpp index 1a809ec..9fba048 100644 --- a/server_http.hpp +++ b/server_http.hpp @@ -84,11 +84,14 @@ namespace SimpleWeb { std::shared_ptr 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); + std::shared_ptr timer; + if(timeout_request>0) + timer=set_timeout_on_socket(socket, timeout_request); boost::asio::async_read_until(*socket, *read_buffer, "\r\n\r\n", [this, socket, read_buffer, timer](const boost::system::error_code& ec, size_t bytes_transferred) { - timer->cancel(); + if(timeout_request>0) + timer->cancel(); if(!ec) { //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" @@ -107,13 +110,16 @@ namespace SimpleWeb { //If content, read that as well 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); + std::shared_ptr timer; + if(timeout_content>0) + timer=set_timeout_on_socket(socket, timeout_content); boost::asio::async_read(*socket, *read_buffer, boost::asio::transfer_exactly(stoull(request->header["Content-Length"])-num_additional_bytes), [this, socket, read_buffer, request, timer] (const boost::system::error_code& ec, size_t bytes_transferred) { - timer->cancel(); + if(timeout_content>0) + timer->cancel(); if(!ec) { //Store pointer to read_buffer as istream object request->content=std::shared_ptr(new std::istream(read_buffer.get())); @@ -176,13 +182,16 @@ namespace SimpleWeb { 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); + std::shared_ptr timer; + if(timeout_content>0) + timer=set_timeout_on_socket(socket, timeout_content); //Capture write_buffer in lambda so it is not destroyed before async_write is finished boost::asio::async_write(*socket, *write_buffer, [this, socket, request, write_buffer, timer] (const boost::system::error_code& ec, size_t bytes_transferred) { - timer->cancel(); + if(timeout_content>0) + timer->cancel(); //HTTP persistent connection (HTTP 1.1): if(!ec && stof(request->http_version)>1.05) process_request_and_respond(socket); diff --git a/server_https.hpp b/server_https.hpp index 70502ac..4aebff5 100644 --- a/server_https.hpp +++ b/server_https.hpp @@ -32,10 +32,13 @@ namespace SimpleWeb { if(!ec) { //Set timeout on the following boost::asio::ssl::stream::async_handshake - auto timer=set_timeout_on_socket(socket, timeout_request); + 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) { - timer->cancel(); + if(timeout_request>0) + timer->cancel(); if(!ec) process_request_and_respond(socket); });