Ed Tanous | 40e9b92 | 2024-09-10 13:50:16 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright OpenBMC Authors |
Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 3 | #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 | |
| 16 | namespace crow |
| 17 | { |
| 18 | |
| 19 | class SseSocketRule : public BaseRule |
| 20 | { |
| 21 | using self_t = SseSocketRule; |
| 22 | |
| 23 | public: |
Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 24 | explicit SseSocketRule(const std::string& ruleIn) : BaseRule(ruleIn) |
| 25 | { |
| 26 | isUpgrade = true; |
| 27 | // Clear GET handler |
| 28 | methodsBitfield = 0; |
| 29 | } |
Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 30 | |
| 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 Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 37 | 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 Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Ed Tanous | f80a87f | 2024-06-16 12:10:33 -0700 | [diff] [blame] | 43 | void handleUpgrade(const Request& req, |
Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 44 | 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 Tanous | 93cf0ac | 2024-03-28 00:35:13 -0700 | [diff] [blame] | 51 | std::move(adaptor), openHandler, closeHandler); |
Ed Tanous | f80a87f | 2024-06-16 12:10:33 -0700 | [diff] [blame] | 52 | myConnection->start(req); |
Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 53 | } |
Ed Tanous | f80a87f | 2024-06-16 12:10:33 -0700 | [diff] [blame] | 54 | void handleUpgrade(const Request& req, |
Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 55 | const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/, |
Ed Tanous | 003301a | 2024-04-16 09:59:19 -0700 | [diff] [blame] | 56 | boost::asio::ssl::stream<boost::asio::ip::tcp::socket>&& |
Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 57 | adaptor) override |
| 58 | { |
| 59 | std::shared_ptr<crow::sse_socket::ConnectionImpl< |
Ed Tanous | 003301a | 2024-04-16 09:59:19 -0700 | [diff] [blame] | 60 | boost::asio::ssl::stream<boost::asio::ip::tcp::socket>>> |
Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 61 | myConnection = std::make_shared<crow::sse_socket::ConnectionImpl< |
Ed Tanous | 003301a | 2024-04-16 09:59:19 -0700 | [diff] [blame] | 62 | boost::asio::ssl::stream<boost::asio::ip::tcp::socket>>>( |
Ed Tanous | 93cf0ac | 2024-03-28 00:35:13 -0700 | [diff] [blame] | 63 | std::move(adaptor), openHandler, closeHandler); |
Ed Tanous | f80a87f | 2024-06-16 12:10:33 -0700 | [diff] [blame] | 64 | myConnection->start(req); |
Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 65 | } |
Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 66 | |
| 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 Tanous | f80a87f | 2024-06-16 12:10:33 -0700 | [diff] [blame] | 82 | std::function<void(crow::sse_socket::Connection&, const crow::Request&)> |
| 83 | openHandler; |
Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 84 | std::function<void(crow::sse_socket::Connection&)> closeHandler; |
| 85 | }; |
| 86 | |
| 87 | } // namespace crow |