mirror of
https://github.com/tokio-rs/axum.git
synced 2026-09-03 00:00:07 +02:00
Move Headers into axum
This commit is contained in:
@@ -19,11 +19,6 @@ use http_body::{
|
|||||||
};
|
};
|
||||||
use std::{borrow::Cow, convert::Infallible, iter};
|
use std::{borrow::Cow, convert::Infallible, iter};
|
||||||
|
|
||||||
mod headers;
|
|
||||||
|
|
||||||
#[doc(inline)]
|
|
||||||
pub use self::headers::Headers;
|
|
||||||
|
|
||||||
/// Type alias for [`http::Response`] whose body type defaults to [`BoxBody`], the most common body
|
/// Type alias for [`http::Response`] whose body type defaults to [`BoxBody`], the most common body
|
||||||
/// type used with axum.
|
/// type used with axum.
|
||||||
pub type Response<T = BoxBody> = http::Response<T>;
|
pub type Response<T = BoxBody> = http::Response<T>;
|
||||||
@@ -356,17 +351,10 @@ impl IntoResponse for StatusCode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<H> IntoResponse for H
|
impl IntoResponse for HeaderMap {
|
||||||
where
|
|
||||||
H: IntoResponseHeaders,
|
|
||||||
{
|
|
||||||
fn into_response(self) -> Response {
|
fn into_response(self) -> Response {
|
||||||
let mut res = Response::new(boxed(Empty::new()));
|
let mut res = Response::new(boxed(Empty::new()));
|
||||||
|
*res.headers_mut() = self;
|
||||||
if let Err(e) = try_extend_headers(res.headers_mut(), self.into_headers()) {
|
|
||||||
return e;
|
|
||||||
}
|
|
||||||
|
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
error[E0277]: the trait bound `bool: axum_core::response::IntoResponseHeaders` is not satisfied
|
error[E0277]: the trait bound `bool: IntoResponse` is not satisfied
|
||||||
--> tests/fail/wrong_return_type.rs:4:23
|
--> tests/fail/wrong_return_type.rs:4:23
|
||||||
|
|
|
|
||||||
4 | async fn handler() -> bool {
|
4 | async fn handler() -> bool {
|
||||||
| ^^^^ the trait `axum_core::response::IntoResponseHeaders` is not implemented for `bool`
|
| ^^^^ the trait `IntoResponse` is not implemented for `bool`
|
||||||
|
|
|
|
||||||
= note: required because of the requirements on the impl of `IntoResponse` for `bool`
|
|
||||||
note: required by a bound in `__axum_debug_check_handler_into_response::{closure#0}::check`
|
note: required by a bound in `__axum_debug_check_handler_into_response::{closure#0}::check`
|
||||||
--> tests/fail/wrong_return_type.rs:4:23
|
--> tests/fail/wrong_return_type.rs:4:23
|
||||||
|
|
|
|
||||||
|
|||||||
@@ -68,6 +68,19 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<H, K, V> IntoResponse for Headers<H>
|
||||||
|
where
|
||||||
|
H: IntoIterator<Item = (K, V)>,
|
||||||
|
K: TryInto<HeaderName>,
|
||||||
|
K::Error: fmt::Display,
|
||||||
|
V: TryInto<HeaderValue>,
|
||||||
|
V::Error: fmt::Display,
|
||||||
|
{
|
||||||
|
fn into_response(self) -> Response {
|
||||||
|
(self, ()).into_response()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct IntoIter<H> {
|
pub struct IntoIter<H> {
|
||||||
@@ -124,7 +137,7 @@ mod tests {
|
|||||||
let res = (Headers(vec![("user-agent", "axum")]), "foo").into_response();
|
let res = (Headers(vec![("user-agent", "axum")]), "foo").into_response();
|
||||||
|
|
||||||
assert_eq!(res.headers()["user-agent"], "axum");
|
assert_eq!(res.headers()["user-agent"], "axum");
|
||||||
let body = crate::body::to_bytes(res.into_body())
|
let body = hyper::body::to_bytes(res.into_body())
|
||||||
.now_or_never()
|
.now_or_never()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
@@ -142,7 +155,7 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(res.headers()["user-agent"], "axum");
|
assert_eq!(res.headers()["user-agent"], "axum");
|
||||||
assert_eq!(res.status(), StatusCode::NOT_FOUND);
|
assert_eq!(res.status(), StatusCode::NOT_FOUND);
|
||||||
let body = crate::body::to_bytes(res.into_body())
|
let body = hyper::body::to_bytes(res.into_body())
|
||||||
.now_or_never()
|
.now_or_never()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
@@ -4,6 +4,7 @@ use crate::body::{Bytes, Full};
|
|||||||
use axum_core::body::boxed;
|
use axum_core::body::boxed;
|
||||||
use http::{header, HeaderValue};
|
use http::{header, HeaderValue};
|
||||||
|
|
||||||
|
mod headers;
|
||||||
mod redirect;
|
mod redirect;
|
||||||
|
|
||||||
pub mod sse;
|
pub mod sse;
|
||||||
@@ -13,10 +14,10 @@ pub mod sse;
|
|||||||
pub use crate::Json;
|
pub use crate::Json;
|
||||||
|
|
||||||
#[doc(inline)]
|
#[doc(inline)]
|
||||||
pub use axum_core::response::{Headers, IntoResponse, Response};
|
pub use axum_core::response::{IntoResponse, IntoResponseHeaders, Response};
|
||||||
|
|
||||||
#[doc(inline)]
|
#[doc(inline)]
|
||||||
pub use self::{redirect::Redirect, sse::Sse};
|
pub use self::{headers::Headers, redirect::Redirect, sse::Sse};
|
||||||
|
|
||||||
/// An HTML response.
|
/// An HTML response.
|
||||||
///
|
///
|
||||||
|
|||||||
Reference in New Issue
Block a user