Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1 | #pragma once |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 2 | #include <array> |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 3 | #include <async_resp.hpp> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 4 | #include <boost/algorithm/string/predicate.hpp> |
Ed Tanous | 609145a | 2018-09-05 16:27:36 -0700 | [diff] [blame] | 5 | #include <boost/asio/buffer.hpp> |
Ed Tanous | 1b0044b | 2018-08-03 14:30:05 -0700 | [diff] [blame] | 6 | #include <boost/beast/websocket.hpp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 7 | #include <functional> |
| 8 | |
Ed Tanous | c94ad49 | 2019-10-10 15:39:33 -0700 | [diff] [blame] | 9 | #include "http_request.h" |
Ed Tanous | 1b0044b | 2018-08-03 14:30:05 -0700 | [diff] [blame] | 10 | |
| 11 | #ifdef BMCWEB_ENABLE_SSL |
| 12 | #include <boost/beast/websocket/ssl.hpp> |
| 13 | #endif |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 14 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 15 | namespace crow |
| 16 | { |
| 17 | namespace websocket |
| 18 | { |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 19 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 20 | struct Connection : std::enable_shared_from_this<Connection> |
| 21 | { |
| 22 | public: |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 23 | explicit Connection(const crow::Request& reqIn) : |
| 24 | req(reqIn.req), userdataPtr(nullptr){}; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 25 | |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 26 | virtual void sendBinary(const std::string_view msg) = 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 27 | virtual void sendBinary(std::string&& msg) = 0; |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 28 | virtual void sendText(const std::string_view msg) = 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 29 | virtual void sendText(std::string&& msg) = 0; |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 30 | virtual void close(const std::string_view msg = "quit") = 0; |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 31 | virtual boost::asio::io_context& get_io_context() = 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 32 | virtual ~Connection() = default; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 33 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 34 | void userdata(void* u) |
| 35 | { |
| 36 | userdataPtr = u; |
| 37 | } |
| 38 | void* userdata() |
| 39 | { |
| 40 | return userdataPtr; |
| 41 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 42 | |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 43 | boost::beast::http::request<boost::beast::http::string_body> req; |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 44 | crow::Response res; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 45 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 46 | private: |
| 47 | void* userdataPtr; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 50 | template <typename Adaptor> class ConnectionImpl : public Connection |
| 51 | { |
| 52 | public: |
| 53 | ConnectionImpl( |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 54 | const crow::Request& reqIn, Adaptor adaptorIn, |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 55 | std::function<void(Connection&, std::shared_ptr<bmcweb::AsyncResp>)> |
| 56 | open_handler, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 57 | std::function<void(Connection&, const std::string&, bool)> |
| 58 | message_handler, |
| 59 | std::function<void(Connection&, const std::string&)> close_handler, |
| 60 | std::function<void(Connection&)> error_handler) : |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 61 | Connection(reqIn), |
Adriana Kobylak | 69664cf | 2019-03-12 10:49:48 -0500 | [diff] [blame] | 62 | ws(std::move(adaptorIn)), inString(), inBuffer(inString, 131088), |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 63 | openHandler(std::move(open_handler)), |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 64 | messageHandler(std::move(message_handler)), |
| 65 | closeHandler(std::move(close_handler)), |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 66 | errorHandler(std::move(error_handler)) |
| 67 | { |
| 68 | BMCWEB_LOG_DEBUG << "Creating new connection " << this; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 69 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 70 | |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 71 | boost::asio::io_context& get_io_context() override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 72 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 73 | return static_cast<boost::asio::io_context&>( |
| 74 | ws.get_executor().context()); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 75 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 76 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 77 | void start() |
| 78 | { |
| 79 | BMCWEB_LOG_DEBUG << "starting connection " << this; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 80 | |
Ed Tanous | fe5b216 | 2019-05-22 14:28:16 -0700 | [diff] [blame] | 81 | using bf = boost::beast::http::field; |
| 82 | |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 83 | std::string_view protocol = req[bf::sec_websocket_protocol]; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 84 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 85 | // Perform the websocket upgrade |
| 86 | ws.async_accept_ex( |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 87 | req, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 88 | [protocol{std::string(protocol)}]( |
| 89 | boost::beast::websocket::response_type& m) { |
| 90 | if (!protocol.empty()) |
| 91 | { |
Ed Tanous | fe5b216 | 2019-05-22 14:28:16 -0700 | [diff] [blame] | 92 | m.insert(bf::sec_websocket_protocol, protocol); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 93 | } |
Ed Tanous | fe5b216 | 2019-05-22 14:28:16 -0700 | [diff] [blame] | 94 | |
| 95 | m.insert(bf::strict_transport_security, "max-age=31536000; " |
| 96 | "includeSubdomains; " |
| 97 | "preload"); |
| 98 | m.insert(bf::pragma, "no-cache"); |
| 99 | m.insert(bf::cache_control, "no-Store,no-Cache"); |
| 100 | m.insert("Content-Security-Policy", "default-src 'self'"); |
| 101 | m.insert("X-XSS-Protection", "1; " |
| 102 | "mode=block"); |
| 103 | m.insert("X-Content-Type-Options", "nosniff"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 104 | }, |
| 105 | [this, self(shared_from_this())](boost::system::error_code ec) { |
| 106 | if (ec) |
| 107 | { |
| 108 | BMCWEB_LOG_ERROR << "Error in ws.async_accept " << ec; |
| 109 | return; |
| 110 | } |
| 111 | acceptDone(); |
| 112 | }); |
| 113 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 114 | |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 115 | void sendBinary(const std::string_view msg) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 116 | { |
| 117 | ws.binary(true); |
| 118 | outBuffer.emplace_back(msg); |
| 119 | doWrite(); |
| 120 | } |
| 121 | |
| 122 | void sendBinary(std::string&& msg) override |
| 123 | { |
| 124 | ws.binary(true); |
| 125 | outBuffer.emplace_back(std::move(msg)); |
| 126 | doWrite(); |
| 127 | } |
| 128 | |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 129 | void sendText(const std::string_view msg) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 130 | { |
| 131 | ws.text(true); |
| 132 | outBuffer.emplace_back(msg); |
| 133 | doWrite(); |
| 134 | } |
| 135 | |
| 136 | void sendText(std::string&& msg) override |
| 137 | { |
| 138 | ws.text(true); |
| 139 | outBuffer.emplace_back(std::move(msg)); |
| 140 | doWrite(); |
| 141 | } |
| 142 | |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 143 | void close(const std::string_view msg) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 144 | { |
| 145 | ws.async_close( |
| 146 | boost::beast::websocket::close_code::normal, |
Ed Tanous | 43b761d | 2019-02-13 20:10:56 -0800 | [diff] [blame] | 147 | [self(shared_from_this())](boost::system::error_code ec) { |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 148 | if (ec == boost::asio::error::operation_aborted) |
| 149 | { |
| 150 | return; |
| 151 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 152 | if (ec) |
| 153 | { |
| 154 | BMCWEB_LOG_ERROR << "Error closing websocket " << ec; |
| 155 | return; |
| 156 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 157 | }); |
| 158 | } |
| 159 | |
| 160 | void acceptDone() |
| 161 | { |
| 162 | BMCWEB_LOG_DEBUG << "Websocket accepted connection"; |
| 163 | |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 164 | auto asyncResp = std::make_shared<bmcweb::AsyncResp>( |
| 165 | res, [this, self(shared_from_this())]() { doRead(); }); |
| 166 | |
| 167 | asyncResp->res.result(boost::beast::http::status::ok); |
| 168 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 169 | if (openHandler) |
| 170 | { |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 171 | openHandler(*this, asyncResp); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 172 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void doRead() |
| 176 | { |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 177 | ws.async_read(inBuffer, |
| 178 | [this, self(shared_from_this())]( |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 179 | boost::beast::error_code ec, std::size_t bytes_read) { |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 180 | if (ec) |
| 181 | { |
| 182 | if (ec != boost::beast::websocket::error::closed) |
| 183 | { |
| 184 | BMCWEB_LOG_ERROR << "doRead error " << ec; |
| 185 | } |
| 186 | if (closeHandler) |
| 187 | { |
| 188 | std::string_view reason = ws.reason().reason; |
| 189 | closeHandler(*this, std::string(reason)); |
| 190 | } |
| 191 | return; |
| 192 | } |
| 193 | if (messageHandler) |
| 194 | { |
| 195 | messageHandler(*this, inString, ws.got_text()); |
| 196 | } |
| 197 | inBuffer.consume(bytes_read); |
| 198 | inString.clear(); |
| 199 | doRead(); |
| 200 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | void doWrite() |
| 204 | { |
| 205 | // If we're already doing a write, ignore the request, it will be picked |
| 206 | // up when the current write is complete |
| 207 | if (doingWrite) |
| 208 | { |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | if (outBuffer.empty()) |
| 213 | { |
| 214 | // Done for now |
| 215 | return; |
| 216 | } |
| 217 | doingWrite = true; |
| 218 | ws.async_write( |
| 219 | boost::asio::buffer(outBuffer.front()), |
| 220 | [this, self(shared_from_this())](boost::beast::error_code ec, |
| 221 | std::size_t bytes_written) { |
| 222 | doingWrite = false; |
| 223 | outBuffer.erase(outBuffer.begin()); |
| 224 | if (ec == boost::beast::websocket::error::closed) |
| 225 | { |
| 226 | // Do nothing here. doRead handler will call the |
| 227 | // closeHandler. |
| 228 | close("Write error"); |
| 229 | return; |
| 230 | } |
| 231 | if (ec) |
| 232 | { |
| 233 | BMCWEB_LOG_ERROR << "Error in ws.async_write " << ec; |
| 234 | return; |
| 235 | } |
| 236 | doWrite(); |
| 237 | }); |
| 238 | } |
| 239 | |
| 240 | private: |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 241 | boost::beast::websocket::stream<Adaptor> ws; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 242 | |
Ed Tanous | 609145a | 2018-09-05 16:27:36 -0700 | [diff] [blame] | 243 | std::string inString; |
| 244 | boost::asio::dynamic_string_buffer<std::string::value_type, |
| 245 | std::string::traits_type, |
| 246 | std::string::allocator_type> |
| 247 | inBuffer; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 248 | std::vector<std::string> outBuffer; |
| 249 | bool doingWrite = false; |
| 250 | |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 251 | std::function<void(Connection&, std::shared_ptr<bmcweb::AsyncResp>)> |
| 252 | openHandler; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 253 | std::function<void(Connection&, const std::string&, bool)> messageHandler; |
| 254 | std::function<void(Connection&, const std::string&)> closeHandler; |
| 255 | std::function<void(Connection&)> errorHandler; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 256 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 257 | } // namespace websocket |
| 258 | } // namespace crow |