1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-05-20 01:08:10 +00:00
actix-web/actix-http/src/http_message.rs

230 lines
6.9 KiB
Rust
Raw Normal View History

use std::{
cell::{Ref, RefMut},
str,
};
2019-02-12 19:07:42 +00:00
use encoding_rs::{Encoding, UTF_8};
use http::header;
2018-04-13 23:02:01 +00:00
use mime::Mime;
use crate::{
error::{ContentTypeError, ParseError},
header::{Header, HeaderMap},
payload::Payload,
Extensions,
};
2021-01-15 02:11:10 +00:00
/// Trait that implements general purpose operations on HTTP messages.
2018-06-25 04:58:04 +00:00
pub trait HttpMessage: Sized {
/// Type of message payload stream
2019-02-13 21:52:11 +00:00
type Stream;
2018-06-25 04:58:04 +00:00
2018-02-28 07:30:26 +00:00
/// Read the message headers.
fn headers(&self) -> &HeaderMap;
2018-06-25 04:58:04 +00:00
/// Message payload stream
2019-02-13 21:52:11 +00:00
fn take_payload(&mut self) -> Payload<Self::Stream>;
2018-06-25 04:58:04 +00:00
/// Returns a reference to the request-local data/extensions container.
2019-12-07 18:46:51 +00:00
fn extensions(&self) -> Ref<'_, Extensions>;
/// Returns a mutable reference to the request-local data/extensions container.
2019-12-07 18:46:51 +00:00
fn extensions_mut(&self) -> RefMut<'_, Extensions>;
2021-01-15 02:11:10 +00:00
/// Get a header.
2018-03-06 03:28:42 +00:00
#[doc(hidden)]
2018-04-13 23:02:01 +00:00
fn get_header<H: Header>(&self) -> Option<H>
where
Self: Sized,
{
2018-03-08 01:40:13 +00:00
if self.headers().contains_key(H::name()) {
H::parse(self).ok()
} else {
None
}
2018-03-06 03:28:42 +00:00
}
2018-04-13 23:02:01 +00:00
2021-01-15 02:11:10 +00:00
/// Read the request content type. If request did not contain a *Content-Type* header, an empty
/// string is 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-13 23:02:01 +00:00
return content_type.split(';').next().unwrap().trim();
}
}
""
}
2022-01-31 21:22:23 +00:00
/// Get content type encoding.
///
/// UTF-8 is used by default, If request charset is not set.
fn encoding(&self) -> Result<&'static Encoding, ContentTypeError> {
if let Some(mime_type) = self.mime_type()? {
if let Some(charset) = mime_type.get_param("charset") {
2023-07-17 01:38:12 +00:00
if let Some(enc) = Encoding::for_label_no_replacement(charset.as_str().as_bytes()) {
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-13 23:02:01 +00:00
return Err(ContentTypeError::ParseError);
}
}
Ok(None)
}
2021-01-15 02:11:10 +00:00
/// 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)
}
}
2018-06-13 17:43:03 +00:00
}
2019-03-05 22:39:06 +00: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 18:46:51 +00:00
fn extensions(&self) -> Ref<'_, Extensions> {
2019-03-05 22:39:06 +00:00
(**self).extensions()
}
/// Mutable reference to a the request's extensions container
2019-12-07 18:46:51 +00:00
fn extensions_mut(&self) -> RefMut<'_, Extensions> {
2019-03-05 22:39:06 +00:00
(**self).extensions_mut()
}
}
#[cfg(test)]
mod tests {
2019-03-17 08:02:51 +00:00
use bytes::Bytes;
use encoding_rs::ISO_8859_2;
2019-01-27 18:59:07 +00:00
use super::*;
use crate::test::TestRequest;
#[test]
fn test_content_type() {
2021-01-15 02:11:10 +00:00
let req = TestRequest::default()
.insert_header(("content-type", "text/plain"))
.finish();
assert_eq!(req.content_type(), "text/plain");
2021-01-15 02:11:10 +00:00
let req = TestRequest::default()
2023-07-05 16:29:10 +00:00
.insert_header(("content-type", "application/json; charset=utf-8"))
2021-01-15 02:11:10 +00:00
.finish();
assert_eq!(req.content_type(), "application/json");
2018-06-25 04:58:04 +00:00
let req = TestRequest::default().finish();
assert_eq!(req.content_type(), "");
}
#[test]
fn test_mime_type() {
2021-01-15 02:11:10 +00:00
let req = TestRequest::default()
.insert_header(("content-type", "application/json"))
.finish();
assert_eq!(req.mime_type().unwrap(), Some(mime::APPLICATION_JSON));
2018-06-25 04:58:04 +00:00
let req = TestRequest::default().finish();
assert_eq!(req.mime_type().unwrap(), None);
2021-01-15 02:11:10 +00:00
let req = TestRequest::default()
.insert_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);
assert_eq!(mt.subtype(), mime::JSON);
}
#[test]
fn test_mime_type_error() {
2021-01-15 02:11:10 +00:00
let req = TestRequest::default()
.insert_header(("content-type", "applicationadfadsfasdflknadsfklnadsfjson"))
.finish();
assert_eq!(Err(ContentTypeError::ParseError), req.mime_type());
}
#[test]
fn test_encoding() {
2018-06-25 04:58:04 +00:00
let req = TestRequest::default().finish();
assert_eq!(UTF_8.name(), req.encoding().unwrap().name());
2021-01-15 02:11:10 +00:00
let req = TestRequest::default()
.insert_header(("content-type", "application/json"))
.finish();
assert_eq!(UTF_8.name(), req.encoding().unwrap().name());
2021-01-15 02:11:10 +00:00
let req = TestRequest::default()
.insert_header(("content-type", "application/json; charset=ISO-8859-2"))
.finish();
assert_eq!(ISO_8859_2, req.encoding().unwrap());
}
#[test]
fn test_encoding_error() {
2021-01-15 02:11:10 +00:00
let req = TestRequest::default()
.insert_header(("content-type", "applicatjson"))
.finish();
2018-05-17 19:20:20 +00:00
assert_eq!(Some(ContentTypeError::ParseError), req.encoding().err());
2021-01-15 02:11:10 +00:00
let req = TestRequest::default()
.insert_header(("content-type", "application/json; charset=kkkttktk"))
.finish();
2018-04-29 16:09:08 +00:00
assert_eq!(
Some(ContentTypeError::UnknownEncoding),
req.encoding().err()
);
}
#[test]
fn test_chunked() {
2018-06-25 04:58:04 +00:00
let req = TestRequest::default().finish();
assert!(!req.chunked().unwrap());
2021-01-15 02:11:10 +00:00
let req = TestRequest::default()
.insert_header((header::TRANSFER_ENCODING, "chunked"))
.finish();
assert!(req.chunked().unwrap());
2018-06-25 04:58:04 +00:00
let req = TestRequest::default()
2021-01-15 02:11:10 +00:00
.insert_header((
2018-06-25 04:58:04 +00:00
header::TRANSFER_ENCODING,
Bytes::from_static(b"some va\xadscc\xacas0xsdasdlue"),
2021-01-15 02:11:10 +00:00
))
2018-12-06 22:32:52 +00:00
.finish();
assert!(req.chunked().is_err());
}
}