| 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 |  | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 5 | #include "async_resp.hpp" | 
| Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 6 | #include "baserule.hpp" | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 7 | #include "http_request.hpp" | 
|  | 8 | #include "logging.hpp" | 
| Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 9 | #include "websocket.hpp" | 
|  | 10 |  | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 11 | #include <boost/asio/ip/tcp.hpp> | 
|  | 12 | #include <boost/asio/ssl/stream.hpp> | 
|  | 13 | #include <boost/beast/http/status.hpp> | 
| Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 14 |  | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 15 | #include <functional> | 
| Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 16 | #include <memory> | 
|  | 17 | #include <string> | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 18 | #include <string_view> | 
| Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 19 | #include <vector> | 
|  | 20 |  | 
|  | 21 | namespace crow | 
|  | 22 | { | 
|  | 23 | class WebSocketRule : public BaseRule | 
|  | 24 | { | 
|  | 25 | using self_t = WebSocketRule; | 
|  | 26 |  | 
|  | 27 | public: | 
| Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 28 | explicit WebSocketRule(const std::string& ruleIn) : BaseRule(ruleIn) | 
|  | 29 | { | 
|  | 30 | isUpgrade = true; | 
|  | 31 | // Clear GET handler | 
|  | 32 | methodsBitfield = 0; | 
|  | 33 | } | 
| Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 34 |  | 
|  | 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 Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 41 | 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 Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 45 | } | 
|  | 46 |  | 
| Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 47 | void handleUpgrade(const Request& req, | 
|  | 48 | const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/, | 
| Ed Tanous | beb96b0 | 2025-02-09 09:22:46 -0800 | [diff] [blame] | 49 | boost::asio::ip::tcp::socket&& adaptor) override; | 
| Ed Tanous | 8db8374 | 2024-04-13 09:11:15 -0700 | [diff] [blame] | 50 |  | 
| Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 51 | void handleUpgrade(const Request& req, | 
|  | 52 | const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/, | 
| Ed Tanous | 003301a | 2024-04-16 09:59:19 -0700 | [diff] [blame] | 53 | boost::asio::ssl::stream<boost::asio::ip::tcp::socket>&& | 
| Ed Tanous | beb96b0 | 2025-02-09 09:22:46 -0800 | [diff] [blame] | 54 | adaptor) override; | 
| Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 55 |  | 
|  | 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: | 
| Ed Tanous | 05c2735 | 2024-10-09 17:00:37 -0700 | [diff] [blame] | 92 | std::function<void(websocket::Connection&)> openHandler; | 
|  | 93 | std::function<void(websocket::Connection&, const std::string&, bool)> | 
| Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 94 | messageHandler; | 
| Ed Tanous | 05c2735 | 2024-10-09 17:00:37 -0700 | [diff] [blame] | 95 | std::function<void(websocket::Connection&, std::string_view, | 
|  | 96 | websocket::MessageType type, | 
| Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 97 | std::function<void()>&& whenComplete)> | 
|  | 98 | messageExHandler; | 
| Ed Tanous | 05c2735 | 2024-10-09 17:00:37 -0700 | [diff] [blame] | 99 | std::function<void(websocket::Connection&, const std::string&)> | 
| Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 100 | closeHandler; | 
| Ed Tanous | 05c2735 | 2024-10-09 17:00:37 -0700 | [diff] [blame] | 101 | std::function<void(websocket::Connection&)> errorHandler; | 
| Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 102 | }; | 
|  | 103 | } // namespace crow |