From 713987f63dfe8338ecc40292e931edc7c41fcab3 Mon Sep 17 00:00:00 2001 From: Toshit Chawda Date: Fri, 12 Jan 2024 17:00:14 -0800 Subject: [PATCH] use headers_map --- client/src/lib.rs | 20 ++++++++--------- client/src/web/index.js | 50 ++++++++++++++++++++++++----------------- 2 files changed, 39 insertions(+), 31 deletions(-) diff --git a/client/src/lib.rs b/client/src/lib.rs index bb87316..a64935a 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -14,7 +14,7 @@ use bytes::Bytes; use http::{uri, HeaderName, HeaderValue, Request, Response}; use hyper::{ body::Incoming, - client::conn::{self as hyper_conn, http1::Builder}, + client::conn::http1::Builder, Uri, }; use js_sys::{Array, Object, Reflect, Uint8Array}; @@ -245,8 +245,15 @@ impl WsTcp { let mut builder = Request::builder().uri(uri.clone()).method(req_method); + let headers_map = builder.headers_mut().replace_err("Failed to get headers")?; + headers_map.insert("Connection", HeaderValue::from_str("close")?); + headers_map.insert("User-Agent", HeaderValue::from_str(&self.useragent)?); + headers_map.insert("Host", HeaderValue::from_str(uri_host)?); + if body_bytes.is_empty() { + headers_map.insert("Content-Length", HeaderValue::from_str("0")?); + } + if let Some(headers) = headers { - let headers_map = builder.headers_mut().replace_err("Failed to get headers")?; for hdr in headers { headers_map.insert( HeaderName::from_bytes(hdr[0].as_bytes()) @@ -257,15 +264,6 @@ impl WsTcp { } } - builder = builder - // this breaks a shit ton of things - // .header("Host", uri_host) - // .header("User-Agent", self.useragent.clone()) - .header("Connection", "close"); - if body_bytes.len() == 0 { - builder = builder.header("Content-Length", 0); - } - let request = builder .body(HttpBody::new(body_bytes)) .replace_err("Failed to make request")?; diff --git a/client/src/web/index.js b/client/src/web/index.js index 0468036..7a0fb80 100644 --- a/client/src/web/index.js +++ b/client/src/web/index.js @@ -1,41 +1,50 @@ (async () => { - console.log( - "%cWASM is significantly slower with DevTools open!", - "color:red;font-size:2rem;font-weight:bold" - ); - await wasm_bindgen("./wstcp_client_bg.wasm"); + console.log( + "%cWASM is significantly slower with DevTools open!", + "color:red;font-size:2rem;font-weight:bold" + ); - const tconn0 = performance.now(); - // args: websocket url, user agent, redirect limit - let wstcp = await new wasm_bindgen.WsTcp("wss://localhost:4000", navigator.userAgent, 10); - const tconn1 = performance.now(); - console.warn(`conn establish took ${tconn1 - tconn0} ms or ${(tconn1 - tconn0) / 1000} s`); + const should_test = (new URL(window.location.href)).searchParams.has("test"); + await wasm_bindgen("./wstcp_client_bg.wasm"); + + const tconn0 = performance.now(); + // args: websocket url, user agent, redirect limit + let wstcp = await new wasm_bindgen.WsTcp("wss://localhost:4000", navigator.userAgent, 10); + const tconn1 = performance.now(); + console.warn(`conn establish took ${tconn1 - tconn0} ms or ${(tconn1 - tconn0) / 1000} s`); + + let resp = await wstcp.fetch("http://httpbin.org/post", {method: "POST", body: "wstcp", headers: {"User-Agent": "wstcp"}}); + console.warn(resp); + console.warn(Object.fromEntries(resp.headers)); + console.warn(await resp.text()); + + if (should_test) { const test_mux = async (url) => { - const t0 = performance.now(); - await wstcp.fetch(url); - const t1 = performance.now(); - return t1 - t0; + const t0 = performance.now(); + await wstcp.fetch(url); + const t1 = performance.now(); + return t1 - t0; }; const test_native = async (url) => { - const t0 = performance.now(); - await fetch(url); - const t1 = performance.now(); - return t1 - t0; + const t0 = performance.now(); + await fetch(url); + const t1 = performance.now(); + return t1 - t0; }; const num_tests = 10; let total_mux = 0; for (const _ of Array(num_tests).keys()) { - total_mux += await test_mux("https://httpbin.org/get"); + total_mux += await test_mux("https://httpbin.org/get"); } total_mux = total_mux / num_tests; let total_native = 0; for (const _ of Array(num_tests).keys()) { - total_native += await test_native("https://httpbin.org/get"); + total_native += await test_native("https://httpbin.org/get"); } total_native = total_native / num_tests; @@ -43,4 +52,5 @@ console.warn(`avg native (10) took ${total_native} ms or ${total_native / 1000} s`); console.warn(`mux - native: ${total_mux - total_native} ms or ${(total_mux - total_native) / 1000} s`); alert("you can open console now"); + } })();