fix: normalize_host handles IPv6 bracket notation
Extract strip_port_from_host into shared utils module and update normalize_host to properly strip brackets from IPv6 addresses like [::1]:443 -> ::1 instead of incorrectly using split(':').next().
This commit is contained in:
@@ -13,19 +13,7 @@ use crate::config::static_config::ListenerConfig;
|
||||
|
||||
const ACME_CHALLENGE_PREFIX: &str = "/.well-known/acme-challenge/";
|
||||
|
||||
fn strip_port_from_host(host: &str) -> &str {
|
||||
if host.starts_with('[') {
|
||||
if let Some(bracket_end) = host.find(']') {
|
||||
&host[..bracket_end + 1]
|
||||
} else {
|
||||
host
|
||||
}
|
||||
} else if let Some(colon_pos) = host.rfind(':') {
|
||||
&host[..colon_pos]
|
||||
} else {
|
||||
host
|
||||
}
|
||||
}
|
||||
use crate::utils::strip_port_from_host;
|
||||
|
||||
pub fn build_redirect_url(host: &str, https_port: u16, path: &str, query: &str) -> String {
|
||||
let hostname = strip_port_from_host(host);
|
||||
@@ -218,29 +206,4 @@ mod tests {
|
||||
let url = build_redirect_url("203.0.113.10", 443, "/", "");
|
||||
assert_eq!(url, "https://203.0.113.10/");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_strip_port_from_host_plain() {
|
||||
assert_eq!(strip_port_from_host("example.com"), "example.com");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_strip_port_from_host_with_port() {
|
||||
assert_eq!(strip_port_from_host("example.com:8080"), "example.com");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_strip_port_from_host_ipv6_bare() {
|
||||
assert_eq!(strip_port_from_host("[::1]"), "[::1]");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_strip_port_from_host_ipv6_with_port() {
|
||||
assert_eq!(strip_port_from_host("[::1]:8080"), "[::1]");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_strip_port_from_host_ipv4_with_port() {
|
||||
assert_eq!(strip_port_from_host("192.168.1.1:8080"), "192.168.1.1");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user