bring back response builder header methods deprecated

This commit is contained in:
Rob Ede 2021-01-14 02:09:44 +00:00
parent e63f733cd6
commit 90e3627736
No known key found for this signature in database
GPG Key ID: C2A3B36E841A91E6
2 changed files with 49 additions and 5 deletions

View File

@ -2,7 +2,7 @@
## Unreleased - 2021-xx-xx ## Unreleased - 2021-xx-xx
### Added ### 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::insert_header` method which allows using typed headers. [#1869]
* `ResponseBuilder::append_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] * `TestRequest::insert_header` method which allows using typed headers. [#1869]

View File

@ -2,6 +2,7 @@
use std::{ use std::{
cell::{Ref, RefMut}, cell::{Ref, RefMut},
convert::TryInto,
fmt, fmt,
future::Future, future::Future,
pin::Pin, pin::Pin,
@ -18,7 +19,7 @@ use crate::cookie::{Cookie, CookieJar};
use crate::error::Error; use crate::error::Error;
use crate::extensions::Extensions; use crate::extensions::Extensions;
use crate::header::{IntoHeaderPair, IntoHeaderValue}; 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::http::{Error as HttpError, HeaderMap, StatusCode};
use crate::message::{BoxedResponseHead, ConnectionType, ResponseHead}; use crate::message::{BoxedResponseHead, ConnectionType, ResponseHead};
@ -348,11 +349,11 @@ impl ResponseBuilder {
/// ///
/// ```rust /// ```rust
/// # use actix_http::Response; /// # use actix_http::Response;
///
/// use actix_http::http::header::ContentType; /// use actix_http::http::header::ContentType;
///
/// Response::Ok() /// Response::Ok()
/// .insert_header(("X-TEST", "value"))
/// .insert_header(ContentType(mime::APPLICATION_JSON)) /// .insert_header(ContentType(mime::APPLICATION_JSON))
/// .insert_header(("X-TEST", "value"))
/// .finish(); /// .finish();
/// ``` /// ```
pub fn insert_header<H>(&mut self, header: H) -> &mut Self pub fn insert_header<H>(&mut self, header: H) -> &mut Self
@ -376,8 +377,9 @@ impl ResponseBuilder {
/// use actix_http::http::header::ContentType; /// use actix_http::http::header::ContentType;
/// ///
/// Response::Ok() /// Response::Ok()
/// .append_header(("X-TEST", "value"))
/// .append_header(ContentType(mime::APPLICATION_JSON)) /// .append_header(ContentType(mime::APPLICATION_JSON))
/// .append_header(("X-TEST", "value1"))
/// .append_header(("X-TEST", "value2"))
/// .finish(); /// .finish();
/// ``` /// ```
pub fn append_header<H>(&mut self, header: H) -> &mut Self pub fn append_header<H>(&mut self, header: H) -> &mut Self
@ -394,6 +396,48 @@ impl ResponseBuilder {
self self
} }
/// Replaced with [`Self::insert_header()`].
#[deprecated = "Replaced with `insert_header((key, value))`."]
pub fn set_header<K, V>(&mut self, key: K, value: V) -> &mut Self
where
K: TryInto<HeaderName>,
K::Error: Into<HttpError>,
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<K, V>(&mut self, key: K, value: V) -> &mut Self
where
K: TryInto<HeaderName>,
K::Error: Into<HttpError>,
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. /// Set the custom reason for the response.
#[inline] #[inline]
pub fn reason(&mut self, reason: &'static str) -> &mut Self { pub fn reason(&mut self, reason: &'static str) -> &mut Self {