From 2254a429d4845d2e2d463c073c079373001bd7cb Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Mon, 22 Mar 2021 05:18:59 +0000 Subject: [PATCH] use forward_ready for service definitions --- actix-cors/Cargo.toml | 1 + actix-cors/src/middleware.rs | 10 ++----- actix-identity/Cargo.toml | 2 +- actix-identity/src/lib.rs | 38 ++++++++++---------------- actix-protobuf/Cargo.toml | 2 +- actix-redis/src/session.rs | 4 +-- actix-session/src/cookie.rs | 8 ++---- actix-web-httpauth/Cargo.toml | 1 + actix-web-httpauth/src/middleware.rs | 40 ++++++++++------------------ 9 files changed, 38 insertions(+), 68 deletions(-) diff --git a/actix-cors/Cargo.toml b/actix-cors/Cargo.toml index 6494ce84d..2317033d9 100644 --- a/actix-cors/Cargo.toml +++ b/actix-cors/Cargo.toml @@ -20,6 +20,7 @@ path = "src/lib.rs" [dependencies] actix-web = { version = "4.0.0-beta.4", default-features = false } +actix-service = "2.0.0-beta.5" derive_more = "0.99.5" futures-util = { version = "0.3.7", default-features = false } log = "0.4" diff --git a/actix-cors/src/middleware.rs b/actix-cors/src/middleware.rs index 49b4df375..ae522bfab 100644 --- a/actix-cors/src/middleware.rs +++ b/actix-cors/src/middleware.rs @@ -1,8 +1,4 @@ -use std::{ - convert::TryInto, - rc::Rc, - task::{Context, Poll}, -}; +use std::{convert::TryInto, rc::Rc}; use actix_web::{ dev::{Service, ServiceRequest, ServiceResponse}, @@ -131,9 +127,7 @@ where type Error = Error; type Future = CorsMiddlewareServiceFuture; - fn poll_ready(&self, cx: &mut Context<'_>) -> Poll> { - self.service.poll_ready(cx) - } + actix_service::forward_ready!(service); fn call(&self, req: ServiceRequest) -> Self::Future { if self.inner.preflight && req.method() == Method::OPTIONS { diff --git a/actix-identity/Cargo.toml b/actix-identity/Cargo.toml index 5542b2cb8..3d84b2ff1 100644 --- a/actix-identity/Cargo.toml +++ b/actix-identity/Cargo.toml @@ -18,7 +18,7 @@ path = "src/lib.rs" [dependencies] actix-web = { version = "4.0.0-beta.4", default-features = false, features = ["secure-cookies"] } actix-service = "2.0.0-beta.5" -futures-util = { version = "0.3", default-features = false } +futures-util = { version = "0.3.7", default-features = false } serde = "1.0" serde_json = "1.0" time = { version = "0.2.7", default-features = false, features = ["std"] } diff --git a/actix-identity/src/lib.rs b/actix-identity/src/lib.rs index 0350a7342..68871c426 100644 --- a/actix-identity/src/lib.rs +++ b/actix-identity/src/lib.rs @@ -12,7 +12,7 @@ //! To access current request identity //! [**Identity**](struct.Identity.html) extractor should be used. //! -//! ```rust +//! ``` //! use actix_web::*; //! use actix_identity::{Identity, CookieIdentityPolicy, IdentityService}; //! @@ -49,11 +49,7 @@ #![deny(rust_2018_idioms)] -use std::cell::RefCell; -use std::future::Future; -use std::rc::Rc; -use std::task::{Context, Poll}; -use std::time::SystemTime; +use std::{future::Future, rc::Rc, time::SystemTime}; use actix_service::{Service, Transform}; use futures_util::future::{ok, FutureExt, LocalBoxFuture, Ready}; @@ -239,22 +235,22 @@ where fn new_transform(&self, service: S) -> Self::Future { ok(IdentityServiceMiddleware { backend: self.backend.clone(), - service: Rc::new(RefCell::new(service)), + service: Rc::new(service), }) } } #[doc(hidden)] pub struct IdentityServiceMiddleware { + service: Rc, backend: Rc, - service: Rc>, } impl Clone for IdentityServiceMiddleware { fn clone(&self) -> Self { Self { - backend: self.backend.clone(), - service: self.service.clone(), + backend: Rc::clone(&self.backend), + service: Rc::clone(&self.service), } } } @@ -270,13 +266,11 @@ where type Error = Error; type Future = LocalBoxFuture<'static, Result>; - fn poll_ready(&self, cx: &mut Context<'_>) -> Poll> { - self.service.borrow_mut().poll_ready(cx) - } + actix_service::forward_ready!(service); fn call(&self, mut req: ServiceRequest) -> Self::Future { - let srv = self.service.clone(); - let backend = self.backend.clone(); + let srv = Rc::clone(&self.service); + let backend = Rc::clone(&self.backend); let fut = self.backend.from_request(&mut req); async move { @@ -285,9 +279,7 @@ where req.extensions_mut() .insert(IdentityItem { id, changed: false }); - // https://github.com/actix/actix-web/issues/1263 - let fut = srv.borrow_mut().call(req); - let mut res = fut.await?; + let mut res = srv.call(req).await?; let id = res.request().extensions_mut().remove::(); if let Some(id) = id { @@ -1132,12 +1124,10 @@ mod tests { let srv = IdentityServiceMiddleware { backend: Rc::new(Ident), - service: Rc::new(RefCell::new(into_service( - |_: ServiceRequest| async move { - actix_rt::time::sleep(std::time::Duration::from_secs(100)).await; - Err::(error::ErrorBadRequest("error")) - }, - ))), + service: Rc::new(into_service(|_: ServiceRequest| async move { + actix_rt::time::sleep(std::time::Duration::from_secs(100)).await; + Err::(error::ErrorBadRequest("error")) + })), }; let srv2 = srv.clone(); diff --git a/actix-protobuf/Cargo.toml b/actix-protobuf/Cargo.toml index f439b6bf5..31fc6cf00 100644 --- a/actix-protobuf/Cargo.toml +++ b/actix-protobuf/Cargo.toml @@ -21,7 +21,7 @@ path = "src/lib.rs" [dependencies] actix-web = { version = "4.0.0-beta.4", default_features = false } actix-rt = "2" -futures-util = { version = "0.3.5", default-features = false } +futures-util = { version = "0.3.7", default-features = false } derive_more = "0.99" prost = "0.7" diff --git a/actix-redis/src/session.rs b/actix-redis/src/session.rs index be80eccbb..31ccd9207 100644 --- a/actix-redis/src/session.rs +++ b/actix-redis/src/session.rs @@ -152,8 +152,8 @@ where actix_service::forward_ready!(service); fn call(&self, mut req: ServiceRequest) -> Self::Future { - let srv = self.service.clone(); - let inner = self.inner.clone(); + let srv = Rc::clone(&self.service); + let inner = Rc::clone(&self.inner); Box::pin(async move { let state = inner.load(&req).await?; diff --git a/actix-session/src/cookie.rs b/actix-session/src/cookie.rs index b8fba0ee1..cb65ddf60 100644 --- a/actix-session/src/cookie.rs +++ b/actix-session/src/cookie.rs @@ -1,8 +1,6 @@ //! Cookie based sessions. See docs for [`CookieSession`]. -use std::collections::HashMap; -use std::rc::Rc; -use std::task::{Context, Poll}; +use std::{collections::HashMap, rc::Rc}; use actix_service::{Service, Transform}; use actix_web::cookie::{Cookie, CookieJar, Key, SameSite}; @@ -326,9 +324,7 @@ where type Error = S::Error; type Future = LocalBoxFuture<'static, Result>; - fn poll_ready(&self, cx: &mut Context<'_>) -> Poll> { - self.service.poll_ready(cx) - } + actix_service::forward_ready!(service); /// On first request, a new session cookie is returned in response, regardless /// of whether any session state is set. With subsequent requests, if the diff --git a/actix-web-httpauth/Cargo.toml b/actix-web-httpauth/Cargo.toml index e3cb9a992..4e705acf2 100644 --- a/actix-web-httpauth/Cargo.toml +++ b/actix-web-httpauth/Cargo.toml @@ -21,6 +21,7 @@ path = "src/lib.rs" [dependencies] actix-web = { version = "4.0.0-beta.4", default_features = false } +actix-service = "2.0.0-beta.5" base64 = "0.13" futures-util = { version = "0.3.7", default-features = false } diff --git a/actix-web-httpauth/src/middleware.rs b/actix-web-httpauth/src/middleware.rs index 24d35c6ea..6f42299fa 100644 --- a/actix-web-httpauth/src/middleware.rs +++ b/actix-web-httpauth/src/middleware.rs @@ -1,11 +1,6 @@ //! HTTP Authentication middleware. -use std::cell::RefCell; -use std::future::Future; -use std::marker::PhantomData; -use std::pin::Pin; -use std::rc::Rc; -use std::sync::Arc; +use std::{future::Future, marker::PhantomData, pin::Pin, rc::Rc, sync::Arc}; use actix_web::{ dev::{Service, ServiceRequest, ServiceResponse, Transform}, @@ -134,7 +129,7 @@ where fn new_transform(&self, service: S) -> Self::Future { future::ok(AuthenticationMiddleware { - service: Rc::new(RefCell::new(service)), + service: Rc::new(service), process_fn: self.process_fn.clone(), _extractor: PhantomData, }) @@ -146,7 +141,7 @@ pub struct AuthenticationMiddleware where T: AuthExtractor, { - service: Rc>, + service: Rc, process_fn: Arc, _extractor: PhantomData, } @@ -163,9 +158,7 @@ where type Error = S::Error; type Future = LocalBoxFuture<'static, Result, Error>>; - fn poll_ready(&self, ctx: &mut Context<'_>) -> Poll> { - self.service.borrow_mut().poll_ready(ctx) - } + actix_service::forward_ready!(service); fn call(&self, req: ServiceRequest) -> Self::Future { let process_fn = Arc::clone(&self.process_fn); @@ -183,9 +176,8 @@ where // TODO: alter to remove ? operator; an error response is required for downstream // middleware to do their thing (eg. cors adding headers) let req = process_fn(req, credentials).await?; - // Ensure `borrow_mut()` and `.await` are on separate lines or else a panic occurs. - let fut = service.borrow_mut().call(req); - fut.await + + service.call(req).await } .boxed_local() } @@ -252,12 +244,10 @@ mod tests { #[actix_rt::test] async fn test_middleware_panic() { let middleware = AuthenticationMiddleware { - service: Rc::new(RefCell::new(into_service( - |_: ServiceRequest| async move { - actix_rt::time::sleep(std::time::Duration::from_secs(1)).await; - Err::(error::ErrorBadRequest("error")) - }, - ))), + service: Rc::new(into_service(|_: ServiceRequest| async move { + actix_rt::time::sleep(std::time::Duration::from_secs(1)).await; + Err::(error::ErrorBadRequest("error")) + })), process_fn: Arc::new(|req, _: BearerAuth| async { Ok(req) }), _extractor: PhantomData, }; @@ -277,12 +267,10 @@ mod tests { #[actix_rt::test] async fn test_middleware_panic_several_orders() { let middleware = AuthenticationMiddleware { - service: Rc::new(RefCell::new(into_service( - |_: ServiceRequest| async move { - actix_rt::time::sleep(std::time::Duration::from_secs(1)).await; - Err::(error::ErrorBadRequest("error")) - }, - ))), + service: Rc::new(into_service(|_: ServiceRequest| async move { + actix_rt::time::sleep(std::time::Duration::from_secs(1)).await; + Err::(error::ErrorBadRequest("error")) + })), process_fn: Arc::new(|req, _: BearerAuth| async { Ok(req) }), _extractor: PhantomData, };