Skip to content

Commit

Permalink
Merge pull request snapview#159 from adambezecny/master
Browse files Browse the repository at this point in the history
WebSocketConfig extended to allow accepting unmasked client frames
  • Loading branch information
daniel-abramov authored Jan 8, 2021
2 parents bd2a43c + c8c2360 commit e1ae387
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
50 changes: 50 additions & 0 deletions examples/srv_accept_unmasked_frames.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
});
}
}
11 changes: 10 additions & 1 deletion src/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize>,
/// 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 {
Expand All @@ -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,
}
}
}
Expand Down Expand Up @@ -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(),
));
Expand Down

0 comments on commit e1ae387

Please sign in to comment.