Don't throw if no-content is provided
In a perfect world, we would implement all no-content cases correctly,
and never set a no-content return code, then provide content. In
practice, in an async context, this is difficult to manage, as has been
proven a couple times to be beyond what we're able to review. This
error should be made less fatal, and still return a response to the
user, rather than throwing and closing the connection entirely.
This patchset implements a corrective action where the content is
dropped in the connection if the handler sets the no-content code.
Tested:
Still need to test. In theory, not possible without a unit test for a
buggy handler.
Signed-off-by: Ed Tanous <ed.tanous@intel.com>
Change-Id: Ie6fea0c3fef107578b855b7c43a634d51b2064de
diff --git a/crow/include/crow/http_connection.h b/crow/include/crow/http_connection.h
index 6f44186..78ee70b 100644
--- a/crow/include/crow/http_connection.h
+++ b/crow/include/crow/http_connection.h
@@ -447,6 +447,17 @@
{
res.body() = std::string(res.reason());
}
+
+ if (res.result() == boost::beast::http::status::no_content)
+ {
+ // Boost beast throws if content is provided on a no-content
+ // response. Ideally, this would never happen, but in the case that
+ // it does, we don't want to throw.
+ BMCWEB_LOG_CRITICAL
+ << "Response content provided but code was no-content";
+ res.body().clear();
+ }
+
res.addHeader(boost::beast::http::field::server, serverName);
res.addHeader(boost::beast::http::field::date, getCachedDateStr());