diff --git a/src/asio_server_response_impl.cc b/src/asio_server_response_impl.cc index 5661e610..814a988c 100644 --- a/src/asio_server_response_impl.cc +++ b/src/asio_server_response_impl.cc @@ -25,6 +25,7 @@ #include "asio_server_response_impl.h" #include "asio_server_stream.h" +#include "asio_server_request_impl.h" #include "asio_server_http2_handler.h" #include "asio_common.h" @@ -78,9 +79,25 @@ void response_impl::end(generator_cb cb) { state_ = response_state::BODY_STARTED; } +namespace { +bool expect_response_body(const std::string &method, int status_code) { + return method != "HEAD" && status_code / 100 != 1 && status_code != 304 && + status_code != 204; +} +} // namespace + void response_impl::start_response() { auto handler = strm_->handler(); + auto &req = strm_->request().impl(); + + // if response body is not expected, nullify it so that HEADERS has + // END_STREAM flag set. + if (!expect_response_body(req.method(), status_code_)) { + state_ = response_state::BODY_STARTED; + generator_cb_ = generator_cb(); + } + if (handler->start_response(*strm_) != 0) { handler->stream_error(strm_->get_stream_id(), NGHTTP2_INTERNAL_ERROR); return;