From 60f9d3eedde7992d06b9debd24438bebdf820e8d Mon Sep 17 00:00:00 2001 From: Alexey Galakhov Date: Tue, 4 Apr 2017 14:02:02 +0200 Subject: [PATCH 1/3] Version 0.1.2 released. Signed-off-by: Alexey Galakhov --- Cargo.toml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f83efd5..02c74f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,15 +6,15 @@ keywords = ["websocket", "io", "web"] authors = ["Daniel Abramov ", "Alexey Galakhov "] license = "MIT" homepage = "https://github.com/snapview/tokio-tungstenite" -documentation = "https://docs.rs/tokio-tungstenite" +documentation = "https://docs.rs/tokio-tungstenite/0.1.2" repository = "https://github.com/snapview/tokio-tungstenite" version = "0.1.2" [dependencies] -futures = "*" -tokio-io = "*" +futures = "0.1.12" +tokio-io = "0.1.1" tungstenite = "0.2.0" -url = "*" +url = "1.4.0" [dev-dependencies] -tokio-core = "*" +tokio-core = "0.1.6" From 15167ab718af14136c5f93afe8c2b0c7178ad508 Mon Sep 17 00:00:00 2001 From: Alex Shapiro Date: Sat, 22 Apr 2017 23:14:10 -0700 Subject: [PATCH 2/3] allow client to initiate connection with extra headers Alter `client_async` to allow connections with a Request object that may contain extra headers, including a WebSocket protocol. --- src/lib.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 93d5ab5..33435fd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,22 +37,58 @@ use tungstenite::handshake::server::ServerHandshake; use tungstenite::handshake::{HandshakeRole, HandshakeError}; use tungstenite::protocol::{WebSocket, Message}; use tungstenite::error::Error as WsError; -use tungstenite::{client, server}; +use tungstenite::server; -/// Create a handshake provided stream and assuming the provided request. +/// A WebSocket request +pub struct Request<'a> { + pub url: Url, + 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>(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> From for Request<'a> { + fn from(u: U) -> Request<'a> { + Request::new(u) + } +} + +/// Creates a WebSocket handshake from a request and a stream. +/// For convenience, the user may call this with a url string, a URL, +/// or a `Request`. Calling with `Request` allows the user to add +/// a WebSocket protocol or other custom headers. /// -/// This function will internally call `client::client` to create a -/// handshake representation and returns a future representing the -/// resolution of the WebSocket handshake. The returned future will resolve -/// to either `WebSocketStream` or `Error` depending if it's successful -/// or not. +/// Internally, this custom creates a handshake representation and returns +/// a future representing the resolution of the WebSocket handshake. The +/// returned future will resolve to either `WebSocketStream` or `Error` +/// depending on whether the handshake is successful. /// /// This is typically used for clients who have already established, for /// example, a TCP connection to the remove server. -pub fn client_async(url: Url, stream: S) -> ConnectAsync { +pub fn client_async<'a, R, S>(request: R, stream: S) -> ConnectAsync + where R: Into>, S: AsyncRead + AsyncWrite { + let Request{url, headers} = request.into(); + let tungstenite_request = tungstenite::handshake::client::Request{url: url, extra_headers: Some(&headers)}; + let handshake = ClientHandshake::start(stream, tungstenite_request).handshake(); + ConnectAsync { inner: MidHandshake { - inner: Some(client::client(url, stream)) + inner: Some(handshake) } } } From c6b76a5c87513dce3c4aef62dcfaebb1e6d868f9 Mon Sep 17 00:00:00 2001 From: Alexey Galakhov Date: Fri, 19 May 2017 18:04:53 +0200 Subject: [PATCH 3/3] Add missing documentation for Url struct Signed-off-by: Alexey Galakhov --- src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 33435fd..0d40efd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -41,7 +41,9 @@ use tungstenite::server; /// 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)>, }