From e975124630b0891ba4c41f82074dff606ddefc31 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Thu, 21 Jun 2018 09:38:59 +0600 Subject: [PATCH] Allow to disable masking for websockets client --- CHANGES.md | 5 +++++ src/ws/client.rs | 22 +++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 173c4e689..d3f443101 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,10 @@ # Changes +## [0.6.14] - 2018-06-21 + +* Allow to disable masking for websockets client + + ## [0.6.13] - 2018-06-13 ### Fixed diff --git a/src/ws/client.rs b/src/ws/client.rs index 1f35c1867..42da20758 100644 --- a/src/ws/client.rs +++ b/src/ws/client.rs @@ -113,6 +113,7 @@ pub struct Client { protocols: Option, conn: Addr, max_size: usize, + no_masking: bool, } impl Client { @@ -132,6 +133,7 @@ impl Client { origin: None, protocols: None, max_size: 65_536, + no_masking: false, conn, }; cl.request.uri(uri.as_ref()); @@ -186,6 +188,12 @@ impl Client { self } + /// Disable payload masking. By default ws client masks frame payload. + pub fn no_masking(mut self) -> Self { + self.no_masking = true; + self + } + /// Set request header pub fn header(mut self, key: K, value: V) -> Self where @@ -248,7 +256,7 @@ impl Client { } // start handshake - ClientHandshake::new(request, self.max_size) + ClientHandshake::new(request, self.max_size, self.no_masking) } } } @@ -269,10 +277,13 @@ pub struct ClientHandshake { key: String, error: Option, max_size: usize, + no_masking: bool, } impl ClientHandshake { - fn new(mut request: ClientRequest, max_size: usize) -> ClientHandshake { + fn new( + mut request: ClientRequest, max_size: usize, no_masking: bool, + ) -> ClientHandshake { // Generate a random key for the `Sec-WebSocket-Key` header. // a base64-encoded (see Section 4 of [RFC4648]) value that, // when decoded, is 16 bytes in length (RFC 6455) @@ -292,6 +303,7 @@ impl ClientHandshake { ClientHandshake { key, max_size, + no_masking, request: Some(request.send()), tx: Some(tx), error: None, @@ -305,6 +317,7 @@ impl ClientHandshake { tx: None, error: Some(err), max_size: 0, + no_masking: false, } } @@ -415,6 +428,7 @@ impl Future for ClientHandshake { ClientReader { inner: Rc::clone(&inner), max_size: self.max_size, + no_masking: self.no_masking, }, ClientWriter { inner }, ))) @@ -424,6 +438,7 @@ impl Future for ClientHandshake { pub struct ClientReader { inner: Rc>, max_size: usize, + no_masking: bool, } impl fmt::Debug for ClientReader { @@ -445,13 +460,14 @@ impl Stream for ClientReader { fn poll(&mut self) -> Poll, Self::Error> { let max_size = self.max_size; + let no_masking = self.no_masking; let inner = self.as_mut(); if inner.closed { return Ok(Async::Ready(None)); } // read - match Frame::parse(&mut inner.rx, false, max_size) { + match Frame::parse(&mut inner.rx, no_masking, max_size) { Ok(Async::Ready(Some(frame))) => { let (_finished, opcode, payload) = frame.unpack();