From 2a6dea344f6a777f489bbf94eac5b35bd2f3970d Mon Sep 17 00:00:00 2001 From: moparisthebest Date: Tue, 31 Jan 2023 22:01:05 -0500 Subject: [PATCH] Send header Access-Control-Allow-Origin: * for outgoing WebSocket connections, thanks singpolyma --- src/websocket/mod.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/websocket/mod.rs b/src/websocket/mod.rs index a407bd3..3a50e3f 100644 --- a/src/websocket/mod.rs +++ b/src/websocket/mod.rs @@ -1,7 +1,14 @@ use anyhow::Result; use futures::StreamExt; use futures_util::stream::{SplitSink, SplitStream}; -use tokio_tungstenite::{tungstenite::protocol::WebSocketConfig, WebSocketStream}; +use tokio_tungstenite::{ + tungstenite::{ + handshake::server::{Request, Response}, + http::header::ACCESS_CONTROL_ALLOW_ORIGIN, + protocol::WebSocketConfig, + }, + WebSocketStream, +}; #[cfg(feature = "incoming")] pub mod incoming; @@ -29,8 +36,16 @@ impl AsyncReadAndWrite for T {} pub async fn incoming_websocket_connection(stream: Box, max_stanza_size_bytes: usize) -> Result<(StanzaRead, StanzaWrite)> { // accept the websocket - // todo: check SEC_WEBSOCKET_PROTOCOL or ORIGIN ? - let stream = tokio_tungstenite::accept_async_with_config(stream, ws_cfg(max_stanza_size_bytes)).await?; + let stream = tokio_tungstenite::accept_hdr_async_with_config( + stream, + |_request: &Request, mut response: Response| { + // todo: check SEC_WEBSOCKET_PROTOCOL or ORIGIN ? + response.headers_mut().append(ACCESS_CONTROL_ALLOW_ORIGIN, "*".parse().expect("known to be good value")); + Ok(response) + }, + ws_cfg(max_stanza_size_bytes), + ) + .await?; let (in_wr, in_rd) = stream.split();