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