snapview / tokio-tungstenite

Future-based Tungstenite for Tokio. Lightweight stream-based WebSocket implementation
MIT License
1.88k stars 236 forks source link

Headers validation checking. #295

Closed blackshadowsoftwareltd closed 1 year ago

blackshadowsoftwareltd commented 1 year ago

I am new in Rust. I read the example of the server-headers.rs file. but I am not understanding how can I check and validate the client-given header when a connection going to be established.

suppose I have a const header like "x". when a client wants to be connected to the server, the client must provide the "x" header. if the client does not submit this header? The server won't be connected.

Please give me some idea or any solution. Thanks

agalakhov commented 1 year ago

Just return Err from the callback instead of Ok, and the server will refuse to connect.

blackshadowsoftwareltd commented 1 year ago

Just return Err from the callback instead of Ok, and the server will refuse to connect.

Is it here?

agalakhov commented 1 year ago

Yes. https://docs.rs/tungstenite/0.20.0/tungstenite/fn.accept_hdr.html https://docs.rs/tungstenite/0.20.0/tungstenite/handshake/server/trait.Callback.html

blackshadowsoftwareltd commented 1 year ago

Just return Err from the callback instead of Ok, and the server will refuse to connect.

not working if I return Err().

Err(tungstenite::Error::ConnectionClosed)
expected `[closure@event.rs:29:24]` to be a closure that returns `Result<Response<()>, Response<Option<String>>>`, but it returns `Result<_, Error>`
expected enum `Result<Response<()>, Response<Option<String>>>`
   found enum `Result<_, tokio_tungstenite::tungstenite::Error>`
required for `[closure@src/events/event.rs:29:24: 29:63]` to implement

Error showing on callback in line:

let stream = tokio_tungstenite::accept_hdr_async(raw_stream, callback)
            .await
            .expect("Failed");