2021-01-09 14:17:19 +01:00
|
|
|
//! Basic binary and string payload extractors.
|
|
|
|
|
|
|
|
use std::{
|
|
|
|
future::Future,
|
|
|
|
pin::Pin,
|
|
|
|
str,
|
|
|
|
task::{Context, Poll},
|
|
|
|
};
|
|
|
|
|
|
|
|
use actix_http::error::{ErrorBadRequest, PayloadError};
|
2019-03-17 08:48:40 +01:00
|
|
|
use bytes::{Bytes, BytesMut};
|
2020-12-16 23:40:26 +01:00
|
|
|
use encoding_rs::{Encoding, UTF_8};
|
2020-05-18 04:47:20 +02:00
|
|
|
use futures_core::stream::Stream;
|
2020-12-16 23:40:26 +01:00
|
|
|
use futures_util::{
|
2021-01-09 14:17:19 +01:00
|
|
|
future::{ready, Either, ErrInto, Ready, TryFutureExt as _},
|
2020-12-17 12:40:49 +01:00
|
|
|
ready,
|
2020-12-16 23:40:26 +01:00
|
|
|
};
|
2019-03-10 18:53:56 +01:00
|
|
|
use mime::Mime;
|
|
|
|
|
2021-01-09 14:17:19 +01:00
|
|
|
use crate::{dev, http::header, web, Error, FromRequest, HttpMessage, HttpRequest};
|
2019-03-10 18:53:56 +01:00
|
|
|
|
2021-01-09 14:17:19 +01:00
|
|
|
/// Extract a request's raw payload stream.
|
2019-03-10 18:53:56 +01:00
|
|
|
///
|
2021-01-09 14:17:19 +01:00
|
|
|
/// See [`PayloadConfig`] for important notes when using this advanced extractor.
|
2019-03-10 18:53:56 +01:00
|
|
|
///
|
2021-01-09 14:17:19 +01:00
|
|
|
/// # Usage
|
|
|
|
/// ```
|
2020-05-18 04:47:20 +02:00
|
|
|
/// use std::future::Future;
|
2021-01-09 14:17:19 +01:00
|
|
|
/// use futures_util::stream::{Stream, StreamExt};
|
|
|
|
/// use actix_web::{post, web};
|
|
|
|
///
|
|
|
|
/// // `body: web::Payload` parameter extracts raw payload stream from request
|
|
|
|
/// #[post("/")]
|
|
|
|
/// async fn index(mut body: web::Payload) -> actix_web::Result<String> {
|
|
|
|
/// // for demonstration only; in a normal case use the `Bytes` extractor
|
|
|
|
/// // collect payload stream into a bytes object
|
2019-11-20 18:33:22 +01:00
|
|
|
/// let mut bytes = web::BytesMut::new();
|
|
|
|
/// while let Some(item) = body.next().await {
|
|
|
|
/// bytes.extend_from_slice(&item?);
|
|
|
|
/// }
|
|
|
|
///
|
2021-01-09 14:17:19 +01:00
|
|
|
/// Ok(format!("Request Body Bytes:\n{:?}", bytes))
|
2019-03-10 18:53:56 +01:00
|
|
|
/// }
|
|
|
|
/// ```
|
2019-10-01 10:05:38 +02:00
|
|
|
pub struct Payload(pub crate::dev::Payload);
|
|
|
|
|
|
|
|
impl Payload {
|
2021-01-09 14:17:19 +01:00
|
|
|
/// Unwrap to inner Payload type.
|
2019-10-01 10:05:38 +02:00
|
|
|
pub fn into_inner(self) -> crate::dev::Payload {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
2019-03-10 18:53:56 +01:00
|
|
|
|
2019-03-25 01:00:59 +01:00
|
|
|
impl Stream for Payload {
|
2019-11-20 18:33:22 +01:00
|
|
|
type Item = Result<Bytes, PayloadError>;
|
2019-03-10 18:53:56 +01:00
|
|
|
|
|
|
|
#[inline]
|
2019-11-20 18:33:22 +01:00
|
|
|
fn poll_next(
|
|
|
|
mut self: Pin<&mut Self>,
|
2019-12-07 19:46:51 +01:00
|
|
|
cx: &mut Context<'_>,
|
2019-11-20 18:33:22 +01:00
|
|
|
) -> Poll<Option<Self::Item>> {
|
|
|
|
Pin::new(&mut self.0).poll_next(cx)
|
2019-03-10 18:53:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-09 14:17:19 +01:00
|
|
|
/// See [here](#usage) for example of usage as an extractor.
|
2019-04-13 23:50:54 +02:00
|
|
|
impl FromRequest for Payload {
|
2019-04-14 01:35:25 +02:00
|
|
|
type Config = PayloadConfig;
|
2019-03-10 18:53:56 +01:00
|
|
|
type Error = Error;
|
2019-11-20 18:33:22 +01:00
|
|
|
type Future = Ready<Result<Payload, Error>>;
|
2019-03-10 18:53:56 +01:00
|
|
|
|
|
|
|
#[inline]
|
2019-04-13 23:50:54 +02:00
|
|
|
fn from_request(_: &HttpRequest, payload: &mut dev::Payload) -> Self::Future {
|
2021-01-09 14:17:19 +01:00
|
|
|
ready(Ok(Payload(payload.take())))
|
2019-03-10 18:53:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-09 14:17:19 +01:00
|
|
|
/// Extract binary data from a request's payload.
|
2019-03-10 18:53:56 +01:00
|
|
|
///
|
2021-01-09 14:17:19 +01:00
|
|
|
/// Collects request payload stream into a [Bytes] instance.
|
2019-03-10 18:53:56 +01:00
|
|
|
///
|
2021-01-09 14:17:19 +01:00
|
|
|
/// Use [`PayloadConfig`] to configure extraction process.
|
2019-03-10 18:53:56 +01:00
|
|
|
///
|
2021-01-09 14:17:19 +01:00
|
|
|
/// # Usage
|
|
|
|
/// ```
|
|
|
|
/// use actix_web::{post, web};
|
2019-03-10 18:53:56 +01:00
|
|
|
///
|
|
|
|
/// /// extract binary data from request
|
2021-01-09 14:17:19 +01:00
|
|
|
/// #[post("/")]
|
|
|
|
/// async fn index(body: web::Bytes) -> String {
|
2019-03-10 18:53:56 +01:00
|
|
|
/// format!("Body {:?}!", body)
|
|
|
|
/// }
|
|
|
|
/// ```
|
2019-04-13 23:50:54 +02:00
|
|
|
impl FromRequest for Bytes {
|
2019-04-14 01:35:25 +02:00
|
|
|
type Config = PayloadConfig;
|
2019-03-10 18:53:56 +01:00
|
|
|
type Error = Error;
|
2020-12-16 23:40:26 +01:00
|
|
|
type Future = Either<ErrInto<HttpMessageBody, Error>, Ready<Result<Bytes, Error>>>;
|
2019-03-10 18:53:56 +01:00
|
|
|
|
|
|
|
#[inline]
|
2019-04-13 23:50:54 +02:00
|
|
|
fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future {
|
2020-07-20 18:40:58 +02:00
|
|
|
// allow both Config and Data<Config>
|
|
|
|
let cfg = PayloadConfig::from_req(req);
|
2019-03-10 18:53:56 +01:00
|
|
|
|
2021-01-09 14:17:19 +01:00
|
|
|
if let Err(err) = cfg.check_mimetype(req) {
|
|
|
|
return Either::Right(ready(Err(err)));
|
2019-03-10 18:53:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let limit = cfg.limit;
|
2019-11-20 18:33:22 +01:00
|
|
|
let fut = HttpMessageBody::new(req, payload).limit(limit);
|
2020-12-16 23:40:26 +01:00
|
|
|
Either::Left(fut.err_into())
|
2019-03-10 18:53:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Extract text information from a request's body.
|
|
|
|
///
|
|
|
|
/// Text extractor automatically decode body according to the request's charset.
|
|
|
|
///
|
2020-12-13 14:28:39 +01:00
|
|
|
/// [**PayloadConfig**](PayloadConfig) allows to configure
|
2019-03-10 18:53:56 +01:00
|
|
|
/// extraction process.
|
|
|
|
///
|
2021-01-09 14:17:19 +01:00
|
|
|
/// # Usage
|
|
|
|
/// ```
|
|
|
|
/// use actix_web::{post, web, FromRequest};
|
2019-03-10 18:53:56 +01:00
|
|
|
///
|
2021-01-09 14:17:19 +01:00
|
|
|
/// // extract text data from request
|
|
|
|
/// #[post("/")]
|
2019-11-21 16:34:04 +01:00
|
|
|
/// async fn index(text: String) -> String {
|
2019-03-10 18:53:56 +01:00
|
|
|
/// format!("Body {}!", text)
|
|
|
|
/// }
|
2019-04-13 23:50:54 +02:00
|
|
|
impl FromRequest for String {
|
2019-04-14 01:35:25 +02:00
|
|
|
type Config = PayloadConfig;
|
2019-03-10 18:53:56 +01:00
|
|
|
type Error = Error;
|
2020-12-16 23:40:26 +01:00
|
|
|
type Future = Either<StringExtractFut, Ready<Result<String, Error>>>;
|
2019-03-10 18:53:56 +01:00
|
|
|
|
|
|
|
#[inline]
|
2019-04-13 23:50:54 +02:00
|
|
|
fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future {
|
2020-07-20 18:40:58 +02:00
|
|
|
let cfg = PayloadConfig::from_req(req);
|
2019-03-10 18:53:56 +01:00
|
|
|
|
|
|
|
// check content-type
|
2021-01-09 14:17:19 +01:00
|
|
|
if let Err(err) = cfg.check_mimetype(req) {
|
|
|
|
return Either::Right(ready(Err(err)));
|
2019-03-10 18:53:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// check charset
|
|
|
|
let encoding = match req.encoding() {
|
|
|
|
Ok(enc) => enc,
|
2021-01-09 14:17:19 +01:00
|
|
|
Err(err) => return Either::Right(ready(Err(err.into()))),
|
2019-03-10 18:53:56 +01:00
|
|
|
};
|
|
|
|
let limit = cfg.limit;
|
2020-12-16 23:40:26 +01:00
|
|
|
let body_fut = HttpMessageBody::new(req, payload).limit(limit);
|
|
|
|
|
|
|
|
Either::Left(StringExtractFut { body_fut, encoding })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct StringExtractFut {
|
|
|
|
body_fut: HttpMessageBody,
|
|
|
|
encoding: &'static Encoding,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Future for StringExtractFut {
|
|
|
|
type Output = Result<String, Error>;
|
|
|
|
|
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
let encoding = self.encoding;
|
|
|
|
|
|
|
|
Pin::new(&mut self.body_fut).poll(cx).map(|out| {
|
|
|
|
let body = out?;
|
|
|
|
bytes_to_string(body, encoding)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-03-10 18:53:56 +01:00
|
|
|
|
2020-12-16 23:40:26 +01:00
|
|
|
fn bytes_to_string(body: Bytes, encoding: &'static Encoding) -> Result<String, Error> {
|
|
|
|
if encoding == UTF_8 {
|
|
|
|
Ok(str::from_utf8(body.as_ref())
|
|
|
|
.map_err(|_| ErrorBadRequest("Can not decode body"))?
|
|
|
|
.to_owned())
|
|
|
|
} else {
|
|
|
|
Ok(encoding
|
|
|
|
.decode_without_bom_handling_and_without_replacement(&body)
|
|
|
|
.map(|s| s.into_owned())
|
|
|
|
.ok_or_else(|| ErrorBadRequest("Can not decode body"))?)
|
2019-03-10 18:53:56 +01:00
|
|
|
}
|
|
|
|
}
|
2020-07-20 18:40:58 +02:00
|
|
|
|
2021-01-09 14:17:19 +01:00
|
|
|
/// Configuration for request payloads.
|
2020-07-20 18:40:58 +02:00
|
|
|
///
|
2021-01-09 14:17:19 +01:00
|
|
|
/// Applies to the built-in `Bytes` and `String` extractors. Note that the `Payload` extractor does
|
2020-07-20 18:40:58 +02:00
|
|
|
/// not automatically check conformance with this configuration to allow more flexibility when
|
|
|
|
/// building extractors on top of `Payload`.
|
2021-01-09 14:17:19 +01:00
|
|
|
///
|
|
|
|
/// By default, the payload size limit is 256kB and there is no mime type condition.
|
2019-03-10 18:53:56 +01:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct PayloadConfig {
|
|
|
|
limit: usize,
|
|
|
|
mimetype: Option<Mime>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PayloadConfig {
|
2021-01-09 14:17:19 +01:00
|
|
|
/// Create new instance with a size limit and no mime type condition.
|
2019-03-10 18:53:56 +01:00
|
|
|
pub fn new(limit: usize) -> Self {
|
2020-12-09 12:22:19 +01:00
|
|
|
Self {
|
|
|
|
limit,
|
|
|
|
..Default::default()
|
|
|
|
}
|
2019-03-10 18:53:56 +01:00
|
|
|
}
|
|
|
|
|
2021-01-09 14:17:19 +01:00
|
|
|
/// Set maximum accepted payload size. The default limit is 256kB.
|
2019-03-10 18:53:56 +01:00
|
|
|
pub fn limit(mut self, limit: usize) -> Self {
|
|
|
|
self.limit = limit;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-01-09 14:17:19 +01:00
|
|
|
/// Set required mime type of the request. By default mime type is not enforced.
|
2019-03-10 18:53:56 +01:00
|
|
|
pub fn mimetype(mut self, mt: Mime) -> Self {
|
|
|
|
self.mimetype = Some(mt);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-04-07 23:43:07 +02:00
|
|
|
fn check_mimetype(&self, req: &HttpRequest) -> Result<(), Error> {
|
2019-03-10 18:53:56 +01:00
|
|
|
// check content-type
|
|
|
|
if let Some(ref mt) = self.mimetype {
|
|
|
|
match req.mime_type() {
|
|
|
|
Ok(Some(ref req_mt)) => {
|
|
|
|
if mt != req_mt {
|
|
|
|
return Err(ErrorBadRequest("Unexpected Content-Type"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(None) => {
|
|
|
|
return Err(ErrorBadRequest("Content-Type is expected"));
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
return Err(err.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-07-20 18:40:58 +02:00
|
|
|
|
2020-09-02 23:12:07 +02:00
|
|
|
/// Extract payload config from app data. Check both `T` and `Data<T>`, in that order, and fall
|
2021-01-09 14:17:19 +01:00
|
|
|
/// back to the default payload config if neither is found.
|
2020-09-02 23:12:07 +02:00
|
|
|
fn from_req(req: &HttpRequest) -> &Self {
|
|
|
|
req.app_data::<Self>()
|
|
|
|
.or_else(|| req.app_data::<web::Data<Self>>().map(|d| d.as_ref()))
|
2020-10-01 11:19:09 +02:00
|
|
|
.unwrap_or(&DEFAULT_CONFIG)
|
2020-07-20 18:40:58 +02:00
|
|
|
}
|
2019-03-10 18:53:56 +01:00
|
|
|
}
|
|
|
|
|
2021-01-15 06:38:50 +01:00
|
|
|
/// Allow shared refs used as defaults.
|
2020-09-02 23:12:07 +02:00
|
|
|
const DEFAULT_CONFIG: PayloadConfig = PayloadConfig {
|
2020-12-17 12:40:49 +01:00
|
|
|
limit: DEFAULT_CONFIG_LIMIT,
|
2020-07-20 18:40:58 +02:00
|
|
|
mimetype: None,
|
|
|
|
};
|
|
|
|
|
2020-12-17 12:40:49 +01:00
|
|
|
const DEFAULT_CONFIG_LIMIT: usize = 262_144; // 2^18 bytes (~256kB)
|
|
|
|
|
2019-03-10 18:53:56 +01:00
|
|
|
impl Default for PayloadConfig {
|
|
|
|
fn default() -> Self {
|
2020-09-02 23:12:07 +02:00
|
|
|
DEFAULT_CONFIG.clone()
|
2019-03-10 18:53:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-09 14:17:19 +01:00
|
|
|
/// Future that resolves to a complete HTTP body payload.
|
2019-03-17 08:48:40 +01:00
|
|
|
///
|
2021-01-09 14:17:19 +01:00
|
|
|
/// By default only 256kB payload is accepted before `PayloadError::Overflow` is returned.
|
|
|
|
/// Use `MessageBody::limit()` method to change upper limit.
|
2019-04-13 23:50:54 +02:00
|
|
|
pub struct HttpMessageBody {
|
2019-03-17 08:48:40 +01:00
|
|
|
limit: usize,
|
|
|
|
length: Option<usize>,
|
2019-12-15 08:28:54 +01:00
|
|
|
#[cfg(feature = "compress")]
|
2020-12-17 12:40:49 +01:00
|
|
|
stream: dev::Decompress<dev::Payload>,
|
2019-12-15 08:28:54 +01:00
|
|
|
#[cfg(not(feature = "compress"))]
|
2020-12-17 12:40:49 +01:00
|
|
|
stream: dev::Payload,
|
|
|
|
buf: BytesMut,
|
2019-03-17 08:48:40 +01:00
|
|
|
err: Option<PayloadError>,
|
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
impl HttpMessageBody {
|
2019-03-17 08:48:40 +01:00
|
|
|
/// Create `MessageBody` for request.
|
2020-06-22 21:09:48 +02:00
|
|
|
#[allow(clippy::borrow_interior_mutable_const)]
|
2019-04-13 23:50:54 +02:00
|
|
|
pub fn new(req: &HttpRequest, payload: &mut dev::Payload) -> HttpMessageBody {
|
2020-12-17 12:40:49 +01:00
|
|
|
let mut length = None;
|
|
|
|
let mut err = None;
|
|
|
|
|
2019-04-07 00:02:02 +02:00
|
|
|
if let Some(l) = req.headers().get(&header::CONTENT_LENGTH) {
|
2020-12-17 12:40:49 +01:00
|
|
|
match l.to_str() {
|
|
|
|
Ok(s) => match s.parse::<usize>() {
|
|
|
|
Ok(l) if l > DEFAULT_CONFIG_LIMIT => {
|
|
|
|
err = Some(PayloadError::Overflow)
|
|
|
|
}
|
|
|
|
Ok(l) => length = Some(l),
|
|
|
|
Err(_) => err = Some(PayloadError::UnknownLength),
|
|
|
|
},
|
|
|
|
Err(_) => err = Some(PayloadError::UnknownLength),
|
2019-03-17 08:48:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-15 08:28:54 +01:00
|
|
|
#[cfg(feature = "compress")]
|
2020-12-17 12:40:49 +01:00
|
|
|
let stream = dev::Decompress::from_headers(payload.take(), req.headers());
|
2019-12-15 08:28:54 +01:00
|
|
|
#[cfg(not(feature = "compress"))]
|
2020-12-17 12:40:49 +01:00
|
|
|
let stream = payload.take();
|
2019-12-15 08:28:54 +01:00
|
|
|
|
2019-03-17 08:48:40 +01:00
|
|
|
HttpMessageBody {
|
2019-12-15 08:28:54 +01:00
|
|
|
stream,
|
2020-12-17 12:40:49 +01:00
|
|
|
limit: DEFAULT_CONFIG_LIMIT,
|
|
|
|
length,
|
|
|
|
buf: BytesMut::with_capacity(8192),
|
|
|
|
err,
|
2019-03-17 08:48:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-09 14:17:19 +01:00
|
|
|
/// Change max size of payload. By default max size is 256kB
|
2019-03-17 08:48:40 +01:00
|
|
|
pub fn limit(mut self, limit: usize) -> Self {
|
2020-12-17 12:40:49 +01:00
|
|
|
if let Some(l) = self.length {
|
|
|
|
if l > limit {
|
|
|
|
self.err = Some(PayloadError::Overflow);
|
|
|
|
}
|
|
|
|
}
|
2019-03-17 08:48:40 +01:00
|
|
|
self.limit = limit;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
impl Future for HttpMessageBody {
|
2019-11-20 18:33:22 +01:00
|
|
|
type Output = Result<Bytes, PayloadError>;
|
2019-03-17 08:48:40 +01:00
|
|
|
|
2020-12-17 12:40:49 +01:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
let this = self.get_mut();
|
2019-03-17 08:48:40 +01:00
|
|
|
|
2021-01-09 14:17:19 +01:00
|
|
|
if let Some(err) = this.err.take() {
|
|
|
|
return Poll::Ready(Err(err));
|
2019-03-17 08:48:40 +01:00
|
|
|
}
|
|
|
|
|
2020-12-17 12:40:49 +01:00
|
|
|
loop {
|
|
|
|
let res = ready!(Pin::new(&mut this.stream).poll_next(cx));
|
|
|
|
match res {
|
|
|
|
Some(chunk) => {
|
|
|
|
let chunk = chunk?;
|
|
|
|
if this.buf.len() + chunk.len() > this.limit {
|
|
|
|
return Poll::Ready(Err(PayloadError::Overflow));
|
2019-03-17 08:48:40 +01:00
|
|
|
} else {
|
2020-12-17 12:40:49 +01:00
|
|
|
this.buf.extend_from_slice(&chunk);
|
2019-03-17 08:48:40 +01:00
|
|
|
}
|
2019-11-20 18:33:22 +01:00
|
|
|
}
|
2020-12-17 12:40:49 +01:00
|
|
|
None => return Poll::Ready(Ok(this.buf.split().freeze())),
|
2019-11-20 18:33:22 +01:00
|
|
|
}
|
2020-12-17 12:40:49 +01:00
|
|
|
}
|
2019-03-17 08:48:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-10 18:53:56 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-03-10 19:04:50 +01:00
|
|
|
use bytes::Bytes;
|
|
|
|
|
2019-03-10 18:53:56 +01:00
|
|
|
use super::*;
|
2020-07-20 18:40:58 +02:00
|
|
|
use crate::http::{header, StatusCode};
|
|
|
|
use crate::test::{call_service, init_service, TestRequest};
|
|
|
|
use crate::{web, App, Responder};
|
2019-03-10 18:53:56 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_payload_config() {
|
2019-04-07 23:43:07 +02:00
|
|
|
let req = TestRequest::default().to_http_request();
|
2019-03-10 18:53:56 +01:00
|
|
|
let cfg = PayloadConfig::default().mimetype(mime::APPLICATION_JSON);
|
|
|
|
assert!(cfg.check_mimetype(&req).is_err());
|
|
|
|
|
2021-01-15 03:11:10 +01:00
|
|
|
let req = TestRequest::default()
|
|
|
|
.insert_header((header::CONTENT_TYPE, "application/x-www-form-urlencoded"))
|
|
|
|
.to_http_request();
|
2019-03-10 18:53:56 +01:00
|
|
|
assert!(cfg.check_mimetype(&req).is_err());
|
|
|
|
|
2021-01-15 03:11:10 +01:00
|
|
|
let req = TestRequest::default()
|
|
|
|
.insert_header((header::CONTENT_TYPE, "application/json"))
|
2019-04-07 23:43:07 +02:00
|
|
|
.to_http_request();
|
2019-03-10 18:53:56 +01:00
|
|
|
assert!(cfg.check_mimetype(&req).is_ok());
|
2020-07-20 18:40:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_config_recall_locations() {
|
|
|
|
async fn bytes_handler(_: Bytes) -> impl Responder {
|
|
|
|
"payload is probably json bytes"
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn string_handler(_: String) -> impl Responder {
|
|
|
|
"payload is probably json string"
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut srv = init_service(
|
|
|
|
App::new()
|
|
|
|
.service(
|
|
|
|
web::resource("/bytes-app-data")
|
|
|
|
.app_data(
|
|
|
|
PayloadConfig::default().mimetype(mime::APPLICATION_JSON),
|
|
|
|
)
|
|
|
|
.route(web::get().to(bytes_handler)),
|
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::resource("/bytes-data")
|
|
|
|
.data(PayloadConfig::default().mimetype(mime::APPLICATION_JSON))
|
|
|
|
.route(web::get().to(bytes_handler)),
|
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::resource("/string-app-data")
|
|
|
|
.app_data(
|
|
|
|
PayloadConfig::default().mimetype(mime::APPLICATION_JSON),
|
|
|
|
)
|
|
|
|
.route(web::get().to(string_handler)),
|
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::resource("/string-data")
|
|
|
|
.data(PayloadConfig::default().mimetype(mime::APPLICATION_JSON))
|
|
|
|
.route(web::get().to(string_handler)),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/bytes-app-data").to_request();
|
|
|
|
let resp = call_service(&mut srv, req).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/bytes-data").to_request();
|
|
|
|
let resp = call_service(&mut srv, req).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/string-app-data").to_request();
|
|
|
|
let resp = call_service(&mut srv, req).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/string-data").to_request();
|
|
|
|
let resp = call_service(&mut srv, req).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/bytes-app-data")
|
2021-01-15 03:11:10 +01:00
|
|
|
.insert_header(header::ContentType(mime::APPLICATION_JSON))
|
2020-07-20 18:40:58 +02:00
|
|
|
.to_request();
|
|
|
|
let resp = call_service(&mut srv, req).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/bytes-data")
|
2021-01-15 03:11:10 +01:00
|
|
|
.insert_header(header::ContentType(mime::APPLICATION_JSON))
|
2020-07-20 18:40:58 +02:00
|
|
|
.to_request();
|
|
|
|
let resp = call_service(&mut srv, req).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/string-app-data")
|
2021-01-15 03:11:10 +01:00
|
|
|
.insert_header(header::ContentType(mime::APPLICATION_JSON))
|
2020-07-20 18:40:58 +02:00
|
|
|
.to_request();
|
|
|
|
let resp = call_service(&mut srv, req).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/string-data")
|
2021-01-15 03:11:10 +01:00
|
|
|
.insert_header(header::ContentType(mime::APPLICATION_JSON))
|
2020-07-20 18:40:58 +02:00
|
|
|
.to_request();
|
|
|
|
let resp = call_service(&mut srv, req).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2019-03-10 18:53:56 +01:00
|
|
|
}
|
2019-03-10 19:04:50 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_bytes() {
|
2021-01-15 03:11:10 +01:00
|
|
|
let (req, mut pl) = TestRequest::default()
|
|
|
|
.insert_header((header::CONTENT_LENGTH, "11"))
|
2019-03-10 19:04:50 +01:00
|
|
|
.set_payload(Bytes::from_static(b"hello=world"))
|
2019-04-07 23:43:07 +02:00
|
|
|
.to_http_parts();
|
2019-03-10 19:04:50 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let s = Bytes::from_request(&req, &mut pl).await.unwrap();
|
2019-03-10 19:04:50 +01:00
|
|
|
assert_eq!(s, Bytes::from_static(b"hello=world"));
|
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_string() {
|
2021-01-15 03:11:10 +01:00
|
|
|
let (req, mut pl) = TestRequest::default()
|
|
|
|
.insert_header((header::CONTENT_LENGTH, "11"))
|
2019-03-10 19:04:50 +01:00
|
|
|
.set_payload(Bytes::from_static(b"hello=world"))
|
2019-04-07 23:43:07 +02:00
|
|
|
.to_http_parts();
|
2019-03-10 19:04:50 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let s = String::from_request(&req, &mut pl).await.unwrap();
|
2019-03-10 19:04:50 +01:00
|
|
|
assert_eq!(s, "hello=world");
|
|
|
|
}
|
2019-03-17 08:48:40 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_message_body() {
|
2021-01-15 03:11:10 +01:00
|
|
|
let (req, mut pl) = TestRequest::default()
|
|
|
|
.insert_header((header::CONTENT_LENGTH, "xxxx"))
|
2019-04-07 23:43:07 +02:00
|
|
|
.to_srv_request()
|
|
|
|
.into_parts();
|
2019-11-26 06:25:50 +01:00
|
|
|
let res = HttpMessageBody::new(&req, &mut pl).await;
|
2019-03-17 08:48:40 +01:00
|
|
|
match res.err().unwrap() {
|
2021-01-04 02:01:35 +01:00
|
|
|
PayloadError::UnknownLength => {}
|
2019-03-17 08:48:40 +01:00
|
|
|
_ => unreachable!("error"),
|
|
|
|
}
|
|
|
|
|
2021-01-15 03:11:10 +01:00
|
|
|
let (req, mut pl) = TestRequest::default()
|
|
|
|
.insert_header((header::CONTENT_LENGTH, "1000000"))
|
2019-04-07 23:43:07 +02:00
|
|
|
.to_srv_request()
|
|
|
|
.into_parts();
|
2019-11-26 06:25:50 +01:00
|
|
|
let res = HttpMessageBody::new(&req, &mut pl).await;
|
2019-03-17 08:48:40 +01:00
|
|
|
match res.err().unwrap() {
|
2021-01-04 02:01:35 +01:00
|
|
|
PayloadError::Overflow => {}
|
2019-03-17 08:48:40 +01:00
|
|
|
_ => unreachable!("error"),
|
|
|
|
}
|
|
|
|
|
2019-04-07 23:43:07 +02:00
|
|
|
let (req, mut pl) = TestRequest::default()
|
2019-03-17 08:48:40 +01:00
|
|
|
.set_payload(Bytes::from_static(b"test"))
|
2019-04-07 23:43:07 +02:00
|
|
|
.to_http_parts();
|
2019-11-26 06:25:50 +01:00
|
|
|
let res = HttpMessageBody::new(&req, &mut pl).await;
|
2019-03-17 08:48:40 +01:00
|
|
|
assert_eq!(res.ok().unwrap(), Bytes::from_static(b"test"));
|
|
|
|
|
2019-04-07 23:43:07 +02:00
|
|
|
let (req, mut pl) = TestRequest::default()
|
2019-03-17 08:48:40 +01:00
|
|
|
.set_payload(Bytes::from_static(b"11111111111111"))
|
2019-04-07 23:43:07 +02:00
|
|
|
.to_http_parts();
|
2019-11-26 06:25:50 +01:00
|
|
|
let res = HttpMessageBody::new(&req, &mut pl).limit(5).await;
|
2019-03-17 08:48:40 +01:00
|
|
|
match res.err().unwrap() {
|
2021-01-04 02:01:35 +01:00
|
|
|
PayloadError::Overflow => {}
|
2019-03-17 08:48:40 +01:00
|
|
|
_ => unreachable!("error"),
|
|
|
|
}
|
|
|
|
}
|
2019-03-10 18:53:56 +01:00
|
|
|
}
|