From 6261b3fb0348cf21d1b968d5b36ac90a51de3b69 Mon Sep 17 00:00:00 2001 From: redxef Date: Wed, 30 Oct 2024 03:25:36 +0100 Subject: [PATCH] fix: none-version, multipart. Improve the multipart inputs, add some logging. --- Cargo.lock | 98 ++++++++++++++++++++++++++++++---------------------- Cargo.toml | 3 +- src/main.rs | 60 ++++++++++++++++++++------------ src/model.rs | 13 +++++-- 4 files changed, 105 insertions(+), 69 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4c9911f..b34ae7e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -107,7 +107,6 @@ dependencies = [ "reqwest", "serde", "serde_json", - "serde_yaml", "sha3", "tokio", "url", @@ -142,12 +141,6 @@ dependencies = [ "crypto-common", ] -[[package]] -name = "equivalent" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" - [[package]] name = "fnv" version = "1.0.7" @@ -178,6 +171,29 @@ version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" +[[package]] +name = "futures-io" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6" + +[[package]] +name = "futures-macro" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "futures-sink" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e575fab7d1e0dcb8d0c7bcf9a63ee213816ab51902e6d244a95819acacf1d4f7" + [[package]] name = "futures-task" version = "0.3.31" @@ -191,9 +207,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" dependencies = [ "futures-core", + "futures-io", + "futures-macro", + "futures-sink", "futures-task", + "memchr", "pin-project-lite", "pin-utils", + "slab", ] [[package]] @@ -223,12 +244,6 @@ version = "0.31.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" -[[package]] -name = "hashbrown" -version = "0.15.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e087f84d4f86bf4b218b927129862374b72199ae7d8657835f1e89000eea4fb" - [[package]] name = "hermit-abi" version = "0.3.9" @@ -347,16 +362,6 @@ dependencies = [ "unicode-normalization", ] -[[package]] -name = "indexmap" -version = "2.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da" -dependencies = [ - "equivalent", - "hashbrown", -] - [[package]] name = "ipnet" version = "2.10.1" @@ -615,10 +620,12 @@ dependencies = [ "sync_wrapper", "tokio", "tokio-rustls", + "tokio-util", "tower-service", "url", "wasm-bindgen", "wasm-bindgen-futures", + "wasm-streams", "web-sys", "webpki-roots", "windows-registry", @@ -741,19 +748,6 @@ dependencies = [ "serde", ] -[[package]] -name = "serde_yaml" -version = "0.9.34+deprecated" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47" -dependencies = [ - "indexmap", - "itoa", - "ryu", - "serde", - "unsafe-libyaml", -] - [[package]] name = "sha3" version = "0.10.8" @@ -900,6 +894,19 @@ dependencies = [ "tokio", ] +[[package]] +name = "tokio-util" +version = "0.7.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61e7c3654c13bcd040d4a03abee2c75b1d14a37b423cf5a813ceae1cc903ec6a" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", +] + [[package]] name = "tower-service" version = "0.3.3" @@ -964,12 +971,6 @@ dependencies = [ "tinyvec", ] -[[package]] -name = "unsafe-libyaml" -version = "0.2.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861" - [[package]] name = "untrusted" version = "0.9.0" @@ -1076,6 +1077,19 @@ version = "0.2.95" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "65fc09f10666a9f147042251e0dda9c18f166ff7de300607007e96bdebc1068d" +[[package]] +name = "wasm-streams" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15053d8d85c7eccdbefef60f06769760a563c7f0a9d6902a13d35c7800b0ad65" +dependencies = [ + "futures-util", + "js-sys", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", +] + [[package]] name = "web-sys" version = "0.3.72" diff --git a/Cargo.toml b/Cargo.toml index 7594e38..3e2cba5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,10 +6,9 @@ edition = "2021" [dependencies] anyhow = "1" hex = "0" -reqwest = { version = "0", features = ["json", "multipart", "rustls-tls"], default-features=false } +reqwest = { version = "0", features = ["json", "multipart", "rustls-tls", "stream"], default-features=false } serde = { version = "1", features = ["derive"] } serde_json = "1" -serde_yaml = "0" sha3 = "0" tokio = { version = "1", features = ["fs", "macros", "rt-multi-thread"] } url = { version = "2", features = ["serde"] } diff --git a/src/main.rs b/src/main.rs index 52f7348..aecacd6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -58,21 +58,21 @@ async fn build_request( request_builder .form(&serde_json::from_slice::>(&buffer)?) } - model::Body::Multipart(fbvec) => { + model::Body::Multipart(mp) => { let mut form = reqwest::multipart::Form::new(); - for mb in fbvec { - let mb_ = srcpath_.to_owned().join(mb); + for (name, path) in &mp.files { + let mb_: PathBuf = srcpath_.to_owned().join(path); + form = form.file(name.clone(), mb_).await?; + } + if let Some(mp_form) = &mp.form { let mut buffer = Vec::::new(); - let mut file = tokio::fs::File::open(&mb_).await?; + let mut file = tokio::fs::File::open(srcpath_.to_owned().join(mp_form)).await?; let read = file.read_buf(&mut buffer).await?; buffer.truncate(read); - let name = mb_ - .file_name() - .ok_or_else(|| anyhow!("Failed to get file name"))? - .to_str() - .ok_or_else(|| anyhow!("Failed to get file name"))? - .to_string(); - form = form.part(name, reqwest::multipart::Part::bytes(buffer)); + let form_hash_map: HashMap = serde_json::from_slice(&buffer)?; + for (k, v) in form_hash_map.into_iter() { + form = form.text(k, v); + } } request_builder.multipart(form) } @@ -85,11 +85,17 @@ async fn build_request( } async fn check(input: model::CheckInput) -> Result { + eprintln!("Calling {:?} {}", &input.source.method, &input.source.url); + let (client, request) = build_request(&input.source, None).await?; let mut response = client.execute(request).await?; + + eprintln!("Response code {}", &response.status()); + eprintln!("Headers {:?}", &response.headers()); + Ok(model::CheckOutput( match input.source.version_check_method { - model::VersionCheckMethod::None => vec![model::Version::None], + model::VersionCheckMethod::None => vec![model::Version::None("none".to_string())], model::VersionCheckMethod::Hash => { let mut hasher = sha3::Sha3_256::new(); while let Some(chunk) = response.chunk().await? { @@ -115,12 +121,18 @@ async fn in_(input: model::InInput, dest: &Path) -> Result { version: input.version, params: input.params, }; + + eprintln!("Calling {:?} {}", &input.source.method, &input.source.url); + let (client, request) = build_request(&input.source, None).await?; let mut response = client.execute(request).await?; + eprintln!("Response code {}", &response.status()); + eprintln!("Headers {:?}", &response.headers()); + let dest_code = dest.to_owned().join("code"); let dest_headers = dest.to_owned().join("headers"); - let dest_bytes = dest.to_owned().join("output"); + let dest_bytes = dest.to_owned().join("body"); let mut dest_code_writer = tokio::io::BufWriter::new(tokio::fs::File::create(dest_code).await?); dest_code_writer @@ -158,7 +170,7 @@ async fn in_(input: model::InInput, dest: &Path) -> Result { let out = model::InOutput { version: match input.source.version_check_method { - model::VersionCheckMethod::None => model::Version::None, + model::VersionCheckMethod::None => model::Version::None("none".to_string()), model::VersionCheckMethod::Hash => model::Version::Hash(hex::encode(hash)), model::VersionCheckMethod::Header(headername) => { let headervalue: &HeaderValue = response @@ -186,18 +198,20 @@ async fn out(input: model::OutInput, src: &Path) -> Result { source: input.source.merge_params(&input.params), params: input.params, }; + + eprintln!("Calling {:?} {}", &input.source.method, &input.source.url); + let (client, request) = build_request(&input.source, Some(src)).await?; let response = client.execute(request).await?; - response.error_for_status()?; - let version = check(model::CheckInput { - source: input.source.clone(), - version: None, - }) - .await? - .0[0] - .clone(); + let err = response.error_for_status_ref().map(|_| ()); + + eprintln!("Response code {}", &response.status()); + eprintln!("Headers {:?}", &response.headers()); + eprintln!("Body {:?}", response.text().await?); + + err?; Ok(model::OutOutput { - version, + version: model::Version::None("none".to_string()), metadata: vec![], }) } diff --git a/src/model.rs b/src/model.rs index 3bbde5e..26a164e 100644 --- a/src/model.rs +++ b/src/model.rs @@ -1,5 +1,6 @@ use anyhow::Result; use serde::{Deserialize, Deserializer, Serialize, Serializer}; +use std::collections::HashMap; use std::path::PathBuf; use std::str::FromStr; @@ -54,12 +55,20 @@ pub enum Auth { Bearer(String), } +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] +pub struct BodyMultipart { + #[serde(default)] + pub files: HashMap, + #[serde(default)] + pub form: Option, +} + #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] #[serde(rename_all = "snake_case")] pub enum Body { Raw(PathBuf), Form(PathBuf), - Multipart(Vec), + Multipart(BodyMultipart), } #[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq)] @@ -129,7 +138,7 @@ impl Source { pub enum Version { Header(String), Hash(String), - None, + None(String), } #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]