Skip to content

Add ability to use and https client. #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: johnsonw/bump-axum
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 54 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ categories = ["web-programming::http-server", "network-programming"]
authors = ["Tom Lubenow"]

[dependencies]
axum = { version = "^0.7", features = ["ws"] }
axum = { version = "^0.8", features = ["ws"] }
base64 = "^0.21"
bytes = "^1.5"
futures-util = "^0.3"
http = "^1.0"
http-body-util = "^0.1"
hyper = { version = "^1.0", features = ["full"] }
hyper-tls = "0.6"
hyper-util = { version = "^0.1", features = ["client", "client-legacy", "http1", "http2", "tokio"] }
sha1 = "^0.10"
tokio = { version = "^1.35", features = ["full"] }
Expand Down
110 changes: 106 additions & 4 deletions src/proxy.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,38 @@
use axum::body::Body;
use http::StatusCode;
use http_body_util::BodyExt;
use hyper_tls::HttpsConnector;
use hyper_util::client::legacy::{connect::HttpConnector, Client};
use std::convert::Infallible;
use tracing::{error, trace};

use crate::websocket;

#[derive(Clone)]
enum ClientConnector {
Http(Client<HttpConnector, Body>),
Tls(Client<HttpsConnector<HttpConnector>, Body>),
}

trait ClientRequest {
fn request(
&self,
req: http::Request<axum::body::Body>,
) -> hyper_util::client::legacy::ResponseFuture;
}

impl ClientRequest for ClientConnector {
fn request(
&self,
req: http::Request<axum::body::Body>,
) -> hyper_util::client::legacy::ResponseFuture {
match self {
ClientConnector::Http(client) => client.request(req),
ClientConnector::Tls(client) => client.request(req),
}
}
}

/// A reverse proxy that forwards HTTP requests to an upstream server.
///
/// The `ReverseProxy` struct handles the forwarding of HTTP requests from a specified path
Expand All @@ -16,7 +42,7 @@ use crate::websocket;
pub struct ReverseProxy {
path: String,
target: String,
client: Client<HttpConnector, Body>,
client: ClientConnector,
}

impl ReverseProxy {
Expand Down Expand Up @@ -55,6 +81,36 @@ impl ReverseProxy {
Self::new_with_client(path, target, client)
}

/// Creates a new `ReverseProxy` instance using an Https connector.
///
/// # Arguments
///
/// * `path` - The base path to match incoming requests against (e.g., "/api")
/// * `target` - The upstream server URL to forward requests to (e.g., "https://api.example.com")
///
/// # Example
///
/// ```rust
/// use axum_reverse_proxy::ReverseProxy;
///
/// let proxy = ReverseProxy::new_tls("/api", "https://api.example.com");
/// ```
pub fn new_tls<S>(path: S, target: S) -> Self
where
S: Into<String>,
{
let connector = HttpsConnector::new();

let client = Client::builder(hyper_util::rt::TokioExecutor::new())
.pool_idle_timeout(std::time::Duration::from_secs(60))
.pool_max_idle_per_host(32)
.retry_canceled_requests(true)
.set_host(true)
.build(connector);

Self::new_with_tls_client(path, target, client)
}

/// Creates a new `ReverseProxy` instance with a custom HTTP client.
///
/// This method allows for more fine-grained control over the proxy behavior by accepting
Expand All @@ -71,14 +127,60 @@ impl ReverseProxy {
/// ```rust
/// use axum_reverse_proxy::ReverseProxy;
/// use hyper_util::client::legacy::{Client, connect::HttpConnector};
/// use hyper_tls::HttpsConnector;
/// use axum::body::Body;
/// use hyper_util::rt::TokioExecutor;
///
/// let client = Client::builder(TokioExecutor::new())
/// .pool_idle_timeout(std::time::Duration::from_secs(120))
/// .build(HttpsConnector::<HttpConnector>::new());
///
/// let proxy = ReverseProxy::new_with_tls_client(
/// "/api",
/// "https://api.example.com",
/// client,
/// );
/// ```
pub fn new_with_tls_client<S>(
path: S,
target: S,
client: Client<HttpsConnector<HttpConnector>, Body>,
) -> Self
where
S: Into<String>,
{
Self {
path: path.into(),
target: target.into(),
client: ClientConnector::Tls(client),
}
}

/// Creates a new `ReverseProxy` instance with a custom HTTPS client.
///
/// This method allows for more fine-grained control over the proxy behavior by accepting
/// a pre-configured HTTPS client.
///
/// # Arguments
///
/// * `path` - The base path to match incoming requests against
/// * `target` - The upstream server URL to forward requests to
/// * `client` - A custom-configured HTTPS client
///
/// # Example
///
/// ```rust
/// use axum_reverse_proxy::ReverseProxy;
/// use hyper_tls::HttpsConnector;
/// use hyper_util::client::legacy::Client;
/// use axum::body::Body;
/// use hyper_util::rt::TokioExecutor;
///
/// let client = Client::builder(TokioExecutor::new())
/// .pool_idle_timeout(std::time::Duration::from_secs(120))
/// .build(HttpConnector::new());
/// .build(HttpsConnector::new());
///
/// let proxy = ReverseProxy::new_with_client(
/// let proxy = ReverseProxy::new_with_tls_client(
/// "/api",
/// "https://api.example.com",
/// client,
Expand All @@ -91,7 +193,7 @@ impl ReverseProxy {
Self {
path: path.into(),
target: target.into(),
client,
client: ClientConnector::Http(client),
}
}

Expand Down