1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 00:21:08 +01:00

feat: expose Identity middleware (#3390)

This commit is contained in:
Rob Ede 2024-06-08 05:26:26 +01:00 committed by GitHub
parent 3db7891303
commit 7c4c26d2df
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 24 additions and 32 deletions

View File

@ -89,5 +89,5 @@ jobs:
- name: Generate API diff - name: Generate API diff
run: | run: |
for f in $(find -mindepth 2 -maxdepth 2 -name Cargo.toml); do 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 done

View File

@ -4,6 +4,7 @@
### Added ### Added
- Add `middleware::Identity` type.
- Add `CustomizeResponder::add_cookie()` method. - Add `CustomizeResponder::add_cookie()` method.
- Add `guard::GuardContext::app_data()` method. - Add `guard::GuardContext::app_data()` method.
- Implement `From<Box<dyn ResponseError>>` for `Error`. - Implement `From<Box<dyn ResponseError>>` for `Error`.

View File

@ -38,15 +38,6 @@ pub struct Compat<T> {
transform: T, transform: T,
} }
#[cfg(test)]
impl Compat<super::Noop> {
pub(crate) fn noop() -> Self {
Self {
transform: super::Noop,
}
}
}
impl<T> Compat<T> { impl<T> Compat<T> {
/// Wrap a middleware to give it broader compatibility. /// Wrap a middleware to give it broader compatibility.
pub fn new(middleware: T) -> Self { pub fn new(middleware: T) -> Self {
@ -152,7 +143,7 @@ mod tests {
use crate::{ use crate::{
dev::ServiceRequest, dev::ServiceRequest,
http::StatusCode, http::StatusCode,
middleware::{self, Condition, Logger}, middleware::{self, Condition, Identity, Logger},
test::{self, call_service, init_service, TestRequest}, test::{self, call_service, init_service, TestRequest},
web, App, HttpResponse, web, App, HttpResponse,
}; };
@ -225,7 +216,7 @@ mod tests {
async fn compat_noop_is_noop() { async fn compat_noop_is_noop() {
let srv = test::ok_service(); let srv = test::ok_service();
let mw = Compat::noop() let mw = Compat::new(Identity)
.new_transform(srv.into_service()) .new_transform(srv.into_service())
.await .await
.unwrap(); .unwrap();

View File

@ -141,7 +141,7 @@ mod tests {
header::{HeaderValue, CONTENT_TYPE}, header::{HeaderValue, CONTENT_TYPE},
StatusCode, StatusCode,
}, },
middleware::{self, ErrorHandlerResponse, ErrorHandlers}, middleware::{self, ErrorHandlerResponse, ErrorHandlers, Identity},
test::{self, TestRequest}, test::{self, TestRequest},
web::Bytes, web::Bytes,
HttpResponse, HttpResponse,
@ -158,7 +158,7 @@ mod tests {
#[test] #[test]
fn compat_with_builtin_middleware() { 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::Logger::default());
let _ = Condition::new(true, middleware::Compress::default()); let _ = Condition::new(true, middleware::Compress::default());
let _ = Condition::new(true, middleware::NormalizePath::trim()); let _ = Condition::new(true, middleware::NormalizePath::trim());

View File

@ -2,35 +2,39 @@
use actix_utils::future::{ready, Ready}; 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. /// 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 Response = S::Response;
type Error = S::Error; type Error = S::Error;
type Transform = NoopService<S>; type Transform = IdentityMiddleware<S>;
type InitError = (); type InitError = ();
type Future = Ready<Result<Self::Transform, Self::InitError>>; type Future = Ready<Result<Self::Transform, Self::InitError>>;
#[inline]
fn new_transform(&self, service: S) -> Self::Future { fn new_transform(&self, service: S) -> Self::Future {
ready(Ok(NoopService { service })) ready(Ok(IdentityMiddleware { service }))
} }
} }
#[doc(hidden)] #[doc(hidden)]
pub(crate) struct NoopService<S> { pub struct IdentityMiddleware<S> {
service: 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 Response = S::Response;
type Error = S::Error; type Error = S::Error;
type Future = S::Future; type Future = S::Future;
crate::dev::forward_ready!(service); forward_ready!(service);
#[inline]
fn call(&self, req: Req) -> Self::Future { fn call(&self, req: Req) -> Self::Future {
self.service.call(req) self.service.call(req)
} }

View File

@ -218,31 +218,27 @@
//! [lab_from_fn]: https://docs.rs/actix-web-lab/latest/actix_web_lab/middleware/fn.from_fn.html //! [lab_from_fn]: https://docs.rs/actix-web-lab/latest/actix_web_lab/middleware/fn.from_fn.html
mod compat; mod compat;
#[cfg(feature = "__compress")]
mod compress;
mod condition; mod condition;
mod default_headers; mod default_headers;
mod err_handlers; mod err_handlers;
mod identity;
mod logger; mod logger;
#[cfg(test)]
mod noop;
mod normalize; mod normalize;
#[cfg(test)] #[cfg(feature = "__compress")]
pub(crate) use self::noop::Noop; pub use self::compress::Compress;
pub use self::{ pub use self::{
compat::Compat, compat::Compat,
condition::Condition, condition::Condition,
default_headers::DefaultHeaders, default_headers::DefaultHeaders,
err_handlers::{ErrorHandlerResponse, ErrorHandlers}, err_handlers::{ErrorHandlerResponse, ErrorHandlers},
identity::Identity,
logger::Logger, logger::Logger,
normalize::{NormalizePath, TrailingSlash}, normalize::{NormalizePath, TrailingSlash},
}; };
#[cfg(feature = "__compress")]
mod compress;
#[cfg(feature = "__compress")]
pub use self::compress::Compress;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;