Skip to content

Commit 9ffe1fb

Browse files
committed
fix: support :port format for bind configuration
This commit adds support for the Go-style :port bind syntax (e.g., :80, :8080) which binds to all interfaces (0.0.0.0) on the specified port. Changes: - Updated parse_bind_config() to handle :port format - Added test_server_bind_colon_prefix_port test - Updated start_server_with_bind() helper to parse :port format Now supports all bind formats: - "80" -> 0.0.0.0:80 - ":80" -> 0.0.0.0:80 (new) - "127.0.0.1:80" -> 127.0.0.1:80 - "127.0.0.1" -> 127.0.0.1:8080 (server mode default)
1 parent 10e3d54 commit 9ffe1fb

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

src/main.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,14 +492,24 @@ async fn main() -> Result<()> {
492492
}
493493

494494
// Parse bind configuration from env vars
495-
// Returns Some(addr) for "port" or "ip:port" formats (including explicit :0)
495+
// Returns Some(addr) for "port", ":port", or "ip:port" formats (including explicit :0)
496496
// Returns None for "ip" only or missing config
497497
fn parse_bind_config(env_var: &str) -> Option<std::net::SocketAddr> {
498498
if let Ok(val) = std::env::var(env_var) {
499+
let val = val.trim();
500+
499501
// First try parsing as "ip:port" (respects explicit :0)
500502
if let Ok(addr) = val.parse::<std::net::SocketAddr>() {
501503
return Some(addr);
502504
}
505+
506+
// Try parsing as ":port" (Go-style) - bind to all interfaces (0.0.0.0)
507+
if let Some(port_str) = val.strip_prefix(':') {
508+
if let Ok(port) = port_str.parse::<u16>() {
509+
return Some(std::net::SocketAddr::from(([0, 0, 0, 0], port)));
510+
}
511+
}
512+
503513
// Try parsing as just a port number - bind to all interfaces (0.0.0.0)
504514
if let Ok(port) = val.parse::<u16>() {
505515
return Some(std::net::SocketAddr::from(([0, 0, 0, 0], port)));

tests/weak_integration.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,9 @@ fn start_server_with_bind(http_bind: &str, https_bind: &str) -> (std::process::C
309309
// Parse expected port from bind config (server mode uses defaults for unspecified)
310310
let expected_port = if let Ok(port) = http_bind.parse::<u16>() {
311311
port
312+
} else if let Some(port_str) = http_bind.strip_prefix(':') {
313+
// Handle :port format
314+
port_str.parse::<u16>().unwrap_or(8080)
312315
} else if let Ok(addr) = http_bind.parse::<std::net::SocketAddr>() {
313316
addr.port()
314317
} else {
@@ -344,6 +347,18 @@ fn test_server_bind_port_only() {
344347
server.kill().ok();
345348
}
346349

350+
#[test]
351+
#[serial]
352+
fn test_server_bind_colon_prefix_port() {
353+
// :port (Go-style) should bind to all interfaces (0.0.0.0)
354+
let (mut server, port) = start_server_with_bind(":19892", ":19893");
355+
assert_eq!(
356+
port, 19892,
357+
"Server should bind to specified port on all interfaces with :port format"
358+
);
359+
server.kill().ok();
360+
}
361+
347362
#[test]
348363
#[serial]
349364
fn test_server_bind_all_interfaces() {

0 commit comments

Comments
 (0)