websocketpp
websocketpp copied to clipboard
CORS setting
Hi. I am having some issues with Chrome's security when delivering video content in my test env to the web browser.Some of the suggestions I get to solve my issue is to reconfigure CORS in the header to allow all origins. That's setting with Access-Control-Allow-Origin: * . Is it something that can be configured on TCP socket header level? If yes,how?
Thanks.
TCP socket level, no, but the WebSocket handshake can return HTTP headers which can be used to carry the CORS information. In your WebSocket++ validate handler, use connection::append_header
(https://docs.websocketpp.org/classwebsocketpp_1_1connection.html#af8cb78acc61b8a3d59f6162cdba6318b) or connection::replace_header
(https://docs.websocketpp.org/classwebsocketpp_1_1connection.html#a96e4adcdb9b3473ca96e2aac27ff7147) as appropriate.
I tried to do this, but the validate_handler never gets called. I must be missing something:
bool validate(server* s, websocketpp::connection_hdl connection) {
s->get_con_from_hdl(connection)->append_header("Access-Control-Allow-Origin", "*");
return true;
}
WebsocketServer::WebsocketServer() {
m_server.init_asio();
m_server.set_open_handler(bind(&WebsocketServer::on_open, this, ::_1));
m_server.set_close_handler(bind(&WebsocketServer::on_close, this, ::_1));
m_server.set_message_handler(bind(&WebsocketServer::on_message, this, ::_1, ::_2));
m_server.set_validate_handler(bind(&validate, &m_server, ::_1));
createThread([this](const std::string& content) {
for (auto it : m_connections) {
m_server.send(it, content, websocketpp::frame::opcode::value::text);
}
});
run(3001);
}
I am getting this kind of messages in the console when I try to connect:
[2018-12-18 15:09:39] [error] Handshake ended with HTTP error: 426
[2018-12-18 15:09:39] [fail] WebSocket Connection [::ffff:127.0.0.1]:57762 v0 "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0" /socket.io/?EIO=3&transport=polling&t=MV1ffJg 426 websocketpp:28 Upgrade required
Hello. Did you ever get CORS to work?
websocket_server::connection_ptr con = wsServer->get_con_from_hdl(hdl); con->append_header("access-control-allow-origin", "*");
client::connection_ptr con = echo_client.get_connection( uri, ec ); con->append_header("access-control-allow-origin", "*"); echo_client.connect( con );