Added keep-alive support to the http server.
This commit is contained in:
parent
92c7b6fbd1
commit
a0582a3e68
@ -65,6 +65,9 @@ class Connection : public std::enable_shared_from_this<Connection>
|
|||||||
std::vector<char> compressed_output;
|
std::vector<char> compressed_output;
|
||||||
// Header compression_header;
|
// Header compression_header;
|
||||||
std::vector<boost::asio::const_buffer> output_buffer;
|
std::vector<boost::asio::const_buffer> output_buffer;
|
||||||
|
//Keep alive support
|
||||||
|
bool keep_alive = false;
|
||||||
|
short processed_requests = 512;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,12 +4,14 @@
|
|||||||
|
|
||||||
#include <boost/assert.hpp>
|
#include <boost/assert.hpp>
|
||||||
#include <boost/bind.hpp>
|
#include <boost/bind.hpp>
|
||||||
|
#include <boost/algorithm/string/predicate.hpp>
|
||||||
#include <boost/iostreams/filter/gzip.hpp>
|
#include <boost/iostreams/filter/gzip.hpp>
|
||||||
#include <boost/iostreams/filtering_stream.hpp>
|
#include <boost/iostreams/filtering_stream.hpp>
|
||||||
|
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <util/log.hpp>
|
||||||
|
|
||||||
namespace osrm
|
namespace osrm
|
||||||
{
|
{
|
||||||
@ -55,6 +57,14 @@ void Connection::handle_read(const boost::system::error_code &error, std::size_t
|
|||||||
current_request.endpoint = TCP_socket.remote_endpoint().address();
|
current_request.endpoint = TCP_socket.remote_endpoint().address();
|
||||||
request_handler.HandleRequest(current_request, current_reply);
|
request_handler.HandleRequest(current_request, current_reply);
|
||||||
|
|
||||||
|
if (boost::iequals(current_request.connection, "close")) {
|
||||||
|
current_reply.headers.emplace_back("Connection", "close");
|
||||||
|
} else {
|
||||||
|
keep_alive = true;
|
||||||
|
current_reply.headers.emplace_back("Connection", "keep-alive");
|
||||||
|
current_reply.headers.emplace_back("Keep-Alive", "timeout=5, max=512");
|
||||||
|
}
|
||||||
|
|
||||||
// compress the result w/ gzip/deflate if requested
|
// compress the result w/ gzip/deflate if requested
|
||||||
switch (compression_type)
|
switch (compression_type)
|
||||||
{
|
{
|
||||||
@ -116,11 +126,18 @@ void Connection::handle_write(const boost::system::error_code &error)
|
|||||||
{
|
{
|
||||||
if (!error)
|
if (!error)
|
||||||
{
|
{
|
||||||
|
if (keep_alive && processed_requests > 0) {
|
||||||
|
--processed_requests;
|
||||||
|
current_request = http::request();
|
||||||
|
request_parser = RequestParser();
|
||||||
|
this->start();
|
||||||
|
} else {
|
||||||
// Initiate graceful connection closure.
|
// Initiate graceful connection closure.
|
||||||
boost::system::error_code ignore_error;
|
boost::system::error_code ignore_error;
|
||||||
TCP_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignore_error);
|
TCP_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignore_error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<char> Connection::compress_buffers(const std::vector<char> &uncompressed_data,
|
std::vector<char> Connection::compress_buffers(const std::vector<char> &uncompressed_data,
|
||||||
const http::compression_type compression_type)
|
const http::compression_type compression_type)
|
||||||
|
@ -103,11 +103,7 @@ boost::asio::const_buffer reply::status_to_buffer(const reply::status_type statu
|
|||||||
return boost::asio::buffer(http_bad_request_string);
|
return boost::asio::buffer(http_bad_request_string);
|
||||||
}
|
}
|
||||||
|
|
||||||
reply::reply() : status(ok)
|
reply::reply() : status(ok) {}
|
||||||
{
|
|
||||||
// We do not currently support keep alive. Always set 'Connection: close'.
|
|
||||||
headers.emplace_back("Connection", "close");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user