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: |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame^] | 23 | explicit Connection(const crow::Request& reqIn, crow::Response& res) : |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 24 | req(reqIn), 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 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 43 | crow::Request 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( |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame^] | 54 | const crow::Request& reqIn, crow::Response& res, Adaptor adaptorIn, |
| 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) : |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame^] | 61 | Connection(reqIn, res), |
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 | |
| 83 | std::string_view protocol = |
| 84 | req.getHeaderValue(bf::sec_websocket_protocol); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 85 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 86 | // Perform the websocket upgrade |
| 87 | ws.async_accept_ex( |
| 88 | req.req, |
| 89 | [protocol{std::string(protocol)}]( |
| 90 | boost::beast::websocket::response_type& m) { |
| 91 | if (!protocol.empty()) |
| 92 | { |
Ed Tanous | fe5b216 | 2019-05-22 14:28:16 -0700 | [diff] [blame] | 93 | m.insert(bf::sec_websocket_protocol, protocol); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 94 | } |
Ed Tanous | fe5b216 | 2019-05-22 14:28:16 -0700 | [diff] [blame] | 95 | |
| 96 | m.insert(bf::strict_transport_security, "max-age=31536000; " |
| 97 | "includeSubdomains; " |
| 98 | "preload"); |
| 99 | m.insert(bf::pragma, "no-cache"); |
| 100 | m.insert(bf::cache_control, "no-Store,no-Cache"); |
| 101 | m.insert("Content-Security-Policy", "default-src 'self'"); |
| 102 | m.insert("X-XSS-Protection", "1; " |
| 103 | "mode=block"); |
| 104 | m.insert("X-Content-Type-Options", "nosniff"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 105 | }, |
| 106 | [this, self(shared_from_this())](boost::system::error_code ec) { |
| 107 | if (ec) |
| 108 | { |
| 109 | BMCWEB_LOG_ERROR << "Error in ws.async_accept " << ec; |
| 110 | return; |
| 111 | } |
| 112 | acceptDone(); |
| 113 | }); |
| 114 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 115 | |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 116 | void sendBinary(const std::string_view msg) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 117 | { |
| 118 | ws.binary(true); |
| 119 | outBuffer.emplace_back(msg); |
| 120 | doWrite(); |
| 121 | } |
| 122 | |
| 123 | void sendBinary(std::string&& msg) override |
| 124 | { |
| 125 | ws.binary(true); |
| 126 | outBuffer.emplace_back(std::move(msg)); |
| 127 | doWrite(); |
| 128 | } |
| 129 | |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 130 | void sendText(const std::string_view msg) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 131 | { |
| 132 | ws.text(true); |
| 133 | outBuffer.emplace_back(msg); |
| 134 | doWrite(); |
| 135 | } |
| 136 | |
| 137 | void sendText(std::string&& msg) override |
| 138 | { |
| 139 | ws.text(true); |
| 140 | outBuffer.emplace_back(std::move(msg)); |
| 141 | doWrite(); |
| 142 | } |
| 143 | |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 144 | void close(const std::string_view msg) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 145 | { |
| 146 | ws.async_close( |
| 147 | boost::beast::websocket::close_code::normal, |
Ed Tanous | 43b761d | 2019-02-13 20:10:56 -0800 | [diff] [blame] | 148 | [self(shared_from_this())](boost::system::error_code ec) { |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 149 | if (ec == boost::asio::error::operation_aborted) |
| 150 | { |
| 151 | return; |
| 152 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 153 | if (ec) |
| 154 | { |
| 155 | BMCWEB_LOG_ERROR << "Error closing websocket " << ec; |
| 156 | return; |
| 157 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 158 | }); |
| 159 | } |
| 160 | |
| 161 | void acceptDone() |
| 162 | { |
| 163 | BMCWEB_LOG_DEBUG << "Websocket accepted connection"; |
| 164 | |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame^] | 165 | auto asyncResp = std::make_shared<bmcweb::AsyncResp>( |
| 166 | res, [this, self(shared_from_this())]() { doRead(); }); |
| 167 | |
| 168 | asyncResp->res.result(boost::beast::http::status::ok); |
| 169 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 170 | if (openHandler) |
| 171 | { |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame^] | 172 | openHandler(*this, asyncResp); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 173 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | void doRead() |
| 177 | { |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 178 | ws.async_read(inBuffer, |
| 179 | [this, self(shared_from_this())]( |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 180 | boost::beast::error_code ec, std::size_t bytes_read) { |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 181 | if (ec) |
| 182 | { |
| 183 | if (ec != boost::beast::websocket::error::closed) |
| 184 | { |
| 185 | BMCWEB_LOG_ERROR << "doRead error " << ec; |
| 186 | } |
| 187 | if (closeHandler) |
| 188 | { |
| 189 | std::string_view reason = ws.reason().reason; |
| 190 | closeHandler(*this, std::string(reason)); |
| 191 | } |
| 192 | return; |
| 193 | } |
| 194 | if (messageHandler) |
| 195 | { |
| 196 | messageHandler(*this, inString, ws.got_text()); |
| 197 | } |
| 198 | inBuffer.consume(bytes_read); |
| 199 | inString.clear(); |
| 200 | doRead(); |
| 201 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | void doWrite() |
| 205 | { |
| 206 | // If we're already doing a write, ignore the request, it will be picked |
| 207 | // up when the current write is complete |
| 208 | if (doingWrite) |
| 209 | { |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | if (outBuffer.empty()) |
| 214 | { |
| 215 | // Done for now |
| 216 | return; |
| 217 | } |
| 218 | doingWrite = true; |
| 219 | ws.async_write( |
| 220 | boost::asio::buffer(outBuffer.front()), |
| 221 | [this, self(shared_from_this())](boost::beast::error_code ec, |
| 222 | std::size_t bytes_written) { |
| 223 | doingWrite = false; |
| 224 | outBuffer.erase(outBuffer.begin()); |
| 225 | if (ec == boost::beast::websocket::error::closed) |
| 226 | { |
| 227 | // Do nothing here. doRead handler will call the |
| 228 | // closeHandler. |
| 229 | close("Write error"); |
| 230 | return; |
| 231 | } |
| 232 | if (ec) |
| 233 | { |
| 234 | BMCWEB_LOG_ERROR << "Error in ws.async_write " << ec; |
| 235 | return; |
| 236 | } |
| 237 | doWrite(); |
| 238 | }); |
| 239 | } |
| 240 | |
| 241 | private: |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 242 | boost::beast::websocket::stream<Adaptor> ws; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 243 | |
Ed Tanous | 609145a | 2018-09-05 16:27:36 -0700 | [diff] [blame] | 244 | std::string inString; |
| 245 | boost::asio::dynamic_string_buffer<std::string::value_type, |
| 246 | std::string::traits_type, |
| 247 | std::string::allocator_type> |
| 248 | inBuffer; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 249 | std::vector<std::string> outBuffer; |
| 250 | bool doingWrite = false; |
| 251 | |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame^] | 252 | std::function<void(Connection&, std::shared_ptr<bmcweb::AsyncResp>)> |
| 253 | openHandler; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 254 | std::function<void(Connection&, const std::string&, bool)> messageHandler; |
| 255 | std::function<void(Connection&, const std::string&)> closeHandler; |
| 256 | std::function<void(Connection&)> errorHandler; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 257 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 258 | } // namespace websocket |
| 259 | } // namespace crow |