http write returns success on connection close (API Change):

fix #767

The write family of HTTP stream algorithms no longer returns
error::end_of_stream when the message indicates that the connection
should be closed.

Actions Required:

* Add code to servers to close the connection after successfully
  writing a message where `message::keep_alive()` would return `false`.
This commit is contained in:
Vinnie Falco
2017-10-22 07:03:04 -07:00
parent 885b9dfe0b
commit d0d4e0a740
16 changed files with 122 additions and 83 deletions
+11 -4
View File
@@ -212,15 +212,18 @@ template<class Stream>
struct send_lambda
{
Stream& stream_;
bool& close_;
boost::system::error_code& ec_;
boost::asio::yield_context yield_;
explicit
send_lambda(
Stream& stream,
bool& close,
boost::system::error_code& ec,
boost::asio::yield_context yield)
: stream_(stream)
, close_(close)
, ec_(ec)
, yield_(yield)
{
@@ -230,6 +233,9 @@ struct send_lambda
void
operator()(http::message<isRequest, Body, Fields>&& msg) const
{
// Determine if we should close the connection after
close_ = ! msg.keep_alive();
// We need the serializer here because the serializer requires
// a non-const file_body, and the message oriented version of
// http::write only works with const messages.
@@ -245,13 +251,14 @@ do_session(
std::string const& doc_root,
boost::asio::yield_context yield)
{
bool close = false;
boost::system::error_code ec;
// This buffer is required to persist across reads
boost::beast::flat_buffer buffer;
// This lambda is used to send messages
send_lambda<tcp::socket> lambda{socket, ec, yield};
send_lambda<tcp::socket> lambda{socket, close, ec, yield};
for(;;)
{
@@ -265,14 +272,14 @@ do_session(
// Send the response
handle_request(doc_root, std::move(req), lambda);
if(ec == http::error::end_of_stream)
if(ec)
return fail(ec, "write");
if(close)
{
// This means we should close the connection, usually because
// the response indicated the "Connection: close" semantic.
break;
}
if(ec)
return fail(ec, "write");
}
// Send a TCP shutdown