Skip to content

Commit b61ed96

Browse files
Update to the newest tungstenite-rs version
* Fixes snapview#35 * Replaces snapview/tokio-tungstenite#40 * Gives a good background for snapview/tokio-tungstenite#41
1 parent 8b3f811 commit b61ed96

File tree

3 files changed

+97
-19
lines changed

3 files changed

+97
-19
lines changed

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ stream = ["bytes"]
2020
futures = "0.1.17"
2121

2222
[dependencies.tungstenite]
23-
version = "0.5.3"
23+
version = "0.6.0"
2424
default-features = false
2525

2626
[dependencies.bytes]
@@ -29,7 +29,7 @@ version = "0.4.6"
2929

3030
[dependencies.native-tls]
3131
optional = true
32-
version = "0.1.5"
32+
version = "0.2.0"
3333

3434
[dependencies.tokio-dns-unofficial]
3535
optional = true
@@ -46,3 +46,6 @@ version = "0.1.4"
4646
[dev-dependencies]
4747
tokio-core = "0.1.12"
4848
url = "1.6.0"
49+
50+
[patch.crates-io]
51+
tokio-tls = { git = "https://github.com/aep/tokio-tls.git", rev = "7865734d2167160cabd4422aca76b8478e643b41" }

src/connect.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ mod encryption {
5757
match mode {
5858
Mode::Plain => Box::new(future::ok(StreamSwitcher::Plain(socket))),
5959
Mode::Tls => {
60-
Box::new(future::result(TlsConnector::builder())
61-
.and_then(move |builder| future::result(builder.build()))
60+
Box::new(future::result(TlsConnector::builder().build())
6261
.and_then(move |connector| connector.connect_async(&domain, socket))
6362
.map(|s| StreamSwitcher::Tls(s))
6463
.map_err(|e| Error::Tls(e)))

src/lib.rs

Lines changed: 91 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@ use std::io::ErrorKind;
3131
use futures::{Poll, Future, Async, AsyncSink, Stream, Sink, StartSend};
3232
use tokio::io::{AsyncRead, AsyncWrite};
3333

34-
use tungstenite::handshake::client::{ClientHandshake, Response, Request};
35-
use tungstenite::handshake::server::{ServerHandshake, Callback, NoCallback};
36-
use tungstenite::handshake::{HandshakeRole, HandshakeError};
37-
use tungstenite::protocol::{WebSocket, Message, Role};
38-
use tungstenite::error::Error as WsError;
39-
use tungstenite::server;
34+
use tungstenite::{
35+
error::Error as WsError,
36+
handshake::{
37+
HandshakeRole, HandshakeError,
38+
client::{ClientHandshake, Response, Request},
39+
server::{ServerHandshake, Callback, NoCallback},
40+
},
41+
protocol::{WebSocket, Message, Role, WebSocketConfig},
42+
server,
43+
};
4044

4145
#[cfg(feature="connect")]
4246
pub use connect::{connect_async, client_async_tls};
@@ -53,14 +57,31 @@ pub use connect::{connect_async, client_async_tls};
5357
///
5458
/// This is typically used for clients who have already established, for
5559
/// example, a TCP connection to the remote server.
56-
pub fn client_async<'a, R, S>(request: R, stream: S) -> ConnectAsync<S>
60+
pub fn client_async<'a, R, S>(
61+
request: R,
62+
stream: S,
63+
) -> ConnectAsync<S>
64+
where
65+
R: Into<Request<'a>>,
66+
S: AsyncRead + AsyncWrite
67+
{
68+
client_async_with_config(request, stream, None)
69+
}
70+
71+
/// The same as `client_async()` but the one can specify a websocket configuration.
72+
/// Please refer to `client_async()` for more details.
73+
pub fn client_async_with_config<'a, R, S>(
74+
request: R,
75+
stream: S,
76+
config: Option<WebSocketConfig>,
77+
) -> ConnectAsync<S>
5778
where
5879
R: Into<Request<'a>>,
5980
S: AsyncRead + AsyncWrite
6081
{
6182
ConnectAsync {
6283
inner: MidHandshake {
63-
inner: Some(ClientHandshake::start(stream, request.into()).handshake())
84+
inner: Some(ClientHandshake::start(stream, request.into(), config).handshake())
6485
}
6586
}
6687
}
@@ -83,19 +104,45 @@ where
83104
accept_hdr_async(stream, NoCallback)
84105
}
85106

107+
/// The same as `accept_async()` but the one can specify a websocket configuration.
108+
/// Please refer to `accept_async()` for more details.
109+
pub fn accept_async_with_config<S>(
110+
stream: S,
111+
config: Option<WebSocketConfig>,
112+
) -> AcceptAsync<S, NoCallback>
113+
where
114+
S: AsyncRead + AsyncWrite,
115+
{
116+
accept_hdr_async_with_config(stream, NoCallback, config)
117+
}
118+
86119
/// Accepts a new WebSocket connection with the provided stream.
87120
///
88121
/// This function does the same as `accept_async()` but accepts an extra callback
89122
/// for header processing. The callback receives headers of the incoming
90123
/// requests and is able to add extra headers to the reply.
91124
pub fn accept_hdr_async<S, C>(stream: S, callback: C) -> AcceptAsync<S, C>
125+
where
126+
S: AsyncRead + AsyncWrite,
127+
C: Callback,
128+
{
129+
accept_hdr_async_with_config(stream, callback, None)
130+
}
131+
132+
/// The same as `accept_hdr_async()` but the one can specify a websocket configuration.
133+
/// Please refer to `accept_hdr_async()` for more details.
134+
pub fn accept_hdr_async_with_config<S, C>(
135+
stream: S,
136+
callback: C,
137+
config: Option<WebSocketConfig>,
138+
) -> AcceptAsync<S, C>
92139
where
93140
S: AsyncRead + AsyncWrite,
94141
C: Callback,
95142
{
96143
AcceptAsync {
97144
inner: MidHandshake {
98-
inner: Some(server::accept_hdr(stream, callback))
145+
inner: Some(server::accept_hdr_with_config(stream, callback, config))
99146
}
100147
}
101148
}
@@ -116,15 +163,24 @@ pub struct WebSocketStream<S> {
116163
impl<S> WebSocketStream<S> {
117164
/// Convert a raw socket into a WebSocketStream without performing a
118165
/// handshake.
119-
pub fn from_raw_socket(stream: S, role: Role) -> Self {
120-
let ws = WebSocket::from_raw_socket(stream, role);
166+
pub fn from_raw_socket(
167+
stream: S,
168+
role: Role,
169+
config: Option<WebSocketConfig>,
170+
) -> Self {
171+
let ws = WebSocket::from_raw_socket(stream, role, config);
121172
WebSocketStream { inner: ws }
122173
}
123174

124175
/// Convert a raw socket into a WebSocketStream without performing a
125176
/// handshake.
126-
pub fn from_partially_read(stream: S, part: Vec<u8>, role: Role) -> Self {
127-
let ws = WebSocket::from_partially_read(stream, part, role);
177+
pub fn from_partially_read(
178+
stream: S,
179+
part: Vec<u8>,
180+
role: Role,
181+
config: Option<WebSocketConfig>,
182+
) -> Self {
183+
let ws = WebSocket::from_partially_read(stream, part, role, config);
128184
WebSocketStream { inner: ws }
129185
}
130186
}
@@ -143,8 +199,7 @@ impl<T> Sink for WebSocketStream<T> where T: AsyncRead + AsyncWrite {
143199
type SinkError = WsError;
144200

145201
fn start_send(&mut self, item: Message) -> StartSend<Message, WsError> {
146-
try!(self.inner.write_message(item).to_async());
147-
Ok(AsyncSink::Ready)
202+
self.inner.write_message(item).to_start_send()
148203
}
149204

150205
fn poll_complete(&mut self) -> Poll<(), WsError> {
@@ -238,3 +293,24 @@ impl<T> ToAsync for Result<T, WsError> {
238293
}
239294
}
240295

296+
trait ToStartSend {
297+
type T;
298+
type E;
299+
fn to_start_send(self) -> StartSend<Self::T, Self::E>;
300+
}
301+
302+
impl ToStartSend for Result<(), WsError> {
303+
type T = Message;
304+
type E = WsError;
305+
fn to_start_send(self) -> StartSend<Self::T, Self::E> {
306+
match self {
307+
Ok(_) => Ok(AsyncSink::Ready),
308+
Err(error) => match error {
309+
WsError::Io(ref err) if err.kind() == ErrorKind::WouldBlock => Ok(AsyncSink::Ready),
310+
WsError::SendQueueFull(msg) => Ok(AsyncSink::NotReady(msg)),
311+
err => Err(err),
312+
}
313+
}
314+
}
315+
}
316+

0 commit comments

Comments
 (0)