mirror of
https://github.com/tokio-rs/axum.git
synced 2025-01-01 00:50:32 +01:00
Export AddExtension
from middleware
(#811)
* Export `AddExtension` from `middleware` * Move `AddExtension` into `extension` module
This commit is contained in:
parent
bad3abb960
commit
2428d99081
6 changed files with 47 additions and 45 deletions
|
@ -58,6 +58,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
- **breaking:** `Redirect::found` has been removed ([#800])
|
||||
- **breaking:** `AddExtensionLayer` has been removed. Use `Extension` instead. It now implements
|
||||
`tower::Layer` ([#807])
|
||||
- **breaking:** `AddExtension` has been moved from the root module to `middleware`
|
||||
- **fixed:** Set `Allow` header when responding with `405 Method Not Allowed` ([#733])
|
||||
- **fixed:** Correctly set the `Content-Length` header for response to `HEAD`
|
||||
requests ([#734])
|
||||
|
|
|
@ -1,37 +0,0 @@
|
|||
// this is vendored from tower-http to reduce public dependencies
|
||||
|
||||
use http::Request;
|
||||
use std::task::{Context, Poll};
|
||||
use tower_service::Service;
|
||||
|
||||
/// Middleware for adding some shareable value to [request extensions].
|
||||
///
|
||||
/// See [Sharing state with handlers](index.html#sharing-state-with-handlers)
|
||||
/// for more details.
|
||||
///
|
||||
/// [request extensions]: https://docs.rs/http/latest/http/struct.Extensions.html
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct AddExtension<S, T> {
|
||||
pub(crate) inner: S,
|
||||
pub(crate) value: T,
|
||||
}
|
||||
|
||||
impl<ResBody, S, T> Service<Request<ResBody>> for AddExtension<S, T>
|
||||
where
|
||||
S: Service<Request<ResBody>>,
|
||||
T: Clone + Send + Sync + 'static,
|
||||
{
|
||||
type Response = S::Response;
|
||||
type Error = S::Error;
|
||||
type Future = S::Future;
|
||||
|
||||
#[inline]
|
||||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
self.inner.poll_ready(cx)
|
||||
}
|
||||
|
||||
fn call(&mut self, mut req: Request<ResBody>) -> Self::Future {
|
||||
req.extensions_mut().insert(self.value.clone());
|
||||
self.inner.call(req)
|
||||
}
|
||||
}
|
|
@ -1,8 +1,15 @@
|
|||
use crate::extract::{rejection::*, FromRequest, RequestParts};
|
||||
use crate::response::IntoResponseParts;
|
||||
use crate::{
|
||||
extract::{rejection::*, FromRequest, RequestParts},
|
||||
response::IntoResponseParts,
|
||||
};
|
||||
use async_trait::async_trait;
|
||||
use axum_core::response::{IntoResponse, Response, ResponseParts};
|
||||
use std::ops::Deref;
|
||||
use http::Request;
|
||||
use std::{
|
||||
ops::Deref,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
use tower_service::Service;
|
||||
|
||||
/// Extractor and response for extensions.
|
||||
///
|
||||
|
@ -120,12 +127,44 @@ impl<S, T> tower_layer::Layer<S> for Extension<T>
|
|||
where
|
||||
T: Clone + Send + Sync + 'static,
|
||||
{
|
||||
type Service = crate::AddExtension<S, T>;
|
||||
type Service = AddExtension<S, T>;
|
||||
|
||||
fn layer(&self, inner: S) -> Self::Service {
|
||||
crate::AddExtension {
|
||||
AddExtension {
|
||||
inner,
|
||||
value: self.0.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Middleware for adding some shareable value to [request extensions].
|
||||
///
|
||||
/// See [Sharing state with handlers](index.html#sharing-state-with-handlers)
|
||||
/// for more details.
|
||||
///
|
||||
/// [request extensions]: https://docs.rs/http/latest/http/struct.Extensions.html
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct AddExtension<S, T> {
|
||||
pub(crate) inner: S,
|
||||
pub(crate) value: T,
|
||||
}
|
||||
|
||||
impl<ResBody, S, T> Service<Request<ResBody>> for AddExtension<S, T>
|
||||
where
|
||||
S: Service<Request<ResBody>>,
|
||||
T: Clone + Send + Sync + 'static,
|
||||
{
|
||||
type Response = S::Response;
|
||||
type Error = S::Error;
|
||||
type Future = S::Future;
|
||||
|
||||
#[inline]
|
||||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
self.inner.poll_ready(cx)
|
||||
}
|
||||
|
||||
fn call(&mut self, mut req: Request<ResBody>) -> Self::Future {
|
||||
req.extensions_mut().insert(self.value.clone());
|
||||
self.inner.call(req)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
//! [`Router::into_make_service_with_connect_info`]: crate::routing::Router::into_make_service_with_connect_info
|
||||
|
||||
use super::{Extension, FromRequest, RequestParts};
|
||||
use crate::AddExtension;
|
||||
use crate::middleware::AddExtension;
|
||||
use async_trait::async_trait;
|
||||
use hyper::server::conn::AddrStream;
|
||||
use std::{
|
||||
|
|
|
@ -392,7 +392,6 @@
|
|||
#[macro_use]
|
||||
pub(crate) mod macros;
|
||||
|
||||
mod add_extension;
|
||||
mod extension;
|
||||
#[cfg(feature = "json")]
|
||||
mod json;
|
||||
|
@ -409,7 +408,6 @@ pub mod routing;
|
|||
#[cfg(test)]
|
||||
mod test_helpers;
|
||||
|
||||
pub use add_extension::AddExtension;
|
||||
#[doc(no_inline)]
|
||||
pub use async_trait::async_trait;
|
||||
#[cfg(feature = "headers")]
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
mod from_fn;
|
||||
|
||||
pub use self::from_fn::{from_fn, FromFn, FromFnLayer, Next};
|
||||
pub use crate::extension::AddExtension;
|
||||
|
||||
pub mod future {
|
||||
//! Future types.
|
||||
|
|
Loading…
Reference in a new issue