From e37e81af0b80e9a90ae49950b6e45cac998340dd Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sun, 24 Mar 2019 17:00:59 -0700 Subject: [PATCH] simplify Payload extractor --- actix-files/Cargo.toml | 8 -------- actix-files/src/config.rs | 31 +++++++++++++---------------- actix-files/src/named.rs | 29 +++++++++++---------------- actix-session/Cargo.toml | 10 +--------- actix-web-actors/tests/test_ws.rs | 11 ++++++----- src/types/payload.rs | 33 +++++++++++++++++-------------- 6 files changed, 51 insertions(+), 71 deletions(-) diff --git a/actix-files/Cargo.toml b/actix-files/Cargo.toml index c0f38b9a6..aa93ac225 100644 --- a/actix-files/Cargo.toml +++ b/actix-files/Cargo.toml @@ -32,12 +32,4 @@ percent-encoding = "1.0" v_htmlescape = "0.4" [dev-dependencies] -actix-rt = "0.1.0" -#actix-server = { version="0.2", features=["ssl"] } actix-web = { path="..", features=["ssl"] } -actix-server = { git = "https://github.com/actix/actix-net.git", features=["ssl"] } -actix-http = { git = "https://github.com/actix/actix-http.git", features=["ssl"] } -actix-http-test = { git = "https://github.com/actix/actix-http.git", features=["ssl"] } -rand = "0.6" -env_logger = "0.6" -serde_derive = "1.0" diff --git a/actix-files/src/config.rs b/actix-files/src/config.rs index da72da201..7ad65ae79 100644 --- a/actix-files/src/config.rs +++ b/actix-files/src/config.rs @@ -1,5 +1,4 @@ -use actix_http::http::header::DispositionType; -use actix_web::http::Method; +use actix_web::http::{header::DispositionType, Method}; use mime; /// Describes `StaticFiles` configiration @@ -11,11 +10,9 @@ use mime; /// /// ## Example /// -/// ```rust,ignore -/// extern crate mime; -/// extern crate actix_web; +/// ```rust /// use actix_web::http::header::DispositionType; -/// use actix_web::fs::{StaticFileConfig, NamedFile}; +/// use actix_files::{StaticFileConfig, NamedFile}; /// /// #[derive(Default)] /// struct MyConfig; @@ -29,10 +26,10 @@ use mime; /// let file = NamedFile::open_with_config("foo.txt", MyConfig); /// ``` pub trait StaticFileConfig: Default { - ///Describes mapping for mime type to content disposition header + /// Describes mapping for mime type to content disposition header /// - ///By default `IMAGE`, `TEXT` and `VIDEO` are mapped to Inline. - ///Others are mapped to Attachment + /// By default `IMAGE`, `TEXT` and `VIDEO` are mapped to Inline. + /// Others are mapped to Attachment fn content_disposition_map(typ: mime::Name) -> DispositionType { match typ { mime::IMAGE | mime::TEXT | mime::VIDEO => DispositionType::Inline, @@ -40,30 +37,30 @@ pub trait StaticFileConfig: Default { } } - ///Describes whether Actix should attempt to calculate `ETag` + /// Describes whether Actix should attempt to calculate `ETag` /// - ///Defaults to `true` + /// Defaults to `true` fn is_use_etag() -> bool { true } - ///Describes whether Actix should use last modified date of file. + /// Describes whether Actix should use last modified date of file. /// - ///Defaults to `true` + /// Defaults to `true` fn is_use_last_modifier() -> bool { true } - ///Describes allowed methods to access static resources. + /// Describes allowed methods to access static resources. /// - ///By default all methods are allowed + /// By default all methods are allowed fn is_method_allowed(_method: &Method) -> bool { true } } -///Default content disposition as described in -///[StaticFileConfig](trait.StaticFileConfig.html) +/// Default content disposition as described in +/// [StaticFileConfig](trait.StaticFileConfig.html) #[derive(Default)] pub struct DefaultConfig; diff --git a/actix-files/src/named.rs b/actix-files/src/named.rs index 6372a183c..2bfa30675 100644 --- a/actix-files/src/named.rs +++ b/actix-files/src/named.rs @@ -11,10 +11,9 @@ use std::os::unix::fs::MetadataExt; use mime; use mime_guess::guess_mime_type; -use actix_http::error::Error; -use actix_http::http::header::{self, ContentDisposition, DispositionParam}; +use actix_web::http::header::{self, ContentDisposition, DispositionParam}; use actix_web::http::{ContentEncoding, Method, StatusCode}; -use actix_web::{HttpMessage, HttpRequest, HttpResponse, Responder}; +use actix_web::{Error, HttpMessage, HttpRequest, HttpResponse, Responder}; use crate::config::{DefaultConfig, StaticFileConfig}; use crate::range::HttpRange; @@ -42,10 +41,8 @@ impl NamedFile { /// /// # Examples /// - /// ```rust,ignore - /// extern crate actix_web; - /// - /// use actix_web::fs::NamedFile; + /// ```rust + /// use actix_files::NamedFile; /// use std::io::{self, Write}; /// use std::env; /// use std::fs::File; @@ -65,8 +62,8 @@ impl NamedFile { /// /// # Examples /// - /// ```rust,ignore - /// use actix_web::fs::NamedFile; + /// ```rust + /// use actix_files::NamedFile; /// /// let file = NamedFile::open("foo.txt"); /// ``` @@ -83,10 +80,8 @@ impl NamedFile { /// /// # Examples /// - /// ```rust,ignore - /// extern crate actix_web; - /// - /// use actix_web::fs::{DefaultConfig, NamedFile}; + /// ```rust + /// use actix_files::{DefaultConfig, NamedFile}; /// use std::io::{self, Write}; /// use std::env; /// use std::fs::File; @@ -147,8 +142,8 @@ impl NamedFile { /// /// # Examples /// - /// ```rust,ignore - /// use actix_web::fs::{DefaultConfig, NamedFile}; + /// ```rust + /// use actix_files::{DefaultConfig, NamedFile}; /// /// let file = NamedFile::open_with_config("foo.txt", DefaultConfig); /// ``` @@ -169,9 +164,9 @@ impl NamedFile { /// /// # Examples /// - /// ```rust,ignore + /// ```rust /// # use std::io; - /// use actix_web::fs::NamedFile; + /// use actix_files::NamedFile; /// /// # fn path() -> io::Result<()> { /// let file = NamedFile::open("test.txt")?; diff --git a/actix-session/Cargo.toml b/actix-session/Cargo.toml index 421c6fc42..554f3d7fc 100644 --- a/actix-session/Cargo.toml +++ b/actix-session/Cargo.toml @@ -25,23 +25,15 @@ cookie-session = ["cookie/secure"] [dependencies] actix-web = { path=".." } -actix-codec = "0.1.1" actix-service = "0.3.3" -actix-utils = "0.3.3" -actix-http = { git = "https://github.com/actix/actix-http.git" } -actix-router = { git = "https://github.com/actix/actix-net.git" } -actix-server = { git = "https://github.com/actix/actix-net.git" } - bytes = "0.4" cookie = { version="0.11", features=["percent-encode"], optional=true } derive_more = "0.14" -encoding = "0.2" futures = "0.1" hashbrown = "0.1.8" -log = "0.4" serde = "1.0" serde_json = "1.0" time = "0.1" [dev-dependencies] -actix-rt = "0.2.0" +actix-rt = "0.2.1" diff --git a/actix-web-actors/tests/test_ws.rs b/actix-web-actors/tests/test_ws.rs index 202d562ca..687cf4314 100644 --- a/actix-web-actors/tests/test_ws.rs +++ b/actix-web-actors/tests/test_ws.rs @@ -26,11 +26,12 @@ impl StreamHandler for Ws { #[test] fn test_simple() { - let mut srv = TestServer::new(|| { - HttpService::new(App::new().service(web::resource("/").to( - |req: HttpRequest, stream: web::Payload<_>| ws::start(Ws, &req, stream), - ))) - }); + let mut srv = + TestServer::new(|| { + HttpService::new(App::new().service(web::resource("/").to( + |req: HttpRequest, stream: web::Payload| ws::start(Ws, &req, stream), + ))) + }); // client service let framed = srv.ws().unwrap(); diff --git a/src/types/payload.rs b/src/types/payload.rs index 402486b66..170b9c627 100644 --- a/src/types/payload.rs +++ b/src/types/payload.rs @@ -23,9 +23,7 @@ use crate::service::ServiceFromRequest; /// use actix_web::{web, error, App, Error, HttpResponse}; /// /// /// extract binary data from request -/// fn index

(body: web::Payload

) -> impl Future -/// where -/// P: Stream +/// fn index(body: web::Payload) -> impl Future /// { /// body.map_err(Error::from) /// .fold(web::BytesMut::new(), move |mut body, chunk| { @@ -45,12 +43,9 @@ use crate::service::ServiceFromRequest; /// ); /// } /// ``` -pub struct Payload(crate::dev::Payload); +pub struct Payload(crate::dev::Payload>>); -impl Stream for Payload -where - T: Stream, -{ +impl Stream for Payload { type Item = Bytes; type Error = PayloadError; @@ -69,9 +64,7 @@ where /// use actix_web::{web, error, App, Error, HttpResponse}; /// /// /// extract binary data from request -/// fn index

(body: web::Payload

) -> impl Future -/// where -/// P: Stream +/// fn index(body: web::Payload) -> impl Future /// { /// body.map_err(Error::from) /// .fold(web::BytesMut::new(), move |mut body, chunk| { @@ -91,16 +84,26 @@ where /// ); /// } /// ``` -impl

FromRequest

for Payload

+impl

FromRequest

for Payload where - P: Stream, + P: Stream + 'static, { type Error = Error; - type Future = Result, Error>; + type Future = Result; #[inline] fn from_request(req: &mut ServiceFromRequest

) -> Self::Future { - Ok(Payload(req.take_payload())) + let pl = match req.take_payload() { + crate::dev::Payload::Stream(s) => { + let pl: Box> = + Box::new(s); + crate::dev::Payload::Stream(pl) + } + crate::dev::Payload::None => crate::dev::Payload::None, + crate::dev::Payload::H1(pl) => crate::dev::Payload::H1(pl), + crate::dev::Payload::H2(pl) => crate::dev::Payload::H2(pl), + }; + Ok(Payload(pl)) } }