Skip to content

Commit

Permalink
To socket addrs from tuple not string
Browse files Browse the repository at this point in the history
  • Loading branch information
DanGould committed Mar 17, 2024
1 parent cea496b commit f610f0d
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use once_cell::sync::Lazy;
use tokio::io::{AsyncRead, AsyncWrite};
use tokio::net::{TcpListener, UnixListener};
use tokio_util::net::Listener;
use tracing::{error, info, instrument};
use tracing::{debug, error, info, instrument};

pub mod error;
mod gateway_uri;
Expand Down Expand Up @@ -159,18 +159,18 @@ async fn forward_request(req: Request<Incoming>) -> Result<Response<Incoming>, E

#[instrument]
pub(crate) fn uri_to_addr(uri: &Uri) -> Option<SocketAddr> {
let authority = uri.authority()?.as_str();
let parts: Vec<&str> = authority.split(':').collect();
let host = parts.first()?;
let port = parts.get(1).and_then(|p| p.parse::<u16>().ok());

let default_port = match uri.scheme_str() {
Some("https") => 443,
_ => 80, // Default to 80 if it's not https or if the scheme is not specified
};

let addr_str = format!("{}:{}", host, port.unwrap_or(default_port));
addr_str.to_socket_addrs().ok()?.next()
let authority = uri.authority()?;

let host = authority.host();
let port = authority.port_u16().or_else(|| {
match uri.scheme_str() {
Some("https") => Some(443),
_ => Some(80), // Default to 80 if it's not https or if the scheme is not specified
}
})?;
let addr = (host, port).to_socket_addrs().ok()?.next()?;
debug!("Resolved address: {:?}", addr);
Some(addr)
}

pub(crate) fn empty() -> BoxBody<Bytes, hyper::Error> {
Expand Down

0 comments on commit f610f0d

Please sign in to comment.