1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-02 21:39:26 +00:00

simplify Payload extractor

This commit is contained in:
Nikolay Kim 2019-03-24 17:00:59 -07:00
parent ed322c175e
commit e37e81af0b
6 changed files with 51 additions and 71 deletions

View file

@ -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"

View file

@ -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;

View file

@ -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<C: StaticFileConfig> NamedFile<C> {
///
/// # 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<C: StaticFileConfig> NamedFile<C> {
///
/// # 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<C: StaticFileConfig> NamedFile<C> {
///
/// # 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")?;

View file

@ -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"

View file

@ -26,11 +26,12 @@ impl StreamHandler<ws::Message, ws::ProtocolError> 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();

View file

@ -23,9 +23,7 @@ use crate::service::ServiceFromRequest;
/// use actix_web::{web, error, App, Error, HttpResponse};
///
/// /// extract binary data from request
/// fn index<P>(body: web::Payload<P>) -> impl Future<Item = HttpResponse, Error = Error>
/// where
/// P: Stream<Item = web::Bytes, Error = error::PayloadError>
/// fn index(body: web::Payload) -> impl Future<Item = HttpResponse, Error = Error>
/// {
/// body.map_err(Error::from)
/// .fold(web::BytesMut::new(), move |mut body, chunk| {
@ -45,12 +43,9 @@ use crate::service::ServiceFromRequest;
/// );
/// }
/// ```
pub struct Payload<T>(crate::dev::Payload<T>);
pub struct Payload(crate::dev::Payload<Box<Stream<Item = Bytes, Error = PayloadError>>>);
impl<T> Stream for Payload<T>
where
T: Stream<Item = Bytes, Error = PayloadError>,
{
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<P>(body: web::Payload<P>) -> impl Future<Item = HttpResponse, Error = Error>
/// where
/// P: Stream<Item = web::Bytes, Error = error::PayloadError>
/// fn index(body: web::Payload) -> impl Future<Item = HttpResponse, Error = Error>
/// {
/// body.map_err(Error::from)
/// .fold(web::BytesMut::new(), move |mut body, chunk| {
@ -91,16 +84,26 @@ where
/// );
/// }
/// ```
impl<P> FromRequest<P> for Payload<P>
impl<P> FromRequest<P> for Payload
where
P: Stream<Item = Bytes, Error = PayloadError>,
P: Stream<Item = Bytes, Error = PayloadError> + 'static,
{
type Error = Error;
type Future = Result<Payload<P>, Error>;
type Future = Result<Payload, Error>;
#[inline]
fn from_request(req: &mut ServiceFromRequest<P>) -> Self::Future {
Ok(Payload(req.take_payload()))
let pl = match req.take_payload() {
crate::dev::Payload::Stream(s) => {
let pl: Box<dyn Stream<Item = Bytes, Error = PayloadError>> =
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))
}
}