Compare commits

..

No commits in common. "6261b3fb0348cf21d1b968d5b36ac90a51de3b69" and "3759b373511f22f5f535962f2161b35c95dce4d9" have entirely different histories.

4 changed files with 76 additions and 117 deletions

98
Cargo.lock generated
View file

@ -107,6 +107,7 @@ dependencies = [
"reqwest",
"serde",
"serde_json",
"serde_yaml",
"sha3",
"tokio",
"url",
@ -141,6 +142,12 @@ 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"
@ -171,29 +178,6 @@ 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"
@ -207,14 +191,9 @@ 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]]
@ -244,6 +223,12 @@ 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"
@ -362,6 +347,16 @@ 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"
@ -620,12 +615,10 @@ dependencies = [
"sync_wrapper",
"tokio",
"tokio-rustls",
"tokio-util",
"tower-service",
"url",
"wasm-bindgen",
"wasm-bindgen-futures",
"wasm-streams",
"web-sys",
"webpki-roots",
"windows-registry",
@ -748,6 +741,19 @@ 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"
@ -894,19 +900,6 @@ 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"
@ -971,6 +964,12 @@ 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"
@ -1077,19 +1076,6 @@ 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"

View file

@ -6,9 +6,10 @@ edition = "2021"
[dependencies]
anyhow = "1"
hex = "0"
reqwest = { version = "0", features = ["json", "multipart", "rustls-tls", "stream"], default-features=false }
reqwest = { version = "0", features = ["json", "multipart", "rustls-tls"], 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"] }

View file

@ -58,21 +58,21 @@ async fn build_request(
request_builder
.form(&serde_json::from_slice::<HashMap<String, String>>(&buffer)?)
}
model::Body::Multipart(mp) => {
model::Body::Multipart(fbvec) => {
let mut form = reqwest::multipart::Form::new();
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 {
for mb in fbvec {
let mb_ = srcpath_.to_owned().join(mb);
let mut buffer = Vec::<u8>::new();
let mut file = tokio::fs::File::open(srcpath_.to_owned().join(mp_form)).await?;
let mut file = tokio::fs::File::open(&mb_).await?;
let read = file.read_buf(&mut buffer).await?;
buffer.truncate(read);
let form_hash_map: HashMap<String, String> = serde_json::from_slice(&buffer)?;
for (k, v) in form_hash_map.into_iter() {
form = form.text(k, v);
}
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));
}
request_builder.multipart(form)
}
@ -85,17 +85,11 @@ async fn build_request(
}
async fn check(input: model::CheckInput) -> Result<model::CheckOutput> {
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("none".to_string())],
model::VersionCheckMethod::None => vec![model::Version::None],
model::VersionCheckMethod::Hash => {
let mut hasher = sha3::Sha3_256::new();
while let Some(chunk) = response.chunk().await? {
@ -121,18 +115,12 @@ async fn in_(input: model::InInput, dest: &Path) -> Result<model::InOutput> {
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("body");
let dest_bytes = dest.to_owned().join("output");
let mut dest_code_writer = tokio::io::BufWriter::new(tokio::fs::File::create(dest_code).await?);
dest_code_writer
@ -170,7 +158,7 @@ async fn in_(input: model::InInput, dest: &Path) -> Result<model::InOutput> {
let out = model::InOutput {
version: match input.source.version_check_method {
model::VersionCheckMethod::None => model::Version::None("none".to_string()),
model::VersionCheckMethod::None => model::Version::None,
model::VersionCheckMethod::Hash => model::Version::Hash(hex::encode(hash)),
model::VersionCheckMethod::Header(headername) => {
let headervalue: &HeaderValue = response
@ -182,13 +170,11 @@ async fn in_(input: model::InInput, dest: &Path) -> Result<model::InOutput> {
},
metadata: vec![],
};
if let Some(input_version) = &input.version {
if out.version != *input_version {
return Err(anyhow!(format!(
"Did not get correct version of resource, requested '{:?}', got '{:?}'",
input.version, out.version
)));
}
if out.version != input.version {
return Err(anyhow!(format!(
"Did not get correct version of resource, requested '{:?}', got '{:?}'",
input.version, out.version
)));
}
Ok(out)
}
@ -198,20 +184,18 @@ async fn out(input: model::OutInput, src: &Path) -> Result<model::OutOutput> {
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?;
let err = response.error_for_status_ref().map(|_| ());
eprintln!("Response code {}", &response.status());
eprintln!("Headers {:?}", &response.headers());
eprintln!("Body {:?}", response.text().await?);
err?;
response.error_for_status()?;
let version = check(model::CheckInput {
source: input.source.clone(),
version: model::Version::None,
})
.await?
.0[0]
.clone();
Ok(model::OutOutput {
version: model::Version::None("none".to_string()),
version,
metadata: vec![],
})
}

View file

@ -1,6 +1,5 @@
use anyhow::Result;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::collections::HashMap;
use std::path::PathBuf;
use std::str::FromStr;
@ -55,20 +54,12 @@ pub enum Auth {
Bearer(String),
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct BodyMultipart {
#[serde(default)]
pub files: HashMap<String, PathBuf>,
#[serde(default)]
pub form: Option<PathBuf>,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum Body {
Raw(PathBuf),
Form(PathBuf),
Multipart(BodyMultipart),
Multipart(Vec<PathBuf>),
}
#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq)]
@ -138,7 +129,7 @@ impl Source {
pub enum Version {
Header(String),
Hash(String),
None(String),
None,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
@ -159,22 +150,19 @@ pub struct Params {
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct CheckInput {
pub source: Source,
#[serde(default)]
pub version: Option<Version>,
pub version: Version,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct InInput {
pub source: Source,
pub version: Option<Version>,
#[serde(default)]
pub version: Version,
pub params: Params,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct OutInput {
pub source: Source,
#[serde(default)]
pub params: Params,
}