reduce some code duplication
This commit is contained in:
parent
f5b079b8e7
commit
47ab0cbf62
@ -63,92 +63,85 @@ void Connection::start()
|
|||||||
|
|
||||||
void Connection::handle_read(const boost::system::error_code &e, std::size_t bytes_transferred)
|
void Connection::handle_read(const boost::system::error_code &e, std::size_t bytes_transferred)
|
||||||
{
|
{
|
||||||
if (!e)
|
if (e)
|
||||||
{
|
{
|
||||||
// no error detected, let's parse the request
|
return;
|
||||||
CompressionType compression_type(noCompression);
|
}
|
||||||
boost::tribool result;
|
|
||||||
boost::tie(result, boost::tuples::ignore) =
|
|
||||||
request_parser->Parse(request,
|
|
||||||
incoming_data_buffer.data(),
|
|
||||||
incoming_data_buffer.data() + bytes_transferred,
|
|
||||||
&compression_type);
|
|
||||||
|
|
||||||
// the request has been parsed
|
// no error detected, let's parse the request
|
||||||
if (result)
|
CompressionType compression_type(noCompression);
|
||||||
|
boost::tribool result;
|
||||||
|
boost::tie(result, boost::tuples::ignore) =
|
||||||
|
request_parser->Parse(request,
|
||||||
|
incoming_data_buffer.data(),
|
||||||
|
incoming_data_buffer.data() + bytes_transferred,
|
||||||
|
&compression_type);
|
||||||
|
|
||||||
|
// the request has been parsed
|
||||||
|
if (result)
|
||||||
|
{
|
||||||
|
request.endpoint = TCP_socket.remote_endpoint().address();
|
||||||
|
request_handler.handle_request(request, reply);
|
||||||
|
|
||||||
|
Header compression_header;
|
||||||
|
std::vector<char> compressed_output;
|
||||||
|
std::vector<boost::asio::const_buffer> output_buffer;
|
||||||
|
|
||||||
|
// compress the result w/ gzip/deflate if requested
|
||||||
|
switch (compression_type)
|
||||||
{
|
{
|
||||||
request.endpoint = TCP_socket.remote_endpoint().address();
|
case deflateRFC1951:
|
||||||
request_handler.handle_request(request, reply);
|
//use deflate for compression
|
||||||
|
compression_header.name = "Content-Encoding";
|
||||||
Header compression_header;
|
compression_header.value = "deflate";
|
||||||
std::vector<char> compressed_output;
|
reply.headers.insert(reply.headers.begin(), compression_header);
|
||||||
std::vector<boost::asio::const_buffer> output_buffer;
|
compressBufferCollection(reply.content, compression_type, compressed_output);
|
||||||
|
reply.setSize(compressed_output.size());
|
||||||
// compress the result w/ gzip/deflate if requested
|
output_buffer = reply.HeaderstoBuffers();
|
||||||
switch (compression_type)
|
output_buffer.push_back(boost::asio::buffer(compressed_output));
|
||||||
{
|
break;
|
||||||
case deflateRFC1951:
|
case gzipRFC1952:
|
||||||
compression_header.name = "Content-Encoding";
|
//use gzip for compression
|
||||||
compression_header.value = "deflate";
|
compression_header.name = "Content-Encoding";
|
||||||
reply.headers.insert(reply.headers.begin(), compression_header);
|
compression_header.value = "gzip";
|
||||||
compressBufferCollection(reply.content, compression_type, compressed_output);
|
reply.headers.insert(reply.headers.begin(), compression_header);
|
||||||
reply.setSize(compressed_output.size());
|
compressBufferCollection(reply.content, compression_type, compressed_output);
|
||||||
output_buffer = reply.HeaderstoBuffers();
|
reply.setSize(compressed_output.size());
|
||||||
output_buffer.push_back(boost::asio::buffer(compressed_output));
|
output_buffer = reply.HeaderstoBuffers();
|
||||||
boost::asio::async_write(
|
output_buffer.push_back(boost::asio::buffer(compressed_output));
|
||||||
TCP_socket,
|
break;
|
||||||
output_buffer,
|
case noCompression:
|
||||||
strand.wrap(boost::bind(&Connection::handle_write,
|
//don't use any compression
|
||||||
this->shared_from_this(),
|
reply.SetUncompressedSize();
|
||||||
boost::asio::placeholders::error)));
|
output_buffer = reply.toBuffers();
|
||||||
break;
|
break;
|
||||||
case gzipRFC1952:
|
|
||||||
compression_header.name = "Content-Encoding";
|
|
||||||
compression_header.value = "gzip";
|
|
||||||
reply.headers.insert(reply.headers.begin(), compression_header);
|
|
||||||
compressBufferCollection(reply.content, compression_type, compressed_output);
|
|
||||||
reply.setSize(compressed_output.size());
|
|
||||||
output_buffer = reply.HeaderstoBuffers();
|
|
||||||
output_buffer.push_back(boost::asio::buffer(compressed_output));
|
|
||||||
boost::asio::async_write(
|
|
||||||
TCP_socket,
|
|
||||||
output_buffer,
|
|
||||||
strand.wrap(boost::bind(&Connection::handle_write,
|
|
||||||
this->shared_from_this(),
|
|
||||||
boost::asio::placeholders::error)));
|
|
||||||
break;
|
|
||||||
case noCompression:
|
|
||||||
reply.SetUncompressedSize();
|
|
||||||
output_buffer = reply.toBuffers();
|
|
||||||
boost::asio::async_write(
|
|
||||||
TCP_socket,
|
|
||||||
output_buffer,
|
|
||||||
strand.wrap(boost::bind(&Connection::handle_write,
|
|
||||||
this->shared_from_this(),
|
|
||||||
boost::asio::placeholders::error)));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else if (!result)
|
boost::asio::async_write(
|
||||||
{ // request is not parseable
|
TCP_socket,
|
||||||
reply = Reply::StockReply(Reply::badRequest);
|
output_buffer,
|
||||||
|
strand.wrap(boost::bind(&Connection::handle_write,
|
||||||
|
this->shared_from_this(),
|
||||||
|
boost::asio::placeholders::error)));
|
||||||
|
}
|
||||||
|
else if (!result)
|
||||||
|
{ // request is not parseable
|
||||||
|
reply = Reply::StockReply(Reply::badRequest);
|
||||||
|
|
||||||
boost::asio::async_write(TCP_socket,
|
boost::asio::async_write(TCP_socket,
|
||||||
reply.toBuffers(),
|
reply.toBuffers(),
|
||||||
strand.wrap(boost::bind(&Connection::handle_write,
|
strand.wrap(boost::bind(&Connection::handle_write,
|
||||||
this->shared_from_this(),
|
this->shared_from_this(),
|
||||||
boost::asio::placeholders::error)));
|
boost::asio::placeholders::error)));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// 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)));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user