blob: 0c9295e9dabb4193648b64b899fcc11765a95800 [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 "http_request.hpp"
7#include "http_response.hpp"
8#include "server_sent_event.hpp"
9
10#include <boost/beast/http/verb.hpp>
11
12#include <functional>
13#include <memory>
14#include <string>
15
16namespace crow
17{
18
19class SseSocketRule : public BaseRule
20{
21 using self_t = SseSocketRule;
22
23 public:
Ed Tanousa3b9eb92024-06-03 08:39:37 -070024 explicit SseSocketRule(const std::string& ruleIn) : BaseRule(ruleIn)
25 {
26 isUpgrade = true;
27 // Clear GET handler
28 methodsBitfield = 0;
29 }
Ed Tanous08bbe112023-04-06 13:10:02 -070030
31 void validate() override {}
32
33 void handle(const Request& /*req*/,
34 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
35 const std::vector<std::string>& /*params*/) override
36 {
Ed Tanousa3b9eb92024-06-03 08:39:37 -070037 BMCWEB_LOG_ERROR(
38 "Handle called on websocket rule. This should never happen");
39 asyncResp->res.result(
40 boost::beast::http::status::internal_server_error);
Ed Tanous08bbe112023-04-06 13:10:02 -070041 }
42
Ed Tanousf80a87f2024-06-16 12:10:33 -070043 void handleUpgrade(const Request& req,
Ed Tanous08bbe112023-04-06 13:10:02 -070044 const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/,
45 boost::asio::ip::tcp::socket&& adaptor) override
46 {
47 std::shared_ptr<
48 crow::sse_socket::ConnectionImpl<boost::asio::ip::tcp::socket>>
49 myConnection = std::make_shared<
50 crow::sse_socket::ConnectionImpl<boost::asio::ip::tcp::socket>>(
Ed Tanous93cf0ac2024-03-28 00:35:13 -070051 std::move(adaptor), openHandler, closeHandler);
Ed Tanousf80a87f2024-06-16 12:10:33 -070052 myConnection->start(req);
Ed Tanous08bbe112023-04-06 13:10:02 -070053 }
Ed Tanousf80a87f2024-06-16 12:10:33 -070054 void handleUpgrade(const Request& req,
Ed Tanous08bbe112023-04-06 13:10:02 -070055 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 {
59 std::shared_ptr<crow::sse_socket::ConnectionImpl<
Ed Tanous003301a2024-04-16 09:59:19 -070060 boost::asio::ssl::stream<boost::asio::ip::tcp::socket>>>
Ed Tanous08bbe112023-04-06 13:10:02 -070061 myConnection = std::make_shared<crow::sse_socket::ConnectionImpl<
Ed Tanous003301a2024-04-16 09:59:19 -070062 boost::asio::ssl::stream<boost::asio::ip::tcp::socket>>>(
Ed Tanous93cf0ac2024-03-28 00:35:13 -070063 std::move(adaptor), openHandler, closeHandler);
Ed Tanousf80a87f2024-06-16 12:10:33 -070064 myConnection->start(req);
Ed Tanous08bbe112023-04-06 13:10:02 -070065 }
Ed Tanous08bbe112023-04-06 13:10:02 -070066
67 template <typename Func>
68 self_t& onopen(Func f)
69 {
70 openHandler = f;
71 return *this;
72 }
73
74 template <typename Func>
75 self_t& onclose(Func f)
76 {
77 closeHandler = f;
78 return *this;
79 }
80
81 private:
Ed Tanousf80a87f2024-06-16 12:10:33 -070082 std::function<void(crow::sse_socket::Connection&, const crow::Request&)>
83 openHandler;
Ed Tanous08bbe112023-04-06 13:10:02 -070084 std::function<void(crow::sse_socket::Connection&)> closeHandler;
85};
86
87} // namespace crow