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: |
| 21 | explicit Connection(const crow::Request& req) : |
| 22 | req(req), userdataPtr(nullptr){}; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 23 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 24 | virtual void sendBinary(const boost::beast::string_view msg) = 0; |
| 25 | virtual void sendBinary(std::string&& msg) = 0; |
| 26 | virtual void sendText(const boost::beast::string_view msg) = 0; |
| 27 | virtual void sendText(std::string&& msg) = 0; |
| 28 | virtual void close(const boost::beast::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 | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame^] | 51 | const crow::Request& req, 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 | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame^] | 57 | inString(), |
| 58 | inBuffer(inString, 4096), ws(std::move(adaptorIn)), Connection(req), |
| 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 | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame^] | 69 | return ws.get_executor().context(); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 70 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 71 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 72 | void start() |
| 73 | { |
| 74 | BMCWEB_LOG_DEBUG << "starting connection " << this; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 75 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 76 | boost::string_view protocol = req.getHeaderValue( |
| 77 | boost::beast::http::field::sec_websocket_protocol); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 78 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 79 | // Perform the websocket upgrade |
| 80 | ws.async_accept_ex( |
| 81 | req.req, |
| 82 | [protocol{std::string(protocol)}]( |
| 83 | boost::beast::websocket::response_type& m) { |
| 84 | if (!protocol.empty()) |
| 85 | { |
| 86 | m.insert(boost::beast::http::field::sec_websocket_protocol, |
| 87 | protocol); |
| 88 | } |
| 89 | }, |
| 90 | [this, self(shared_from_this())](boost::system::error_code ec) { |
| 91 | if (ec) |
| 92 | { |
| 93 | BMCWEB_LOG_ERROR << "Error in ws.async_accept " << ec; |
| 94 | return; |
| 95 | } |
| 96 | acceptDone(); |
| 97 | }); |
| 98 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 99 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 100 | void sendBinary(const boost::beast::string_view msg) override |
| 101 | { |
| 102 | ws.binary(true); |
| 103 | outBuffer.emplace_back(msg); |
| 104 | doWrite(); |
| 105 | } |
| 106 | |
| 107 | void sendBinary(std::string&& msg) override |
| 108 | { |
| 109 | ws.binary(true); |
| 110 | outBuffer.emplace_back(std::move(msg)); |
| 111 | doWrite(); |
| 112 | } |
| 113 | |
| 114 | void sendText(const boost::beast::string_view msg) override |
| 115 | { |
| 116 | ws.text(true); |
| 117 | outBuffer.emplace_back(msg); |
| 118 | doWrite(); |
| 119 | } |
| 120 | |
| 121 | void sendText(std::string&& msg) override |
| 122 | { |
| 123 | ws.text(true); |
| 124 | outBuffer.emplace_back(std::move(msg)); |
| 125 | doWrite(); |
| 126 | } |
| 127 | |
| 128 | void close(const boost::beast::string_view msg) override |
| 129 | { |
| 130 | ws.async_close( |
| 131 | boost::beast::websocket::close_code::normal, |
| 132 | [this, self(shared_from_this())](boost::system::error_code ec) { |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame^] | 133 | if (ec == boost::asio::error::operation_aborted) |
| 134 | { |
| 135 | return; |
| 136 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 137 | if (ec) |
| 138 | { |
| 139 | BMCWEB_LOG_ERROR << "Error closing websocket " << ec; |
| 140 | return; |
| 141 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 142 | }); |
| 143 | } |
| 144 | |
| 145 | void acceptDone() |
| 146 | { |
| 147 | BMCWEB_LOG_DEBUG << "Websocket accepted connection"; |
| 148 | |
| 149 | if (openHandler) |
| 150 | { |
| 151 | openHandler(*this); |
| 152 | } |
| 153 | doRead(); |
| 154 | } |
| 155 | |
| 156 | void doRead() |
| 157 | { |
| 158 | ws.async_read( |
| 159 | inBuffer, [this, self(shared_from_this())]( |
| 160 | boost::beast::error_code ec, std::size_t bytes_read) { |
| 161 | if (ec) |
| 162 | { |
| 163 | if (ec != boost::beast::websocket::error::closed) |
| 164 | { |
| 165 | BMCWEB_LOG_ERROR << "doRead error " << ec; |
| 166 | } |
| 167 | if (closeHandler) |
| 168 | { |
| 169 | boost::beast::string_view reason = ws.reason().reason; |
| 170 | closeHandler(*this, std::string(reason)); |
| 171 | } |
| 172 | return; |
| 173 | } |
| 174 | if (messageHandler) |
| 175 | { |
Ed Tanous | 609145a | 2018-09-05 16:27:36 -0700 | [diff] [blame] | 176 | messageHandler(*this, inString, ws.got_text()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 177 | } |
Ed Tanous | 609145a | 2018-09-05 16:27:36 -0700 | [diff] [blame] | 178 | inBuffer.consume(bytes_read); |
| 179 | inString.clear(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 180 | doRead(); |
| 181 | }); |
| 182 | } |
| 183 | |
| 184 | void doWrite() |
| 185 | { |
| 186 | // If we're already doing a write, ignore the request, it will be picked |
| 187 | // up when the current write is complete |
| 188 | if (doingWrite) |
| 189 | { |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | if (outBuffer.empty()) |
| 194 | { |
| 195 | // Done for now |
| 196 | return; |
| 197 | } |
| 198 | doingWrite = true; |
| 199 | ws.async_write( |
| 200 | boost::asio::buffer(outBuffer.front()), |
| 201 | [this, self(shared_from_this())](boost::beast::error_code ec, |
| 202 | std::size_t bytes_written) { |
| 203 | doingWrite = false; |
| 204 | outBuffer.erase(outBuffer.begin()); |
| 205 | if (ec == boost::beast::websocket::error::closed) |
| 206 | { |
| 207 | // Do nothing here. doRead handler will call the |
| 208 | // closeHandler. |
| 209 | close("Write error"); |
| 210 | return; |
| 211 | } |
| 212 | if (ec) |
| 213 | { |
| 214 | BMCWEB_LOG_ERROR << "Error in ws.async_write " << ec; |
| 215 | return; |
| 216 | } |
| 217 | doWrite(); |
| 218 | }); |
| 219 | } |
| 220 | |
| 221 | private: |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame^] | 222 | boost::beast::websocket::stream<Adaptor> ws; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 223 | |
Ed Tanous | 609145a | 2018-09-05 16:27:36 -0700 | [diff] [blame] | 224 | std::string inString; |
| 225 | boost::asio::dynamic_string_buffer<std::string::value_type, |
| 226 | std::string::traits_type, |
| 227 | std::string::allocator_type> |
| 228 | inBuffer; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 229 | std::vector<std::string> outBuffer; |
| 230 | bool doingWrite = false; |
| 231 | |
| 232 | std::function<void(Connection&)> openHandler; |
| 233 | std::function<void(Connection&, const std::string&, bool)> messageHandler; |
| 234 | std::function<void(Connection&, const std::string&)> closeHandler; |
| 235 | std::function<void(Connection&)> errorHandler; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 236 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 237 | } // namespace websocket |
| 238 | } // namespace crow |