mirror of
https://github.com/fafhrd91/actix-web
synced 2025-01-18 05:41:50 +01:00
simplify Payload extractor
This commit is contained in:
parent
ed322c175e
commit
e37e81af0b
@ -32,12 +32,4 @@ percent-encoding = "1.0"
|
|||||||
v_htmlescape = "0.4"
|
v_htmlescape = "0.4"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-rt = "0.1.0"
|
|
||||||
#actix-server = { version="0.2", features=["ssl"] }
|
|
||||||
actix-web = { path="..", 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"
|
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
use actix_http::http::header::DispositionType;
|
use actix_web::http::{header::DispositionType, Method};
|
||||||
use actix_web::http::Method;
|
|
||||||
use mime;
|
use mime;
|
||||||
|
|
||||||
/// Describes `StaticFiles` configiration
|
/// Describes `StaticFiles` configiration
|
||||||
@ -11,11 +10,9 @@ use mime;
|
|||||||
///
|
///
|
||||||
/// ## Example
|
/// ## Example
|
||||||
///
|
///
|
||||||
/// ```rust,ignore
|
/// ```rust
|
||||||
/// extern crate mime;
|
|
||||||
/// extern crate actix_web;
|
|
||||||
/// use actix_web::http::header::DispositionType;
|
/// use actix_web::http::header::DispositionType;
|
||||||
/// use actix_web::fs::{StaticFileConfig, NamedFile};
|
/// use actix_files::{StaticFileConfig, NamedFile};
|
||||||
///
|
///
|
||||||
/// #[derive(Default)]
|
/// #[derive(Default)]
|
||||||
/// struct MyConfig;
|
/// struct MyConfig;
|
||||||
|
@ -11,10 +11,9 @@ use std::os::unix::fs::MetadataExt;
|
|||||||
use mime;
|
use mime;
|
||||||
use mime_guess::guess_mime_type;
|
use mime_guess::guess_mime_type;
|
||||||
|
|
||||||
use actix_http::error::Error;
|
use actix_web::http::header::{self, ContentDisposition, DispositionParam};
|
||||||
use actix_http::http::header::{self, ContentDisposition, DispositionParam};
|
|
||||||
use actix_web::http::{ContentEncoding, Method, StatusCode};
|
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::config::{DefaultConfig, StaticFileConfig};
|
||||||
use crate::range::HttpRange;
|
use crate::range::HttpRange;
|
||||||
@ -42,10 +41,8 @@ impl NamedFile {
|
|||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```rust,ignore
|
/// ```rust
|
||||||
/// extern crate actix_web;
|
/// use actix_files::NamedFile;
|
||||||
///
|
|
||||||
/// use actix_web::fs::NamedFile;
|
|
||||||
/// use std::io::{self, Write};
|
/// use std::io::{self, Write};
|
||||||
/// use std::env;
|
/// use std::env;
|
||||||
/// use std::fs::File;
|
/// use std::fs::File;
|
||||||
@ -65,8 +62,8 @@ impl NamedFile {
|
|||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```rust,ignore
|
/// ```rust
|
||||||
/// use actix_web::fs::NamedFile;
|
/// use actix_files::NamedFile;
|
||||||
///
|
///
|
||||||
/// let file = NamedFile::open("foo.txt");
|
/// let file = NamedFile::open("foo.txt");
|
||||||
/// ```
|
/// ```
|
||||||
@ -83,10 +80,8 @@ impl<C: StaticFileConfig> NamedFile<C> {
|
|||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```rust,ignore
|
/// ```rust
|
||||||
/// extern crate actix_web;
|
/// use actix_files::{DefaultConfig, NamedFile};
|
||||||
///
|
|
||||||
/// use actix_web::fs::{DefaultConfig, NamedFile};
|
|
||||||
/// use std::io::{self, Write};
|
/// use std::io::{self, Write};
|
||||||
/// use std::env;
|
/// use std::env;
|
||||||
/// use std::fs::File;
|
/// use std::fs::File;
|
||||||
@ -147,8 +142,8 @@ impl<C: StaticFileConfig> NamedFile<C> {
|
|||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```rust,ignore
|
/// ```rust
|
||||||
/// use actix_web::fs::{DefaultConfig, NamedFile};
|
/// use actix_files::{DefaultConfig, NamedFile};
|
||||||
///
|
///
|
||||||
/// let file = NamedFile::open_with_config("foo.txt", DefaultConfig);
|
/// let file = NamedFile::open_with_config("foo.txt", DefaultConfig);
|
||||||
/// ```
|
/// ```
|
||||||
@ -169,9 +164,9 @@ impl<C: StaticFileConfig> NamedFile<C> {
|
|||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```rust,ignore
|
/// ```rust
|
||||||
/// # use std::io;
|
/// # use std::io;
|
||||||
/// use actix_web::fs::NamedFile;
|
/// use actix_files::NamedFile;
|
||||||
///
|
///
|
||||||
/// # fn path() -> io::Result<()> {
|
/// # fn path() -> io::Result<()> {
|
||||||
/// let file = NamedFile::open("test.txt")?;
|
/// let file = NamedFile::open("test.txt")?;
|
||||||
|
@ -25,23 +25,15 @@ cookie-session = ["cookie/secure"]
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = { path=".." }
|
actix-web = { path=".." }
|
||||||
actix-codec = "0.1.1"
|
|
||||||
actix-service = "0.3.3"
|
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"
|
bytes = "0.4"
|
||||||
cookie = { version="0.11", features=["percent-encode"], optional=true }
|
cookie = { version="0.11", features=["percent-encode"], optional=true }
|
||||||
derive_more = "0.14"
|
derive_more = "0.14"
|
||||||
encoding = "0.2"
|
|
||||||
futures = "0.1"
|
futures = "0.1"
|
||||||
hashbrown = "0.1.8"
|
hashbrown = "0.1.8"
|
||||||
log = "0.4"
|
|
||||||
serde = "1.0"
|
serde = "1.0"
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
time = "0.1"
|
time = "0.1"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-rt = "0.2.0"
|
actix-rt = "0.2.1"
|
||||||
|
@ -26,9 +26,10 @@ impl StreamHandler<ws::Message, ws::ProtocolError> for Ws {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_simple() {
|
fn test_simple() {
|
||||||
let mut srv = TestServer::new(|| {
|
let mut srv =
|
||||||
|
TestServer::new(|| {
|
||||||
HttpService::new(App::new().service(web::resource("/").to(
|
HttpService::new(App::new().service(web::resource("/").to(
|
||||||
|req: HttpRequest, stream: web::Payload<_>| ws::start(Ws, &req, stream),
|
|req: HttpRequest, stream: web::Payload| ws::start(Ws, &req, stream),
|
||||||
)))
|
)))
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -23,9 +23,7 @@ use crate::service::ServiceFromRequest;
|
|||||||
/// use actix_web::{web, error, App, Error, HttpResponse};
|
/// use actix_web::{web, error, App, Error, HttpResponse};
|
||||||
///
|
///
|
||||||
/// /// extract binary data from request
|
/// /// extract binary data from request
|
||||||
/// fn index<P>(body: web::Payload<P>) -> impl Future<Item = HttpResponse, Error = Error>
|
/// fn index(body: web::Payload) -> impl Future<Item = HttpResponse, Error = Error>
|
||||||
/// where
|
|
||||||
/// P: Stream<Item = web::Bytes, Error = error::PayloadError>
|
|
||||||
/// {
|
/// {
|
||||||
/// body.map_err(Error::from)
|
/// body.map_err(Error::from)
|
||||||
/// .fold(web::BytesMut::new(), move |mut body, chunk| {
|
/// .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>
|
impl Stream for Payload {
|
||||||
where
|
|
||||||
T: Stream<Item = Bytes, Error = PayloadError>,
|
|
||||||
{
|
|
||||||
type Item = Bytes;
|
type Item = Bytes;
|
||||||
type Error = PayloadError;
|
type Error = PayloadError;
|
||||||
|
|
||||||
@ -69,9 +64,7 @@ where
|
|||||||
/// use actix_web::{web, error, App, Error, HttpResponse};
|
/// use actix_web::{web, error, App, Error, HttpResponse};
|
||||||
///
|
///
|
||||||
/// /// extract binary data from request
|
/// /// extract binary data from request
|
||||||
/// fn index<P>(body: web::Payload<P>) -> impl Future<Item = HttpResponse, Error = Error>
|
/// fn index(body: web::Payload) -> impl Future<Item = HttpResponse, Error = Error>
|
||||||
/// where
|
|
||||||
/// P: Stream<Item = web::Bytes, Error = error::PayloadError>
|
|
||||||
/// {
|
/// {
|
||||||
/// body.map_err(Error::from)
|
/// body.map_err(Error::from)
|
||||||
/// .fold(web::BytesMut::new(), move |mut body, chunk| {
|
/// .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
|
where
|
||||||
P: Stream<Item = Bytes, Error = PayloadError>,
|
P: Stream<Item = Bytes, Error = PayloadError> + 'static,
|
||||||
{
|
{
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = Result<Payload<P>, Error>;
|
type Future = Result<Payload, Error>;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from_request(req: &mut ServiceFromRequest<P>) -> Self::Future {
|
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))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user