mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-27 09:42:57 +01:00
feat: expose Identity middleware (#3390)
This commit is contained in:
parent
3db7891303
commit
7c4c26d2df
2
.github/workflows/lint.yml
vendored
2
.github/workflows/lint.yml
vendored
@ -89,5 +89,5 @@ jobs:
|
||||
- name: Generate API diff
|
||||
run: |
|
||||
for f in $(find -mindepth 2 -maxdepth 2 -name Cargo.toml); do
|
||||
cargo public-api --manifest-path "$f" diff ${{ github.event.pull_request.base.sha }}..${{ github.sha }}
|
||||
cargo public-api --manifest-path "$f" --simplified diff ${{ github.event.pull_request.base.sha }}..${{ github.sha }}
|
||||
done
|
||||
|
@ -4,6 +4,7 @@
|
||||
|
||||
### Added
|
||||
|
||||
- Add `middleware::Identity` type.
|
||||
- Add `CustomizeResponder::add_cookie()` method.
|
||||
- Add `guard::GuardContext::app_data()` method.
|
||||
- Implement `From<Box<dyn ResponseError>>` for `Error`.
|
||||
|
@ -38,15 +38,6 @@ pub struct Compat<T> {
|
||||
transform: T,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl Compat<super::Noop> {
|
||||
pub(crate) fn noop() -> Self {
|
||||
Self {
|
||||
transform: super::Noop,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Compat<T> {
|
||||
/// Wrap a middleware to give it broader compatibility.
|
||||
pub fn new(middleware: T) -> Self {
|
||||
@ -152,7 +143,7 @@ mod tests {
|
||||
use crate::{
|
||||
dev::ServiceRequest,
|
||||
http::StatusCode,
|
||||
middleware::{self, Condition, Logger},
|
||||
middleware::{self, Condition, Identity, Logger},
|
||||
test::{self, call_service, init_service, TestRequest},
|
||||
web, App, HttpResponse,
|
||||
};
|
||||
@ -225,7 +216,7 @@ mod tests {
|
||||
async fn compat_noop_is_noop() {
|
||||
let srv = test::ok_service();
|
||||
|
||||
let mw = Compat::noop()
|
||||
let mw = Compat::new(Identity)
|
||||
.new_transform(srv.into_service())
|
||||
.await
|
||||
.unwrap();
|
||||
|
@ -141,7 +141,7 @@ mod tests {
|
||||
header::{HeaderValue, CONTENT_TYPE},
|
||||
StatusCode,
|
||||
},
|
||||
middleware::{self, ErrorHandlerResponse, ErrorHandlers},
|
||||
middleware::{self, ErrorHandlerResponse, ErrorHandlers, Identity},
|
||||
test::{self, TestRequest},
|
||||
web::Bytes,
|
||||
HttpResponse,
|
||||
@ -158,7 +158,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn compat_with_builtin_middleware() {
|
||||
let _ = Condition::new(true, middleware::Compat::noop());
|
||||
let _ = Condition::new(true, middleware::Compat::new(Identity));
|
||||
let _ = Condition::new(true, middleware::Logger::default());
|
||||
let _ = Condition::new(true, middleware::Compress::default());
|
||||
let _ = Condition::new(true, middleware::NormalizePath::trim());
|
||||
|
@ -2,35 +2,39 @@
|
||||
|
||||
use actix_utils::future::{ready, Ready};
|
||||
|
||||
use crate::dev::{Service, Transform};
|
||||
use crate::dev::{forward_ready, Service, Transform};
|
||||
|
||||
/// A no-op middleware that passes through request and response untouched.
|
||||
pub(crate) struct Noop;
|
||||
#[derive(Debug, Clone, Default)]
|
||||
#[non_exhaustive]
|
||||
pub struct Identity;
|
||||
|
||||
impl<S: Service<Req>, Req> Transform<S, Req> for Noop {
|
||||
impl<S: Service<Req>, Req> Transform<S, Req> for Identity {
|
||||
type Response = S::Response;
|
||||
type Error = S::Error;
|
||||
type Transform = NoopService<S>;
|
||||
type Transform = IdentityMiddleware<S>;
|
||||
type InitError = ();
|
||||
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
||||
|
||||
#[inline]
|
||||
fn new_transform(&self, service: S) -> Self::Future {
|
||||
ready(Ok(NoopService { service }))
|
||||
ready(Ok(IdentityMiddleware { service }))
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub(crate) struct NoopService<S> {
|
||||
pub struct IdentityMiddleware<S> {
|
||||
service: S,
|
||||
}
|
||||
|
||||
impl<S: Service<Req>, Req> Service<Req> for NoopService<S> {
|
||||
impl<S: Service<Req>, Req> Service<Req> for IdentityMiddleware<S> {
|
||||
type Response = S::Response;
|
||||
type Error = S::Error;
|
||||
type Future = S::Future;
|
||||
|
||||
crate::dev::forward_ready!(service);
|
||||
forward_ready!(service);
|
||||
|
||||
#[inline]
|
||||
fn call(&self, req: Req) -> Self::Future {
|
||||
self.service.call(req)
|
||||
}
|
@ -218,31 +218,27 @@
|
||||
//! [lab_from_fn]: https://docs.rs/actix-web-lab/latest/actix_web_lab/middleware/fn.from_fn.html
|
||||
|
||||
mod compat;
|
||||
#[cfg(feature = "__compress")]
|
||||
mod compress;
|
||||
mod condition;
|
||||
mod default_headers;
|
||||
mod err_handlers;
|
||||
mod identity;
|
||||
mod logger;
|
||||
#[cfg(test)]
|
||||
mod noop;
|
||||
mod normalize;
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) use self::noop::Noop;
|
||||
#[cfg(feature = "__compress")]
|
||||
pub use self::compress::Compress;
|
||||
pub use self::{
|
||||
compat::Compat,
|
||||
condition::Condition,
|
||||
default_headers::DefaultHeaders,
|
||||
err_handlers::{ErrorHandlerResponse, ErrorHandlers},
|
||||
identity::Identity,
|
||||
logger::Logger,
|
||||
normalize::{NormalizePath, TrailingSlash},
|
||||
};
|
||||
|
||||
#[cfg(feature = "__compress")]
|
||||
mod compress;
|
||||
|
||||
#[cfg(feature = "__compress")]
|
||||
pub use self::compress::Compress;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
Loading…
Reference in New Issue
Block a user