diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..b78c43b --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +# 0.12.0 + +- Add facilities to allow clients to follow HTTP 3XX redirects. +- Allow accepting unmasked clients on the server side to be compatible with some legacy / invalid clients. +- Update of dependencies and documentation fixes. diff --git a/Cargo.toml b/Cargo.toml index c754ea5..eae6255 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,9 +7,9 @@ authors = ["Alexey Galakhov", "Daniel Abramov"] license = "MIT/Apache-2.0" readme = "README.md" homepage = "https://github.com/snapview/tungstenite-rs" -documentation = "https://docs.rs/tungstenite/0.11.1" +documentation = "https://docs.rs/tungstenite/0.12.0" repository = "https://github.com/snapview/tungstenite-rs" -version = "0.11.1" +version = "0.12.0" edition = "2018" [package.metadata.docs.rs] @@ -27,7 +27,7 @@ byteorder = "1.3.2" bytes = "1.0" http = "0.2" httparse = "1.3.4" -input_buffer = { version = "0.4.0", git = "https://github.com/snapview/input_buffer.git" } +input_buffer = "0.4.0" log = "0.4.8" rand = "0.8.0" sha-1 = "0.9" diff --git a/examples/srv_accept_unmasked_frames.rs b/examples/srv_accept_unmasked_frames.rs new file mode 100644 index 0000000..953f9f1 --- /dev/null +++ b/examples/srv_accept_unmasked_frames.rs @@ -0,0 +1,50 @@ +use std::{net::TcpListener, thread::spawn}; +use tungstenite::{ + handshake::server::{Request, Response}, + protocol::WebSocketConfig, + server::accept_hdr_with_config, +}; + +fn main() { + env_logger::init(); + let server = TcpListener::bind("127.0.0.1:3012").unwrap(); + for stream in server.incoming() { + spawn(move || { + let callback = |req: &Request, mut response: Response| { + println!("Received a new ws handshake"); + println!("The request's path is: {}", req.uri().path()); + println!("The request's headers are:"); + for (ref header, _value) in req.headers() { + println!("* {}", header); + } + + // Let's add an additional header to our response to the client. + let headers = response.headers_mut(); + headers.append("MyCustomHeader", ":)".parse().unwrap()); + headers.append("SOME_TUNGSTENITE_HEADER", "header_value".parse().unwrap()); + + Ok(response) + }; + + let config = Some(WebSocketConfig { + max_send_queue: None, + max_message_size: None, + max_frame_size: None, + // This setting allows to accept client frames which are not masked + // This is not in compliance with RFC 6455 but might be handy in some + // rare cases where it is necessary to integrate with existing/legacy + // clients which are sending unmasked frames + accept_unmasked_frames: true, + }); + + let mut websocket = accept_hdr_with_config(stream.unwrap(), callback, config).unwrap(); + + loop { + let msg = websocket.read_message().unwrap(); + if msg.is_binary() || msg.is_text() { + println!("received message {}", msg); + } + } + }); + } +} diff --git a/src/handshake/server.rs b/src/handshake/server.rs index e60acb5..53227ab 100644 --- a/src/handshake/server.rs +++ b/src/handshake/server.rs @@ -94,7 +94,7 @@ pub fn create_response_with_body( } // Assumes that this is a valid response -fn write_response(w: &mut dyn io::Write, response: &HttpResponse) -> Result<()> { +fn write_response(mut w: impl io::Write, response: &HttpResponse) -> Result<()> { writeln!( w, "{version:?} {status}\r", diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs index 72485e9..63763f0 100644 --- a/src/protocol/mod.rs +++ b/src/protocol/mod.rs @@ -50,6 +50,12 @@ pub struct WebSocketConfig { /// be reasonably big for all normal use-cases but small enough to prevent memory eating /// by a malicious user. pub max_frame_size: Option, + /// When set to `true`, the server will accept and handle unmasked frames + /// from the client. According to the RFC 6455, the server must close the + /// connection to the client in such cases, however it seems like there are + /// some popular libraries that are sending unmasked frames, ignoring the RFC. + /// By default this option is set to `false`, i.e. according to RFC 6455. + pub accept_unmasked_frames: bool, } impl Default for WebSocketConfig { @@ -58,6 +64,7 @@ impl Default for WebSocketConfig { max_send_queue: None, max_message_size: Some(64 << 20), max_frame_size: Some(16 << 20), + accept_unmasked_frames: false, } } } @@ -446,9 +453,11 @@ impl WebSocketContext { // A server MUST remove masking for data frames received from a client // as described in Section 5.3. (RFC 6455) frame.apply_mask() - } else { + } else if !self.config.accept_unmasked_frames { // The server MUST close the connection upon receiving a // frame that is not masked. (RFC 6455) + // The only exception here is if the user explicitly accepts given + // stream by setting WebSocketConfig.accept_unmasked_frames to true return Err(Error::Protocol( "Received an unmasked frame from client".into(), ));