2019-03-05 22:16:26 +01:00
|
|
|
use std::cell::{Ref, RefMut};
|
2019-02-13 22:52:11 +01:00
|
|
|
use std::str;
|
2019-02-12 20:07:42 +01:00
|
|
|
|
2019-06-18 08:43:25 +02:00
|
|
|
use encoding_rs::{Encoding, UTF_8};
|
2019-04-07 00:02:02 +02:00
|
|
|
use http::header;
|
2018-04-14 01:02:01 +02:00
|
|
|
use mime::Mime;
|
2018-02-28 00:03:28 +01:00
|
|
|
|
2019-03-30 05:13:39 +01:00
|
|
|
use crate::cookie::Cookie;
|
|
|
|
use crate::error::{ContentTypeError, CookieParseError, ParseError};
|
2019-03-05 22:16:26 +01:00
|
|
|
use crate::extensions::Extensions;
|
2019-04-07 00:02:02 +02:00
|
|
|
use crate::header::{Header, HeaderMap};
|
2019-02-12 20:07:42 +01:00
|
|
|
use crate::payload::Payload;
|
2018-02-28 00:03:28 +01:00
|
|
|
|
2019-03-05 22:16:26 +01:00
|
|
|
struct Cookies(Vec<Cookie<'static>>);
|
|
|
|
|
2018-02-28 08:30:26 +01:00
|
|
|
/// Trait that implements general purpose operations on http messages
|
2018-06-25 06:58:04 +02:00
|
|
|
pub trait HttpMessage: Sized {
|
|
|
|
/// Type of message payload stream
|
2019-02-13 22:52:11 +01:00
|
|
|
type Stream;
|
2018-06-25 06:58:04 +02:00
|
|
|
|
2018-02-28 08:30:26 +01:00
|
|
|
/// Read the message headers.
|
2018-02-28 00:03:28 +01:00
|
|
|
fn headers(&self) -> &HeaderMap;
|
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
/// Message payload stream
|
2019-02-13 22:52:11 +01:00
|
|
|
fn take_payload(&mut self) -> Payload<Self::Stream>;
|
2018-06-25 06:58:04 +02:00
|
|
|
|
2019-03-05 22:16:26 +01:00
|
|
|
/// Request's extensions container
|
2019-12-07 19:46:51 +01:00
|
|
|
fn extensions(&self) -> Ref<'_, Extensions>;
|
2019-03-05 22:16:26 +01:00
|
|
|
|
|
|
|
/// Mutable reference to a the request's extensions container
|
2019-12-07 19:46:51 +01:00
|
|
|
fn extensions_mut(&self) -> RefMut<'_, Extensions>;
|
2019-03-05 22:16:26 +01:00
|
|
|
|
2018-03-06 04:28:42 +01:00
|
|
|
#[doc(hidden)]
|
|
|
|
/// Get a header
|
2018-04-14 01:02:01 +02:00
|
|
|
fn get_header<H: Header>(&self) -> Option<H>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2018-03-08 02:40:13 +01:00
|
|
|
if self.headers().contains_key(H::name()) {
|
|
|
|
H::parse(self).ok()
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2018-03-06 04:28:42 +01:00
|
|
|
}
|
2018-04-14 01:02:01 +02:00
|
|
|
|
2018-02-28 00:03:28 +01:00
|
|
|
/// 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() {
|
2018-04-14 01:02:01 +02:00
|
|
|
return content_type.split(';').next().unwrap().trim();
|
2018-02-28 00:03:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
""
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get content type encoding
|
|
|
|
///
|
|
|
|
/// UTF-8 is used by default, If request charset is not set.
|
2019-06-18 08:43:25 +02:00
|
|
|
fn encoding(&self) -> Result<&'static Encoding, ContentTypeError> {
|
2018-02-28 00:03:28 +01:00
|
|
|
if let Some(mime_type) = self.mime_type()? {
|
|
|
|
if let Some(charset) = mime_type.get_param("charset") {
|
2019-06-18 08:43:25 +02:00
|
|
|
if let Some(enc) =
|
|
|
|
Encoding::for_label_no_replacement(charset.as_str().as_bytes())
|
|
|
|
{
|
2018-02-28 00:03:28 +01:00
|
|
|
Ok(enc)
|
|
|
|
} else {
|
|
|
|
Err(ContentTypeError::UnknownEncoding)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(UTF_8)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(UTF_8)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert the request content type to a known mime type.
|
|
|
|
fn mime_type(&self) -> Result<Option<Mime>, ContentTypeError> {
|
|
|
|
if let Some(content_type) = self.headers().get(header::CONTENT_TYPE) {
|
|
|
|
if let Ok(content_type) = content_type.to_str() {
|
|
|
|
return match content_type.parse() {
|
|
|
|
Ok(mt) => Ok(Some(mt)),
|
|
|
|
Err(_) => Err(ContentTypeError::ParseError),
|
|
|
|
};
|
|
|
|
} else {
|
2018-04-14 01:02:01 +02:00
|
|
|
return Err(ContentTypeError::ParseError);
|
2018-02-28 00:03:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if request has chunked transfer encoding
|
|
|
|
fn chunked(&self) -> Result<bool, ParseError> {
|
|
|
|
if let Some(encodings) = self.headers().get(header::TRANSFER_ENCODING) {
|
|
|
|
if let Ok(s) = encodings.to_str() {
|
|
|
|
Ok(s.to_lowercase().contains("chunked"))
|
|
|
|
} else {
|
|
|
|
Err(ParseError::Header)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 22:16:26 +01:00
|
|
|
/// Load request cookies.
|
|
|
|
#[inline]
|
2019-12-07 19:46:51 +01:00
|
|
|
fn cookies(&self) -> Result<Ref<'_, Vec<Cookie<'static>>>, CookieParseError> {
|
2019-03-05 22:16:26 +01:00
|
|
|
if self.extensions().get::<Cookies>().is_none() {
|
|
|
|
let mut cookies = Vec::new();
|
|
|
|
for hdr in self.headers().get_all(header::COOKIE) {
|
|
|
|
let s =
|
|
|
|
str::from_utf8(hdr.as_bytes()).map_err(CookieParseError::from)?;
|
|
|
|
for cookie_str in s.split(';').map(|s| s.trim()) {
|
|
|
|
if !cookie_str.is_empty() {
|
|
|
|
cookies.push(Cookie::parse_encoded(cookie_str)?.into_owned());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.extensions_mut().insert(Cookies(cookies));
|
|
|
|
}
|
|
|
|
Ok(Ref::map(self.extensions(), |ext| {
|
|
|
|
&ext.get::<Cookies>().unwrap().0
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return request cookie.
|
|
|
|
fn cookie(&self, name: &str) -> Option<Cookie<'static>> {
|
|
|
|
if let Ok(cookies) = self.cookies() {
|
|
|
|
for cookie in cookies.iter() {
|
|
|
|
if cookie.name() == name {
|
|
|
|
return Some(cookie.to_owned());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
2018-06-13 19:43:03 +02:00
|
|
|
}
|
|
|
|
|
2019-03-05 23:39:06 +01:00
|
|
|
impl<'a, T> HttpMessage for &'a mut T
|
|
|
|
where
|
|
|
|
T: HttpMessage,
|
|
|
|
{
|
|
|
|
type Stream = T::Stream;
|
|
|
|
|
|
|
|
fn headers(&self) -> &HeaderMap {
|
|
|
|
(**self).headers()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Message payload stream
|
|
|
|
fn take_payload(&mut self) -> Payload<Self::Stream> {
|
|
|
|
(**self).take_payload()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Request's extensions container
|
2019-12-07 19:46:51 +01:00
|
|
|
fn extensions(&self) -> Ref<'_, Extensions> {
|
2019-03-05 23:39:06 +01:00
|
|
|
(**self).extensions()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Mutable reference to a the request's extensions container
|
2019-12-07 19:46:51 +01:00
|
|
|
fn extensions_mut(&self) -> RefMut<'_, Extensions> {
|
2019-03-05 23:39:06 +01:00
|
|
|
(**self).extensions_mut()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-28 00:03:28 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-03-17 09:02:51 +01:00
|
|
|
use bytes::Bytes;
|
2019-06-18 08:43:25 +02:00
|
|
|
use encoding_rs::ISO_8859_2;
|
2018-02-28 00:03:28 +01:00
|
|
|
|
2019-01-27 19:59:07 +01:00
|
|
|
use super::*;
|
|
|
|
use crate::test::TestRequest;
|
|
|
|
|
2018-02-28 00:03:28 +01:00
|
|
|
#[test]
|
|
|
|
fn test_content_type() {
|
|
|
|
let req = TestRequest::with_header("content-type", "text/plain").finish();
|
|
|
|
assert_eq!(req.content_type(), "text/plain");
|
2018-04-14 01:02:01 +02:00
|
|
|
let req =
|
|
|
|
TestRequest::with_header("content-type", "application/json; charset=utf=8")
|
|
|
|
.finish();
|
2018-02-28 00:03:28 +01:00
|
|
|
assert_eq!(req.content_type(), "application/json");
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::default().finish();
|
2018-02-28 00:03:28 +01:00
|
|
|
assert_eq!(req.content_type(), "");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mime_type() {
|
|
|
|
let req = TestRequest::with_header("content-type", "application/json").finish();
|
|
|
|
assert_eq!(req.mime_type().unwrap(), Some(mime::APPLICATION_JSON));
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::default().finish();
|
2018-02-28 00:03:28 +01:00
|
|
|
assert_eq!(req.mime_type().unwrap(), None);
|
2018-04-14 01:02:01 +02:00
|
|
|
let req =
|
|
|
|
TestRequest::with_header("content-type", "application/json; charset=utf-8")
|
|
|
|
.finish();
|
2018-02-28 00:03:28 +01:00
|
|
|
let mt = req.mime_type().unwrap().unwrap();
|
|
|
|
assert_eq!(mt.get_param(mime::CHARSET), Some(mime::UTF_8));
|
|
|
|
assert_eq!(mt.type_(), mime::APPLICATION);
|
|
|
|
assert_eq!(mt.subtype(), mime::JSON);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mime_type_error() {
|
|
|
|
let req = TestRequest::with_header(
|
2018-04-14 01:02:01 +02:00
|
|
|
"content-type",
|
|
|
|
"applicationadfadsfasdflknadsfklnadsfjson",
|
2018-12-06 23:32:52 +01:00
|
|
|
)
|
|
|
|
.finish();
|
2018-02-28 00:03:28 +01:00
|
|
|
assert_eq!(Err(ContentTypeError::ParseError), req.mime_type());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_encoding() {
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::default().finish();
|
2018-02-28 00:03:28 +01:00
|
|
|
assert_eq!(UTF_8.name(), req.encoding().unwrap().name());
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
let req = TestRequest::with_header("content-type", "application/json").finish();
|
2018-02-28 00:03:28 +01:00
|
|
|
assert_eq!(UTF_8.name(), req.encoding().unwrap().name());
|
|
|
|
|
|
|
|
let req = TestRequest::with_header(
|
2018-04-14 01:02:01 +02:00
|
|
|
"content-type",
|
|
|
|
"application/json; charset=ISO-8859-2",
|
2018-12-06 23:32:52 +01:00
|
|
|
)
|
|
|
|
.finish();
|
2019-06-18 08:43:25 +02:00
|
|
|
assert_eq!(ISO_8859_2, req.encoding().unwrap());
|
2018-02-28 00:03:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_encoding_error() {
|
2018-04-14 01:02:01 +02:00
|
|
|
let req = TestRequest::with_header("content-type", "applicatjson").finish();
|
2018-05-17 21:20:20 +02:00
|
|
|
assert_eq!(Some(ContentTypeError::ParseError), req.encoding().err());
|
2018-02-28 00:03:28 +01:00
|
|
|
|
|
|
|
let req = TestRequest::with_header(
|
2018-04-14 01:02:01 +02:00
|
|
|
"content-type",
|
|
|
|
"application/json; charset=kkkttktk",
|
2018-12-06 23:32:52 +01:00
|
|
|
)
|
|
|
|
.finish();
|
2018-04-29 18:09:08 +02:00
|
|
|
assert_eq!(
|
|
|
|
Some(ContentTypeError::UnknownEncoding),
|
|
|
|
req.encoding().err()
|
|
|
|
);
|
2018-02-28 00:03:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_chunked() {
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::default().finish();
|
2018-02-28 00:03:28 +01:00
|
|
|
assert!(!req.chunked().unwrap());
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
let req =
|
|
|
|
TestRequest::with_header(header::TRANSFER_ENCODING, "chunked").finish();
|
2018-02-28 00:03:28 +01:00
|
|
|
assert!(req.chunked().unwrap());
|
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::default()
|
|
|
|
.header(
|
|
|
|
header::TRANSFER_ENCODING,
|
|
|
|
Bytes::from_static(b"some va\xadscc\xacas0xsdasdlue"),
|
2018-12-06 23:32:52 +01:00
|
|
|
)
|
|
|
|
.finish();
|
2018-02-28 00:03:28 +01:00
|
|
|
assert!(req.chunked().is_err());
|
|
|
|
}
|
|
|
|
}
|