From 5334036f489b18335ca2742a3d86fdaab134a47c Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Fri, 7 Jun 2024 16:03:35 +0100 Subject: [PATCH] chore: make append_header infallible --- actix-web/CHANGES.md | 2 +- actix-web/src/response/customize_responder.rs | 40 +++++++++---------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/actix-web/CHANGES.md b/actix-web/CHANGES.md index 2ee35c44b..abbc8f5e0 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -5,6 +5,7 @@ ### Added - Implement `From>` for `Error`. +- Add `CustomizeResponder::add_cookie()` method. ## 4.6.0 @@ -43,7 +44,6 @@ - Updated `zstd` dependency to `0.13`. - Compression middleware now prefers brotli over zstd over gzip. -- Added .customize().add_cookie() for `CustomizeResponder` ### Fixed diff --git a/actix-web/src/response/customize_responder.rs b/actix-web/src/response/customize_responder.rs index 8035357d5..6a43ac5e6 100644 --- a/actix-web/src/response/customize_responder.rs +++ b/actix-web/src/response/customize_responder.rs @@ -5,15 +5,9 @@ use actix_http::{ StatusCode, }; -#[cfg(feature = "cookies")] -use { - actix_http::header::{self, HeaderValue}, - cookie::Cookie, -}; - use crate::{HttpRequest, HttpResponse, Responder}; -/// Allows overriding status code and headers (cookies) for a [`Responder`]. +/// Allows overriding status code and headers (including cookies) for a [`Responder`]. /// /// Created by calling the [`customize`](Responder::customize) method on a [`Responder`] type. pub struct CustomizeResponder { @@ -144,19 +138,27 @@ impl CustomizeResponder { } } - /// Append a cookie to the final response. + /// Appends a `cookie` to the final response. /// /// # Errors - /// Returns an error if the cookie results in a malformed `Set-Cookie` header. + /// + /// Final response will be an error if `cookie` cannot be converted into a valid header value. #[cfg(feature = "cookies")] - pub fn add_cookie(mut self, cookie: &Cookie<'_>) -> Result { + pub fn add_cookie(mut self, cookie: &crate::cookie::Cookie<'_>) -> Self { + use actix_http::header::{TryIntoHeaderValue as _, SET_COOKIE}; + if let Some(inner) = self.inner() { - HeaderValue::from_str(&cookie.to_string()) - .map(|cookie| inner.append_headers.append(header::SET_COOKIE, cookie)) - .map_err(Into::::into)? + match cookie.to_string().try_into_value() { + Ok(val) => { + inner.append_headers.append(SET_COOKIE, val); + } + Err(err) => { + self.error = Some(err.into()); + } + } } - Ok(self) + self } } @@ -197,10 +199,7 @@ mod tests { use super::*; use crate::{ cookie::Cookie, - http::{ - header::{HeaderValue, CONTENT_TYPE}, - StatusCode, - }, + http::header::{HeaderValue, CONTENT_TYPE}, test::TestRequest, }; @@ -239,13 +238,12 @@ mod tests { .to_string() .customize() .add_cookie(&Cookie::new("name", "value")) - .unwrap() .respond_to(&req); - assert_eq!(res.status(), StatusCode::OK); + assert!(res.status().is_success()); assert_eq!( res.cookies().collect::>>(), - vec![Cookie::new("name", "value")] + vec![Cookie::new("name", "value")], ); assert_eq!( to_bytes(res.into_body()).await.unwrap(),