diff --git a/Cargo.toml b/Cargo.toml index 1bc3af3b2..0b4ad38a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -56,8 +56,8 @@ ssl = ["openssl", "actix-server/ssl"] [dependencies] actix-codec = "0.1.0" -actix-service = "0.3.0" -actix-utils = "0.3.0" +actix-service = "0.3.2" +actix-utils = "0.3.1" actix-rt = "0.1.0" actix-http = { git = "https://github.com/actix/actix-http.git" } @@ -99,3 +99,6 @@ serde_derive = "1.0" lto = true opt-level = 3 codegen-units = 1 + +[patch.crates-io] +actix-service = { git = "https://github.com/actix/actix-net.git" } diff --git a/src/app.rs b/src/app.rs index e1479080a..8336fcca3 100644 --- a/src/app.rs +++ b/src/app.rs @@ -7,8 +7,8 @@ use actix_http::{Extensions, PayloadStream, Request, Response}; use actix_router::{Path, ResourceDef, ResourceInfo, Router, Url}; use actix_service::boxed::{self, BoxedNewService, BoxedService}; use actix_service::{ - AndThenNewService, ApplyNewService, IntoNewService, IntoNewTransform, NewService, - NewTransform, Service, + AndThenNewService, ApplyTransform, IntoNewService, IntoTransform, NewService, + Service, Transform, }; use futures::future::{ok, Either, FutureResult}; use futures::{Async, Future, IntoFuture, Poll}; @@ -237,17 +237,17 @@ where >, > where - M: NewTransform< + M: Transform< AppRouting
, Request = ServiceRequest
,
Response = ServiceResponse,
Error = (),
InitError = (),
>,
- F: IntoNewTransform ,
Response = ServiceResponse ;
+ type Response = ServiceResponse , srv: &mut S) -> Self::Future {
+ fn call(&mut self, req: ServiceRequest ) -> Self::Future {
// negotiate content-encoding
let encoding = if let Some(val) = req.headers.get(ACCEPT_ENCODING) {
if let Ok(enc) = val.to_str() {
- AcceptEncoding::parse(enc, self.0)
+ AcceptEncoding::parse(enc, self.encoding)
} else {
ContentEncoding::Identity
}
@@ -66,7 +93,7 @@ where
CompressResponse {
encoding,
- fut: srv.call(req),
+ fut: self.service.call(req),
}
}
}
@@ -102,18 +129,6 @@ where
}
}
-impl ;
+ type Response = ServiceResponse;
+ type Error = S::Error;
+ type InitError = ();
+ type Transform = DefaultHeadersMiddleware ;
type Response = ServiceResponse;
type Error = S::Error;
type Future = Box ) -> Self::Future {
let inner = self.inner.clone();
- Box::new(srv.call(req).map(move |mut res| {
+ Box::new(self.service.call(req).map(move |mut res| {
// set response headers
for (key, value) in inner.headers.iter() {
if !res.headers().contains_key(key) {
@@ -143,32 +157,44 @@ mod tests {
#[test]
fn test_default_headers() {
- let mut mw = DefaultHeaders::new().header(CONTENT_TYPE, "0001");
- let mut srv = FnService::new(|req: ServiceRequest<_>| {
+ let srv = FnService::new(|req: ServiceRequest<_>| {
req.into_response(HttpResponse::Ok().finish())
});
+ let mut mw = block_on(
+ DefaultHeaders::new()
+ .header(CONTENT_TYPE, "0001")
+ .new_transform(srv),
+ )
+ .unwrap();
let req = TestRequest::default().to_service();
- let resp = block_on(mw.call(req, &mut srv)).unwrap();
+ let resp = block_on(mw.call(req)).unwrap();
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "0001");
let req = TestRequest::default().to_service();
- let mut srv = FnService::new(|req: ServiceRequest<_>| {
+ let srv = FnService::new(|req: ServiceRequest<_>| {
req.into_response(HttpResponse::Ok().header(CONTENT_TYPE, "0002").finish())
});
- let resp = block_on(mw.call(req, &mut srv)).unwrap();
+ let mut mw = block_on(
+ DefaultHeaders::new()
+ .header(CONTENT_TYPE, "0001")
+ .new_transform(srv),
+ )
+ .unwrap();
+ let resp = block_on(mw.call(req)).unwrap();
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "0002");
}
#[test]
fn test_content_type() {
- let mut mw = DefaultHeaders::new().content_type();
- let mut srv = FnService::new(|req: ServiceRequest<_>| {
+ let srv = FnService::new(|req: ServiceRequest<_>| {
req.into_response(HttpResponse::Ok().finish())
});
+ let mut mw =
+ block_on(DefaultHeaders::new().content_type().new_transform(srv)).unwrap();
let req = TestRequest::default().to_service();
- let resp = block_on(mw.call(req, &mut srv)).unwrap();
+ let resp = block_on(mw.call(req)).unwrap();
assert_eq!(
resp.headers().get(CONTENT_TYPE).unwrap(),
"application/octet-stream"
diff --git a/src/middleware/mod.rs b/src/middleware/mod.rs
index 85127ee28..fc9923029 100644
--- a/src/middleware/mod.rs
+++ b/src/middleware/mod.rs
@@ -1,8 +1,3 @@
-use std::marker::PhantomData;
-
-use actix_service::{NewTransform, Service, Transform};
-use futures::future::{ok, FutureResult};
-
#[cfg(any(feature = "brotli", feature = "flate2"))]
mod compress;
#[cfg(any(feature = "brotli", feature = "flate2"))]
@@ -10,43 +5,3 @@ pub use self::compress::Compress;
mod defaultheaders;
pub use self::defaultheaders::DefaultHeaders;
-
-/// Helper for middleware service factory
-pub struct MiddlewareFactory ,
Response = ServiceResponse,
Error = (),
InitError = (),
>,
- F: IntoNewTransform ,
Response = ServiceResponse,
Error = (),
InitError = (),
>,
- F: IntoNewTransform Transform for Compress
+where
+ P: 'static,
+ B: MessageBody,
+ S: Service;
+ type Future = FutureResult {
+ service: S,
+ encoding: ContentEncoding,
+}
+
+impl Service for CompressMiddleware
where
P: 'static,
B: MessageBody,
@@ -49,14 +76,14 @@ where
type Future = CompressResponse;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
- Ok(Async::Ready(()))
+ self.service.poll_ready()
}
- fn call(&mut self, req: ServiceRequest IntoNewTransform IntoNewTransform Transform for DefaultHeaders
where
- S: Service;
+ type Future = FutureResult Transform for DefaultHeaders
+pub struct DefaultHeadersMiddleware {
+ service: S,
+ inner: Rc Service for DefaultHeadersMiddleware
where
- S: Service + Clone,
- S: Service,
-{
- tr: T,
- _t: PhantomData,
-}
-
-impl + Clone,
- S: Service,
-{
- pub fn new(tr: T) -> Self {
- MiddlewareFactory {
- tr,
- _t: PhantomData,
- }
- }
-}
-
-impl for MiddlewareFactory + Clone,
- S: Service,
-{
- type Request = T::Request;
- type Response = T::Response;
- type Error = T::Error;
- type Transform = T;
- type InitError = ();
- type Future = FutureResult