Added possibility of no timeout (=0)
This commit is contained in:
parent
c12e026e0e
commit
cdf7114f2d
3 changed files with 21 additions and 9 deletions
|
|
@ -11,7 +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
|
* 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
|
* Simple way to add REST resources using regex for path, and anonymous functions
|
||||||
|
|
||||||
###Usage
|
###Usage
|
||||||
|
|
|
||||||
|
|
@ -84,11 +84,14 @@ namespace SimpleWeb {
|
||||||
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
|
//Set timeout on the following boost::asio::async-read or write function
|
||||||
auto timer=set_timeout_on_socket(socket, timeout_request);
|
std::shared_ptr<boost::asio::deadline_timer> 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",
|
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) {
|
[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) {
|
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"
|
||||||
|
|
@ -107,13 +110,16 @@ namespace SimpleWeb {
|
||||||
//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
|
//Set timeout on the following boost::asio::async-read or write function
|
||||||
auto timer=set_timeout_on_socket(socket, timeout_content);
|
std::shared_ptr<boost::asio::deadline_timer> timer;
|
||||||
|
if(timeout_content>0)
|
||||||
|
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, timer]
|
[this, socket, read_buffer, request, timer]
|
||||||
(const boost::system::error_code& ec, size_t bytes_transferred) {
|
(const boost::system::error_code& ec, size_t bytes_transferred) {
|
||||||
timer->cancel();
|
if(timeout_content>0)
|
||||||
|
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()));
|
||||||
|
|
@ -176,13 +182,16 @@ namespace SimpleWeb {
|
||||||
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
|
//Set timeout on the following boost::asio::async-read or write function
|
||||||
auto timer=set_timeout_on_socket(socket, timeout_content);
|
std::shared_ptr<boost::asio::deadline_timer> 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
|
//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, timer]
|
[this, socket, request, write_buffer, timer]
|
||||||
(const boost::system::error_code& ec, size_t bytes_transferred) {
|
(const boost::system::error_code& ec, size_t bytes_transferred) {
|
||||||
timer->cancel();
|
if(timeout_content>0)
|
||||||
|
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);
|
||||||
|
|
|
||||||
|
|
@ -32,10 +32,13 @@ namespace SimpleWeb {
|
||||||
|
|
||||||
if(!ec) {
|
if(!ec) {
|
||||||
//Set timeout on the following boost::asio::ssl::stream::async_handshake
|
//Set timeout on the following boost::asio::ssl::stream::async_handshake
|
||||||
auto timer=set_timeout_on_socket(socket, timeout_request);
|
std::shared_ptr<boost::asio::deadline_timer> 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]
|
(*socket).async_handshake(boost::asio::ssl::stream_base::server, [this, socket, timer]
|
||||||
(const boost::system::error_code& ec) {
|
(const boost::system::error_code& ec) {
|
||||||
timer->cancel();
|
if(timeout_request>0)
|
||||||
|
timer->cancel();
|
||||||
if(!ec)
|
if(!ec)
|
||||||
process_request_and_respond(socket);
|
process_request_and_respond(socket);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue