mirror of
https://github.com/fafhrd91/actix-web
synced 2025-07-01 16:55:08 +02:00
21
src/app.rs
21
src/app.rs
@ -5,10 +5,11 @@ use std::marker::PhantomData;
|
||||
use std::rc::Rc;
|
||||
|
||||
use actix_http::body::{Body, MessageBody};
|
||||
use actix_http::Extensions;
|
||||
use actix_http::{Extensions, Request};
|
||||
use actix_service::boxed::{self, BoxServiceFactory};
|
||||
use actix_service::{
|
||||
apply, apply_fn_factory, IntoServiceFactory, ServiceFactory, Transform,
|
||||
apply, apply_fn_factory, IntoServiceFactory, ServiceFactory, ServiceFactoryExt,
|
||||
Transform,
|
||||
};
|
||||
use futures_util::future::FutureExt;
|
||||
|
||||
@ -63,8 +64,8 @@ impl<T, B> App<T, B>
|
||||
where
|
||||
B: MessageBody,
|
||||
T: ServiceFactory<
|
||||
ServiceRequest,
|
||||
Config = (),
|
||||
Request = ServiceRequest,
|
||||
Response = ServiceResponse<B>,
|
||||
Error = Error,
|
||||
InitError = (),
|
||||
@ -268,10 +269,10 @@ where
|
||||
/// ```
|
||||
pub fn default_service<F, U>(mut self, f: F) -> Self
|
||||
where
|
||||
F: IntoServiceFactory<U>,
|
||||
F: IntoServiceFactory<U, ServiceRequest>,
|
||||
U: ServiceFactory<
|
||||
ServiceRequest,
|
||||
Config = (),
|
||||
Request = ServiceRequest,
|
||||
Response = ServiceResponse,
|
||||
Error = Error,
|
||||
> + 'static,
|
||||
@ -353,8 +354,8 @@ where
|
||||
mw: M,
|
||||
) -> App<
|
||||
impl ServiceFactory<
|
||||
ServiceRequest,
|
||||
Config = (),
|
||||
Request = ServiceRequest,
|
||||
Response = ServiceResponse<B1>,
|
||||
Error = Error,
|
||||
InitError = (),
|
||||
@ -364,7 +365,7 @@ where
|
||||
where
|
||||
M: Transform<
|
||||
T::Service,
|
||||
Request = ServiceRequest,
|
||||
ServiceRequest,
|
||||
Response = ServiceResponse<B1>,
|
||||
Error = Error,
|
||||
InitError = (),
|
||||
@ -420,8 +421,8 @@ where
|
||||
mw: F,
|
||||
) -> App<
|
||||
impl ServiceFactory<
|
||||
ServiceRequest,
|
||||
Config = (),
|
||||
Request = ServiceRequest,
|
||||
Response = ServiceResponse<B1>,
|
||||
Error = Error,
|
||||
InitError = (),
|
||||
@ -447,12 +448,12 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, B> IntoServiceFactory<AppInit<T, B>> for App<T, B>
|
||||
impl<T, B> IntoServiceFactory<AppInit<T, B>, Request> for App<T, B>
|
||||
where
|
||||
B: MessageBody,
|
||||
T: ServiceFactory<
|
||||
ServiceRequest,
|
||||
Config = (),
|
||||
Request = ServiceRequest,
|
||||
Response = ServiceResponse<B>,
|
||||
Error = Error,
|
||||
InitError = (),
|
||||
|
@ -29,8 +29,8 @@ type BoxResponse = LocalBoxFuture<'static, Result<ServiceResponse, Error>>;
|
||||
pub struct AppInit<T, B>
|
||||
where
|
||||
T: ServiceFactory<
|
||||
ServiceRequest,
|
||||
Config = (),
|
||||
Request = ServiceRequest,
|
||||
Response = ServiceResponse<B>,
|
||||
Error = Error,
|
||||
InitError = (),
|
||||
@ -46,22 +46,21 @@ where
|
||||
pub(crate) external: RefCell<Vec<ResourceDef>>,
|
||||
}
|
||||
|
||||
impl<T, B> ServiceFactory for AppInit<T, B>
|
||||
impl<T, B> ServiceFactory<Request> for AppInit<T, B>
|
||||
where
|
||||
T: ServiceFactory<
|
||||
ServiceRequest,
|
||||
Config = (),
|
||||
Request = ServiceRequest,
|
||||
Response = ServiceResponse<B>,
|
||||
Error = Error,
|
||||
InitError = (),
|
||||
>,
|
||||
{
|
||||
type Config = AppConfig;
|
||||
type Request = Request;
|
||||
type Response = ServiceResponse<B>;
|
||||
type Error = T::Error;
|
||||
type InitError = T::InitError;
|
||||
type Config = AppConfig;
|
||||
type Service = AppInitService<T::Service, B>;
|
||||
type InitError = T::InitError;
|
||||
type Future = AppInitResult<T, B>;
|
||||
|
||||
fn new_service(&self, config: AppConfig) -> Self::Future {
|
||||
@ -132,7 +131,7 @@ where
|
||||
#[pin_project::pin_project]
|
||||
pub struct AppInitResult<T, B>
|
||||
where
|
||||
T: ServiceFactory,
|
||||
T: ServiceFactory<ServiceRequest>,
|
||||
{
|
||||
#[pin]
|
||||
endpoint_fut: T::Future,
|
||||
@ -155,8 +154,8 @@ where
|
||||
impl<T, B> Future for AppInitResult<T, B>
|
||||
where
|
||||
T: ServiceFactory<
|
||||
ServiceRequest,
|
||||
Config = (),
|
||||
Request = ServiceRequest,
|
||||
Response = ServiceResponse<B>,
|
||||
Error = Error,
|
||||
InitError = (),
|
||||
@ -214,7 +213,7 @@ where
|
||||
/// Service to convert `Request` to a `ServiceRequest<S>`
|
||||
pub struct AppInitService<T, B>
|
||||
where
|
||||
T: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
T: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
{
|
||||
service: T,
|
||||
rmap: Rc<ResourceMap>,
|
||||
@ -223,11 +222,10 @@ where
|
||||
pool: &'static HttpRequestPool,
|
||||
}
|
||||
|
||||
impl<T, B> Service for AppInitService<T, B>
|
||||
impl<T, B> Service<Request> for AppInitService<T, B>
|
||||
where
|
||||
T: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
T: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
{
|
||||
type Request = Request;
|
||||
type Response = ServiceResponse<B>;
|
||||
type Error = T::Error;
|
||||
type Future = T::Future;
|
||||
@ -263,7 +261,7 @@ where
|
||||
|
||||
impl<T, B> Drop for AppInitService<T, B>
|
||||
where
|
||||
T: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
T: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
{
|
||||
fn drop(&mut self) {
|
||||
self.pool.clear();
|
||||
@ -275,9 +273,8 @@ pub struct AppRoutingFactory {
|
||||
default: Rc<HttpNewService>,
|
||||
}
|
||||
|
||||
impl ServiceFactory for AppRoutingFactory {
|
||||
impl ServiceFactory<ServiceRequest> for AppRoutingFactory {
|
||||
type Config = ();
|
||||
type Request = ServiceRequest;
|
||||
type Response = ServiceResponse;
|
||||
type Error = Error;
|
||||
type InitError = ();
|
||||
@ -386,8 +383,7 @@ pub struct AppRouting {
|
||||
default: Option<HttpService>,
|
||||
}
|
||||
|
||||
impl Service for AppRouting {
|
||||
type Request = ServiceRequest;
|
||||
impl Service<ServiceRequest> for AppRouting {
|
||||
type Response = ServiceResponse;
|
||||
type Error = Error;
|
||||
type Future = BoxResponse;
|
||||
@ -434,9 +430,8 @@ impl AppEntry {
|
||||
}
|
||||
}
|
||||
|
||||
impl ServiceFactory for AppEntry {
|
||||
impl ServiceFactory<ServiceRequest> for AppEntry {
|
||||
type Config = ();
|
||||
type Request = ServiceRequest;
|
||||
type Response = ServiceResponse;
|
||||
type Error = Error;
|
||||
type InitError = ();
|
||||
|
@ -105,10 +105,10 @@ impl AppService {
|
||||
factory: F,
|
||||
nested: Option<Rc<ResourceMap>>,
|
||||
) where
|
||||
F: IntoServiceFactory<S>,
|
||||
F: IntoServiceFactory<S, ServiceRequest>,
|
||||
S: ServiceFactory<
|
||||
ServiceRequest,
|
||||
Config = (),
|
||||
Request = ServiceRequest,
|
||||
Response = ServiceResponse,
|
||||
Error = Error,
|
||||
InitError = (),
|
||||
|
@ -82,14 +82,13 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<F, T, R> ServiceFactory for HandlerService<F, T, R>
|
||||
impl<F, T, R> ServiceFactory<ServiceRequest> for HandlerService<F, T, R>
|
||||
where
|
||||
F: Handler<T, R>,
|
||||
T: FromRequest,
|
||||
R: Future,
|
||||
R::Output: Responder,
|
||||
{
|
||||
type Request = ServiceRequest;
|
||||
type Response = ServiceResponse;
|
||||
type Error = Error;
|
||||
type Config = ();
|
||||
@ -102,15 +101,14 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
// Handler is both it's ServiceHandler and Service Type.
|
||||
impl<F, T, R> Service for HandlerService<F, T, R>
|
||||
// HandlerService is both it's ServiceFactory and Service Type.
|
||||
impl<F, T, R> Service<ServiceRequest> for HandlerService<F, T, R>
|
||||
where
|
||||
F: Handler<T, R>,
|
||||
T: FromRequest,
|
||||
R: Future,
|
||||
R::Output: Responder,
|
||||
{
|
||||
type Request = ServiceRequest;
|
||||
type Response = ServiceResponse;
|
||||
type Error = Error;
|
||||
type Future = HandlerServiceFuture<F, T, R>;
|
||||
@ -119,7 +117,7 @@ where
|
||||
Poll::Ready(Ok(()))
|
||||
}
|
||||
|
||||
fn call(&mut self, req: Self::Request) -> Self::Future {
|
||||
fn call(&mut self, req: ServiceRequest) -> Self::Future {
|
||||
let (req, mut payload) = req.into_parts();
|
||||
let fut = T::from_request(&req, &mut payload);
|
||||
HandlerServiceFuture::Extract(fut, Some(req), self.hnd.clone())
|
||||
|
@ -51,16 +51,15 @@ impl Default for Compress {
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, B> Transform<S> for Compress
|
||||
impl<S, B> Transform<S, ServiceRequest> for Compress
|
||||
where
|
||||
B: MessageBody,
|
||||
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
{
|
||||
type Request = ServiceRequest;
|
||||
type Response = ServiceResponse<Encoder<B>>;
|
||||
type Error = Error;
|
||||
type InitError = ();
|
||||
type Transform = CompressMiddleware<S>;
|
||||
type InitError = ();
|
||||
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
||||
|
||||
fn new_transform(&self, service: S) -> Self::Future {
|
||||
@ -76,12 +75,11 @@ pub struct CompressMiddleware<S> {
|
||||
encoding: ContentEncoding,
|
||||
}
|
||||
|
||||
impl<S, B> Service for CompressMiddleware<S>
|
||||
impl<S, B> Service<ServiceRequest> for CompressMiddleware<S>
|
||||
where
|
||||
B: MessageBody,
|
||||
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
{
|
||||
type Request = ServiceRequest;
|
||||
type Response = ServiceResponse<Encoder<B>>;
|
||||
type Error = Error;
|
||||
type Future = CompressResponse<S, B>;
|
||||
@ -115,7 +113,7 @@ where
|
||||
#[pin_project]
|
||||
pub struct CompressResponse<S, B>
|
||||
where
|
||||
S: Service,
|
||||
S: Service<ServiceRequest>,
|
||||
B: MessageBody,
|
||||
{
|
||||
#[pin]
|
||||
@ -127,7 +125,7 @@ where
|
||||
impl<S, B> Future for CompressResponse<S, B>
|
||||
where
|
||||
B: MessageBody,
|
||||
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
{
|
||||
type Output = Result<ServiceResponse<Encoder<B>>, Error>;
|
||||
|
||||
|
@ -31,19 +31,18 @@ impl<T> Condition<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, T> Transform<S> for Condition<T>
|
||||
impl<S, T, Req> Transform<S, Req> for Condition<T>
|
||||
where
|
||||
S: Service + 'static,
|
||||
T: Transform<S, Request = S::Request, Response = S::Response, Error = S::Error>,
|
||||
S: Service<Req> + 'static,
|
||||
T: Transform<S, Req, Response = S::Response, Error = S::Error>,
|
||||
T::Future: 'static,
|
||||
T::InitError: 'static,
|
||||
T::Transform: 'static,
|
||||
{
|
||||
type Request = S::Request;
|
||||
type Response = S::Response;
|
||||
type Error = S::Error;
|
||||
type InitError = T::InitError;
|
||||
type Transform = ConditionMiddleware<T::Transform, S>;
|
||||
type InitError = T::InitError;
|
||||
type Future = LocalBoxFuture<'static, Result<Self::Transform, Self::InitError>>;
|
||||
|
||||
fn new_transform(&self, service: S) -> Self::Future {
|
||||
@ -66,12 +65,11 @@ pub enum ConditionMiddleware<E, D> {
|
||||
Disable(D),
|
||||
}
|
||||
|
||||
impl<E, D> Service for ConditionMiddleware<E, D>
|
||||
impl<E, D, Req> Service<Req> for ConditionMiddleware<E, D>
|
||||
where
|
||||
E: Service,
|
||||
D: Service<Request = E::Request, Response = E::Response, Error = E::Error>,
|
||||
E: Service<Req>,
|
||||
D: Service<Req, Response = E::Response, Error = E::Error>,
|
||||
{
|
||||
type Request = E::Request;
|
||||
type Response = E::Response;
|
||||
type Error = E::Error;
|
||||
type Future = Either<E::Future, D::Future>;
|
||||
@ -84,7 +82,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
fn call(&mut self, req: E::Request) -> Self::Future {
|
||||
fn call(&mut self, req: Req) -> Self::Future {
|
||||
use ConditionMiddleware::*;
|
||||
match self {
|
||||
Enable(service) => Either::Left(service.call(req)),
|
||||
|
@ -93,12 +93,11 @@ impl DefaultHeaders {
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, B> Transform<S> for DefaultHeaders
|
||||
impl<S, B> Transform<S, ServiceRequest> for DefaultHeaders
|
||||
where
|
||||
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
S::Future: 'static,
|
||||
{
|
||||
type Request = ServiceRequest;
|
||||
type Response = ServiceResponse<B>;
|
||||
type Error = Error;
|
||||
type Transform = DefaultHeadersMiddleware<S>;
|
||||
@ -118,12 +117,11 @@ pub struct DefaultHeadersMiddleware<S> {
|
||||
inner: Rc<Inner>,
|
||||
}
|
||||
|
||||
impl<S, B> Service for DefaultHeadersMiddleware<S>
|
||||
impl<S, B> Service<ServiceRequest> for DefaultHeadersMiddleware<S>
|
||||
where
|
||||
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
S::Future: 'static,
|
||||
{
|
||||
type Request = ServiceRequest;
|
||||
type Response = ServiceResponse<B>;
|
||||
type Error = Error;
|
||||
type Future = DefaultHeaderFuture<S, B>;
|
||||
@ -145,7 +143,7 @@ where
|
||||
}
|
||||
|
||||
#[pin_project::pin_project]
|
||||
pub struct DefaultHeaderFuture<S: Service, B> {
|
||||
pub struct DefaultHeaderFuture<S: Service<ServiceRequest>, B> {
|
||||
#[pin]
|
||||
fut: S::Future,
|
||||
inner: Rc<Inner>,
|
||||
@ -154,7 +152,7 @@ pub struct DefaultHeaderFuture<S: Service, B> {
|
||||
|
||||
impl<S, B> Future for DefaultHeaderFuture<S, B>
|
||||
where
|
||||
S: Service<Response = ServiceResponse<B>, Error = Error>,
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
{
|
||||
type Output = <S::Future as Future>::Output;
|
||||
|
||||
|
@ -81,17 +81,16 @@ impl<B> ErrorHandlers<B> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, B> Transform<S> for ErrorHandlers<B>
|
||||
impl<S, B> Transform<S, ServiceRequest> for ErrorHandlers<B>
|
||||
where
|
||||
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
S::Future: 'static,
|
||||
B: 'static,
|
||||
{
|
||||
type Request = ServiceRequest;
|
||||
type Response = ServiceResponse<B>;
|
||||
type Error = Error;
|
||||
type InitError = ();
|
||||
type Transform = ErrorHandlersMiddleware<S, B>;
|
||||
type InitError = ();
|
||||
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
||||
|
||||
fn new_transform(&self, service: S) -> Self::Future {
|
||||
@ -108,13 +107,12 @@ pub struct ErrorHandlersMiddleware<S, B> {
|
||||
handlers: Rc<FxHashMap<StatusCode, Box<ErrorHandler<B>>>>,
|
||||
}
|
||||
|
||||
impl<S, B> Service for ErrorHandlersMiddleware<S, B>
|
||||
impl<S, B> Service<ServiceRequest> for ErrorHandlersMiddleware<S, B>
|
||||
where
|
||||
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
S::Future: 'static,
|
||||
B: 'static,
|
||||
{
|
||||
type Request = ServiceRequest;
|
||||
type Response = ServiceResponse<B>;
|
||||
type Error = Error;
|
||||
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
|
||||
|
@ -179,12 +179,11 @@ impl Default for Logger {
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, B> Transform<S> for Logger
|
||||
impl<S, B> Transform<S, ServiceRequest> for Logger
|
||||
where
|
||||
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
B: MessageBody,
|
||||
{
|
||||
type Request = ServiceRequest;
|
||||
type Response = ServiceResponse<StreamLog<B>>;
|
||||
type Error = Error;
|
||||
type InitError = ();
|
||||
@ -216,12 +215,11 @@ pub struct LoggerMiddleware<S> {
|
||||
service: S,
|
||||
}
|
||||
|
||||
impl<S, B> Service for LoggerMiddleware<S>
|
||||
impl<S, B> Service<ServiceRequest> for LoggerMiddleware<S>
|
||||
where
|
||||
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
B: MessageBody,
|
||||
{
|
||||
type Request = ServiceRequest;
|
||||
type Response = ServiceResponse<StreamLog<B>>;
|
||||
type Error = Error;
|
||||
type Future = LoggerResponse<S, B>;
|
||||
@ -262,19 +260,19 @@ where
|
||||
pub struct LoggerResponse<S, B>
|
||||
where
|
||||
B: MessageBody,
|
||||
S: Service,
|
||||
S: Service<ServiceRequest>,
|
||||
{
|
||||
#[pin]
|
||||
fut: S::Future,
|
||||
time: OffsetDateTime,
|
||||
format: Option<Format>,
|
||||
_t: PhantomData<(B,)>,
|
||||
_t: PhantomData<B>,
|
||||
}
|
||||
|
||||
impl<S, B> Future for LoggerResponse<S, B>
|
||||
where
|
||||
B: MessageBody,
|
||||
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
{
|
||||
type Output = Result<ServiceResponse<StreamLog<B>>, Error>;
|
||||
|
||||
|
@ -91,16 +91,15 @@ impl NormalizePath {
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, B> Transform<S> for NormalizePath
|
||||
impl<S, B> Transform<S, ServiceRequest> for NormalizePath
|
||||
where
|
||||
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
S::Future: 'static,
|
||||
{
|
||||
type Request = ServiceRequest;
|
||||
type Response = ServiceResponse<B>;
|
||||
type Error = Error;
|
||||
type InitError = ();
|
||||
type Transform = NormalizePathNormalization<S>;
|
||||
type InitError = ();
|
||||
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
||||
|
||||
fn new_transform(&self, service: S) -> Self::Future {
|
||||
@ -119,12 +118,11 @@ pub struct NormalizePathNormalization<S> {
|
||||
trailing_slash_behavior: TrailingSlash,
|
||||
}
|
||||
|
||||
impl<S, B> Service for NormalizePathNormalization<S>
|
||||
impl<S, B> Service<ServiceRequest> for NormalizePathNormalization<S>
|
||||
where
|
||||
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
S::Future: 'static,
|
||||
{
|
||||
type Request = ServiceRequest;
|
||||
type Response = ServiceResponse<B>;
|
||||
type Error = Error;
|
||||
type Future = S::Future;
|
||||
|
@ -9,7 +9,8 @@ use actix_http::{Error, Extensions, Response};
|
||||
use actix_router::IntoPattern;
|
||||
use actix_service::boxed::{self, BoxService, BoxServiceFactory};
|
||||
use actix_service::{
|
||||
apply, apply_fn_factory, IntoServiceFactory, Service, ServiceFactory, Transform,
|
||||
apply, apply_fn_factory, IntoServiceFactory, Service, ServiceFactory,
|
||||
ServiceFactoryExt, Transform,
|
||||
};
|
||||
use futures_core::future::LocalBoxFuture;
|
||||
|
||||
@ -78,8 +79,8 @@ impl Resource {
|
||||
impl<T> Resource<T>
|
||||
where
|
||||
T: ServiceFactory<
|
||||
ServiceRequest,
|
||||
Config = (),
|
||||
Request = ServiceRequest,
|
||||
Response = ServiceResponse,
|
||||
Error = Error,
|
||||
InitError = (),
|
||||
@ -250,8 +251,8 @@ where
|
||||
mw: M,
|
||||
) -> Resource<
|
||||
impl ServiceFactory<
|
||||
ServiceRequest,
|
||||
Config = (),
|
||||
Request = ServiceRequest,
|
||||
Response = ServiceResponse,
|
||||
Error = Error,
|
||||
InitError = (),
|
||||
@ -260,7 +261,7 @@ where
|
||||
where
|
||||
M: Transform<
|
||||
T::Service,
|
||||
Request = ServiceRequest,
|
||||
ServiceRequest,
|
||||
Response = ServiceResponse,
|
||||
Error = Error,
|
||||
InitError = (),
|
||||
@ -317,8 +318,8 @@ where
|
||||
mw: F,
|
||||
) -> Resource<
|
||||
impl ServiceFactory<
|
||||
ServiceRequest,
|
||||
Config = (),
|
||||
Request = ServiceRequest,
|
||||
Response = ServiceResponse,
|
||||
Error = Error,
|
||||
InitError = (),
|
||||
@ -345,10 +346,10 @@ where
|
||||
/// default handler from `App` or `Scope`.
|
||||
pub fn default_service<F, U>(mut self, f: F) -> Self
|
||||
where
|
||||
F: IntoServiceFactory<U>,
|
||||
F: IntoServiceFactory<U, ServiceRequest>,
|
||||
U: ServiceFactory<
|
||||
ServiceRequest,
|
||||
Config = (),
|
||||
Request = ServiceRequest,
|
||||
Response = ServiceResponse,
|
||||
Error = Error,
|
||||
> + 'static,
|
||||
@ -368,8 +369,8 @@ where
|
||||
impl<T> HttpServiceFactory for Resource<T>
|
||||
where
|
||||
T: ServiceFactory<
|
||||
ServiceRequest,
|
||||
Config = (),
|
||||
Request = ServiceRequest,
|
||||
Response = ServiceResponse,
|
||||
Error = Error,
|
||||
InitError = (),
|
||||
@ -398,11 +399,11 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> IntoServiceFactory<T> for Resource<T>
|
||||
impl<T> IntoServiceFactory<T, ServiceRequest> for Resource<T>
|
||||
where
|
||||
T: ServiceFactory<
|
||||
ServiceRequest,
|
||||
Config = (),
|
||||
Request = ServiceRequest,
|
||||
Response = ServiceResponse,
|
||||
Error = Error,
|
||||
InitError = (),
|
||||
@ -425,13 +426,12 @@ pub struct ResourceFactory {
|
||||
default: Rc<RefCell<Option<Rc<HttpNewService>>>>,
|
||||
}
|
||||
|
||||
impl ServiceFactory for ResourceFactory {
|
||||
type Config = ();
|
||||
type Request = ServiceRequest;
|
||||
impl ServiceFactory<ServiceRequest> for ResourceFactory {
|
||||
type Response = ServiceResponse;
|
||||
type Error = Error;
|
||||
type InitError = ();
|
||||
type Config = ();
|
||||
type Service = ResourceService;
|
||||
type InitError = ();
|
||||
type Future = CreateResourceService;
|
||||
|
||||
fn new_service(&self, _: ()) -> Self::Future {
|
||||
@ -520,8 +520,7 @@ pub struct ResourceService {
|
||||
default: Option<HttpService>,
|
||||
}
|
||||
|
||||
impl Service for ResourceService {
|
||||
type Request = ServiceRequest;
|
||||
impl Service<ServiceRequest> for ResourceService {
|
||||
type Response = ServiceResponse;
|
||||
type Error = Error;
|
||||
type Future = LocalBoxFuture<'static, Result<ServiceResponse, Error>>;
|
||||
@ -567,9 +566,8 @@ impl ResourceEndpoint {
|
||||
}
|
||||
}
|
||||
|
||||
impl ServiceFactory for ResourceEndpoint {
|
||||
impl ServiceFactory<ServiceRequest> for ResourceEndpoint {
|
||||
type Config = ();
|
||||
type Request = ServiceRequest;
|
||||
type Response = ServiceResponse;
|
||||
type Error = Error;
|
||||
type InitError = ();
|
||||
@ -585,7 +583,7 @@ impl ServiceFactory for ResourceEndpoint {
|
||||
mod tests {
|
||||
use std::time::Duration;
|
||||
|
||||
use actix_rt::time::delay_for;
|
||||
use actix_rt::time::sleep;
|
||||
use actix_service::Service;
|
||||
use futures_util::future::ok;
|
||||
|
||||
@ -653,7 +651,7 @@ mod tests {
|
||||
async fn test_to() {
|
||||
let mut srv =
|
||||
init_service(App::new().service(web::resource("/test").to(|| async {
|
||||
delay_for(Duration::from_millis(100)).await;
|
||||
sleep(Duration::from_millis(100)).await;
|
||||
Ok::<_, Error>(HttpResponse::Ok())
|
||||
})))
|
||||
.await;
|
||||
|
38
src/route.rs
38
src/route.rs
@ -18,7 +18,7 @@ use crate::HttpResponse;
|
||||
|
||||
type BoxedRouteService = Box<
|
||||
dyn Service<
|
||||
Request = ServiceRequest,
|
||||
ServiceRequest,
|
||||
Response = ServiceResponse,
|
||||
Error = Error,
|
||||
Future = LocalBoxFuture<'static, Result<ServiceResponse, Error>>,
|
||||
@ -27,8 +27,8 @@ type BoxedRouteService = Box<
|
||||
|
||||
type BoxedRouteNewService = Box<
|
||||
dyn ServiceFactory<
|
||||
ServiceRequest,
|
||||
Config = (),
|
||||
Request = ServiceRequest,
|
||||
Response = ServiceResponse,
|
||||
Error = Error,
|
||||
InitError = (),
|
||||
@ -63,9 +63,8 @@ impl Route {
|
||||
}
|
||||
}
|
||||
|
||||
impl ServiceFactory for Route {
|
||||
impl ServiceFactory<ServiceRequest> for Route {
|
||||
type Config = ();
|
||||
type Request = ServiceRequest;
|
||||
type Response = ServiceResponse;
|
||||
type Error = Error;
|
||||
type InitError = ();
|
||||
@ -117,8 +116,7 @@ impl RouteService {
|
||||
}
|
||||
}
|
||||
|
||||
impl Service for RouteService {
|
||||
type Request = ServiceRequest;
|
||||
impl Service<ServiceRequest> for RouteService {
|
||||
type Response = ServiceResponse;
|
||||
type Error = Error;
|
||||
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
|
||||
@ -233,7 +231,7 @@ impl Route {
|
||||
|
||||
struct RouteNewService<T>
|
||||
where
|
||||
T: ServiceFactory<Request = ServiceRequest, Error = Error>,
|
||||
T: ServiceFactory<ServiceRequest, Error = Error>,
|
||||
{
|
||||
service: T,
|
||||
}
|
||||
@ -241,33 +239,32 @@ where
|
||||
impl<T> RouteNewService<T>
|
||||
where
|
||||
T: ServiceFactory<
|
||||
ServiceRequest,
|
||||
Config = (),
|
||||
Request = ServiceRequest,
|
||||
Response = ServiceResponse,
|
||||
Error = Error,
|
||||
>,
|
||||
T::Future: 'static,
|
||||
T::Service: 'static,
|
||||
<T::Service as Service>::Future: 'static,
|
||||
<T::Service as Service<ServiceRequest>>::Future: 'static,
|
||||
{
|
||||
pub fn new(service: T) -> Self {
|
||||
RouteNewService { service }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> ServiceFactory for RouteNewService<T>
|
||||
impl<T> ServiceFactory<ServiceRequest> for RouteNewService<T>
|
||||
where
|
||||
T: ServiceFactory<
|
||||
ServiceRequest,
|
||||
Config = (),
|
||||
Request = ServiceRequest,
|
||||
Response = ServiceResponse,
|
||||
Error = Error,
|
||||
>,
|
||||
T::Future: 'static,
|
||||
T::Service: 'static,
|
||||
<T::Service as Service>::Future: 'static,
|
||||
<T::Service as Service<ServiceRequest>>::Future: 'static,
|
||||
{
|
||||
type Request = ServiceRequest;
|
||||
type Response = ServiceResponse;
|
||||
type Error = Error;
|
||||
type Config = ();
|
||||
@ -289,16 +286,15 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
struct RouteServiceWrapper<T: Service> {
|
||||
struct RouteServiceWrapper<T: Service<ServiceRequest>> {
|
||||
service: T,
|
||||
}
|
||||
|
||||
impl<T> Service for RouteServiceWrapper<T>
|
||||
impl<T> Service<ServiceRequest> for RouteServiceWrapper<T>
|
||||
where
|
||||
T::Future: 'static,
|
||||
T: Service<Request = ServiceRequest, Response = ServiceResponse, Error = Error>,
|
||||
T: Service<ServiceRequest, Response = ServiceResponse, Error = Error>,
|
||||
{
|
||||
type Request = ServiceRequest;
|
||||
type Response = ServiceResponse;
|
||||
type Error = Error;
|
||||
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
|
||||
@ -316,7 +312,7 @@ where
|
||||
mod tests {
|
||||
use std::time::Duration;
|
||||
|
||||
use actix_rt::time::delay_for;
|
||||
use actix_rt::time::sleep;
|
||||
use bytes::Bytes;
|
||||
use serde_derive::Serialize;
|
||||
|
||||
@ -340,16 +336,16 @@ mod tests {
|
||||
Err::<HttpResponse, _>(error::ErrorBadRequest("err"))
|
||||
}))
|
||||
.route(web::post().to(|| async {
|
||||
delay_for(Duration::from_millis(100)).await;
|
||||
sleep(Duration::from_millis(100)).await;
|
||||
Ok::<_, ()>(HttpResponse::Created())
|
||||
}))
|
||||
.route(web::delete().to(|| async {
|
||||
delay_for(Duration::from_millis(100)).await;
|
||||
sleep(Duration::from_millis(100)).await;
|
||||
Err::<HttpResponse, _>(error::ErrorBadRequest("err"))
|
||||
})),
|
||||
)
|
||||
.service(web::resource("/json").route(web::get().to(|| async {
|
||||
delay_for(Duration::from_millis(25)).await;
|
||||
sleep(Duration::from_millis(25)).await;
|
||||
web::Json(MyObject {
|
||||
name: "test".to_string(),
|
||||
})
|
||||
|
30
src/scope.rs
30
src/scope.rs
@ -9,7 +9,8 @@ use actix_http::{Extensions, Response};
|
||||
use actix_router::{ResourceDef, ResourceInfo, Router};
|
||||
use actix_service::boxed::{self, BoxService, BoxServiceFactory};
|
||||
use actix_service::{
|
||||
apply, apply_fn_factory, IntoServiceFactory, Service, ServiceFactory, Transform,
|
||||
apply, apply_fn_factory, IntoServiceFactory, Service, ServiceFactory,
|
||||
ServiceFactoryExt, Transform,
|
||||
};
|
||||
use futures_core::future::LocalBoxFuture;
|
||||
|
||||
@ -88,8 +89,8 @@ impl Scope {
|
||||
impl<T> Scope<T>
|
||||
where
|
||||
T: ServiceFactory<
|
||||
ServiceRequest,
|
||||
Config = (),
|
||||
Request = ServiceRequest,
|
||||
Response = ServiceResponse,
|
||||
Error = Error,
|
||||
InitError = (),
|
||||
@ -284,10 +285,10 @@ where
|
||||
/// If default resource is not registered, app's default resource is being used.
|
||||
pub fn default_service<F, U>(mut self, f: F) -> Self
|
||||
where
|
||||
F: IntoServiceFactory<U>,
|
||||
F: IntoServiceFactory<U, ServiceRequest>,
|
||||
U: ServiceFactory<
|
||||
ServiceRequest,
|
||||
Config = (),
|
||||
Request = ServiceRequest,
|
||||
Response = ServiceResponse,
|
||||
Error = Error,
|
||||
> + 'static,
|
||||
@ -317,8 +318,8 @@ where
|
||||
mw: M,
|
||||
) -> Scope<
|
||||
impl ServiceFactory<
|
||||
ServiceRequest,
|
||||
Config = (),
|
||||
Request = ServiceRequest,
|
||||
Response = ServiceResponse,
|
||||
Error = Error,
|
||||
InitError = (),
|
||||
@ -327,7 +328,7 @@ where
|
||||
where
|
||||
M: Transform<
|
||||
T::Service,
|
||||
Request = ServiceRequest,
|
||||
ServiceRequest,
|
||||
Response = ServiceResponse,
|
||||
Error = Error,
|
||||
InitError = (),
|
||||
@ -382,8 +383,8 @@ where
|
||||
mw: F,
|
||||
) -> Scope<
|
||||
impl ServiceFactory<
|
||||
ServiceRequest,
|
||||
Config = (),
|
||||
Request = ServiceRequest,
|
||||
Response = ServiceResponse,
|
||||
Error = Error,
|
||||
InitError = (),
|
||||
@ -409,8 +410,8 @@ where
|
||||
impl<T> HttpServiceFactory for Scope<T>
|
||||
where
|
||||
T: ServiceFactory<
|
||||
ServiceRequest,
|
||||
Config = (),
|
||||
Request = ServiceRequest,
|
||||
Response = ServiceResponse,
|
||||
Error = Error,
|
||||
InitError = (),
|
||||
@ -480,9 +481,8 @@ pub struct ScopeFactory {
|
||||
default: Rc<RefCell<Option<Rc<HttpNewService>>>>,
|
||||
}
|
||||
|
||||
impl ServiceFactory for ScopeFactory {
|
||||
impl ServiceFactory<ServiceRequest> for ScopeFactory {
|
||||
type Config = ();
|
||||
type Request = ServiceRequest;
|
||||
type Response = ServiceResponse;
|
||||
type Error = Error;
|
||||
type InitError = ();
|
||||
@ -601,8 +601,7 @@ pub struct ScopeService {
|
||||
_ready: Option<(ServiceRequest, ResourceInfo)>,
|
||||
}
|
||||
|
||||
impl Service for ScopeService {
|
||||
type Request = ServiceRequest;
|
||||
impl Service<ServiceRequest> for ScopeService {
|
||||
type Response = ServiceResponse;
|
||||
type Error = Error;
|
||||
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
|
||||
@ -653,13 +652,12 @@ impl ScopeEndpoint {
|
||||
}
|
||||
}
|
||||
|
||||
impl ServiceFactory for ScopeEndpoint {
|
||||
type Config = ();
|
||||
type Request = ServiceRequest;
|
||||
impl ServiceFactory<ServiceRequest> for ScopeEndpoint {
|
||||
type Response = ServiceResponse;
|
||||
type Error = Error;
|
||||
type InitError = ();
|
||||
type Config = ();
|
||||
type Service = ScopeService;
|
||||
type InitError = ();
|
||||
type Future = ScopeFactoryResponse;
|
||||
|
||||
fn new_service(&self, _: ()) -> Self::Future {
|
||||
|
@ -20,9 +20,9 @@ use actix_service::pipeline_factory;
|
||||
use futures_util::future::ok;
|
||||
|
||||
#[cfg(feature = "openssl")]
|
||||
use actix_tls::openssl::{AlpnError, SslAcceptor, SslAcceptorBuilder};
|
||||
use actix_tls::accept::openssl::{AlpnError, SslAcceptor, SslAcceptorBuilder};
|
||||
#[cfg(feature = "rustls")]
|
||||
use actix_tls::rustls::ServerConfig as RustlsServerConfig;
|
||||
use actix_tls::accept::rustls::ServerConfig as RustlsServerConfig;
|
||||
|
||||
use crate::config::AppConfig;
|
||||
|
||||
@ -58,8 +58,8 @@ struct Config {
|
||||
pub struct HttpServer<F, I, S, B>
|
||||
where
|
||||
F: Fn() -> I + Send + Clone + 'static,
|
||||
I: IntoServiceFactory<S>,
|
||||
S: ServiceFactory<Config = AppConfig, Request = Request>,
|
||||
I: IntoServiceFactory<S, Request>,
|
||||
S: ServiceFactory<Request, Config = AppConfig>,
|
||||
S::Error: Into<Error>,
|
||||
S::InitError: fmt::Debug,
|
||||
S::Response: Into<Response<B>>,
|
||||
@ -67,7 +67,7 @@ where
|
||||
{
|
||||
pub(super) factory: F,
|
||||
config: Arc<Mutex<Config>>,
|
||||
backlog: i32,
|
||||
backlog: u32,
|
||||
sockets: Vec<Socket>,
|
||||
builder: ServerBuilder,
|
||||
on_connect_fn: Option<Arc<dyn Fn(&dyn Any, &mut Extensions) + Send + Sync>>,
|
||||
@ -77,12 +77,13 @@ where
|
||||
impl<F, I, S, B> HttpServer<F, I, S, B>
|
||||
where
|
||||
F: Fn() -> I + Send + Clone + 'static,
|
||||
I: IntoServiceFactory<S>,
|
||||
S: ServiceFactory<Config = AppConfig, Request = Request>,
|
||||
I: IntoServiceFactory<S, Request>,
|
||||
S: ServiceFactory<Request, Config = AppConfig> + 'static,
|
||||
S::Error: Into<Error> + 'static,
|
||||
S::InitError: fmt::Debug,
|
||||
S::Response: Into<Response<B>> + 'static,
|
||||
<S::Service as Service>::Future: 'static,
|
||||
<S::Service as Service<Request>>::Future: 'static,
|
||||
S::Service: 'static,
|
||||
B: MessageBody + 'static,
|
||||
{
|
||||
/// Create new http server with application factory
|
||||
@ -147,7 +148,7 @@ where
|
||||
/// Generally set in the 64-2048 range. Default value is 2048.
|
||||
///
|
||||
/// This method should be called before `bind()` method call.
|
||||
pub fn backlog(mut self, backlog: i32) -> Self {
|
||||
pub fn backlog(mut self, backlog: u32) -> Self {
|
||||
self.backlog = backlog;
|
||||
self.builder = self.builder.backlog(backlog);
|
||||
self
|
||||
@ -170,8 +171,10 @@ where
|
||||
/// limit the global TLS CPU usage.
|
||||
///
|
||||
/// By default max connections is set to a 256.
|
||||
#[allow(unused_variables)]
|
||||
pub fn max_connection_rate(self, num: usize) -> Self {
|
||||
actix_tls::max_concurrent_tls_connect(num);
|
||||
#[cfg(any(feature = "rustls", feature = "openssl"))]
|
||||
actix_tls::accept::max_concurrent_tls_connect(num);
|
||||
self
|
||||
}
|
||||
|
||||
@ -603,8 +606,8 @@ where
|
||||
impl<F, I, S, B> HttpServer<F, I, S, B>
|
||||
where
|
||||
F: Fn() -> I + Send + Clone + 'static,
|
||||
I: IntoServiceFactory<S>,
|
||||
S: ServiceFactory<Config = AppConfig, Request = Request>,
|
||||
I: IntoServiceFactory<S, Request>,
|
||||
S: ServiceFactory<Request, Config = AppConfig>,
|
||||
S::Error: Into<Error>,
|
||||
S::InitError: fmt::Debug,
|
||||
S::Response: Into<Response<B>>,
|
||||
@ -639,7 +642,7 @@ where
|
||||
|
||||
fn create_tcp_listener(
|
||||
addr: net::SocketAddr,
|
||||
backlog: i32,
|
||||
backlog: u32,
|
||||
) -> io::Result<net::TcpListener> {
|
||||
use socket2::{Domain, Protocol, Socket, Type};
|
||||
let domain = match addr {
|
||||
@ -649,6 +652,8 @@ fn create_tcp_listener(
|
||||
let socket = Socket::new(domain, Type::stream(), Some(Protocol::tcp()))?;
|
||||
socket.set_reuse_address(true)?;
|
||||
socket.bind(&addr.into())?;
|
||||
// clamp backlog to max u32 that fits in i32 range
|
||||
let backlog = backlog.min(i32::MAX as u32) as i32;
|
||||
socket.listen(backlog)?;
|
||||
Ok(socket.into_tcp_listener())
|
||||
}
|
||||
|
@ -486,10 +486,10 @@ impl WebService {
|
||||
/// Set a service factory implementation and generate web service.
|
||||
pub fn finish<T, F>(self, service: F) -> impl HttpServiceFactory
|
||||
where
|
||||
F: IntoServiceFactory<T>,
|
||||
F: IntoServiceFactory<T, ServiceRequest>,
|
||||
T: ServiceFactory<
|
||||
ServiceRequest,
|
||||
Config = (),
|
||||
Request = ServiceRequest,
|
||||
Response = ServiceResponse,
|
||||
Error = Error,
|
||||
InitError = (),
|
||||
@ -514,8 +514,8 @@ struct WebServiceImpl<T> {
|
||||
impl<T> HttpServiceFactory for WebServiceImpl<T>
|
||||
where
|
||||
T: ServiceFactory<
|
||||
ServiceRequest,
|
||||
Config = (),
|
||||
Request = ServiceRequest,
|
||||
Response = ServiceResponse,
|
||||
Error = Error,
|
||||
InitError = (),
|
||||
|
54
src/test.rs
54
src/test.rs
@ -11,7 +11,7 @@ use actix_http::http::{Error as HttpError, Method, StatusCode, Uri, Version};
|
||||
use actix_http::test::TestRequest as HttpTestRequest;
|
||||
use actix_http::{cookie::Cookie, ws, Extensions, HttpService, Request};
|
||||
use actix_router::{Path, ResourceDef, Url};
|
||||
use actix_rt::{time::delay_for, System};
|
||||
use actix_rt::{time::sleep, System};
|
||||
use actix_service::{
|
||||
map_config, IntoService, IntoServiceFactory, Service, ServiceFactory,
|
||||
};
|
||||
@ -37,16 +37,14 @@ use crate::{Error, HttpRequest, HttpResponse};
|
||||
|
||||
/// Create service that always responds with `HttpResponse::Ok()`
|
||||
pub fn ok_service(
|
||||
) -> impl Service<Request = ServiceRequest, Response = ServiceResponse<Body>, Error = Error>
|
||||
{
|
||||
) -> impl Service<ServiceRequest, Response = ServiceResponse<Body>, Error = Error> {
|
||||
default_service(StatusCode::OK)
|
||||
}
|
||||
|
||||
/// Create service that responds with response with specified status code
|
||||
pub fn default_service(
|
||||
status_code: StatusCode,
|
||||
) -> impl Service<Request = ServiceRequest, Response = ServiceResponse<Body>, Error = Error>
|
||||
{
|
||||
) -> impl Service<ServiceRequest, Response = ServiceResponse<Body>, Error = Error> {
|
||||
(move |req: ServiceRequest| {
|
||||
ok(req.into_response(HttpResponse::build(status_code).finish()))
|
||||
})
|
||||
@ -77,12 +75,12 @@ pub fn default_service(
|
||||
/// ```
|
||||
pub async fn init_service<R, S, B, E>(
|
||||
app: R,
|
||||
) -> impl Service<Request = Request, Response = ServiceResponse<B>, Error = E>
|
||||
) -> impl Service<Request, Response = ServiceResponse<B>, Error = E>
|
||||
where
|
||||
R: IntoServiceFactory<S>,
|
||||
R: IntoServiceFactory<S, Request>,
|
||||
S: ServiceFactory<
|
||||
Request,
|
||||
Config = AppConfig,
|
||||
Request = Request,
|
||||
Response = ServiceResponse<B>,
|
||||
Error = E,
|
||||
>,
|
||||
@ -96,15 +94,12 @@ where
|
||||
/// Fallible version of init_service that allows testing data factory errors.
|
||||
pub(crate) async fn try_init_service<R, S, B, E>(
|
||||
app: R,
|
||||
) -> Result<
|
||||
impl Service<Request = Request, Response = ServiceResponse<B>, Error = E>,
|
||||
S::InitError,
|
||||
>
|
||||
) -> Result<impl Service<Request, Response = ServiceResponse<B>, Error = E>, S::InitError>
|
||||
where
|
||||
R: IntoServiceFactory<S>,
|
||||
R: IntoServiceFactory<S, Request>,
|
||||
S: ServiceFactory<
|
||||
Request,
|
||||
Config = AppConfig,
|
||||
Request = Request,
|
||||
Response = ServiceResponse<B>,
|
||||
Error = E,
|
||||
>,
|
||||
@ -138,7 +133,7 @@ where
|
||||
/// ```
|
||||
pub async fn call_service<S, R, B, E>(app: &mut S, req: R) -> S::Response
|
||||
where
|
||||
S: Service<Request = R, Response = ServiceResponse<B>, Error = E>,
|
||||
S: Service<R, Response = ServiceResponse<B>, Error = E>,
|
||||
E: std::fmt::Debug,
|
||||
{
|
||||
app.call(req).await.unwrap()
|
||||
@ -171,7 +166,7 @@ where
|
||||
/// ```
|
||||
pub async fn read_response<S, B>(app: &mut S, req: Request) -> Bytes
|
||||
where
|
||||
S: Service<Request = Request, Response = ServiceResponse<B>, Error = Error>,
|
||||
S: Service<Request, Response = ServiceResponse<B>, Error = Error>,
|
||||
B: MessageBody + Unpin,
|
||||
{
|
||||
let mut resp = app
|
||||
@ -321,7 +316,7 @@ where
|
||||
/// ```
|
||||
pub async fn read_response_json<S, B, T>(app: &mut S, req: Request) -> T
|
||||
where
|
||||
S: Service<Request = Request, Response = ServiceResponse<B>, Error = Error>,
|
||||
S: Service<Request, Response = ServiceResponse<B>, Error = Error>,
|
||||
B: MessageBody + Unpin,
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
@ -602,7 +597,7 @@ impl TestRequest {
|
||||
/// Complete request creation, calls service and waits for response future completion.
|
||||
pub async fn send_request<S, B, E>(self, app: &mut S) -> S::Response
|
||||
where
|
||||
S: Service<Request = Request, Response = ServiceResponse<B>, Error = E>,
|
||||
S: Service<Request, Response = ServiceResponse<B>, Error = E>,
|
||||
E: std::fmt::Debug,
|
||||
{
|
||||
let req = self.to_request();
|
||||
@ -639,12 +634,12 @@ impl TestRequest {
|
||||
pub fn start<F, I, S, B>(factory: F) -> TestServer
|
||||
where
|
||||
F: Fn() -> I + Send + Clone + 'static,
|
||||
I: IntoServiceFactory<S>,
|
||||
S: ServiceFactory<Config = AppConfig, Request = Request> + 'static,
|
||||
I: IntoServiceFactory<S, Request>,
|
||||
S: ServiceFactory<Request, Config = AppConfig> + 'static,
|
||||
S::Error: Into<Error> + 'static,
|
||||
S::InitError: fmt::Debug,
|
||||
S::Response: Into<HttpResponse<B>> + 'static,
|
||||
<S::Service as Service>::Future: 'static,
|
||||
<S::Service as Service<Request>>::Future: 'static,
|
||||
B: MessageBody + 'static,
|
||||
{
|
||||
start_with(TestServerConfig::default(), factory)
|
||||
@ -678,12 +673,12 @@ where
|
||||
pub fn start_with<F, I, S, B>(cfg: TestServerConfig, factory: F) -> TestServer
|
||||
where
|
||||
F: Fn() -> I + Send + Clone + 'static,
|
||||
I: IntoServiceFactory<S>,
|
||||
S: ServiceFactory<Config = AppConfig, Request = Request> + 'static,
|
||||
I: IntoServiceFactory<S, Request>,
|
||||
S: ServiceFactory<Request, Config = AppConfig> + 'static,
|
||||
S::Error: Into<Error> + 'static,
|
||||
S::InitError: fmt::Debug,
|
||||
S::Response: Into<HttpResponse<B>> + 'static,
|
||||
<S::Service as Service>::Future: 'static,
|
||||
<S::Service as Service<Request>>::Future: 'static,
|
||||
B: MessageBody + 'static,
|
||||
{
|
||||
let (tx, rx) = mpsc::channel();
|
||||
@ -788,10 +783,13 @@ where
|
||||
}),
|
||||
},
|
||||
}
|
||||
.unwrap()
|
||||
.start();
|
||||
.unwrap();
|
||||
|
||||
sys.block_on(async {
|
||||
let srv = srv.start();
|
||||
tx.send((System::current(), srv, local_addr)).unwrap();
|
||||
});
|
||||
|
||||
tx.send((System::current(), srv, local_addr)).unwrap();
|
||||
sys.run()
|
||||
});
|
||||
|
||||
@ -1022,7 +1020,7 @@ impl TestServer {
|
||||
pub async fn stop(self) {
|
||||
self.server.stop(true).await;
|
||||
self.system.stop();
|
||||
delay_for(time::Duration::from_millis(100)).await;
|
||||
sleep(time::Duration::from_millis(100)).await;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -367,7 +367,7 @@ where
|
||||
let json = if let Ok(Some(mime)) = req.mime_type() {
|
||||
mime.subtype() == mime::JSON
|
||||
|| mime.suffix() == Some(mime::JSON)
|
||||
|| ctype.as_ref().map_or(false, |predicate| predicate(mime))
|
||||
|| ctype.map_or(false, |predicate| predicate(mime))
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
@ -5,7 +5,6 @@ use std::future::Future;
|
||||
|
||||
pub use actix_http::Response as HttpResponse;
|
||||
pub use bytes::{Buf, BufMut, Bytes, BytesMut};
|
||||
pub use futures_channel::oneshot::Canceled;
|
||||
|
||||
use crate::error::BlockingError;
|
||||
use crate::extract::FromRequest;
|
||||
|
Reference in New Issue
Block a user