Formatted the code.
This commit is contained in:
parent
22550d078f
commit
a7b7d77e1f
@ -69,7 +69,7 @@ class Connection : public std::enable_shared_from_this<Connection>
|
||||
std::vector<char> compressed_output;
|
||||
// Header compression_header;
|
||||
std::vector<boost::asio::const_buffer> output_buffer;
|
||||
//Keep alive support
|
||||
// Keep alive support
|
||||
bool keep_alive = false;
|
||||
short processed_requests = 512;
|
||||
short keepalive_timeout = 5; // In seconds
|
||||
|
@ -2,16 +2,16 @@
|
||||
#include "server/request_handler.hpp"
|
||||
#include "server/request_parser.hpp"
|
||||
|
||||
#include <boost/algorithm/string/predicate.hpp>
|
||||
#include <boost/assert.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
#include <boost/algorithm/string/predicate.hpp>
|
||||
#include <boost/iostreams/filter/gzip.hpp>
|
||||
#include <boost/iostreams/filtering_stream.hpp>
|
||||
|
||||
#include <iterator>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <util/log.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace osrm
|
||||
{
|
||||
@ -29,18 +29,16 @@ boost::asio::ip::tcp::socket &Connection::socket() { return TCP_socket; }
|
||||
void Connection::start()
|
||||
{
|
||||
TCP_socket.async_read_some(
|
||||
boost::asio::buffer(incoming_data_buffer),
|
||||
strand.wrap(boost::bind(&Connection::handle_read,
|
||||
this->shared_from_this(),
|
||||
boost::asio::placeholders::error,
|
||||
boost::asio::placeholders::bytes_transferred)));
|
||||
boost::asio::buffer(incoming_data_buffer),
|
||||
strand.wrap(boost::bind(&Connection::handle_read,
|
||||
this->shared_from_this(),
|
||||
boost::asio::placeholders::error,
|
||||
boost::asio::placeholders::bytes_transferred)));
|
||||
|
||||
//init async timer
|
||||
// init async timer
|
||||
timer.cancel();
|
||||
timer.expires_from_now(boost::posix_time::seconds(keepalive_timeout));
|
||||
timer.async_wait(boost::bind(&Connection::handle_timeout,
|
||||
this->shared_from_this()));
|
||||
|
||||
timer.async_wait(boost::bind(&Connection::handle_timeout, this->shared_from_this()));
|
||||
}
|
||||
|
||||
void Connection::handle_read(const boost::system::error_code &error, std::size_t bytes_transferred)
|
||||
@ -54,9 +52,9 @@ void Connection::handle_read(const boost::system::error_code &error, std::size_t
|
||||
http::compression_type compression_type(http::no_compression);
|
||||
RequestParser::RequestStatus result;
|
||||
std::tie(result, compression_type) =
|
||||
request_parser.parse(current_request,
|
||||
incoming_data_buffer.data(),
|
||||
incoming_data_buffer.data() + bytes_transferred);
|
||||
request_parser.parse(current_request,
|
||||
incoming_data_buffer.data(),
|
||||
incoming_data_buffer.data() + bytes_transferred);
|
||||
|
||||
// the request has been parsed
|
||||
if (result == RequestParser::RequestStatus::valid)
|
||||
@ -64,9 +62,12 @@ void Connection::handle_read(const boost::system::error_code &error, std::size_t
|
||||
current_request.endpoint = TCP_socket.remote_endpoint().address();
|
||||
request_handler.HandleRequest(current_request, current_reply);
|
||||
|
||||
if (boost::iequals(current_request.connection, "close")) {
|
||||
if (boost::iequals(current_request.connection, "close"))
|
||||
{
|
||||
current_reply.headers.emplace_back("Connection", "close");
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
keep_alive = true;
|
||||
current_reply.headers.emplace_back("Connection", "keep-alive");
|
||||
current_reply.headers.emplace_back("Keep-Alive", "timeout=5, max=512");
|
||||
@ -75,29 +76,29 @@ void Connection::handle_read(const boost::system::error_code &error, std::size_t
|
||||
// compress the result w/ gzip/deflate if requested
|
||||
switch (compression_type)
|
||||
{
|
||||
case http::deflate_rfc1951:
|
||||
// use deflate for compression
|
||||
current_reply.headers.insert(current_reply.headers.begin(),
|
||||
{"Content-Encoding", "deflate"});
|
||||
compressed_output = compress_buffers(current_reply.content, compression_type);
|
||||
current_reply.set_size(static_cast<unsigned>(compressed_output.size()));
|
||||
output_buffer = current_reply.headers_to_buffers();
|
||||
output_buffer.push_back(boost::asio::buffer(compressed_output));
|
||||
break;
|
||||
case http::gzip_rfc1952:
|
||||
// use gzip for compression
|
||||
current_reply.headers.insert(current_reply.headers.begin(),
|
||||
{"Content-Encoding", "gzip"});
|
||||
compressed_output = compress_buffers(current_reply.content, compression_type);
|
||||
current_reply.set_size(static_cast<unsigned>(compressed_output.size()));
|
||||
output_buffer = current_reply.headers_to_buffers();
|
||||
output_buffer.push_back(boost::asio::buffer(compressed_output));
|
||||
break;
|
||||
case http::no_compression:
|
||||
// don't use any compression
|
||||
current_reply.set_uncompressed_size();
|
||||
output_buffer = current_reply.to_buffers();
|
||||
break;
|
||||
case http::deflate_rfc1951:
|
||||
// use deflate for compression
|
||||
current_reply.headers.insert(current_reply.headers.begin(),
|
||||
{"Content-Encoding", "deflate"});
|
||||
compressed_output = compress_buffers(current_reply.content, compression_type);
|
||||
current_reply.set_size(static_cast<unsigned>(compressed_output.size()));
|
||||
output_buffer = current_reply.headers_to_buffers();
|
||||
output_buffer.push_back(boost::asio::buffer(compressed_output));
|
||||
break;
|
||||
case http::gzip_rfc1952:
|
||||
// use gzip for compression
|
||||
current_reply.headers.insert(current_reply.headers.begin(),
|
||||
{"Content-Encoding", "gzip"});
|
||||
compressed_output = compress_buffers(current_reply.content, compression_type);
|
||||
current_reply.set_size(static_cast<unsigned>(compressed_output.size()));
|
||||
output_buffer = current_reply.headers_to_buffers();
|
||||
output_buffer.push_back(boost::asio::buffer(compressed_output));
|
||||
break;
|
||||
case http::no_compression:
|
||||
// don't use any compression
|
||||
current_reply.set_uncompressed_size();
|
||||
output_buffer = current_reply.to_buffers();
|
||||
break;
|
||||
}
|
||||
// write result to stream
|
||||
boost::asio::async_write(TCP_socket,
|
||||
@ -120,11 +121,11 @@ void Connection::handle_read(const boost::system::error_code &error, std::size_t
|
||||
{
|
||||
// we don't have a result yet, so continue reading
|
||||
TCP_socket.async_read_some(
|
||||
boost::asio::buffer(incoming_data_buffer),
|
||||
strand.wrap(boost::bind(&Connection::handle_read,
|
||||
this->shared_from_this(),
|
||||
boost::asio::placeholders::error,
|
||||
boost::asio::placeholders::bytes_transferred)));
|
||||
boost::asio::buffer(incoming_data_buffer),
|
||||
strand.wrap(boost::bind(&Connection::handle_read,
|
||||
this->shared_from_this(),
|
||||
boost::asio::placeholders::error,
|
||||
boost::asio::placeholders::bytes_transferred)));
|
||||
}
|
||||
}
|
||||
|
||||
@ -133,12 +134,15 @@ void Connection::handle_write(const boost::system::error_code &error)
|
||||
{
|
||||
if (!error)
|
||||
{
|
||||
if (keep_alive && processed_requests > 0) {
|
||||
if (keep_alive && processed_requests > 0)
|
||||
{
|
||||
--processed_requests;
|
||||
current_request = http::request();
|
||||
request_parser = RequestParser();
|
||||
this->start();
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
// Initiate graceful connection closure.
|
||||
handle_timeout();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user