changed from reqwest to hyper and added own httpclient file
This commit is contained in:
parent
4562c1c294
commit
9dbf18d307
9 changed files with 1287 additions and 61 deletions
1208
Cargo.lock
generated
1208
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -8,8 +8,10 @@ authors = ["Manuel Keidel", "Patryk Hegenberg", "Krzysztof Stankiewicz"]
|
|||
|
||||
[dependencies]
|
||||
clap = { version = "4.5.4", features = ["derive"] }
|
||||
# reqwest = { version = "0.12.3", features = ["blocking", "json"] }
|
||||
reqwest = { version = "0.12.3", features = ["blocking", "json"] }
|
||||
hyper = { version = "1", features = ["full"] }
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
# http-body-util = "0.1"
|
||||
# hyper-util = { version = "0.1", features = ["full"] }
|
||||
http-body-util = "0.1"
|
||||
hyper-util = { version = "0.1", features = ["full"] }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
|
|
|
|||
|
|
@ -1,8 +0,0 @@
|
|||
[package]
|
||||
name = "caesar_core"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
12
src/args.rs
12
src/args.rs
|
|
@ -1,7 +1,7 @@
|
|||
use crate::receiver;
|
||||
use crate::sender;
|
||||
use clap::{Parser, Subcommand};
|
||||
use reqwest::blocking::Client;
|
||||
// use reqwest::blocking::Client;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(version, about, long_about = None)]
|
||||
|
|
@ -62,10 +62,14 @@ impl Args {
|
|||
pub fn new() -> Self {
|
||||
Self::parse()
|
||||
}
|
||||
pub fn run(&self, client: Client) -> Result<(), Box<dyn std::error::Error>> {
|
||||
pub async fn run(
|
||||
&self,
|
||||
// client: Client,
|
||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
match &self.command {
|
||||
Some(Commands::Send { relay, file }) => {
|
||||
sender::send_info(client, file.as_deref().unwrap_or("test.txt"))?;
|
||||
println!("Stuff");
|
||||
sender::send_info(file.as_deref().unwrap_or("test.txt")).await?;
|
||||
}
|
||||
Some(Commands::Receive {
|
||||
relay,
|
||||
|
|
@ -73,7 +77,7 @@ impl Args {
|
|||
name,
|
||||
}) => {
|
||||
let transfer_name = name.as_deref().unwrap_or("None");
|
||||
receiver::download_info(client, transfer_name)?
|
||||
receiver::download_info(transfer_name).await?
|
||||
}
|
||||
Some(Commands::Serve { port }) => {}
|
||||
Some(Commands::Config {
|
||||
|
|
|
|||
54
src/http_client.rs
Normal file
54
src/http_client.rs
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
use http_body_util::{BodyExt, Empty, Full};
|
||||
use hyper::{body::Bytes, Request, StatusCode};
|
||||
use hyper_util::rt::TokioIo;
|
||||
use tokio::{
|
||||
io::{self, AsyncWriteExt},
|
||||
net::TcpStream,
|
||||
};
|
||||
|
||||
pub async fn send_request(
|
||||
url: &str,
|
||||
method: &str,
|
||||
body: Option<String>,
|
||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
let url = url.parse::<hyper::Uri>()?;
|
||||
let host = url.host().expect("uri has no host");
|
||||
let port = url.port_u16().unwrap_or(80);
|
||||
let address = format!("{}:{}", host, port);
|
||||
let stream = TcpStream::connect(address).await?;
|
||||
let io = TokioIo::new(stream);
|
||||
let (mut sender, conn) = hyper::client::conn::http1::handshake(io).await?;
|
||||
tokio::task::spawn(async move {
|
||||
if let Err(err) = conn.await {
|
||||
eprintln!("Connection failed: {:?}", err);
|
||||
}
|
||||
});
|
||||
|
||||
let authority = url.authority().unwrap().clone();
|
||||
let send_body = match body {
|
||||
Some(body_str) => Full::<Bytes>::from(Bytes::from(body_str)),
|
||||
None => Full::<Bytes>::from(Bytes::from("")),
|
||||
};
|
||||
|
||||
let req = Request::builder()
|
||||
.method(method)
|
||||
.uri(url)
|
||||
.header(hyper::header::HOST, authority.as_str())
|
||||
.header(hyper::header::CONTENT_TYPE, "application/json")
|
||||
.body(send_body)?;
|
||||
|
||||
let mut res = sender.send_request(req).await?;
|
||||
|
||||
println!("Response status: {}", res.status());
|
||||
|
||||
if res.status() == StatusCode::OK {
|
||||
while let Some(next) = res.frame().await {
|
||||
let frame = next?;
|
||||
if let Some(chunk) = frame.data_ref() {
|
||||
io::stdout().write_all(chunk).await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
10
src/main.rs
10
src/main.rs
|
|
@ -1,13 +1,13 @@
|
|||
use reqwest::blocking::Client;
|
||||
|
||||
mod args;
|
||||
pub mod http_client;
|
||||
pub mod receiver;
|
||||
pub mod sender;
|
||||
|
||||
fn main() {
|
||||
let client = Client::new();
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
let args = args::Args::new();
|
||||
if let Err(e) = args.run(client) {
|
||||
if let Err(e) = args.run().await {
|
||||
eprintln!("Error {e}");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,16 +1,13 @@
|
|||
use reqwest::{blocking::Client, StatusCode};
|
||||
use std::collections::HashMap;
|
||||
use crate::http_client;
|
||||
|
||||
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
||||
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
|
||||
|
||||
pub fn download_info(client: Client, filename: &str) -> Result<()> {
|
||||
let res = client
|
||||
.get(format!("http://192.168.178.43:1323/download/{}", filename))
|
||||
.send()?;
|
||||
|
||||
if res.status() == StatusCode::OK {
|
||||
let json: HashMap<String, String> = res.json()?;
|
||||
println!("JSON Response: {:?}", json);
|
||||
}
|
||||
pub async fn download_info(filename: &str) -> Result<()> {
|
||||
http_client::send_request(
|
||||
format!("http://192.168.178.43:1323/download/{}", filename).trim(),
|
||||
"GET",
|
||||
None,
|
||||
)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,23 +1,21 @@
|
|||
use reqwest::{blocking::Client, StatusCode};
|
||||
use crate::http_client;
|
||||
use std::collections::HashMap;
|
||||
|
||||
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
||||
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
|
||||
|
||||
pub fn send_info(client: Client, file: &str) -> Result<()> {
|
||||
pub async fn send_info(file: &str) -> Result<()> {
|
||||
let mut map = HashMap::new();
|
||||
map.insert("keyword", "test");
|
||||
map.insert("files", file);
|
||||
|
||||
let res = client
|
||||
.post("http://192.168.178.43:1323/upload")
|
||||
.json(&map)
|
||||
.send()?;
|
||||
let json_data = serde_json::to_string(&map)?;
|
||||
|
||||
http_client::send_request(
|
||||
"http://192.168.178.43:1323/upload".trim(),
|
||||
"POST",
|
||||
Some(json_data),
|
||||
)
|
||||
.await?;
|
||||
|
||||
if res.status() == StatusCode::OK {
|
||||
let json: HashMap<String, String> = res.json()?;
|
||||
println!("JSON Response: {:?}", json);
|
||||
} else {
|
||||
println!("Error: Faile to send request");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue