From f9c3948c514ea1413f4b82b9c45312e986690071 Mon Sep 17 00:00:00 2001 From: Daniel Abramov Date: Fri, 1 Sep 2017 17:09:26 +0200 Subject: [PATCH] Replace redundant `Request` structure Let's reuse the corresponding structure from tungstenite. --- Cargo.toml | 6 +++--- src/lib.rs | 44 ++------------------------------------------ 2 files changed, 5 insertions(+), 45 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 95d5cc1..30eed04 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,10 +19,10 @@ stream = ["bytes"] [dependencies] futures = "0.1.14" tokio-io = "0.1.2" -url = "1.5.1" [dependencies.tungstenite] -version = "0.4.0" +git = "https://github.com/snapview/tungstenite-rs.git" +branch = "request_minor" default-features = false [dependencies.bytes] @@ -47,4 +47,4 @@ version = "0.1.3" [dev-dependencies] tokio-core = "0.1.9" - +url = "1.5.1" diff --git a/src/lib.rs b/src/lib.rs index 59539ae..e9bfb3b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,7 +18,6 @@ extern crate futures; extern crate tokio_io; extern crate tungstenite; -extern crate url; #[cfg(feature="connect")] mod connect; @@ -31,9 +30,7 @@ use std::io::ErrorKind; use futures::{Poll, Future, Async, AsyncSink, Stream, Sink, StartSend}; use tokio_io::{AsyncRead, AsyncWrite}; -use url::Url; - -use tungstenite::handshake::client::{ClientHandshake, Response}; +use tungstenite::handshake::client::{ClientHandshake, Response, Request}; use tungstenite::handshake::server::{ServerHandshake, Callback}; use tungstenite::handshake::{HandshakeRole, HandshakeError}; use tungstenite::protocol::{WebSocket, Message}; @@ -43,37 +40,6 @@ use tungstenite::server; #[cfg(feature="connect")] 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>(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 @@ -91,15 +57,9 @@ where R: Into>, 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 { inner: MidHandshake { - inner: Some(handshake) + inner: Some(ClientHandshake::start(stream, request.into()).handshake()) } } }