summaryrefslogtreecommitdiff
path: root/meta-openbmc-mods/meta-common/recipes-phosphor/interfaces/bmcweb/http_routing/0001-Add-asyncResp-support-during-handleUpgrade.patch
blob: b3aa11774f2791099d682e52839dbef76060aba2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
From f2c3271c8eb405a05a3ec383791e1adc3c4a7f86 Mon Sep 17 00:00:00 2001
From: P Dheeraj Srujan Kumar <p.dheeraj.srujan.kumar@intel.com>
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 <p.dheeraj.srujan.kumar@intel.com>
---
 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 <typename Adaptor>
-    void handleUpgrade(const Request& req, Response& res, Adaptor&& adaptor)
+    void handleUpgrade(const Request& req,
+                       const std::shared_ptr<bmcweb::AsyncResp>& 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<bmcweb::AsyncResp>(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<bmcweb::AsyncResp>(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 <typename Adaptor>
-    void handleUpgrade(const Request& req, Response& res, Adaptor&& adaptor)
+    void handleUpgrade(const Request& req,
+                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
+                       Adaptor&& adaptor)
     {
         if (static_cast<size_t>(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<uint32_t>(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 (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