Merge branch 'devel'

pull/12/head
Alexey Galakhov 8 years ago
commit 0e8113e6e0
  1. 4
      Cargo.toml
  2. 78
      src/client.rs
  3. 10
      src/error.rs

@ -1,7 +1,7 @@
[package] [package]
name = "tungstenite" name = "tungstenite"
description = "Lightweight stream-based WebSocket implementation" description = "Lightweight stream-based WebSocket implementation"
categories = ["web-programming:websocket", "network-programming"] categories = ["web-programming::websocket", "network-programming"]
keywords = ["websocket", "io", "web"] keywords = ["websocket", "io", "web"]
authors = ["Alexey Galakhov"] authors = ["Alexey Galakhov"]
license = "MIT" license = "MIT"
@ -9,7 +9,7 @@ readme = "README.md"
homepage = "https://github.com/snapview/tungstenite-rs" homepage = "https://github.com/snapview/tungstenite-rs"
documentation = "https://docs.rs/tungstenite/0.1.1" documentation = "https://docs.rs/tungstenite/0.1.1"
repository = "https://github.com/snapview/tungstenite-rs" repository = "https://github.com/snapview/tungstenite-rs"
version = "0.1.1" version = "0.1.2"
[features] [features]
default = ["tls"] default = ["tls"]

@ -7,7 +7,52 @@ use std::io::{Read, Write};
use url::Url; use url::Url;
#[cfg(feature="tls")] #[cfg(feature="tls")]
use native_tls::{TlsStream, TlsConnector, HandshakeError as TlsHandshakeError}; mod encryption {
use std::net::TcpStream;
use native_tls::{TlsConnector, HandshakeError as TlsHandshakeError};
pub use native_tls::TlsStream;
pub use stream::Stream as StreamSwitcher;
pub type AutoStream = StreamSwitcher<TcpStream, TlsStream<TcpStream>>;
use stream::Mode;
use error::Result;
pub fn wrap_stream(stream: TcpStream, domain: &str, mode: Mode) -> Result<AutoStream> {
match mode {
Mode::Plain => Ok(StreamSwitcher::Plain(stream)),
Mode::Tls => {
let connector = TlsConnector::builder()?.build()?;
connector.connect(domain, stream)
.map_err(|e| match e {
TlsHandshakeError::Failure(f) => f.into(),
TlsHandshakeError::Interrupted(_) => panic!("Bug: TLS handshake not blocked"),
})
.map(StreamSwitcher::Tls)
}
}
}
}
#[cfg(not(feature="tls"))]
mod encryption {
use std::net::TcpStream;
use stream::Mode;
use error::{Error, Result};
pub type AutoStream = TcpStream;
pub fn wrap_stream(stream: TcpStream, _domain: &str, mode: Mode) -> Result<AutoStream> {
match mode {
Mode::Plain => Ok(stream),
Mode::Tls => Err(Error::Url("TLS support not compiled in.".into())),
}
}
}
pub use self::encryption::AutoStream;
use self::encryption::wrap_stream;
use protocol::WebSocket; use protocol::WebSocket;
use handshake::HandshakeError; use handshake::HandshakeError;
@ -15,13 +60,6 @@ use handshake::client::{ClientHandshake, Request};
use stream::Mode; use stream::Mode;
use error::{Error, Result}; use error::{Error, Result};
#[cfg(feature="tls")]
use stream::Stream as StreamSwitcher;
#[cfg(feature="tls")]
pub type AutoStream = StreamSwitcher<TcpStream, TlsStream<TcpStream>>;
#[cfg(not(feature="tls"))]
pub type AutoStream = TcpStream;
/// Connect to the given WebSocket in blocking mode. /// Connect to the given WebSocket in blocking mode.
/// ///
@ -46,30 +84,6 @@ pub fn connect(url: Url) -> Result<WebSocket<AutoStream>> {
}) })
} }
#[cfg(feature="tls")]
fn wrap_stream(stream: TcpStream, domain: &str, mode: Mode) -> Result<AutoStream> {
match mode {
Mode::Plain => Ok(StreamSwitcher::Plain(stream)),
Mode::Tls => {
let connector = TlsConnector::builder()?.build()?;
connector.connect(domain, stream)
.map_err(|e| match e {
TlsHandshakeError::Failure(f) => f.into(),
TlsHandshakeError::Interrupted(_) => panic!("Bug: TLS handshake not blocked"),
})
.map(StreamSwitcher::Tls)
}
}
}
#[cfg(not(feature="tls"))]
fn wrap_stream(stream: TcpStream, _domain: &str, mode: Mode) -> Result<AutoStream> {
match mode {
Mode::Plain => Ok(stream),
Mode::Tls => Err(Error::Url("TLS support not compiled in.".into())),
}
}
fn connect_to_some<A>(addrs: A, url: &Url, mode: Mode) -> Result<AutoStream> fn connect_to_some<A>(addrs: A, url: &Url, mode: Mode) -> Result<AutoStream>
where A: Iterator<Item=SocketAddr> where A: Iterator<Item=SocketAddr>
{ {

@ -12,7 +12,9 @@ use std::string;
use httparse; use httparse;
#[cfg(feature="tls")] #[cfg(feature="tls")]
use native_tls; pub mod tls {
pub use native_tls::Error;
}
pub type Result<T> = result::Result<T, Error>; pub type Result<T> = result::Result<T, Error>;
@ -25,7 +27,7 @@ pub enum Error {
Io(io::Error), Io(io::Error),
#[cfg(feature="tls")] #[cfg(feature="tls")]
/// TLS error /// TLS error
Tls(native_tls::Error), Tls(tls::Error),
/// Buffer capacity exhausted /// Buffer capacity exhausted
Capacity(Cow<'static, str>), Capacity(Cow<'static, str>),
/// Protocol violation /// Protocol violation
@ -89,8 +91,8 @@ impl From<string::FromUtf8Error> for Error {
} }
#[cfg(feature="tls")] #[cfg(feature="tls")]
impl From<native_tls::Error> for Error { impl From<tls::Error> for Error {
fn from(err: native_tls::Error) -> Self { fn from(err: tls::Error) -> Self {
Error::Tls(err) Error::Tls(err)
} }
} }

Loading…
Cancel
Save