diff --git a/include/boost/beast/http/impl/message.hpp b/include/boost/beast/http/impl/message.hpp index 4b101ad3b9..b2888f1099 100644 --- a/include/boost/beast/http/impl/message.hpp +++ b/include/boost/beast/http/impl/message.hpp @@ -390,7 +390,7 @@ prepare_payload(std::false_type) { auto const n = payload_size(); if( (! n || *n > 0) && ( - (status_class(this->result()) == status_class::informational || + (to_status_class(this->result()) == status_class::informational || this->result() == status::no_content || this->result() == status::not_modified))) { diff --git a/test/beast/http/fields.cpp b/test/beast/http/fields.cpp index c1aa3bbe7c..4e257c0a5d 100644 --- a/test/beast/http/fields.cpp +++ b/test/beast/http/fields.cpp @@ -672,6 +672,32 @@ class fields_test : public beast::unit_test::suite BEAST_EXPECT(res.count(field::content_length) == 0); BEAST_EXPECT(res[field::transfer_encoding] == "chunked"); } + + // a body is not allowed on 1xx, 204 or 304 + { + for(auto code : { + status::continue_, // 100 + status::switching_protocols,// 101 + status::processing, // 102 + status::early_hints, // 103 + status::no_content, // 204 + status::not_modified}) // 304 + { + response res; + res.version(11); + res.result(code); + res.body() = 1; + BEAST_THROWS(res.prepare_payload(), std::invalid_argument); + } + + // an ordinary 2xx response with a body is unaffected + response res; + res.version(11); + res.result(status::ok); + res.body() = 1; + res.prepare_payload(); + BEAST_EXPECT(res[field::content_length] == "1"); + } } void