mirror of
https://github.com/fafhrd91/actix-web
synced 2025-09-01 01:16:59 +02:00
add rustfmt config
This commit is contained in:
@@ -1,45 +1,45 @@
|
||||
use std::str;
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use futures::{Future, Stream, Poll};
|
||||
use http_range::HttpRange;
|
||||
use serde::de::DeserializeOwned;
|
||||
use mime::Mime;
|
||||
use serde_urlencoded;
|
||||
use encoding::all::UTF_8;
|
||||
use encoding::EncodingRef;
|
||||
use encoding::types::{Encoding, DecoderTrap};
|
||||
use encoding::all::UTF_8;
|
||||
use encoding::label::encoding_from_whatwg_label;
|
||||
use encoding::types::{DecoderTrap, Encoding};
|
||||
use futures::{Future, Poll, Stream};
|
||||
use http::{header, HeaderMap};
|
||||
use http_range::HttpRange;
|
||||
use mime::Mime;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde_urlencoded;
|
||||
use std::str;
|
||||
|
||||
use json::JsonBody;
|
||||
use error::{ContentTypeError, HttpRangeError, ParseError, PayloadError, UrlencodedError};
|
||||
use header::Header;
|
||||
use json::JsonBody;
|
||||
use multipart::Multipart;
|
||||
use error::{ParseError, ContentTypeError,
|
||||
HttpRangeError, PayloadError, UrlencodedError};
|
||||
|
||||
|
||||
/// Trait that implements general purpose operations on http messages
|
||||
pub trait HttpMessage {
|
||||
|
||||
/// Read the message headers.
|
||||
fn headers(&self) -> &HeaderMap;
|
||||
|
||||
#[doc(hidden)]
|
||||
/// Get a header
|
||||
fn get_header<H: Header>(&self) -> Option<H> where Self: Sized {
|
||||
fn get_header<H: Header>(&self) -> Option<H>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
if self.headers().contains_key(H::name()) {
|
||||
H::parse(self).ok()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Read the request content type. If request does not contain
|
||||
/// *Content-Type* header, empty str get returned.
|
||||
fn content_type(&self) -> &str {
|
||||
if let Some(content_type) = self.headers().get(header::CONTENT_TYPE) {
|
||||
if let Ok(content_type) = content_type.to_str() {
|
||||
return content_type.split(';').next().unwrap().trim()
|
||||
return content_type.split(';').next().unwrap().trim();
|
||||
}
|
||||
}
|
||||
""
|
||||
@@ -73,7 +73,7 @@ pub trait HttpMessage {
|
||||
Err(_) => Err(ContentTypeError::ParseError),
|
||||
};
|
||||
} else {
|
||||
return Err(ContentTypeError::ParseError)
|
||||
return Err(ContentTypeError::ParseError);
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
@@ -96,8 +96,10 @@ pub trait HttpMessage {
|
||||
/// `size` is full size of response (file).
|
||||
fn range(&self, size: u64) -> Result<Vec<HttpRange>, HttpRangeError> {
|
||||
if let Some(range) = self.headers().get(header::RANGE) {
|
||||
HttpRange::parse(unsafe{str::from_utf8_unchecked(range.as_bytes())}, size)
|
||||
.map_err(|e| e.into())
|
||||
HttpRange::parse(
|
||||
unsafe { str::from_utf8_unchecked(range.as_bytes()) },
|
||||
size,
|
||||
).map_err(|e| e.into())
|
||||
} else {
|
||||
Ok(Vec::new())
|
||||
}
|
||||
@@ -105,8 +107,9 @@ pub trait HttpMessage {
|
||||
|
||||
/// Load http message body.
|
||||
///
|
||||
/// By default only 256Kb payload reads to a memory, then `PayloadError::Overflow`
|
||||
/// get returned. Use `MessageBody::limit()` method to change upper limit.
|
||||
/// By default only 256Kb payload reads to a memory, then
|
||||
/// `PayloadError::Overflow` get returned. Use `MessageBody::limit()`
|
||||
/// method to change upper limit.
|
||||
///
|
||||
/// ## Server example
|
||||
///
|
||||
@@ -131,14 +134,15 @@ pub trait HttpMessage {
|
||||
/// # fn main() {}
|
||||
/// ```
|
||||
fn body(self) -> MessageBody<Self>
|
||||
where Self: Stream<Item=Bytes, Error=PayloadError> + Sized
|
||||
where
|
||||
Self: Stream<Item = Bytes, Error = PayloadError> + Sized,
|
||||
{
|
||||
MessageBody::new(self)
|
||||
}
|
||||
|
||||
/// Parse `application/x-www-form-urlencoded` encoded request's body.
|
||||
/// Return `UrlEncoded` future. Form can be deserialized to any type that implements
|
||||
/// `Deserialize` trait from *serde*.
|
||||
/// Return `UrlEncoded` future. Form can be deserialized to any type that
|
||||
/// implements `Deserialize` trait from *serde*.
|
||||
///
|
||||
/// Returns error:
|
||||
///
|
||||
@@ -167,7 +171,8 @@ pub trait HttpMessage {
|
||||
/// # fn main() {}
|
||||
/// ```
|
||||
fn urlencoded<T: DeserializeOwned>(self) -> UrlEncoded<Self, T>
|
||||
where Self: Stream<Item=Bytes, Error=PayloadError> + Sized
|
||||
where
|
||||
Self: Stream<Item = Bytes, Error = PayloadError> + Sized,
|
||||
{
|
||||
UrlEncoded::new(self)
|
||||
}
|
||||
@@ -205,7 +210,8 @@ pub trait HttpMessage {
|
||||
/// # fn main() {}
|
||||
/// ```
|
||||
fn json<T: DeserializeOwned>(self) -> JsonBody<Self, T>
|
||||
where Self: Stream<Item=Bytes, Error=PayloadError> + Sized
|
||||
where
|
||||
Self: Stream<Item = Bytes, Error = PayloadError> + Sized,
|
||||
{
|
||||
JsonBody::new(self)
|
||||
}
|
||||
@@ -247,7 +253,8 @@ pub trait HttpMessage {
|
||||
/// # fn main() {}
|
||||
/// ```
|
||||
fn multipart(self) -> Multipart<Self>
|
||||
where Self: Stream<Item=Bytes, Error=PayloadError> + Sized
|
||||
where
|
||||
Self: Stream<Item = Bytes, Error = PayloadError> + Sized,
|
||||
{
|
||||
let boundary = Multipart::boundary(self.headers());
|
||||
Multipart::new(boundary, self)
|
||||
@@ -258,11 +265,10 @@ pub trait HttpMessage {
|
||||
pub struct MessageBody<T> {
|
||||
limit: usize,
|
||||
req: Option<T>,
|
||||
fut: Option<Box<Future<Item=Bytes, Error=PayloadError>>>,
|
||||
fut: Option<Box<Future<Item = Bytes, Error = PayloadError>>>,
|
||||
}
|
||||
|
||||
impl<T> MessageBody<T> {
|
||||
|
||||
/// Create `RequestBody` for request.
|
||||
pub fn new(req: T) -> MessageBody<T> {
|
||||
MessageBody {
|
||||
@@ -280,7 +286,8 @@ impl<T> MessageBody<T> {
|
||||
}
|
||||
|
||||
impl<T> Future for MessageBody<T>
|
||||
where T: HttpMessage + Stream<Item=Bytes, Error=PayloadError> + 'static
|
||||
where
|
||||
T: HttpMessage + Stream<Item = Bytes, Error = PayloadError> + 'static,
|
||||
{
|
||||
type Item = Bytes;
|
||||
type Error = PayloadError;
|
||||
@@ -313,11 +320,14 @@ impl<T> Future for MessageBody<T>
|
||||
Ok(body)
|
||||
}
|
||||
})
|
||||
.map(|body| body.freeze())
|
||||
.map(|body| body.freeze()),
|
||||
));
|
||||
}
|
||||
|
||||
self.fut.as_mut().expect("UrlEncoded could not be used second time").poll()
|
||||
self.fut
|
||||
.as_mut()
|
||||
.expect("UrlEncoded could not be used second time")
|
||||
.poll()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -325,7 +335,7 @@ impl<T> Future for MessageBody<T>
|
||||
pub struct UrlEncoded<T, U> {
|
||||
req: Option<T>,
|
||||
limit: usize,
|
||||
fut: Option<Box<Future<Item=U, Error=UrlencodedError>>>,
|
||||
fut: Option<Box<Future<Item = U, Error = UrlencodedError>>>,
|
||||
}
|
||||
|
||||
impl<T, U> UrlEncoded<T, U> {
|
||||
@@ -345,8 +355,9 @@ impl<T, U> UrlEncoded<T, U> {
|
||||
}
|
||||
|
||||
impl<T, U> Future for UrlEncoded<T, U>
|
||||
where T: HttpMessage + Stream<Item=Bytes, Error=PayloadError> + 'static,
|
||||
U: DeserializeOwned + 'static
|
||||
where
|
||||
T: HttpMessage + Stream<Item = Bytes, Error = PayloadError> + 'static,
|
||||
U: DeserializeOwned + 'static,
|
||||
{
|
||||
type Item = U;
|
||||
type Error = UrlencodedError;
|
||||
@@ -354,7 +365,7 @@ impl<T, U> Future for UrlEncoded<T, U>
|
||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||
if let Some(req) = self.req.take() {
|
||||
if req.chunked().unwrap_or(false) {
|
||||
return Err(UrlencodedError::Chunked)
|
||||
return Err(UrlencodedError::Chunked);
|
||||
} else if let Some(len) = req.headers().get(header::CONTENT_LENGTH) {
|
||||
if let Ok(s) = len.to_str() {
|
||||
if let Ok(len) = s.parse::<u64>() {
|
||||
@@ -362,18 +373,19 @@ impl<T, U> Future for UrlEncoded<T, U>
|
||||
return Err(UrlencodedError::Overflow);
|
||||
}
|
||||
} else {
|
||||
return Err(UrlencodedError::UnknownLength)
|
||||
return Err(UrlencodedError::UnknownLength);
|
||||
}
|
||||
} else {
|
||||
return Err(UrlencodedError::UnknownLength)
|
||||
return Err(UrlencodedError::UnknownLength);
|
||||
}
|
||||
}
|
||||
|
||||
// check content type
|
||||
if req.content_type().to_lowercase() != "application/x-www-form-urlencoded" {
|
||||
return Err(UrlencodedError::ContentType)
|
||||
return Err(UrlencodedError::ContentType);
|
||||
}
|
||||
let encoding = req.encoding().map_err(|_| UrlencodedError::ContentType)?;
|
||||
let encoding = req.encoding()
|
||||
.map_err(|_| UrlencodedError::ContentType)?;
|
||||
|
||||
// future
|
||||
let limit = self.limit;
|
||||
@@ -392,7 +404,8 @@ impl<T, U> Future for UrlEncoded<T, U>
|
||||
serde_urlencoded::from_bytes::<U>(&body)
|
||||
.map_err(|_| UrlencodedError::Parse)
|
||||
} else {
|
||||
let body = encoding.decode(&body, DecoderTrap::Strict)
|
||||
let body = encoding
|
||||
.decode(&body, DecoderTrap::Strict)
|
||||
.map_err(|_| UrlencodedError::Parse)?;
|
||||
serde_urlencoded::from_str::<U>(&body)
|
||||
.map_err(|_| UrlencodedError::Parse)
|
||||
@@ -401,19 +414,22 @@ impl<T, U> Future for UrlEncoded<T, U>
|
||||
self.fut = Some(Box::new(fut));
|
||||
}
|
||||
|
||||
self.fut.as_mut().expect("UrlEncoded could not be used second time").poll()
|
||||
self.fut
|
||||
.as_mut()
|
||||
.expect("UrlEncoded could not be used second time")
|
||||
.poll()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use mime;
|
||||
use encoding::Encoding;
|
||||
use encoding::all::ISO_8859_2;
|
||||
use futures::Async;
|
||||
use http::{Method, Version, Uri};
|
||||
use http::{Method, Uri, Version};
|
||||
use httprequest::HttpRequest;
|
||||
use mime;
|
||||
use std::str::FromStr;
|
||||
use test::TestRequest;
|
||||
|
||||
@@ -421,8 +437,9 @@ mod tests {
|
||||
fn test_content_type() {
|
||||
let req = TestRequest::with_header("content-type", "text/plain").finish();
|
||||
assert_eq!(req.content_type(), "text/plain");
|
||||
let req = TestRequest::with_header(
|
||||
"content-type", "application/json; charset=utf=8").finish();
|
||||
let req =
|
||||
TestRequest::with_header("content-type", "application/json; charset=utf=8")
|
||||
.finish();
|
||||
assert_eq!(req.content_type(), "application/json");
|
||||
let req = HttpRequest::default();
|
||||
assert_eq!(req.content_type(), "");
|
||||
@@ -434,8 +451,9 @@ mod tests {
|
||||
assert_eq!(req.mime_type().unwrap(), Some(mime::APPLICATION_JSON));
|
||||
let req = HttpRequest::default();
|
||||
assert_eq!(req.mime_type().unwrap(), None);
|
||||
let req = TestRequest::with_header(
|
||||
"content-type", "application/json; charset=utf-8").finish();
|
||||
let req =
|
||||
TestRequest::with_header("content-type", "application/json; charset=utf-8")
|
||||
.finish();
|
||||
let mt = req.mime_type().unwrap().unwrap();
|
||||
assert_eq!(mt.get_param(mime::CHARSET), Some(mime::UTF_8));
|
||||
assert_eq!(mt.type_(), mime::APPLICATION);
|
||||
@@ -445,7 +463,9 @@ mod tests {
|
||||
#[test]
|
||||
fn test_mime_type_error() {
|
||||
let req = TestRequest::with_header(
|
||||
"content-type", "applicationadfadsfasdflknadsfklnadsfjson").finish();
|
||||
"content-type",
|
||||
"applicationadfadsfasdflknadsfklnadsfjson",
|
||||
).finish();
|
||||
assert_eq!(Err(ContentTypeError::ParseError), req.mime_type());
|
||||
}
|
||||
|
||||
@@ -454,24 +474,32 @@ mod tests {
|
||||
let req = HttpRequest::default();
|
||||
assert_eq!(UTF_8.name(), req.encoding().unwrap().name());
|
||||
|
||||
let req = TestRequest::with_header(
|
||||
"content-type", "application/json").finish();
|
||||
let req = TestRequest::with_header("content-type", "application/json").finish();
|
||||
assert_eq!(UTF_8.name(), req.encoding().unwrap().name());
|
||||
|
||||
let req = TestRequest::with_header(
|
||||
"content-type", "application/json; charset=ISO-8859-2").finish();
|
||||
"content-type",
|
||||
"application/json; charset=ISO-8859-2",
|
||||
).finish();
|
||||
assert_eq!(ISO_8859_2.name(), req.encoding().unwrap().name());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_encoding_error() {
|
||||
let req = TestRequest::with_header(
|
||||
"content-type", "applicatjson").finish();
|
||||
assert_eq!(Some(ContentTypeError::ParseError), req.encoding().err());
|
||||
let req = TestRequest::with_header("content-type", "applicatjson").finish();
|
||||
assert_eq!(
|
||||
Some(ContentTypeError::ParseError),
|
||||
req.encoding().err()
|
||||
);
|
||||
|
||||
let req = TestRequest::with_header(
|
||||
"content-type", "application/json; charset=kkkttktk").finish();
|
||||
assert_eq!(Some(ContentTypeError::UnknownEncoding), req.encoding().err());
|
||||
"content-type",
|
||||
"application/json; charset=kkkttktk",
|
||||
).finish();
|
||||
assert_eq!(
|
||||
Some(ContentTypeError::UnknownEncoding),
|
||||
req.encoding().err()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -495,17 +523,26 @@ mod tests {
|
||||
let req = HttpRequest::default();
|
||||
assert!(!req.chunked().unwrap());
|
||||
|
||||
let req = TestRequest::with_header(header::TRANSFER_ENCODING, "chunked").finish();
|
||||
let req =
|
||||
TestRequest::with_header(header::TRANSFER_ENCODING, "chunked").finish();
|
||||
assert!(req.chunked().unwrap());
|
||||
|
||||
let mut headers = HeaderMap::new();
|
||||
let s = unsafe{str::from_utf8_unchecked(b"some va\xadscc\xacas0xsdasdlue".as_ref())};
|
||||
let s = unsafe {
|
||||
str::from_utf8_unchecked(b"some va\xadscc\xacas0xsdasdlue".as_ref())
|
||||
};
|
||||
|
||||
headers.insert(header::TRANSFER_ENCODING,
|
||||
header::HeaderValue::from_str(s).unwrap());
|
||||
headers.insert(
|
||||
header::TRANSFER_ENCODING,
|
||||
header::HeaderValue::from_str(s).unwrap(),
|
||||
);
|
||||
let req = HttpRequest::new(
|
||||
Method::GET, Uri::from_str("/").unwrap(),
|
||||
Version::HTTP_11, headers, None);
|
||||
Method::GET,
|
||||
Uri::from_str("/").unwrap(),
|
||||
Version::HTTP_11,
|
||||
headers,
|
||||
None,
|
||||
);
|
||||
assert!(req.chunked().is_err());
|
||||
}
|
||||
|
||||
@@ -540,51 +577,75 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_urlencoded_error() {
|
||||
let req = TestRequest::with_header(header::TRANSFER_ENCODING, "chunked").finish();
|
||||
assert_eq!(req.urlencoded::<Info>()
|
||||
.poll().err().unwrap(), UrlencodedError::Chunked);
|
||||
let req =
|
||||
TestRequest::with_header(header::TRANSFER_ENCODING, "chunked").finish();
|
||||
assert_eq!(
|
||||
req.urlencoded::<Info>().poll().err().unwrap(),
|
||||
UrlencodedError::Chunked
|
||||
);
|
||||
|
||||
let req = TestRequest::with_header(
|
||||
header::CONTENT_TYPE, "application/x-www-form-urlencoded")
|
||||
.header(header::CONTENT_LENGTH, "xxxx")
|
||||
header::CONTENT_TYPE,
|
||||
"application/x-www-form-urlencoded",
|
||||
).header(header::CONTENT_LENGTH, "xxxx")
|
||||
.finish();
|
||||
assert_eq!(req.urlencoded::<Info>()
|
||||
.poll().err().unwrap(), UrlencodedError::UnknownLength);
|
||||
assert_eq!(
|
||||
req.urlencoded::<Info>().poll().err().unwrap(),
|
||||
UrlencodedError::UnknownLength
|
||||
);
|
||||
|
||||
let req = TestRequest::with_header(
|
||||
header::CONTENT_TYPE, "application/x-www-form-urlencoded")
|
||||
.header(header::CONTENT_LENGTH, "1000000")
|
||||
header::CONTENT_TYPE,
|
||||
"application/x-www-form-urlencoded",
|
||||
).header(header::CONTENT_LENGTH, "1000000")
|
||||
.finish();
|
||||
assert_eq!(req.urlencoded::<Info>()
|
||||
.poll().err().unwrap(), UrlencodedError::Overflow);
|
||||
assert_eq!(
|
||||
req.urlencoded::<Info>().poll().err().unwrap(),
|
||||
UrlencodedError::Overflow
|
||||
);
|
||||
|
||||
let req = TestRequest::with_header(
|
||||
header::CONTENT_TYPE, "text/plain")
|
||||
let req = TestRequest::with_header(header::CONTENT_TYPE, "text/plain")
|
||||
.header(header::CONTENT_LENGTH, "10")
|
||||
.finish();
|
||||
assert_eq!(req.urlencoded::<Info>()
|
||||
.poll().err().unwrap(), UrlencodedError::ContentType);
|
||||
assert_eq!(
|
||||
req.urlencoded::<Info>().poll().err().unwrap(),
|
||||
UrlencodedError::ContentType
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_urlencoded() {
|
||||
let mut req = TestRequest::with_header(
|
||||
header::CONTENT_TYPE, "application/x-www-form-urlencoded")
|
||||
.header(header::CONTENT_LENGTH, "11")
|
||||
header::CONTENT_TYPE,
|
||||
"application/x-www-form-urlencoded",
|
||||
).header(header::CONTENT_LENGTH, "11")
|
||||
.finish();
|
||||
req.payload_mut().unread_data(Bytes::from_static(b"hello=world"));
|
||||
req.payload_mut()
|
||||
.unread_data(Bytes::from_static(b"hello=world"));
|
||||
|
||||
let result = req.urlencoded::<Info>().poll().ok().unwrap();
|
||||
assert_eq!(result, Async::Ready(Info{hello: "world".to_owned()}));
|
||||
assert_eq!(
|
||||
result,
|
||||
Async::Ready(Info {
|
||||
hello: "world".to_owned()
|
||||
})
|
||||
);
|
||||
|
||||
let mut req = TestRequest::with_header(
|
||||
header::CONTENT_TYPE, "application/x-www-form-urlencoded; charset=utf-8")
|
||||
.header(header::CONTENT_LENGTH, "11")
|
||||
header::CONTENT_TYPE,
|
||||
"application/x-www-form-urlencoded; charset=utf-8",
|
||||
).header(header::CONTENT_LENGTH, "11")
|
||||
.finish();
|
||||
req.payload_mut().unread_data(Bytes::from_static(b"hello=world"));
|
||||
req.payload_mut()
|
||||
.unread_data(Bytes::from_static(b"hello=world"));
|
||||
|
||||
let result = req.urlencoded().poll().ok().unwrap();
|
||||
assert_eq!(result, Async::Ready(Info{hello: "world".to_owned()}));
|
||||
assert_eq!(
|
||||
result,
|
||||
Async::Ready(Info {
|
||||
hello: "world".to_owned()
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -602,14 +663,16 @@ mod tests {
|
||||
}
|
||||
|
||||
let mut req = HttpRequest::default();
|
||||
req.payload_mut().unread_data(Bytes::from_static(b"test"));
|
||||
req.payload_mut()
|
||||
.unread_data(Bytes::from_static(b"test"));
|
||||
match req.body().poll().ok().unwrap() {
|
||||
Async::Ready(bytes) => assert_eq!(bytes, Bytes::from_static(b"test")),
|
||||
_ => unreachable!("error"),
|
||||
}
|
||||
|
||||
let mut req = HttpRequest::default();
|
||||
req.payload_mut().unread_data(Bytes::from_static(b"11111111111111"));
|
||||
req.payload_mut()
|
||||
.unread_data(Bytes::from_static(b"11111111111111"));
|
||||
match req.body().limit(5).poll().err().unwrap() {
|
||||
PayloadError::Overflow => (),
|
||||
_ => unreachable!("error"),
|
||||
|
Reference in New Issue
Block a user