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-20 10:19:58 -07:00
parent 885b9dfe0b
commit d0d4e0a740
16 changed files with 122 additions and 83 deletions
@@ -213,13 +213,16 @@ template<class Stream>
struct send_lambda
{
Stream& stream_;
bool& close_;
boost::system::error_code& ec_;
explicit
send_lambda(
Stream& stream,
bool& close,
boost::system::error_code& ec)
: stream_(stream)
, close_(close)
, ec_(ec)
{
}
@@ -228,6 +231,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.
@@ -243,6 +249,7 @@ do_session(
ssl::context& ctx,
std::string const& doc_root)
{
bool close = false;
boost::system::error_code ec;
// Construct the stream around the socket
@@ -257,7 +264,7 @@ do_session(
boost::beast::flat_buffer buffer;
// This lambda is used to send messages
send_lambda<ssl::stream<tcp::socket&>> lambda{stream, ec};
send_lambda<ssl::stream<tcp::socket&>> lambda{stream, close, ec};
for(;;)
{
@@ -271,14 +278,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");
}
// Perform the SSL shutdown