From 140a74c0715c2004ebf6af5948983a52ce540f4f Mon Sep 17 00:00:00 2001 From: ItsEthra <107059409+ItsEthra@users.noreply.github.com> Date: Sun, 3 Dec 2023 15:13:07 +0300 Subject: [PATCH] Use http::Uri in HxCurrentUrl --- src/extractors.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/extractors.rs b/src/extractors.rs index 7e7304f..bb366f1 100644 --- a/src/extractors.rs +++ b/src/extractors.rs @@ -42,10 +42,10 @@ where /// This is set on every request made by htmx itself. As its name implies, it /// just contains the current url. /// -/// This extractor will always return a value. If the header is not present, it -/// will return `None`. +/// This extractor will always return a value. If the header is not present, or extractor fails to parse the url +/// it will return `None`. #[derive(Debug, Clone)] -pub struct HxCurrentUrl(pub Option); +pub struct HxCurrentUrl(pub Option); #[async_trait] impl FromRequestParts for HxCurrentUrl @@ -56,9 +56,11 @@ where async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { if let Some(url) = parts.headers.get(HX_CURRENT_URL) { - if let Ok(url) = url.to_str() { - return Ok(HxCurrentUrl(Some(url.to_string()))); - } + let url = url + .to_str() + .ok() + .and_then(|url| url.parse::().ok()); + return Ok(HxCurrentUrl(url)); } return Ok(HxCurrentUrl(None));