From 90e36277361b7278dc1e9998ce1ef66b32cf5670 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Thu, 14 Jan 2021 02:09:44 +0000 Subject: [PATCH] bring back response builder header methods deprecated --- actix-http/CHANGES.md | 2 +- actix-http/src/response.rs | 52 +++++++++++++++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index cdfe277fb..ed3764587 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -2,7 +2,7 @@ ## Unreleased - 2021-xx-xx ### Added -* `IntoHeaderPair` trait that allows using types and non-typed headers in the same methods. [#1869] +* `IntoHeaderPair` trait that allows using typed and untyped headers in the same methods. [#1869] * `ResponseBuilder::insert_header` method which allows using typed headers. [#1869] * `ResponseBuilder::append_header` method which allows using typed headers. [#1869] * `TestRequest::insert_header` method which allows using typed headers. [#1869] diff --git a/actix-http/src/response.rs b/actix-http/src/response.rs index bda547b1b..880f93c6a 100644 --- a/actix-http/src/response.rs +++ b/actix-http/src/response.rs @@ -2,6 +2,7 @@ use std::{ cell::{Ref, RefMut}, + convert::TryInto, fmt, future::Future, pin::Pin, @@ -18,7 +19,7 @@ use crate::cookie::{Cookie, CookieJar}; use crate::error::Error; use crate::extensions::Extensions; use crate::header::{IntoHeaderPair, IntoHeaderValue}; -use crate::http::header::{self, HeaderValue}; +use crate::http::header::{self, HeaderName, HeaderValue}; use crate::http::{Error as HttpError, HeaderMap, StatusCode}; use crate::message::{BoxedResponseHead, ConnectionType, ResponseHead}; @@ -348,11 +349,11 @@ impl ResponseBuilder { /// /// ```rust /// # use actix_http::Response; - /// /// use actix_http::http::header::ContentType; + /// /// Response::Ok() - /// .insert_header(("X-TEST", "value")) /// .insert_header(ContentType(mime::APPLICATION_JSON)) + /// .insert_header(("X-TEST", "value")) /// .finish(); /// ``` pub fn insert_header(&mut self, header: H) -> &mut Self @@ -376,8 +377,9 @@ impl ResponseBuilder { /// use actix_http::http::header::ContentType; /// /// Response::Ok() - /// .append_header(("X-TEST", "value")) /// .append_header(ContentType(mime::APPLICATION_JSON)) + /// .append_header(("X-TEST", "value1")) + /// .append_header(("X-TEST", "value2")) /// .finish(); /// ``` pub fn append_header(&mut self, header: H) -> &mut Self @@ -394,6 +396,48 @@ impl ResponseBuilder { self } + /// Replaced with [`Self::insert_header()`]. + #[deprecated = "Replaced with `insert_header((key, value))`."] + pub fn set_header(&mut self, key: K, value: V) -> &mut Self + where + K: TryInto, + K::Error: Into, + V: IntoHeaderValue, + { + if self.err.is_some() { + return self; + } + + match (key.try_into(), value.try_into_value()) { + (Ok(name), Ok(value)) => return self.insert_header((name, value)), + (Err(err), _) => self.err = Some(err.into()), + (_, Err(err)) => self.err = Some(err.into()), + } + + self + } + + /// Replaced with [`Self::append_header()`]. + #[deprecated = "Replaced with `append_header((key, value))`."] + pub fn header(&mut self, key: K, value: V) -> &mut Self + where + K: TryInto, + K::Error: Into, + V: IntoHeaderValue, + { + if self.err.is_some() { + return self; + } + + match (key.try_into(), value.try_into_value()) { + (Ok(name), Ok(value)) => return self.append_header((name, value)), + (Err(err), _) => self.err = Some(err.into()), + (_, Err(err)) => self.err = Some(err.into()), + } + + self + } + /// Set the custom reason for the response. #[inline] pub fn reason(&mut self, reason: &'static str) -> &mut Self {