From f2c3271c8eb405a05a3ec383791e1adc3c4a7f86 Mon Sep 17 00:00:00 2001 From: P Dheeraj Srujan Kumar Date: Mon, 18 Oct 2021 22:45:37 +0530 Subject: [PATCH] Add asyncResp support during handleUpgrade The current implementation uses the earlier method of using the response object and calling response.end() to initiate completion handler. This commit modifies the implementation to use asyncResp, where the completion handler gets called asynchronously as the response object goes out of scope. Tested : - websocket_test.py Passed - KVM was functional in WebUI. - POST to /redfish/v1/EventService/Subscriptions/SSE returned an error message as expected and the connection was kept alive. - GET on /redfish/v1/EventService/Subscriptions/SSE (SSE subscription) was successful. The existing connection was successfully closed and upgraded to SSE connection. Change-Id: I2d76b34a49a6432c507d939b21b37c1ced761f8e Signed-off-by: P Dheeraj Srujan Kumar --- http/app.hpp | 6 ++++-- http/http_connection.hpp | 30 +++++++++++++++++++++++++----- http/routing.hpp | 37 +++++++++++++++++++++---------------- 3 files changed, 50 insertions(+), 23 deletions(-) diff --git a/http/app.hpp b/http/app.hpp index 4735197..c46dcf7 100644 --- a/http/app.hpp +++ b/http/app.hpp @@ -45,9 +45,11 @@ class App } template - void handleUpgrade(const Request& req, Response& res, Adaptor&& adaptor) + void handleUpgrade(const Request& req, + const std::shared_ptr& asyncResp, + Adaptor&& adaptor) { - router.handleUpgrade(req, res, std::move(adaptor)); + router.handleUpgrade(req, asyncResp, std::move(adaptor)); } void handle(Request& req, diff --git a/http/http_connection.hpp b/http/http_connection.hpp index 9d53c17..cdd3707 100644 --- a/http/http_connection.hpp +++ b/http/http_connection.hpp @@ -361,6 +361,7 @@ class Connection : boost::asio::post(self->adaptor.get_executor(), [self] { self->completeRequest(); }); }); + auto asyncResp = std::make_shared(res); if ((thisReq.isUpgrade() && boost::iequals( @@ -369,13 +370,32 @@ class Connection : (req->url == "/redfish/v1/EventService/Subscriptions/SSE")) { BMCWEB_LOG_DEBUG << "Request: " << this << " is getting upgraded"; - handler->handleUpgrade(thisReq, res, std::move(adaptor)); - // delete lambda with self shared_ptr - // to enable connection destruction - res.setCompleteRequestHandler(nullptr); + res.setCompleteRequestHandler([self(shared_from_this())] { + if (self->res.resultInt() != 200) + { + // When any error occurs during handle upgradation, + // the result in response will be set to respective + // error. By default the Result will be OK (200), + // which implies successful handle upgrade. Response + // needs to be sent over this connection only on + // failure. + boost::asio::post(self->adaptor.get_executor(), + [self] { self->completeRequest(); }); + } + else + { + // Set Complete request handler to NULL to remove + // the shared pointer of connection to enable + // connection destruction. As the connection would + // get upgraded, we wouldn't need this connection + // any longer + self->res.setCompleteRequestHandler(nullptr); + } + }); + handler->handleUpgrade(thisReq, asyncResp, std::move(adaptor)); return; } - auto asyncResp = std::make_shared(res); + handler->handle(thisReq, asyncResp); } diff --git a/http/routing.hpp b/http/routing.hpp index 25e4ce8..858f146 100644 --- a/http/routing.hpp +++ b/http/routing.hpp @@ -1202,12 +1202,13 @@ class Router } template - void handleUpgrade(const Request& req, Response& res, Adaptor&& adaptor) + void handleUpgrade(const Request& req, + const std::shared_ptr& asyncResp, + Adaptor&& adaptor) { if (static_cast(req.method()) >= perMethods.size()) { - res.result(boost::beast::http::status::not_found); - res.end(); + asyncResp->res.result(boost::beast::http::status::not_found); return; } @@ -1220,8 +1221,7 @@ class Router if (!ruleIndex) { BMCWEB_LOG_DEBUG << "Cannot match rules " << req.url; - res.result(boost::beast::http::status::not_found); - res.end(); + asyncResp->res.result(boost::beast::http::status::not_found); return; } @@ -1234,23 +1234,24 @@ class Router { BMCWEB_LOG_INFO << "Redirecting to a url with trailing slash: " << req.url; - res.result(boost::beast::http::status::moved_permanently); + asyncResp->res.result( + boost::beast::http::status::moved_permanently); // TODO absolute url building if (req.getHeaderValue("Host").empty()) { - res.addHeader("Location", std::string(req.url) + "/"); + asyncResp->res.addHeader("Location", + std::string(req.url) + "/"); } else { - res.addHeader( + asyncResp->res.addHeader( "Location", req.isSecure ? "https://" : "http://" + std::string(req.getHeaderValue("Host")) + std::string(req.url) + "/"); } - res.end(); return; } @@ -1261,8 +1262,7 @@ class Router << " with " << req.methodString() << "(" << static_cast(req.method()) << ") / " << rules[ruleIndex]->getMethods(); - res.result(boost::beast::http::status::not_found); - res.end(); + asyncResp->res.result(boost::beast::http::status::not_found); return; } @@ -1273,13 +1273,18 @@ class Router // any uncaught exceptions become 500s try { - rules[ruleIndex]->handleUpgrade(req, res, std::move(adaptor)); + // Creating temporary response object to call handleUpgrade + // We cannot pass the asyncResp as it will be destroyed + // The response object is not initialized as handleUpgrade wouldn't + // be using this object + crow::Response resp; + rules[ruleIndex]->handleUpgrade(req, resp, std::move(adaptor)); } catch (const std::exception& e) { BMCWEB_LOG_ERROR << "An uncaught exception occurred: " << e.what(); - res.result(boost::beast::http::status::internal_server_error); - res.end(); + asyncResp->res.result( + boost::beast::http::status::internal_server_error); return; } catch (...) @@ -1287,8 +1292,8 @@ class Router BMCWEB_LOG_ERROR << "An uncaught exception occurred. The type was unknown " "so no information was available."; - res.result(boost::beast::http::status::internal_server_error); - res.end(); + asyncResp->res.result( + boost::beast::http::status::internal_server_error); return; } } -- 2.17.1