Skip to content
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ cargo add rhoxy
Or add the following line to your Cargo.toml:

```
rhoxy = "0.2.5"
rhoxy = "0.2.6"
```

### Source Install
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ async fn handle_connection(stream: TcpStream, peer_addr: std::net::SocketAddr) -

let (method, url_string) = rhoxy::extract_request_parts(&mut reader).await?;

let protocol = rhoxy::protocol::Protocol::get_protocol_from_method(&method).await;
let protocol = rhoxy::protocol::Protocol::get_protocol_from_method(&method);

info!("[{peer_addr}::{}] {url_string}", protocol.to_string().await);
info!("[{peer_addr}::{}] {url_string}", protocol.display());

if url_string == rhoxy::constants::HEALTH_ENDPOINT_PATH {
return rhoxy::handle_health_check(&mut writer).await;
Expand Down
44 changes: 37 additions & 7 deletions src/protocol/http.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
use anyhow::Result;
use http::Method;
use reqwest::Url;
use std::{collections::HashMap, time::Duration};
use std::{collections::HashMap, sync::LazyLock, time::Duration};
use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt};
use tracing::{debug, error};

use crate::constants;

static HTTP_CLIENT: LazyLock<reqwest::Client> = LazyLock::new(|| {
reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.connect_timeout(Duration::from_secs(10))
.pool_max_idle_per_host(20)
.pool_idle_timeout(Duration::from_secs(90))
.tcp_keepalive(Duration::from_secs(60))
.http2_keep_alive_interval(Duration::from_secs(30))
.http2_keep_alive_timeout(Duration::from_secs(10))
.http2_keep_alive_while_idle(true)
.no_proxy()
.build()
.expect("Failed to build HTTP client")
});

#[derive(Debug)]
struct HttpRequest {
method: Method,
Expand Down Expand Up @@ -90,18 +105,19 @@ where
}

async fn send_request(request: &HttpRequest) -> Result<reqwest::Response> {
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(10))
.no_proxy()
.build()?;
let mut req = HTTP_CLIENT.request(request.method.clone(), request.url.clone());

let mut req = client.request(request.method.clone(), request.url.clone());
for (key, value) in &request.headers {
req = req.header(key, value);
let key_lower = key.to_lowercase();
if !is_hop_by_hop_header(&key_lower) {
req = req.header(key, value);
}
}

if let Some(body) = &request.body {
req = req.body(body.clone());
}

let response = req.send().await?;
Ok(response)
}
Expand Down Expand Up @@ -183,6 +199,20 @@ const fn http_version_to_string(version: http::Version) -> &'static str {
}
}

fn is_hop_by_hop_header(header: &str) -> bool {
matches!(
header,
"connection"
| "keep-alive"
| "proxy-authenticate"
| "proxy-authorization"
| "te"
| "trailers"
| "transfer-encoding"
| "upgrade"
)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
4 changes: 2 additions & 2 deletions src/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ impl Protocol {
}
}

pub async fn to_string(&self) -> String {
pub fn display(&self) -> String {
match self {
Protocol::Http => "HTTP".to_string(),
Protocol::Https => "HTTPS".to_string(),
}
}

pub async fn get_protocol_from_method(method: &Method) -> Self {
pub fn get_protocol_from_method(method: &Method) -> Self {
if method == Method::CONNECT {
Protocol::Https
} else {
Expand Down
Loading