blob: 61743d5d710ce902bb3c441fbfdfef30bfefd581 [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Ed Tanous08bbe112023-04-06 13:10:02 -07003#pragma once
4
5#include "baserule.hpp"
6#include "websocket.hpp"
7
8#include <boost/beast/http/verb.hpp>
9
10#include <memory>
11#include <string>
12#include <vector>
13
14namespace crow
15{
16class WebSocketRule : public BaseRule
17{
18 using self_t = WebSocketRule;
19
20 public:
Ed Tanousa3b9eb92024-06-03 08:39:37 -070021 explicit WebSocketRule(const std::string& ruleIn) : BaseRule(ruleIn)
22 {
23 isUpgrade = true;
24 // Clear GET handler
25 methodsBitfield = 0;
26 }
Ed Tanous08bbe112023-04-06 13:10:02 -070027
28 void validate() override {}
29
30 void handle(const Request& /*req*/,
31 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
32 const std::vector<std::string>& /*params*/) override
33 {
Ed Tanousa3b9eb92024-06-03 08:39:37 -070034 BMCWEB_LOG_ERROR(
35 "Handle called on websocket rule. This should never happen");
36 asyncResp->res.result(
37 boost::beast::http::status::internal_server_error);
Ed Tanous08bbe112023-04-06 13:10:02 -070038 }
39
Ed Tanous08bbe112023-04-06 13:10:02 -070040 void handleUpgrade(const Request& req,
41 const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/,
42 boost::asio::ip::tcp::socket&& adaptor) override
43 {
Ed Tanous62598e32023-07-17 17:06:25 -070044 BMCWEB_LOG_DEBUG("Websocket handles upgrade");
Ed Tanous08bbe112023-04-06 13:10:02 -070045 std::shared_ptr<
46 crow::websocket::ConnectionImpl<boost::asio::ip::tcp::socket>>
47 myConnection = std::make_shared<
48 crow::websocket::ConnectionImpl<boost::asio::ip::tcp::socket>>(
Ed Tanous5ebb9d32023-02-27 18:20:47 -080049 req.url(), req.session, std::move(adaptor), openHandler,
50 messageHandler, messageExHandler, closeHandler, errorHandler);
51 myConnection->start(req);
Ed Tanous08bbe112023-04-06 13:10:02 -070052 }
Ed Tanous8db83742024-04-13 09:11:15 -070053
Ed Tanous08bbe112023-04-06 13:10:02 -070054 void handleUpgrade(const Request& req,
55 const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/,
Ed Tanous003301a2024-04-16 09:59:19 -070056 boost::asio::ssl::stream<boost::asio::ip::tcp::socket>&&
Ed Tanous08bbe112023-04-06 13:10:02 -070057 adaptor) override
58 {
Ed Tanous62598e32023-07-17 17:06:25 -070059 BMCWEB_LOG_DEBUG("Websocket handles upgrade");
Ed Tanous08bbe112023-04-06 13:10:02 -070060 std::shared_ptr<crow::websocket::ConnectionImpl<
Ed Tanous003301a2024-04-16 09:59:19 -070061 boost::asio::ssl::stream<boost::asio::ip::tcp::socket>>>
Ed Tanous08bbe112023-04-06 13:10:02 -070062 myConnection = std::make_shared<crow::websocket::ConnectionImpl<
Ed Tanous003301a2024-04-16 09:59:19 -070063 boost::asio::ssl::stream<boost::asio::ip::tcp::socket>>>(
Ed Tanous5ebb9d32023-02-27 18:20:47 -080064 req.url(), req.session, std::move(adaptor), openHandler,
65 messageHandler, messageExHandler, closeHandler, errorHandler);
66 myConnection->start(req);
Ed Tanous08bbe112023-04-06 13:10:02 -070067 }
Ed Tanous08bbe112023-04-06 13:10:02 -070068
69 template <typename Func>
70 self_t& onopen(Func f)
71 {
72 openHandler = f;
73 return *this;
74 }
75
76 template <typename Func>
77 self_t& onmessage(Func f)
78 {
79 messageHandler = f;
80 return *this;
81 }
82
83 template <typename Func>
84 self_t& onmessageex(Func f)
85 {
86 messageExHandler = f;
87 return *this;
88 }
89
90 template <typename Func>
91 self_t& onclose(Func f)
92 {
93 closeHandler = f;
94 return *this;
95 }
96
97 template <typename Func>
98 self_t& onerror(Func f)
99 {
100 errorHandler = f;
101 return *this;
102 }
103
104 protected:
105 std::function<void(crow::websocket::Connection&)> openHandler;
106 std::function<void(crow::websocket::Connection&, const std::string&, bool)>
107 messageHandler;
108 std::function<void(crow::websocket::Connection&, std::string_view,
109 crow::websocket::MessageType type,
110 std::function<void()>&& whenComplete)>
111 messageExHandler;
112 std::function<void(crow::websocket::Connection&, const std::string&)>
113 closeHandler;
114 std::function<void(crow::websocket::Connection&)> errorHandler;
115};
116} // namespace crow