1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 00:21:08 +01:00

Allow to disable masking for websockets client

This commit is contained in:
Nikolay Kim 2018-06-21 09:38:59 +06:00
parent 280eae4335
commit cfe6725eb4
2 changed files with 23 additions and 3 deletions

View File

@ -50,6 +50,10 @@
* Remove `HttpMessage::range()` * Remove `HttpMessage::range()`
## [0.6.14] - 2018-06-21
* Allow to disable masking for websockets client
## [0.6.13] - 2018-06-11 ## [0.6.13] - 2018-06-11

View File

@ -127,6 +127,7 @@ pub struct Client {
protocols: Option<String>, protocols: Option<String>,
conn: Addr<ClientConnector>, conn: Addr<ClientConnector>,
max_size: usize, max_size: usize,
no_masking: bool,
} }
impl Client { impl Client {
@ -144,6 +145,7 @@ impl Client {
origin: None, origin: None,
protocols: None, protocols: None,
max_size: 65_536, max_size: 65_536,
no_masking: false,
conn, conn,
}; };
cl.request.uri(uri.as_ref()); cl.request.uri(uri.as_ref());
@ -198,6 +200,12 @@ impl Client {
self 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 /// Set request header
pub fn header<K, V>(mut self, key: K, value: V) -> Self pub fn header<K, V>(mut self, key: K, value: V) -> Self
where where
@ -260,7 +268,7 @@ impl Client {
} }
// start handshake // start handshake
ClientHandshake::new(request, self.max_size) ClientHandshake::new(request, self.max_size, self.no_masking)
} }
} }
} }
@ -281,10 +289,13 @@ pub struct ClientHandshake {
key: String, key: String,
error: Option<ClientError>, error: Option<ClientError>,
max_size: usize, max_size: usize,
no_masking: bool,
} }
impl ClientHandshake { 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. // Generate a random key for the `Sec-WebSocket-Key` header.
// a base64-encoded (see Section 4 of [RFC4648]) value that, // a base64-encoded (see Section 4 of [RFC4648]) value that,
// when decoded, is 16 bytes in length (RFC 6455) // when decoded, is 16 bytes in length (RFC 6455)
@ -304,6 +315,7 @@ impl ClientHandshake {
ClientHandshake { ClientHandshake {
key, key,
max_size, max_size,
no_masking,
request: Some(request.send()), request: Some(request.send()),
tx: Some(tx), tx: Some(tx),
error: None, error: None,
@ -317,6 +329,7 @@ impl ClientHandshake {
tx: None, tx: None,
error: Some(err), error: Some(err),
max_size: 0, max_size: 0,
no_masking: false,
} }
} }
@ -427,6 +440,7 @@ impl Future for ClientHandshake {
ClientReader { ClientReader {
inner: Rc::clone(&inner), inner: Rc::clone(&inner),
max_size: self.max_size, max_size: self.max_size,
no_masking: self.no_masking,
}, },
ClientWriter { inner }, ClientWriter { inner },
))) )))
@ -437,6 +451,7 @@ impl Future for ClientHandshake {
pub struct ClientReader { pub struct ClientReader {
inner: Rc<RefCell<Inner>>, inner: Rc<RefCell<Inner>>,
max_size: usize, max_size: usize,
no_masking: bool,
} }
impl fmt::Debug for ClientReader { impl fmt::Debug for ClientReader {
@ -451,13 +466,14 @@ impl Stream for ClientReader {
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> { fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
let max_size = self.max_size; let max_size = self.max_size;
let no_masking = self.no_masking;
let mut inner = self.inner.borrow_mut(); let mut inner = self.inner.borrow_mut();
if inner.closed { if inner.closed {
return Ok(Async::Ready(None)); return Ok(Async::Ready(None));
} }
// read // 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))) => { Ok(Async::Ready(Some(frame))) => {
let (_finished, opcode, payload) = frame.unpack(); let (_finished, opcode, payload) = frame.unpack();