blob: 56bc6cafb84c3a498a09c00ba1d4f6f48ec120d9 [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
Ed Tanousd7857202025-01-28 15:32:26 -08005#include "async_resp.hpp"
Ed Tanous08bbe112023-04-06 13:10:02 -07006#include "baserule.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -08007#include "http_request.hpp"
8#include "logging.hpp"
Ed Tanous08bbe112023-04-06 13:10:02 -07009#include "websocket.hpp"
10
Ed Tanousd7857202025-01-28 15:32:26 -080011#include <boost/asio/ip/tcp.hpp>
12#include <boost/asio/ssl/stream.hpp>
13#include <boost/beast/http/status.hpp>
Ed Tanous08bbe112023-04-06 13:10:02 -070014
Ed Tanousd7857202025-01-28 15:32:26 -080015#include <functional>
Ed Tanous08bbe112023-04-06 13:10:02 -070016#include <memory>
17#include <string>
Ed Tanousd7857202025-01-28 15:32:26 -080018#include <string_view>
Ed Tanous08bbe112023-04-06 13:10:02 -070019#include <vector>
20
21namespace crow
22{
23class WebSocketRule : public BaseRule
24{
25 using self_t = WebSocketRule;
26
27 public:
Ed Tanousa3b9eb92024-06-03 08:39:37 -070028 explicit WebSocketRule(const std::string& ruleIn) : BaseRule(ruleIn)
29 {
30 isUpgrade = true;
31 // Clear GET handler
32 methodsBitfield = 0;
33 }
Ed Tanous08bbe112023-04-06 13:10:02 -070034
35 void validate() override {}
36
37 void handle(const Request& /*req*/,
38 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
39 const std::vector<std::string>& /*params*/) override
40 {
Ed Tanousa3b9eb92024-06-03 08:39:37 -070041 BMCWEB_LOG_ERROR(
42 "Handle called on websocket rule. This should never happen");
43 asyncResp->res.result(
44 boost::beast::http::status::internal_server_error);
Ed Tanous08bbe112023-04-06 13:10:02 -070045 }
46
Ed Tanous08bbe112023-04-06 13:10:02 -070047 void handleUpgrade(const Request& req,
48 const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/,
Ed Tanousbeb96b02025-02-09 09:22:46 -080049 boost::asio::ip::tcp::socket&& adaptor) override;
Ed Tanous8db83742024-04-13 09:11:15 -070050
Ed Tanous08bbe112023-04-06 13:10:02 -070051 void handleUpgrade(const Request& req,
52 const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/,
Ed Tanous003301a2024-04-16 09:59:19 -070053 boost::asio::ssl::stream<boost::asio::ip::tcp::socket>&&
Ed Tanousbeb96b02025-02-09 09:22:46 -080054 adaptor) override;
Ed Tanous08bbe112023-04-06 13:10:02 -070055
56 template <typename Func>
57 self_t& onopen(Func f)
58 {
59 openHandler = f;
60 return *this;
61 }
62
63 template <typename Func>
64 self_t& onmessage(Func f)
65 {
66 messageHandler = f;
67 return *this;
68 }
69
70 template <typename Func>
71 self_t& onmessageex(Func f)
72 {
73 messageExHandler = f;
74 return *this;
75 }
76
77 template <typename Func>
78 self_t& onclose(Func f)
79 {
80 closeHandler = f;
81 return *this;
82 }
83
84 template <typename Func>
85 self_t& onerror(Func f)
86 {
87 errorHandler = f;
88 return *this;
89 }
90
91 protected:
92 std::function<void(crow::websocket::Connection&)> openHandler;
93 std::function<void(crow::websocket::Connection&, const std::string&, bool)>
94 messageHandler;
95 std::function<void(crow::websocket::Connection&, std::string_view,
96 crow::websocket::MessageType type,
97 std::function<void()>&& whenComplete)>
98 messageExHandler;
99 std::function<void(crow::websocket::Connection&, const std::string&)>
100 closeHandler;
101 std::function<void(crow::websocket::Connection&)> errorHandler;
102};
103} // namespace crow