mirror of
https://github.com/actix/actix-extras.git
synced 2024-12-03 19:42:13 +01:00
use forward_ready for service definitions
This commit is contained in:
parent
b0854ed144
commit
2254a429d4
@ -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"
|
||||
|
@ -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<B>;
|
||||
|
||||
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
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 {
|
||||
|
@ -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"] }
|
||||
|
@ -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<S, T> {
|
||||
service: Rc<S>,
|
||||
backend: Rc<T>,
|
||||
service: Rc<RefCell<S>>,
|
||||
}
|
||||
|
||||
impl<S, T> Clone for IdentityServiceMiddleware<S, T> {
|
||||
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<Self::Response, Self::Error>>;
|
||||
|
||||
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
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::<IdentityItem>();
|
||||
|
||||
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::<ServiceResponse, _>(error::ErrorBadRequest("error"))
|
||||
},
|
||||
))),
|
||||
service: Rc::new(into_service(|_: ServiceRequest| async move {
|
||||
actix_rt::time::sleep(std::time::Duration::from_secs(100)).await;
|
||||
Err::<ServiceResponse, _>(error::ErrorBadRequest("error"))
|
||||
})),
|
||||
};
|
||||
|
||||
let srv2 = srv.clone();
|
||||
|
@ -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"
|
||||
|
||||
|
@ -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?;
|
||||
|
@ -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<Self::Response, Self::Error>>;
|
||||
|
||||
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
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
|
||||
|
@ -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 }
|
||||
|
||||
|
@ -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<S, F, T>
|
||||
where
|
||||
T: AuthExtractor,
|
||||
{
|
||||
service: Rc<RefCell<S>>,
|
||||
service: Rc<S>,
|
||||
process_fn: Arc<F>,
|
||||
_extractor: PhantomData<T>,
|
||||
}
|
||||
@ -163,9 +158,7 @@ where
|
||||
type Error = S::Error;
|
||||
type Future = LocalBoxFuture<'static, Result<ServiceResponse<B>, Error>>;
|
||||
|
||||
fn poll_ready(&self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
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::<ServiceResponse, _>(error::ErrorBadRequest("error"))
|
||||
},
|
||||
))),
|
||||
service: Rc::new(into_service(|_: ServiceRequest| async move {
|
||||
actix_rt::time::sleep(std::time::Duration::from_secs(1)).await;
|
||||
Err::<ServiceResponse, _>(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::<ServiceResponse, _>(error::ErrorBadRequest("error"))
|
||||
},
|
||||
))),
|
||||
service: Rc::new(into_service(|_: ServiceRequest| async move {
|
||||
actix_rt::time::sleep(std::time::Duration::from_secs(1)).await;
|
||||
Err::<ServiceResponse, _>(error::ErrorBadRequest("error"))
|
||||
})),
|
||||
process_fn: Arc::new(|req, _: BearerAuth| async { Ok(req) }),
|
||||
_extractor: PhantomData,
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user