Middleware support for websockets
I would like to be able to use middleware for websocket connections. For instance, I would like to be able to deny connections before they are upgraded to websockets based on http headers, etc.
You can use this: https://github.com/zxmarcos/crow/commit/bea1ba3797fbf8bbab209edbbf765d0e6d5960d1
I am using this to check for a HTTP header that contains my auth token. If you return false in onaccept handler then connection will be closed.
It seems like there is only partial support. When the onaccept fires the "middleware_context" is null. For example when using "crow::SessionMiddleware" I can't seem to get a valid session inside of the onaccept method. I get a read violation when I try.
crow::App<crow::CookieParser, crow::SessionMiddleware<crow::InMemoryStore>> app{crow::SessionMiddleware<crow::InMemoryStore>{crow::InMemoryStore{}}}; CROW_WEBSOCKET_ROUTE(app, "/play") .onaccept([&](const crow::request &req, void **userdata) { auto &session = app.get_context<crow::SessionMiddleware<crow::InMemoryStore>>(req); if (!session.exists()) { return false; } }) .onopen([&](crow::websocket::connection &conn) { }) .onclose([&](crow::websocket::connection &conn, const std::string &reason) { }) .onmessage([&](crow::websocket::connection &conn, const std::string &data, bool is_binary) { });