reject 1xx responses with a body in prepare_payload

This commit is contained in:
Sahana Bogar
2026-07-01 17:15:13 +05:30
committed by Mohammad Nejati
parent 56a7c3af49
commit 3c1f91fa62
2 changed files with 27 additions and 1 deletions
+1 -1
View File
@@ -390,7 +390,7 @@ prepare_payload(std::false_type)
{ {
auto const n = payload_size(); auto const n = payload_size();
if( (! n || *n > 0) && ( 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::no_content ||
this->result() == status::not_modified))) this->result() == status::not_modified)))
{ {
+26
View File
@@ -672,6 +672,32 @@ public:
BEAST_EXPECT(res.count(field::content_length) == 0); BEAST_EXPECT(res.count(field::content_length) == 0);
BEAST_EXPECT(res[field::transfer_encoding] == "chunked"); 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<sized_body> 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<sized_body> res;
res.version(11);
res.result(status::ok);
res.body() = 1;
res.prepare_payload();
BEAST_EXPECT(res[field::content_length] == "1");
}
} }
void void