use headers_map

This commit is contained in:
Toshit Chawda 2024-01-12 17:00:14 -08:00
parent 85b57b8019
commit 713987f63d
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D
2 changed files with 39 additions and 31 deletions

View file

@ -14,7 +14,7 @@ use bytes::Bytes;
use http::{uri, HeaderName, HeaderValue, Request, Response}; use http::{uri, HeaderName, HeaderValue, Request, Response};
use hyper::{ use hyper::{
body::Incoming, body::Incoming,
client::conn::{self as hyper_conn, http1::Builder}, client::conn::http1::Builder,
Uri, Uri,
}; };
use js_sys::{Array, Object, Reflect, Uint8Array}; 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 mut builder = Request::builder().uri(uri.clone()).method(req_method);
if let Some(headers) = headers {
let headers_map = builder.headers_mut().replace_err("Failed to get headers")?; 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 {
for hdr in headers { for hdr in headers {
headers_map.insert( headers_map.insert(
HeaderName::from_bytes(hdr[0].as_bytes()) 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 let request = builder
.body(HttpBody::new(body_bytes)) .body(HttpBody::new(body_bytes))
.replace_err("Failed to make request")?; .replace_err("Failed to make request")?;

View file

@ -3,6 +3,9 @@
"%cWASM is significantly slower with DevTools open!", "%cWASM is significantly slower with DevTools open!",
"color:red;font-size:2rem;font-weight:bold" "color:red;font-size:2rem;font-weight:bold"
); );
const should_test = (new URL(window.location.href)).searchParams.has("test");
await wasm_bindgen("./wstcp_client_bg.wasm"); await wasm_bindgen("./wstcp_client_bg.wasm");
const tconn0 = performance.now(); const tconn0 = performance.now();
@ -11,6 +14,12 @@
const tconn1 = performance.now(); const tconn1 = performance.now();
console.warn(`conn establish took ${tconn1 - tconn0} ms or ${(tconn1 - tconn0) / 1000} s`); 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 test_mux = async (url) => {
const t0 = performance.now(); const t0 = performance.now();
await wstcp.fetch(url); await wstcp.fetch(url);
@ -43,4 +52,5 @@
console.warn(`avg native (10) took ${total_native} ms or ${total_native / 1000} s`); 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`); console.warn(`mux - native: ${total_mux - total_native} ms or ${(total_mux - total_native) / 1000} s`);
alert("you can open console now"); alert("you can open console now");
}
})(); })();