From 9ce4120ec3ab125cfa0918c936d03e2ee7849cb6 Mon Sep 17 00:00:00 2001 From: Stanislav Ravas Date: Tue, 8 Oct 2024 23:35:13 +0200 Subject: [PATCH] fix(request): if host is ipv6 address, enclose it in square brackets Related RFC requires it this way, and also at least apache would reject non-bracketed ipv6 host address with Bad Request. --- src/request.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/request.rs b/src/request.rs index a9238e5..674032a 100644 --- a/src/request.rs +++ b/src/request.rs @@ -2,6 +2,8 @@ use crate::headers::ContentType; use crate::Error; use core::fmt::Write as _; +use core::net::Ipv6Addr; +use core::str::FromStr; use embedded_io::Error as _; use embedded_io_async::Write; use heapless::String; @@ -139,7 +141,17 @@ where } } if let Some(host) = &self.host { - write_header(c, "Host", host).await?; + let is_ipv6 = Ipv6Addr::from_str(host).is_ok(); + write_str(c, "Host").await?; + write_str(c, ": ").await?; + if is_ipv6 { + write_str(c, "[").await?; + } + write_str(c, host).await?; + if is_ipv6 { + write_str(c, "]").await?; + } + write_str(c, "\r\n").await?; } if let Some(content_type) = &self.content_type { write_header(c, "Content-Type", content_type.as_str()).await?;