Skip to content

Commit

Permalink
feat: expose Identity middleware (#3390)
Browse files Browse the repository at this point in the history
  • Loading branch information
robjtede committed Jun 8, 2024
1 parent 3db7891 commit 7c4c26d
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions actix-web/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
13 changes: 2 additions & 11 deletions actix-web/src/middleware/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
};
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions actix-web/src/middleware/condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
16 changes: 6 additions & 10 deletions actix-web/src/middleware/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down

0 comments on commit 7c4c26d

Please sign in to comment.