From 7fc4a5e85c0e71d136faa114c1aa749d701534fd Mon Sep 17 00:00:00 2001 From: Yarn Date: Thu, 20 Aug 2020 11:43:46 -0700 Subject: [PATCH 1/2] Add websockets --- Cargo.toml | 2 ++ src/lib.rs | 1 + src/websocket/mod.rs | 2 ++ src/websocket/upgrade.rs | 58 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 src/websocket/mod.rs create mode 100644 src/websocket/upgrade.rs diff --git a/Cargo.toml b/Cargo.toml index 50cc5fbac..8aef0ac3e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,7 @@ __internal__bench = [] async-h1 = { version = "2.0.1", optional = true } async-sse = "4.0.0" async-std = { version = "1.6.0", features = ["unstable"] } +async-tungstenite = "0.8" femme = "2.0.1" http-types = "2.2.1" kv-log-macro = "1.0.4" @@ -45,6 +46,7 @@ logtest = "2.0.0" async-trait = "0.1.36" futures-util = "0.3.5" pin-project-lite = "0.1.7" +websocket_handshake = "0.1.0" [dev-dependencies] async-std = { version = "1.6.0", features = ["unstable", "attributes"] } diff --git a/src/lib.rs b/src/lib.rs index 07cd85aab..7b4ecfc7c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -213,6 +213,7 @@ pub mod log; pub mod prelude; pub mod security; pub mod sse; +pub mod websocket; pub mod utils; pub use endpoint::Endpoint; diff --git a/src/websocket/mod.rs b/src/websocket/mod.rs new file mode 100644 index 000000000..55b42ba74 --- /dev/null +++ b/src/websocket/mod.rs @@ -0,0 +1,2 @@ +mod upgrade; +pub use upgrade::upgrade; diff --git a/src/websocket/upgrade.rs b/src/websocket/upgrade.rs new file mode 100644 index 000000000..7a965a9e4 --- /dev/null +++ b/src/websocket/upgrade.rs @@ -0,0 +1,58 @@ +use crate::http::{Body, StatusCode}; +use crate::{Request, Response, Result, Error}; + +use std::fmt; + +use async_std::future::Future; + +use async_tungstenite::WebSocketStream; +use async_tungstenite::tungstenite::protocol::Role; + +pub struct Handle { + ws: WebSocketStream>, +} + +impl Handle { + pub fn into_inner(self) -> WebSocketStream> { + self.ws + } +} + +impl fmt::Debug for Handle { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Handle") + .field("ws", &"...") + .finish() + } +} + +/// Upgrade an existing HTTP connection to a websocket connection. +pub fn upgrade(req: Request, handler: F) -> Result +where + State: Clone + Send + Sync + 'static, + F: FnOnce(Request, Handle) -> Fut + Send + Sync + 'static, + Fut: Future> + Send + Sync + 'static, +{ + if req.version() != Some(http_types::Version::Http1_1) { + return Err(Error::from_str( + StatusCode::NotAcceptable, + "Websockets are not supported on http version != 1.1.", + )) + } + + let mut res: Response = websocket_handshake::http1_1::check_request_headers(req.as_ref()) + .map_err(|err| Error::new(StatusCode::BadRequest, err))? + .make_response().into(); + + let body = Body::io(|io| async move { + let ws = WebSocketStream::from_raw_socket(io, Role::Server, None).await; + + let handle = Handle { + ws: ws + }; + (handler)(req, handle).await.unwrap(); + }); + res.set_body(body); + + Ok(res) +} From adb1950d638b82d5324749fef1194e8cd51952b9 Mon Sep 17 00:00:00 2001 From: Yarn Date: Thu, 20 Aug 2020 23:12:10 -0700 Subject: [PATCH 2/2] add patched dependencies --- Cargo.toml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8aef0ac3e..d21e21a72 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,12 +32,12 @@ unstable = [] __internal__bench = [] [dependencies] -async-h1 = { version = "2.0.1", optional = true } +async-h1 = { version = "2.1.2", optional = true } async-sse = "4.0.0" async-std = { version = "1.6.0", features = ["unstable"] } async-tungstenite = "0.8" femme = "2.0.1" -http-types = "2.2.1" +http-types = "2.4.0" kv-log-macro = "1.0.4" serde = "1.0.102" serde_json = "1.0.41" @@ -67,3 +67,7 @@ required-features = ["unstable"] name = "router" harness = false +[patch.crates-io] +http-types = { git = "https://github.com/Yarn/http-types.git", rev = "4ba7c547d37f7e172e69a6bd619336e067f70103" } +async-h1 = { git = "https://github.com/Yarn/async-h1.git", rev = "5bca9bea599e3f500e608fcfea32399985c83f31" } +