Skip to content

Commit

Permalink
OK
Browse files Browse the repository at this point in the history
  • Loading branch information
hatoo committed Nov 17, 2024
1 parent 16fc3db commit 567db4f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 43 deletions.
8 changes: 0 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ resolver = "2"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
reqwest = ["dep:reqwest"]

[dependencies]
tokio = { version = "1.39.3", features = [
"macros",
Expand All @@ -39,7 +36,6 @@ hyper-util = { version = "0.1.7", features = ["tokio"] }
native-tls = { version = "0.2.12", features = ["alpn"] }
thiserror = "2.0.2"
moka = { version = "0.12.8", features = ["sync"] }
reqwest = { version = "0.12.7", optional = true }

[dev-dependencies]
axum = { version = "0.7.2", features = ["http2"] }
Expand All @@ -48,7 +44,3 @@ rcgen = { version = "0.13.1", features = ["x509-parser"] }
reqwest = { version = "0.12.7", features = ["native-tls-alpn"] }
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
winnow = "0.6.20"

[[example]]
name = "reqwest_proxy"
required-features = ["reqwest"]
36 changes: 33 additions & 3 deletions examples/reqwest_proxy.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/// This example demonstrates how to use `http-mitm-proxy` with `reqwest`.
use std::path::PathBuf;

use bytes::Bytes;
use clap::{Args, Parser};
use http_mitm_proxy::MitmProxy;
use hyper::service::service_fn;
use hyper::{body::Body, service::service_fn};
use moka::sync::Cache;
use tracing_subscriber::EnvFilter;

Expand Down Expand Up @@ -86,14 +88,14 @@ async fn main() {
// You can modify request here
// or You can just return response anywhere

let req = http_mitm_proxy::reqwest_client::to_reqwest(req);
let req = to_reqwest(req);
let res = client.execute(req).await?;

println!("{} -> {}", uri, res.status());

// You can modify response here

Ok::<_, reqwest::Error>(http_mitm_proxy::reqwest_client::from_reqwest(res))
Ok::<_, reqwest::Error>(from_reqwest(res))
}
}),
)
Expand All @@ -118,3 +120,31 @@ async fn main() {

server.await;
}

fn to_reqwest<T>(req: hyper::Request<T>) -> reqwest::Request
where
T: Body + Send + Sync + 'static,
T::Data: Into<Bytes>,
T::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
{
let (parts, body) = req.into_parts();
let url = reqwest::Url::parse(&parts.uri.to_string()).unwrap();
let mut req = reqwest::Request::new(parts.method, url);
*req.headers_mut() = parts.headers;
req.body_mut().replace(reqwest::Body::wrap(body));
*req.version_mut() = parts.version;

req
}

fn from_reqwest(res: reqwest::Response) -> hyper::Response<reqwest::Body> {
let mut hres = hyper::Response::builder()
.status(res.status())
.version(res.version());

*hres.headers_mut().unwrap() = res.headers().clone();

let body = reqwest::Body::from(res);

hres.body(body).unwrap()
}
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ pub use moka;
pub use tokio_native_tls;

pub mod default_client;
#[cfg(feature = "reqwest")]
pub mod reqwest_client;
mod tls;

pub use default_client::DefaultClient;
Expand Down
30 changes: 0 additions & 30 deletions src/reqwest_client.rs

This file was deleted.

0 comments on commit 567db4f

Please sign in to comment.