mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-23 16:21:06 +01:00
generalize impl Responder for HttpResponse (#2567)
Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
parent
49cfabeaf5
commit
2462b6dd5d
@ -1,6 +1,10 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## Unreleased - 2021-xx-xx
|
## Unreleased - 2021-xx-xx
|
||||||
|
### Changed
|
||||||
|
- `HttpResponse` can now be used as a `Responder` with any body type.
|
||||||
|
|
||||||
|
[#2501]: https://github.com/actix/actix-web/pull/2501
|
||||||
|
|
||||||
|
|
||||||
## 4.0.0-beta.19 - 2022-01-04
|
## 4.0.0-beta.19 - 2022-01-04
|
||||||
|
@ -118,6 +118,7 @@ futures-util = { version = "0.3.7", default-features = false, features = ["std"]
|
|||||||
rand = "0.8"
|
rand = "0.8"
|
||||||
rcgen = "0.8"
|
rcgen = "0.8"
|
||||||
rustls-pemfile = "0.2"
|
rustls-pemfile = "0.2"
|
||||||
|
static_assertions = "1"
|
||||||
tls-openssl = { package = "openssl", version = "0.10.9" }
|
tls-openssl = { package = "openssl", version = "0.10.9" }
|
||||||
tls-rustls = { package = "rustls", version = "0.20.0" }
|
tls-rustls = { package = "rustls", version = "0.20.0" }
|
||||||
zstd = "0.9"
|
zstd = "0.9"
|
||||||
|
@ -23,7 +23,7 @@ use cookie::{Cookie, CookieJar};
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
error::{Error, JsonPayloadError},
|
error::{Error, JsonPayloadError},
|
||||||
BoxError, HttpResponse,
|
BoxError, HttpRequest, HttpResponse, Responder,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// An HTTP response builder.
|
/// An HTTP response builder.
|
||||||
@ -424,6 +424,15 @@ impl Future for HttpResponseBuilder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Responder for HttpResponseBuilder {
|
||||||
|
type Body = BoxBody;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn respond_to(mut self, _: &HttpRequest) -> HttpResponse<Self::Body> {
|
||||||
|
self.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use actix_http::body;
|
use actix_http::body;
|
||||||
|
@ -1,12 +1,9 @@
|
|||||||
use actix_http::{
|
use actix_http::{
|
||||||
body::{EitherBody, MessageBody},
|
body::EitherBody, error::HttpError, header::HeaderMap, header::TryIntoHeaderPair,
|
||||||
error::HttpError,
|
|
||||||
header::HeaderMap,
|
|
||||||
header::TryIntoHeaderPair,
|
|
||||||
StatusCode,
|
StatusCode,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{BoxError, HttpRequest, HttpResponse, Responder};
|
use crate::{HttpRequest, HttpResponse, Responder};
|
||||||
|
|
||||||
/// Allows overriding status code and headers for a [`Responder`].
|
/// Allows overriding status code and headers for a [`Responder`].
|
||||||
///
|
///
|
||||||
@ -143,7 +140,6 @@ impl<R: Responder> CustomizeResponder<R> {
|
|||||||
impl<T> Responder for CustomizeResponder<T>
|
impl<T> Responder for CustomizeResponder<T>
|
||||||
where
|
where
|
||||||
T: Responder,
|
T: Responder,
|
||||||
<T::Body as MessageBody>::Error: Into<BoxError>,
|
|
||||||
{
|
{
|
||||||
type Body = EitherBody<T::Body>;
|
type Body = EitherBody<T::Body>;
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ use actix_http::{
|
|||||||
};
|
};
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
|
|
||||||
use crate::{BoxError, Error, HttpRequest, HttpResponse, HttpResponseBuilder};
|
use crate::{Error, HttpRequest, HttpResponse};
|
||||||
|
|
||||||
use super::CustomizeResponder;
|
use super::CustomizeResponder;
|
||||||
|
|
||||||
@ -57,15 +57,6 @@ pub trait Responder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Responder for HttpResponse {
|
|
||||||
type Body = BoxBody;
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn respond_to(self, _: &HttpRequest) -> HttpResponse<Self::Body> {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Responder for actix_http::Response<BoxBody> {
|
impl Responder for actix_http::Response<BoxBody> {
|
||||||
type Body = BoxBody;
|
type Body = BoxBody;
|
||||||
|
|
||||||
@ -75,15 +66,6 @@ impl Responder for actix_http::Response<BoxBody> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Responder for HttpResponseBuilder {
|
|
||||||
type Body = BoxBody;
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn respond_to(mut self, _: &HttpRequest) -> HttpResponse<Self::Body> {
|
|
||||||
self.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Responder for actix_http::ResponseBuilder {
|
impl Responder for actix_http::ResponseBuilder {
|
||||||
type Body = BoxBody;
|
type Body = BoxBody;
|
||||||
|
|
||||||
@ -96,7 +78,6 @@ impl Responder for actix_http::ResponseBuilder {
|
|||||||
impl<T> Responder for Option<T>
|
impl<T> Responder for Option<T>
|
||||||
where
|
where
|
||||||
T: Responder,
|
T: Responder,
|
||||||
<T::Body as MessageBody>::Error: Into<BoxError>,
|
|
||||||
{
|
{
|
||||||
type Body = EitherBody<T::Body>;
|
type Body = EitherBody<T::Body>;
|
||||||
|
|
||||||
@ -111,7 +92,6 @@ where
|
|||||||
impl<T, E> Responder for Result<T, E>
|
impl<T, E> Responder for Result<T, E>
|
||||||
where
|
where
|
||||||
T: Responder,
|
T: Responder,
|
||||||
<T::Body as MessageBody>::Error: Into<BoxError>,
|
|
||||||
E: Into<Error>,
|
E: Into<Error>,
|
||||||
{
|
{
|
||||||
type Body = EitherBody<T::Body>;
|
type Body = EitherBody<T::Body>;
|
||||||
|
@ -22,7 +22,7 @@ use {
|
|||||||
cookie::Cookie,
|
cookie::Cookie,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{error::Error, HttpResponseBuilder};
|
use crate::{error::Error, HttpRequest, HttpResponseBuilder, Responder};
|
||||||
|
|
||||||
/// An outgoing response.
|
/// An outgoing response.
|
||||||
pub struct HttpResponse<B = BoxBody> {
|
pub struct HttpResponse<B = BoxBody> {
|
||||||
@ -311,6 +311,18 @@ impl Future for HttpResponse<BoxBody> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<B> Responder for HttpResponse<B>
|
||||||
|
where
|
||||||
|
B: MessageBody + 'static,
|
||||||
|
{
|
||||||
|
type Body = B;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn respond_to(self, _: &HttpRequest) -> HttpResponse<Self::Body> {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(feature = "cookies")]
|
#[cfg(feature = "cookies")]
|
||||||
pub struct CookieIter<'a> {
|
pub struct CookieIter<'a> {
|
||||||
iter: std::slice::Iter<'a, HeaderValue>,
|
iter: std::slice::Iter<'a, HeaderValue>,
|
||||||
@ -333,9 +345,16 @@ impl<'a> Iterator for CookieIter<'a> {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use static_assertions::assert_impl_all;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::http::header::{HeaderValue, COOKIE};
|
use crate::http::header::{HeaderValue, COOKIE};
|
||||||
|
|
||||||
|
assert_impl_all!(HttpResponse: Responder);
|
||||||
|
assert_impl_all!(HttpResponse<String>: Responder);
|
||||||
|
assert_impl_all!(HttpResponse<&'static str>: Responder);
|
||||||
|
assert_impl_all!(HttpResponse<crate::body::None>: Responder);
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_debug() {
|
fn test_debug() {
|
||||||
let resp = HttpResponse::Ok()
|
let resp = HttpResponse::Ok()
|
||||||
|
Loading…
Reference in New Issue
Block a user