Repository: websockets-rs/rust-websocket (language: rust)

Issue:
Unmasked data error
I have errors with version 0.14:

```
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value:
DataFrameError("Expected unmasked data frame")', ../src/libcore\result.rs:741
```

When I try `hyper` example Chrome also logs:

```
WebSocket connection to 'ws://127.0.0.1:2794/' failed:
A server must not mask any frames that it sends to the client.
```

Any example doesn't work. How to fix?

Env:
64 bit + rustc 1.6.0-nightly (1805bba39 2015-11-26) + Win 10



Discussion / hints:
Looks like `Server` masks ows frames and expects unmasked frames from client...

```
5.  Data Framing

5.1.  Overview

   In the WebSocket Protocol, data is transmitted using a sequence of
   frames.  To avoid confusing network intermediaries (such as
   intercepting proxies) and for security reasons that are further
   discussed in Section 10.3, a client MUST mask all frames that it
   sends to the server (see Section 5.3 for further details).  (Note
   that masking is done whether or not the WebSocket Protocol is running
   over TLS.)  The server MUST close the connection upon receiving a
   frame that is not masked.  In this case, a server MAY send a Close
   frame with a status code of 1002 (protocol error) as defined in
   Section 7.4.1.  A server MUST NOT mask any frames that it sends to
   the client.  A client MUST close a connection if it detects a masked
   frame.  In this case, it MAY use the status code 1002 (protocol
   error) as defined in Section 7.4.1.  (These rules might be relaxed in
   a future specification.)
```


Fix patch:
diff --git a/src/client/response.rs b/src/client/response.rs
index 4a7477c186..880646037b 100644
--- a/src/client/response.rs
+++ b/src/client/response.rs
@@ -129,8 +129,8 @@ impl<R: Read, W: Write> Response<R, W> {
 	/// Does not check if the response was valid. Use `validate()` to ensure that the response constitutes a successful handshake.
 	pub fn begin(self) -> Client<DataFrame, Sender<W>, Receiver<R>> {
 		let (reader, writer) = self.into_inner();
-		let sender = Sender::new(writer);
-		let receiver = Receiver::new(reader.into_inner());
+		let sender = Sender::new(writer, true);
+		let receiver = Receiver::new(reader.into_inner(), false);
 		Client::new(sender, receiver)
 	}
 }
diff --git a/src/receiver.rs b/src/receiver.rs
index e90cdad6c5..c00c4110c3 100644
--- a/src/receiver.rs
+++ b/src/receiver.rs
@@ -14,16 +14,18 @@ use ws;
 /// DataFrames and Messages.
 pub struct Receiver<R> {
 	inner: BufReader<R>,
-	buffer: Vec<DataFrame>
+	buffer: Vec<DataFrame>,
+	mask: bool,
 }
 
 impl<R> Receiver<R>
 where R: Read {
 	/// Create a new Receiver using the specified Reader.
-	pub fn new(reader: R) -> Receiver<R> {
+	pub fn new(reader: R, mask: bool) -> Receiver<R> {
 		Receiver {
 			inner: BufReader::new(reader),
-			buffer: Vec::new()
+			buffer: Vec::new(),
+			mask: mask,
 		}
 	}
 	/// Returns a reference to the underlying Reader.
@@ -53,7 +55,7 @@ impl Receiver<WebSocketStream> {
 impl<R: Read> ws::Receiver<DataFrame> for Receiver<R> {
 	/// Reads a single data frame from the remote endpoint.
 	fn recv_dataframe(&mut self) -> WebSocketResult<DataFrame> {
-		DataFrame::read_dataframe(&mut self.inner, false)
+		DataFrame::read_dataframe(&mut self.inner, self.mask)
 	}
 	/// Returns the data frames that constitute one message.
 	fn recv_message_dataframes(&mut self) -> WebSocketResult<Vec<DataFrame>> {
diff --git a/src/sender.rs b/src/sender.rs
index 8744be7257..62e41ad599 100644
--- a/src/sender.rs
+++ b/src/sender.rs
@@ -11,14 +11,16 @@ use ws;
 /// A Sender that wraps a Writer and provides a default implementation using
 /// DataFrames and Messages.
 pub struct Sender<W> {
-	inner: W
+	inner: W,
+	mask: bool,
 }
 
 impl<W> Sender<W> {
 	/// Create a new WebSocketSender using the specified Writer.
-	pub fn new(writer: W) -> Sender<W> {
+	pub fn new(writer: W, mask: bool) -> Sender<W> {
 		Sender {
-			inner: writer
+			inner: writer,
+			mask: mask,
 		}
 	}
 	/// Returns a reference to the underlying Writer.
@@ -49,6 +51,6 @@ impl<W: Write> ws::Sender for Sender<W> {
 	/// Sends a single data frame to the remote endpoint.
 	fn send_dataframe<D>(&mut self, dataframe: &D) -> WebSocketResult<()>
 	where D: DataFrame {
-		dataframe.write_to(&mut self.inner, true)
+		dataframe.write_to(&mut self.inner, self.mask)
 	}
 }
diff --git a/src/server/response.rs b/src/server/response.rs
index 963a1aee5e..9f7516d697 100644
--- a/src/server/response.rs
+++ b/src/server/response.rs
@@ -142,8 +142,8 @@ impl<R: Read, W: Write> Response<R, W> {
 		try!(write!(self.get_mut_writer(), "{} {}\r\n", version, status));
 		try!(write!(self.get_mut_writer(), "{}\r\n", headers));
 		let (reader, writer) = self.into_inner();
-		let sender = Sender::new(writer);
-		let receiver = Receiver::new(reader);
+		let sender = Sender::new(writer, false);
+		let receiver = Receiver::new(reader, true);
 		Ok(Client::new(sender, receiver))
 	}
 }


Test patch: