asio: Don't send response body if it is not expected by status code or method

This commit is contained in:
Tatsuhiro Tsujikawa 2015-03-06 02:15:34 +09:00
parent 44c0d32a1b
commit 38788d707b

View File

@ -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;