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
|
||||||
{
|
{
|
||||||
@ -26,12 +28,12 @@ boost::asio::ip::tcp::socket &Connection::socket() { return TCP_socket; }
|
|||||||
/// Start the first asynchronous operation for the connection.
|
/// Start the first asynchronous operation for the connection.
|
||||||
void Connection::start()
|
void Connection::start()
|
||||||
{
|
{
|
||||||
TCP_socket.async_read_some(
|
TCP_socket.async_read_some(
|
||||||
boost::asio::buffer(incoming_data_buffer),
|
boost::asio::buffer(incoming_data_buffer),
|
||||||
strand.wrap(boost::bind(&Connection::handle_read,
|
strand.wrap(boost::bind(&Connection::handle_read,
|
||||||
this->shared_from_this(),
|
this->shared_from_this(),
|
||||||
boost::asio::placeholders::error,
|
boost::asio::placeholders::error,
|
||||||
boost::asio::placeholders::bytes_transferred)));
|
boost::asio::placeholders::bytes_transferred)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Connection::handle_read(const boost::system::error_code &error, std::size_t bytes_transferred)
|
void Connection::handle_read(const boost::system::error_code &error, std::size_t bytes_transferred)
|
||||||
@ -45,9 +47,9 @@ void Connection::handle_read(const boost::system::error_code &error, std::size_t
|
|||||||
http::compression_type compression_type(http::no_compression);
|
http::compression_type compression_type(http::no_compression);
|
||||||
RequestParser::RequestStatus result;
|
RequestParser::RequestStatus result;
|
||||||
std::tie(result, compression_type) =
|
std::tie(result, compression_type) =
|
||||||
request_parser.parse(current_request,
|
request_parser.parse(current_request,
|
||||||
incoming_data_buffer.data(),
|
incoming_data_buffer.data(),
|
||||||
incoming_data_buffer.data() + bytes_transferred);
|
incoming_data_buffer.data() + bytes_transferred);
|
||||||
|
|
||||||
// the request has been parsed
|
// the request has been parsed
|
||||||
if (result == RequestParser::RequestStatus::valid)
|
if (result == RequestParser::RequestStatus::valid)
|
||||||
@ -55,32 +57,40 @@ 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)
|
||||||
{
|
{
|
||||||
case http::deflate_rfc1951:
|
case http::deflate_rfc1951:
|
||||||
// use deflate for compression
|
// use deflate for compression
|
||||||
current_reply.headers.insert(current_reply.headers.begin(),
|
current_reply.headers.insert(current_reply.headers.begin(),
|
||||||
{"Content-Encoding", "deflate"});
|
{"Content-Encoding", "deflate"});
|
||||||
compressed_output = compress_buffers(current_reply.content, compression_type);
|
compressed_output = compress_buffers(current_reply.content, compression_type);
|
||||||
current_reply.set_size(static_cast<unsigned>(compressed_output.size()));
|
current_reply.set_size(static_cast<unsigned>(compressed_output.size()));
|
||||||
output_buffer = current_reply.headers_to_buffers();
|
output_buffer = current_reply.headers_to_buffers();
|
||||||
output_buffer.push_back(boost::asio::buffer(compressed_output));
|
output_buffer.push_back(boost::asio::buffer(compressed_output));
|
||||||
break;
|
break;
|
||||||
case http::gzip_rfc1952:
|
case http::gzip_rfc1952:
|
||||||
// use gzip for compression
|
// use gzip for compression
|
||||||
current_reply.headers.insert(current_reply.headers.begin(),
|
current_reply.headers.insert(current_reply.headers.begin(),
|
||||||
{"Content-Encoding", "gzip"});
|
{"Content-Encoding", "gzip"});
|
||||||
compressed_output = compress_buffers(current_reply.content, compression_type);
|
compressed_output = compress_buffers(current_reply.content, compression_type);
|
||||||
current_reply.set_size(static_cast<unsigned>(compressed_output.size()));
|
current_reply.set_size(static_cast<unsigned>(compressed_output.size()));
|
||||||
output_buffer = current_reply.headers_to_buffers();
|
output_buffer = current_reply.headers_to_buffers();
|
||||||
output_buffer.push_back(boost::asio::buffer(compressed_output));
|
output_buffer.push_back(boost::asio::buffer(compressed_output));
|
||||||
break;
|
break;
|
||||||
case http::no_compression:
|
case http::no_compression:
|
||||||
// don't use any compression
|
// don't use any compression
|
||||||
current_reply.set_uncompressed_size();
|
current_reply.set_uncompressed_size();
|
||||||
output_buffer = current_reply.to_buffers();
|
output_buffer = current_reply.to_buffers();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// write result to stream
|
// write result to stream
|
||||||
boost::asio::async_write(TCP_socket,
|
boost::asio::async_write(TCP_socket,
|
||||||
@ -103,11 +113,11 @@ void Connection::handle_read(const boost::system::error_code &error, std::size_t
|
|||||||
{
|
{
|
||||||
// we don't have a result yet, so continue reading
|
// we don't have a result yet, so continue reading
|
||||||
TCP_socket.async_read_some(
|
TCP_socket.async_read_some(
|
||||||
boost::asio::buffer(incoming_data_buffer),
|
boost::asio::buffer(incoming_data_buffer),
|
||||||
strand.wrap(boost::bind(&Connection::handle_read,
|
strand.wrap(boost::bind(&Connection::handle_read,
|
||||||
this->shared_from_this(),
|
this->shared_from_this(),
|
||||||
boost::asio::placeholders::error,
|
boost::asio::placeholders::error,
|
||||||
boost::asio::placeholders::bytes_transferred)));
|
boost::asio::placeholders::bytes_transferred)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,9 +126,16 @@ void Connection::handle_write(const boost::system::error_code &error)
|
|||||||
{
|
{
|
||||||
if (!error)
|
if (!error)
|
||||||
{
|
{
|
||||||
// Initiate graceful connection closure.
|
if (keep_alive && processed_requests > 0) {
|
||||||
boost::system::error_code ignore_error;
|
--processed_requests;
|
||||||
TCP_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignore_error);
|
current_request = http::request();
|
||||||
|
request_parser = RequestParser();
|
||||||
|
this->start();
|
||||||
|
} else {
|
||||||
|
// Initiate graceful connection closure.
|
||||||
|
boost::system::error_code ignore_error;
|
||||||
|
TCP_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignore_error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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