Merge pull request #17 from snapview/tokio-updates

Update tokio and remove some warnings
pull/1/head
Alexey Galakhov 7 years ago committed by GitHub
commit f19722ec6b
  1. 8
      Cargo.toml
  2. 57
      src/connect.rs
  3. 44
      src/lib.rs

@ -8,7 +8,7 @@ license = "MIT"
homepage = "https://github.com/snapview/tokio-tungstenite" homepage = "https://github.com/snapview/tokio-tungstenite"
documentation = "https://docs.rs/tokio-tungstenite/0.3.0" documentation = "https://docs.rs/tokio-tungstenite/0.3.0"
repository = "https://github.com/snapview/tokio-tungstenite" repository = "https://github.com/snapview/tokio-tungstenite"
version = "0.3.0" version = "0.4.0"
[features] [features]
default = ["connect", "tls"] default = ["connect", "tls"]
@ -19,10 +19,10 @@ stream = ["bytes"]
[dependencies] [dependencies]
futures = "0.1.14" futures = "0.1.14"
tokio-io = "0.1.2" tokio-io = "0.1.2"
url = "1.5.1"
[dependencies.tungstenite] [dependencies.tungstenite]
version = "0.4.0" git = "https://github.com/snapview/tungstenite-rs.git" # FIXME: set to 0.5.0 after tungstenite's approval
branch = "request_minor"
default-features = false default-features = false
[dependencies.bytes] [dependencies.bytes]
@ -47,4 +47,4 @@ version = "0.1.3"
[dev-dependencies] [dev-dependencies]
tokio-core = "0.1.9" tokio-core = "0.1.9"
url = "1.5.1"

@ -9,8 +9,7 @@ use self::tokio_core::net::TcpStream;
use self::tokio_core::reactor::Remote; use self::tokio_core::reactor::Remote;
use self::tokio_dns::tcp_connect; use self::tokio_dns::tcp_connect;
use futures::future; use futures::{future, Future};
use futures::{Future, BoxFuture};
use tungstenite::Error; use tungstenite::Error;
use tungstenite::client::url_mode; use tungstenite::client::url_mode;
use tungstenite::handshake::client::Response; use tungstenite::handshake::client::Response;
@ -36,8 +35,7 @@ mod encryption {
use std::io::{Read, Write, Result as IoResult}; use std::io::{Read, Write, Result as IoResult};
use futures::{Future, BoxFuture}; use futures::{future, Future};
use futures::future;
use tungstenite::Error; use tungstenite::Error;
use tungstenite::stream::Mode; use tungstenite::stream::Mode;
@ -53,16 +51,17 @@ mod encryption {
} }
} }
pub fn wrap_stream(socket: TcpStream, domain: String, mode: Mode) -> BoxFuture<AutoStream, Error> { pub fn wrap_stream(socket: TcpStream, domain: String, mode: Mode)
-> Box<Future<Item=AutoStream, Error=Error>>
{
match mode { match mode {
Mode::Plain => future::ok(StreamSwitcher::Plain(socket)).boxed(), Mode::Plain => Box::new(future::ok(StreamSwitcher::Plain(socket))),
Mode::Tls => { Mode::Tls => {
future::result(TlsConnector::builder()) Box::new(future::result(TlsConnector::builder())
.and_then(move |builder| future::result(builder.build())) .and_then(move |builder| future::result(builder.build()))
.and_then(move |connector| connector.connect_async(&domain, socket)) .and_then(move |connector| connector.connect_async(&domain, socket))
.map(|s| StreamSwitcher::Tls(s)) .map(|s| StreamSwitcher::Tls(s))
.map_err(|e| Error::Tls(e)) .map_err(|e| Error::Tls(e)))
.boxed()
} }
} }
} }
@ -72,18 +71,19 @@ mod encryption {
mod encryption { mod encryption {
use super::tokio_core::net::TcpStream; use super::tokio_core::net::TcpStream;
use futures::{Future, BoxFuture}; use futures::{future, Future};
use futures::future;
use tungstenite::Error; use tungstenite::Error;
use tungstenite::stream::Mode; use tungstenite::stream::Mode;
pub type AutoStream = TcpStream; pub type AutoStream = TcpStream;
pub fn wrap_stream(socket: TcpStream, _domain: String, mode: Mode) -> BoxFuture<AutoStream, Error> { pub fn wrap_stream(socket: TcpStream, _domain: String, mode: Mode)
-> Box<Future<Item=AutoStream, Error=Error>>
{
match mode { match mode {
Mode::Plain => future::ok(socket).boxed(), Mode::Plain => Box::new(future::ok(socket)),
Mode::Tls => future::err(Error::Url("TLS support not compiled in.".into())).boxed(), Mode::Tls => Box::new(future::err(Error::Url("TLS support not compiled in.".into()))),
} }
} }
} }
@ -92,7 +92,7 @@ use self::encryption::{AutoStream, wrap_stream};
/// Connect to a given URL. /// Connect to a given URL.
pub fn connect_async<R>(request: R, handle: Remote) pub fn connect_async<R>(request: R, handle: Remote)
-> BoxFuture<(WebSocketStream<AutoStream>, Response), Error> -> Box<Future<Item=(WebSocketStream<AutoStream>, Response), Error=Error>>
where where
R: Into<Request<'static>> R: Into<Request<'static>>
{ {
@ -101,21 +101,20 @@ where
// Make sure we check domain and mode first. URL must be valid. // Make sure we check domain and mode first. URL must be valid.
let mode = match url_mode(&request.url) { let mode = match url_mode(&request.url) {
Ok(m) => m, Ok(m) => m,
Err(e) => return future::err(e.into()).boxed(), Err(e) => return Box::new(future::err(e.into())),
}; };
let domain = match request.url.host_str() { let domain = match request.url.host_str() {
Some(d) => d.to_string(), Some(d) => d.to_string(),
None => return future::err(Error::Url("No host name in the URL".into())).boxed(), None => return Box::new(future::err(Error::Url("No host name in the URL".into()))),
}; };
let port = request.url.port_or_known_default().expect("Bug: port unknown"); let port = request.url.port_or_known_default().expect("Bug: port unknown");
tcp_connect((domain.as_str(), port), handle).map_err(|e| e.into()) Box::new(tcp_connect((domain.as_str(), port), handle).map_err(|e| e.into())
.and_then(move |socket| wrap_stream(socket, domain, mode)) .and_then(move |socket| wrap_stream(socket, domain, mode))
.and_then(|mut stream| { .and_then(|mut stream| {
NoDelay::set_nodelay(&mut stream, true) NoDelay::set_nodelay(&mut stream, true)
.map(move |()| stream) .map(move |()| stream)
.map_err(|e| e.into()) .map_err(|e| e.into())
}) })
.and_then(move |stream| client_async(request, stream)) .and_then(move |stream| client_async(request, stream)))
.boxed()
} }

@ -18,7 +18,6 @@
extern crate futures; extern crate futures;
extern crate tokio_io; extern crate tokio_io;
extern crate tungstenite; extern crate tungstenite;
extern crate url;
#[cfg(feature="connect")] #[cfg(feature="connect")]
mod connect; mod connect;
@ -31,9 +30,7 @@ use std::io::ErrorKind;
use futures::{Poll, Future, Async, AsyncSink, Stream, Sink, StartSend}; use futures::{Poll, Future, Async, AsyncSink, Stream, Sink, StartSend};
use tokio_io::{AsyncRead, AsyncWrite}; use tokio_io::{AsyncRead, AsyncWrite};
use url::Url; use tungstenite::handshake::client::{ClientHandshake, Response, Request};
use tungstenite::handshake::client::{ClientHandshake, Response};
use tungstenite::handshake::server::{ServerHandshake, Callback}; use tungstenite::handshake::server::{ServerHandshake, Callback};
use tungstenite::handshake::{HandshakeRole, HandshakeError}; use tungstenite::handshake::{HandshakeRole, HandshakeError};
use tungstenite::protocol::{WebSocket, Message}; use tungstenite::protocol::{WebSocket, Message};
@ -43,37 +40,6 @@ use tungstenite::server;
#[cfg(feature="connect")] #[cfg(feature="connect")]
pub use connect::connect_async; pub use connect::connect_async;
/// A WebSocket request
pub struct Request<'a> {
/// URL of the request.
pub url: Url,
/// Extra headers, if any.
pub headers: Vec<(&'a str, &'a str)>,
}
impl<'a> Request<'a> {
/// Constructs a new WebSocket request with a URL or URL string
pub fn new<U: Into<Url>>(url: U) -> Self {
Request{url: url.into(), headers: vec![]}
}
/// Adds a WebSocket protocol to the request
pub fn add_protocol(&mut self, protocol: &'a str) {
self.headers.push(("Sec-WebSocket-Protocol", protocol));
}
/// Adds a custom header to the request
pub fn add_header(&mut self, name: &'a str, value: &'a str) {
self.headers.push((name, value));
}
}
impl<'a, U: Into<Url>> From<U> for Request<'a> {
fn from(u: U) -> Request<'a> {
Request::new(u)
}
}
/// Creates a WebSocket handshake from a request and a stream. /// Creates a WebSocket handshake from a request and a stream.
/// For convenience, the user may call this with a url string, a URL, /// For convenience, the user may call this with a url string, a URL,
/// or a `Request`. Calling with `Request` allows the user to add /// or a `Request`. Calling with `Request` allows the user to add
@ -91,15 +57,9 @@ where
R: Into<Request<'a>>, R: Into<Request<'a>>,
S: AsyncRead + AsyncWrite S: AsyncRead + AsyncWrite
{ {
let Request{ url, headers } = request.into();
let tungstenite_request = {
tungstenite::handshake::client::Request { url, extra_headers: Some(&headers) }
};
let handshake = ClientHandshake::start(stream, tungstenite_request).handshake();
ConnectAsync { ConnectAsync {
inner: MidHandshake { inner: MidHandshake {
inner: Some(handshake) inner: Some(ClientHandshake::start(stream, request.into()).handshake())
} }
} }
} }

Loading…
Cancel
Save