crow icon indicating copy to clipboard operation
crow copied to clipboard

Middleware support for websockets

Open mafrost opened this issue 9 years ago • 2 comments

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.

mafrost avatar Nov 30 '16 22:11 mafrost

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.

zxmarcos avatar Dec 28 '16 13:12 zxmarcos

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) { });

disks86 avatar Oct 15 '23 03:10 disks86