140e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0 240e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors 308bbe119SEd Tanous #pragma once 408bbe119SEd Tanous 5d7857201SEd Tanous #include "async_resp.hpp" 608bbe119SEd Tanous #include "baserule.hpp" 7d7857201SEd Tanous #include "http_request.hpp" 8d7857201SEd Tanous #include "logging.hpp" 908bbe119SEd Tanous #include "websocket.hpp" 1008bbe119SEd Tanous 11d7857201SEd Tanous #include <boost/asio/ip/tcp.hpp> 12d7857201SEd Tanous #include <boost/asio/ssl/stream.hpp> 13d7857201SEd Tanous #include <boost/beast/http/status.hpp> 1408bbe119SEd Tanous 15d7857201SEd Tanous #include <functional> 1608bbe119SEd Tanous #include <memory> 1708bbe119SEd Tanous #include <string> 18d7857201SEd Tanous #include <string_view> 1908bbe119SEd Tanous #include <vector> 2008bbe119SEd Tanous 2108bbe119SEd Tanous namespace crow 2208bbe119SEd Tanous { 2308bbe119SEd Tanous class WebSocketRule : public BaseRule 2408bbe119SEd Tanous { 2508bbe119SEd Tanous using self_t = WebSocketRule; 2608bbe119SEd Tanous 2708bbe119SEd Tanous public: WebSocketRule(const std::string & ruleIn)28a3b9eb98SEd Tanous explicit WebSocketRule(const std::string& ruleIn) : BaseRule(ruleIn) 29a3b9eb98SEd Tanous { 30a3b9eb98SEd Tanous isUpgrade = true; 31a3b9eb98SEd Tanous // Clear GET handler 32a3b9eb98SEd Tanous methodsBitfield = 0; 33a3b9eb98SEd Tanous } 3408bbe119SEd Tanous validate()3508bbe119SEd Tanous void validate() override {} 3608bbe119SEd Tanous handle(const Request &,const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::vector<std::string> &)3708bbe119SEd Tanous void handle(const Request& /*req*/, 3808bbe119SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 3908bbe119SEd Tanous const std::vector<std::string>& /*params*/) override 4008bbe119SEd Tanous { 41a3b9eb98SEd Tanous BMCWEB_LOG_ERROR( 42a3b9eb98SEd Tanous "Handle called on websocket rule. This should never happen"); 43a3b9eb98SEd Tanous asyncResp->res.result( 44a3b9eb98SEd Tanous boost::beast::http::status::internal_server_error); 4508bbe119SEd Tanous } 4608bbe119SEd Tanous 4708bbe119SEd Tanous void handleUpgrade(const Request& req, 4808bbe119SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/, 49*beb96b0bSEd Tanous boost::asio::ip::tcp::socket&& adaptor) override; 508db83747SEd Tanous 5108bbe119SEd Tanous void handleUpgrade(const Request& req, 5208bbe119SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/, 53003301a2SEd Tanous boost::asio::ssl::stream<boost::asio::ip::tcp::socket>&& 54*beb96b0bSEd Tanous adaptor) override; 5508bbe119SEd Tanous 5608bbe119SEd Tanous template <typename Func> onopen(Func f)5708bbe119SEd Tanous self_t& onopen(Func f) 5808bbe119SEd Tanous { 5908bbe119SEd Tanous openHandler = f; 6008bbe119SEd Tanous return *this; 6108bbe119SEd Tanous } 6208bbe119SEd Tanous 6308bbe119SEd Tanous template <typename Func> onmessage(Func f)6408bbe119SEd Tanous self_t& onmessage(Func f) 6508bbe119SEd Tanous { 6608bbe119SEd Tanous messageHandler = f; 6708bbe119SEd Tanous return *this; 6808bbe119SEd Tanous } 6908bbe119SEd Tanous 7008bbe119SEd Tanous template <typename Func> onmessageex(Func f)7108bbe119SEd Tanous self_t& onmessageex(Func f) 7208bbe119SEd Tanous { 7308bbe119SEd Tanous messageExHandler = f; 7408bbe119SEd Tanous return *this; 7508bbe119SEd Tanous } 7608bbe119SEd Tanous 7708bbe119SEd Tanous template <typename Func> onclose(Func f)7808bbe119SEd Tanous self_t& onclose(Func f) 7908bbe119SEd Tanous { 8008bbe119SEd Tanous closeHandler = f; 8108bbe119SEd Tanous return *this; 8208bbe119SEd Tanous } 8308bbe119SEd Tanous 8408bbe119SEd Tanous template <typename Func> onerror(Func f)8508bbe119SEd Tanous self_t& onerror(Func f) 8608bbe119SEd Tanous { 8708bbe119SEd Tanous errorHandler = f; 8808bbe119SEd Tanous return *this; 8908bbe119SEd Tanous } 9008bbe119SEd Tanous 9108bbe119SEd Tanous protected: 9208bbe119SEd Tanous std::function<void(crow::websocket::Connection&)> openHandler; 9308bbe119SEd Tanous std::function<void(crow::websocket::Connection&, const std::string&, bool)> 9408bbe119SEd Tanous messageHandler; 9508bbe119SEd Tanous std::function<void(crow::websocket::Connection&, std::string_view, 9608bbe119SEd Tanous crow::websocket::MessageType type, 9708bbe119SEd Tanous std::function<void()>&& whenComplete)> 9808bbe119SEd Tanous messageExHandler; 9908bbe119SEd Tanous std::function<void(crow::websocket::Connection&, const std::string&)> 10008bbe119SEd Tanous closeHandler; 10108bbe119SEd Tanous std::function<void(crow::websocket::Connection&)> errorHandler; 10208bbe119SEd Tanous }; 10308bbe119SEd Tanous } // namespace crow 104