Rearrange/clean code in connection
The code here is fairly complex, and can be simplified without actually
changing any of the logic. The largest thing that this patchset
changes, is that two levels of scope are removed by preferring to handle
the errors and return, rather than waiting until the end of the function
to handle the errors. This is more cleanup that can be done because of
the removal of middlewares.
Tested:
Tested using curl on both an endpoint that requires auth (ie
redfish/v1/Systems) and an endpoint that didn't (ie redfish/v1) as well
as with and without basic auth. All combinations gave the expected
result.
Signed-off-by: Ed Tanous <edtanous@google.com>
Change-Id: I72ea5276b8e8758823e41412457ae0c4ecca1aab
diff --git a/http/http_connection.hpp b/http/http_connection.hpp
index 6d1c13c..afd823a 100644
--- a/http/http_connection.hpp
+++ b/http/http_connection.hpp
@@ -316,15 +316,14 @@
// Fetch the client IP address
readClientIp();
- bool isInvalidRequest = false;
-
// Check for HTTP version 1.1.
if (parser->get().version() == 11)
{
if (parser->get()[boost::beast::http::field::host].empty())
{
- isInvalidRequest = true;
res.result(boost::beast::http::status::bad_request);
+ completeRequest();
+ return;
}
}
@@ -347,48 +346,39 @@
BMCWEB_LOG_ERROR << p.what();
}
- if (!isInvalidRequest)
- {
- res.setCompleteRequestHandler(nullptr);
- res.isAliveHelper = [this]() -> bool { return isAlive(); };
+ res.setCompleteRequestHandler(nullptr);
+ res.isAliveHelper = [this]() -> bool { return isAlive(); };
- req->ioService = static_cast<decltype(req->ioService)>(
- &adaptor.get_executor().context());
+ req->ioService = static_cast<decltype(req->ioService)>(
+ &adaptor.get_executor().context());
- if (!res.completed)
- {
- res.setCompleteRequestHandler([self(shared_from_this())] {
- boost::asio::post(self->adaptor.get_executor(),
- [self] { self->completeRequest(); });
- });
- if (req->isUpgrade() &&
- boost::iequals(
- req->getHeaderValue(boost::beast::http::field::upgrade),
- "websocket"))
- {
- handler->handleUpgrade(*req, res, std::move(adaptor));
- // delete lambda with self shared_ptr
- // to enable connection destruction
- res.setCompleteRequestHandler(nullptr);
- return;
- }
- auto asyncResp = std::make_shared<bmcweb::AsyncResp>(res);
- handler->handle(*req, asyncResp);
- }
- else
- {
- completeRequest();
- }
- }
- else
+ if (res.completed)
{
completeRequest();
+ return;
}
+ res.setCompleteRequestHandler([self(shared_from_this())] {
+ boost::asio::post(self->adaptor.get_executor(),
+ [self] { self->completeRequest(); });
+ });
+
+ if (req->isUpgrade() &&
+ boost::iequals(
+ req->getHeaderValue(boost::beast::http::field::upgrade),
+ "websocket"))
+ {
+ handler->handleUpgrade(*req, res, std::move(adaptor));
+ // delete lambda with self shared_ptr
+ // to enable connection destruction
+ res.setCompleteRequestHandler(nullptr);
+ return;
+ }
+ auto asyncResp = std::make_shared<bmcweb::AsyncResp>(res);
+ handler->handle(*req, asyncResp);
}
bool isAlive()
{
-
if constexpr (std::is_same_v<Adaptor,
boost::beast::ssl_stream<
boost::asio::ip::tcp::socket>>)