Skip to content

Commit

Permalink
Merge pull request #71 from hatoo/clean-types2
Browse files Browse the repository at this point in the history
Clean types 2
  • Loading branch information
hatoo authored Nov 8, 2024
2 parents 91fb0b2 + 9a45ba1 commit aa2c876
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use hyper::{
};
use hyper_util::rt::{TokioExecutor, TokioIo};
use moka::sync::Cache;
use std::{borrow::Borrow, future::Future, sync::Arc};
use std::{borrow::Borrow, error::Error as StdError, future::Future, sync::Arc};
use tls::{generate_cert, CertifiedKeyDer};
use tokio::net::{TcpListener, TcpStream, ToSocketAddrs};

Expand Down Expand Up @@ -49,20 +49,24 @@ impl<C> MitmProxy<C> {

// pub type Handler<B, E> = Fn(Request<Incoming>) -> Result<Response<B>, E>;

impl<C: Borrow<rcgen::CertifiedKey> + Send + Sync + 'static> MitmProxy<C> {
impl<C> MitmProxy<C>
where
C: Borrow<rcgen::CertifiedKey> + Send + Sync + 'static,
{
/// Bind to a socket address and return a future that runs the proxy server.
/// URL for requests that passed to service are full URL including scheme.
pub async fn bind<A: ToSocketAddrs, S, B, E>(
pub async fn bind<A: ToSocketAddrs, S>(
self,
addr: A,
service: S,
) -> Result<impl Future<Output = ()>, std::io::Error>
where
S: HttpService<Incoming, ResBody = B, Error = E, Future: Send> + Send + Clone + 'static,
B::Data: Send + 'static,
B: Body + Send + Sync + 'static,
B::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
E: std::error::Error + Send + Sync + 'static,
S: HttpService<Incoming> + Clone + Send + 'static,
S::Error: Into<Box<dyn StdError + Send + Sync>>,
S::ResBody: Send + Sync + 'static,
<S::ResBody as Body>::Data: Send,
<S::ResBody as Body>::Error: Into<Box<dyn StdError + Send + Sync>>,
S::Future: Send,
{
let listener = TcpListener::bind(addr).await?;

Expand Down Expand Up @@ -100,16 +104,21 @@ impl<C: Borrow<rcgen::CertifiedKey> + Send + Sync + 'static> MitmProxy<C> {
/// See `examples/https.rs` for usage.
/// If you want to serve simple HTTP proxy server, you can use `bind` method instead.
/// `bind` will call this method internally.
pub fn wrap_service<S, B, E>(
pub fn wrap_service<S>(
proxy: Arc<Self>,
service: S,
) -> impl HttpService<Incoming, ResBody = BoxBody<B::Data, B::Error>, Future: Send>
) -> impl HttpService<
Incoming,
ResBody = BoxBody<<S::ResBody as Body>::Data, <S::ResBody as Body>::Error>,
Future: Send,
>
where
S: HttpService<Incoming, ResBody = B, Error = E, Future: Send> + Send + Clone + 'static,
B::Data: Send + 'static,
B: Body + Send + Sync + 'static,
B::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
E: std::error::Error + Send + Sync + 'static,
S: HttpService<Incoming> + Clone + Send + 'static,
S::Error: Into<Box<dyn StdError + Send + Sync>>,
S::ResBody: Send + Sync + 'static,
<S::ResBody as Body>::Data: Send,
<S::ResBody as Body>::Error: Into<Box<dyn StdError + Send + Sync>>,
S::Future: Send,
{
service_fn(move |req| {
let proxy = proxy.clone();
Expand Down

0 comments on commit aa2c876

Please sign in to comment.