From a9f076e521433190bc526fa3eb4587090f48b909 Mon Sep 17 00:00:00 2001 From: P Dheeraj Srujan Kumar Date: Mon, 18 Oct 2021 22:45:37 +0530 Subject: Add asyncResp support to handleUpgrade This commit enables passing down the asyncResp (of the connection) to the handler of upgraded connections. This is already in place for normal requests (i.e. Class Router -> handle()) This change would enable any async calls that would be required before upgrade of the connection. For example, as on today, we have only Authentication of user in place for upgraded connection, but not Authorization. So, this asyncResp could further be used for such dbus calls to return informative response. This commit updates the signature of all the handleUpgrade() functions present in router.hpp to take in asyncResp object instead of normal response. Tested : - websocket_test.py Passed - KVM was functional in WebUI. Change-Id: I1c6c91f126b734e1b5573d5ef204fe2bf6ed6c26 Signed-off-by: P Dheeraj Srujan Kumar --- http/http_connection.hpp | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'http/http_connection.hpp') diff --git a/http/http_connection.hpp b/http/http_connection.hpp index c28cde37ae..d5d02e545a 100644 --- a/http/http_connection.hpp +++ b/http/http_connection.hpp @@ -249,10 +249,21 @@ class Connection : thisReq.getHeaderValue(boost::beast::http::field::upgrade), "websocket")) { - handler->handleUpgrade(thisReq, res, std::move(adaptor)); - // delete lambda with self shared_ptr - // to enable connection destruction - asyncResp->res.setCompleteRequestHandler(nullptr); + asyncResp->res.setCompleteRequestHandler( + [self(shared_from_this())](crow::Response& thisRes) { + if (thisRes.result() != boost::beast::http::status::ok) + { + // When any error occurs before 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. + self->completeRequest(thisRes); + return; + } + }); + handler->handleUpgrade(thisReq, asyncResp, std::move(adaptor)); return; } std::string_view expected = -- cgit v1.2.3