diff --git a/client/build.sh b/client/build.sh index d33b608..68a1dbe 100755 --- a/client/build.sh +++ b/client/build.sh @@ -11,7 +11,7 @@ wasm-bindgen --weak-refs --target no-modules --no-modules-global epoxy --out-dir echo "[ws] bindgen finished" mv out/epoxy_client_bg.wasm out/epoxy_client_unoptimized.wasm -time wasm-opt out/epoxy_client_unoptimized.wasm -o out/epoxy_client_bg.wasm +time wasm-opt -O4 out/epoxy_client_unoptimized.wasm -o out/epoxy_client_bg.wasm echo "[ws] optimized" AUTOGENERATED_SOURCE=$(<"out/epoxy_client.js") diff --git a/client/demo.js b/client/demo.js index 77876b2..e757b51 100644 --- a/client/demo.js +++ b/client/demo.js @@ -67,11 +67,13 @@ () => console.log("closed"), err => console.error(err), msg => console.log(msg), - "ws://localhost:9000", + "wss://echo.websocket.events", [], "localhost" ); - await ws.send("data"); + while (true) { + await ws.send("data"); + } } else { let resp = await epoxy_client.fetch("https://httpbin.org/get"); console.warn(resp, Object.fromEntries(resp.headers)); diff --git a/client/src/lib.rs b/client/src/lib.rs index d47d6c0..30aabde 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -170,7 +170,7 @@ impl EpoxyClient { .await .replace_err("Failed to create multiplexor channel")?; - if *url.scheme().replace_err("URL must have a scheme")? == uri::Scheme::HTTPS { + if utils::get_is_secure(url)? { let cloned_uri = url_host.to_string().clone(); let connector = TlsConnector::from(self.rustls_config.clone()); let io = connector diff --git a/client/src/utils.rs b/client/src/utils.rs index 69a2613..077a120 100644 --- a/client/src/utils.rs +++ b/client/src/utils.rs @@ -130,8 +130,27 @@ pub fn is_redirect(code: u16) -> bool { [301, 302, 303, 307, 308].contains(&code) } +pub fn get_is_secure(url: &Uri) -> Result { + let url_scheme = url.scheme().replace_err("URL must have a scheme")?; + let url_scheme_str = url.scheme_str().replace_err("URL must have a scheme")?; + // can't use match, compiler error + // error: to use a constant of type `Scheme` in a pattern, `Scheme` must be annotated with `#[derive(PartialEq, Eq)]` + if *url_scheme == uri::Scheme::HTTP { + Ok(false) + } else if *url_scheme == uri::Scheme::HTTPS { + Ok(true) + } else if url_scheme_str == "ws" { + Ok(false) + } else if url_scheme_str == "wss" { + Ok(true) + } else { + return Ok(false); + } +} + pub fn get_url_port(url: &Uri) -> Result { let url_scheme = url.scheme().replace_err("URL must have a scheme")?; + let url_scheme_str = url.scheme_str().replace_err("URL must have a scheme")?; if let Some(port) = url.port() { Ok(port.as_u16()) } else { @@ -141,6 +160,10 @@ pub fn get_url_port(url: &Uri) -> Result { Ok(80) } else if *url_scheme == uri::Scheme::HTTPS { Ok(443) + } else if url_scheme_str == "ws" { + Ok(80) + } else if url_scheme_str == "wss" { + Ok(443) } else { return Err(jerr!("Failed to coerce port from scheme")); } diff --git a/client/src/websocket.rs b/client/src/websocket.rs index ecd6601..d480a3c 100644 --- a/client/src/websocket.rs +++ b/client/src/websocket.rs @@ -51,15 +51,9 @@ impl EpxWebSocket { let key = STANDARD.encode(rand); - let pathstr = if let Some(p) = url.path_and_query() { - p.to_string() - } else { - url.path().to_string() - }; - let mut builder = Request::builder() .method("GET") - .uri(pathstr) + .uri(url.clone()) .header("Host", host) .header("Origin", origin) .header(UPGRADE, "websocket")