summaryrefslogtreecommitdiff
path: root/http/http_response.hpp
diff options
context:
space:
mode:
authorEd Tanous <edtanous@google.com>2023-05-15 21:44:27 +0300
committerEd Tanous <ed@tanous.net>2023-05-19 18:52:09 +0300
commiteea9c979498cdcd6df68c5fcdf28086e45466672 (patch)
tree432e5220a992506a1897b7ef20b0e7081e7dcdc0 /http/http_response.hpp
parentbb8f3bb4c83ea5411fec38c9bd51298448164d8b (diff)
downloadbmcweb-eea9c979498cdcd6df68c5fcdf28086e45466672.tar.xz
Clean up preparePayload
boost::beast::http::message::prepare_payload [1] can throw, which isn't really the behavior we want (as it throws to the io_context). Luckily, every part of that function is using public methods, and we can simplify it. In past commits, we've worked around this issue: 6295becabb9edba2edb53a3c0dddc13d2ffac8dd This is an attempt to fix it properly. [1] https://github.com/boostorg/beast/blob/ae01f0201dbf940cbc32d96d7a78dc584a02ab26/include/boost/beast/http/impl/message.hpp#L398 Redfish service validator passes Signed-off-by: Ed Tanous <edtanous@google.com> Change-Id: Ie88ddeecfd226bba75a7659cfb7ddddd38eb27cb
Diffstat (limited to 'http/http_response.hpp')
-rw-r--r--http/http_response.hpp28
1 files changed, 27 insertions, 1 deletions
diff --git a/http/http_response.hpp b/http/http_response.hpp
index 2af12c9d05..58e047ca26 100644
--- a/http/http_response.hpp
+++ b/http/http_response.hpp
@@ -143,7 +143,33 @@ struct Response
void preparePayload()
{
- stringResponse->prepare_payload();
+ // This code is a throw-free equivalent to
+ // beast::http::message::prepare_payload
+ boost::optional<uint64_t> pSize = stringResponse->payload_size();
+ using boost::beast::http::status;
+ using boost::beast::http::status_class;
+ using boost::beast::http::to_status_class;
+ if (!pSize)
+ {
+ pSize = 0;
+ }
+ else
+ {
+ bool is1XXReturn = to_status_class(stringResponse->result()) ==
+ status_class::informational;
+ if (*pSize > 0 &&
+ (is1XXReturn ||
+ stringResponse->result() == status::no_content ||
+ stringResponse->result() == status::not_modified))
+ {
+ BMCWEB_LOG_CRITICAL
+ << this
+ << " Response content provided but code was no-content or not_modified, which aren't allowed to have a body";
+ pSize = 0;
+ body().clear();
+ }
+ }
+ stringResponse->content_length(*pSize);
}
void clear()