From 9083de531ee8983af0773b4e37d05ec0d9ca052f Mon Sep 17 00:00:00 2001 From: Toshit Chawda Date: Tue, 9 Jan 2024 09:04:59 -0800 Subject: [PATCH] untested body and request type support --- Cargo.lock | 1 + client/Cargo.toml | 1 + client/src/lib.rs | 41 ++++++++++++++++++++++++++++++++++++----- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a7ab1f4..45c3c42 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1437,6 +1437,7 @@ dependencies = [ "tokio-rustls", "wasm-bindgen", "wasm-bindgen-futures", + "web-sys", "webpki-roots", "ws_stream_wasm", ] diff --git a/client/Cargo.toml b/client/Cargo.toml index 2896c8d..4260dd6 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -25,6 +25,7 @@ futures-util = "0.3.30" js-sys = "0.3.66" webpki-roots = "0.26.0" tokio-rustls = "0.25.0" +web-sys = { version = "0.3.66", features = ["TextEncoder"] } [dependencies.getrandom] features = ["js"] diff --git a/client/src/lib.rs b/client/src/lib.rs index 922bee6..ef89788 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -11,7 +11,8 @@ use std::sync::Arc; use bytes::Bytes; use http::{uri, Request}; use hyper::{body::Incoming, client::conn as hyper_conn}; -use js_sys::Object; +use web_sys::TextEncoder; +use js_sys::{Object, Reflect, Uint8Array}; use penguin_mux_wasm::{Multiplexor, MuxStream, Role}; use tokio_rustls::{client::TlsStream, rustls, rustls::RootCertStore, TlsConnector}; use wasm_bindgen::prelude::*; @@ -87,6 +88,30 @@ impl WsTcpWorker { } }; + let req_method_string: String = match Reflect::get(&options, &JsValue::from_str("method")) { + Ok(val) => val.as_string().unwrap_or("GET".to_string()), + Err(_) => "GET".to_string(), + }; + let req_method: http::Method = + http::Method::try_from(>::as_ref(&req_method_string)) + .expect_throw("Invalid http method"); + + let body: Option> = Reflect::get(&options, &JsValue::from_str("body")).map(|val| { + if val.is_string() { + let str = val.as_string().expect_throw("Failed to encode body into uint8array"); + let encoder = TextEncoder::new().expect_throw("Failed to encode body into uint8array"); + let encoded = encoder.encode_with_input(str.as_ref()); + Some(encoded) + } else { + Some(Uint8Array::new(&val).to_vec()) + } + }).unwrap_or(None); + + let body_bytes: Bytes = match body { + Some(vec) => Bytes::from(vec), + None => Bytes::new() + }; + let channel = self .mux .client_new_stream_channel(uri_host.as_bytes(), uri_port) @@ -96,8 +121,8 @@ impl WsTcpWorker { let request = Request::builder() .header("Host", uri_host) .header("Connection", "close") - .method("GET") - .body(HttpBody::new(Bytes::new())) + .method(req_method) + .body(HttpBody::new(body_bytes)) .expect_throw("Failed to create request"); let resp: hyper::Response; @@ -126,7 +151,10 @@ impl WsTcpWorker { }); debug!("sending req tls"); - resp = req_sender.send_request(request).await.expect_throw("Failed to send request"); + resp = req_sender + .send_request(request) + .await + .expect_throw("Failed to send request"); debug!("recieved resp"); } else { let io = TokioIo::new(channel); @@ -140,7 +168,10 @@ impl WsTcpWorker { } }); debug!("sending req"); - resp = req_sender.send_request(request).await.expect_throw("Failed to send request"); + resp = req_sender + .send_request(request) + .await + .expect_throw("Failed to send request"); debug!("recieved resp"); }