2019-03-10 17:53:56 +00:00
|
|
|
//! Form extractor
|
|
|
|
|
2019-11-20 17:33:22 +00:00
|
|
|
use std::future::Future;
|
|
|
|
use std::pin::Pin;
|
2019-03-10 17:53:56 +00:00
|
|
|
use std::rc::Rc;
|
2019-11-20 17:33:22 +00:00
|
|
|
use std::task::{Context, Poll};
|
2019-03-10 17:53:56 +00:00
|
|
|
use std::{fmt, ops};
|
|
|
|
|
2019-07-20 11:55:52 +00:00
|
|
|
use actix_http::{Error, HttpMessage, Payload, Response};
|
2019-04-13 21:50:54 +00:00
|
|
|
use bytes::BytesMut;
|
2019-06-18 06:43:25 +00:00
|
|
|
use encoding_rs::{Encoding, UTF_8};
|
2020-05-18 02:47:20 +00:00
|
|
|
use futures_util::future::{err, ok, FutureExt, LocalBoxFuture, Ready};
|
|
|
|
use futures_util::StreamExt;
|
2019-03-10 17:53:56 +00:00
|
|
|
use serde::de::DeserializeOwned;
|
2019-07-20 11:55:52 +00:00
|
|
|
use serde::Serialize;
|
2019-03-10 17:53:56 +00:00
|
|
|
|
2019-12-15 07:28:54 +00:00
|
|
|
#[cfg(feature = "compress")]
|
2019-04-13 21:50:54 +00:00
|
|
|
use crate::dev::Decompress;
|
2019-03-17 08:08:56 +00:00
|
|
|
use crate::error::UrlencodedError;
|
2019-03-10 17:53:56 +00:00
|
|
|
use crate::extract::FromRequest;
|
2019-07-20 11:55:52 +00:00
|
|
|
use crate::http::{
|
|
|
|
header::{ContentType, CONTENT_LENGTH},
|
|
|
|
StatusCode,
|
|
|
|
};
|
2019-03-10 17:53:56 +00:00
|
|
|
use crate::request::HttpRequest;
|
2019-07-20 11:55:52 +00:00
|
|
|
use crate::responder::Responder;
|
2019-03-10 17:53:56 +00:00
|
|
|
|
2019-07-20 11:55:52 +00:00
|
|
|
/// Form data helper (`application/x-www-form-urlencoded`)
|
|
|
|
///
|
|
|
|
/// Can be use to extract url-encoded data from the request body,
|
|
|
|
/// or send url-encoded data as the response.
|
|
|
|
///
|
|
|
|
/// ## Extract
|
2019-03-10 17:53:56 +00:00
|
|
|
///
|
|
|
|
/// To extract typed information from request's body, the type `T` must
|
|
|
|
/// implement the `Deserialize` trait from *serde*.
|
|
|
|
///
|
|
|
|
/// [**FormConfig**](struct.FormConfig.html) allows to configure extraction
|
|
|
|
/// process.
|
|
|
|
///
|
2019-07-20 11:55:52 +00:00
|
|
|
/// ### Example
|
2019-03-10 17:53:56 +00:00
|
|
|
/// ```rust
|
2019-10-07 05:29:11 +00:00
|
|
|
/// use actix_web::web;
|
|
|
|
/// use serde_derive::Deserialize;
|
2019-03-10 17:53:56 +00:00
|
|
|
///
|
|
|
|
/// #[derive(Deserialize)]
|
|
|
|
/// struct FormData {
|
|
|
|
/// username: String,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// /// Extract form data using serde.
|
|
|
|
/// /// This handler get called only if content type is *x-www-form-urlencoded*
|
|
|
|
/// /// and content of the request could be deserialized to a `FormData` struct
|
|
|
|
/// fn index(form: web::Form<FormData>) -> String {
|
|
|
|
/// format!("Welcome {}!", form.username)
|
|
|
|
/// }
|
|
|
|
/// # fn main() {}
|
|
|
|
/// ```
|
2019-07-20 11:55:52 +00:00
|
|
|
///
|
|
|
|
/// ## Respond
|
|
|
|
///
|
|
|
|
/// The `Form` type also allows you to respond with well-formed url-encoded data:
|
|
|
|
/// simply return a value of type Form<T> where T is the type to be url-encoded.
|
|
|
|
/// The type must implement `serde::Serialize`;
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
/// ```rust
|
2019-10-07 05:29:11 +00:00
|
|
|
/// use actix_web::*;
|
|
|
|
/// use serde_derive::Serialize;
|
|
|
|
///
|
2019-07-20 11:55:52 +00:00
|
|
|
/// #[derive(Serialize)]
|
|
|
|
/// struct SomeForm {
|
|
|
|
/// name: String,
|
|
|
|
/// age: u8
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // Will return a 200 response with header
|
|
|
|
/// // `Content-Type: application/x-www-form-urlencoded`
|
|
|
|
/// // and body "name=actix&age=123"
|
|
|
|
/// fn index() -> web::Form<SomeForm> {
|
|
|
|
/// web::Form(SomeForm {
|
|
|
|
/// name: "actix".into(),
|
|
|
|
/// age: 123
|
|
|
|
/// })
|
|
|
|
/// }
|
|
|
|
/// # fn main() {}
|
|
|
|
/// ```
|
|
|
|
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
2019-03-10 17:53:56 +00:00
|
|
|
pub struct Form<T>(pub T);
|
|
|
|
|
|
|
|
impl<T> Form<T> {
|
|
|
|
/// Deconstruct to an inner value
|
|
|
|
pub fn into_inner(self) -> T {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> ops::Deref for Form<T> {
|
|
|
|
type Target = T;
|
|
|
|
|
|
|
|
fn deref(&self) -> &T {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> ops::DerefMut for Form<T> {
|
|
|
|
fn deref_mut(&mut self) -> &mut T {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
impl<T> FromRequest for Form<T>
|
2019-03-10 17:53:56 +00:00
|
|
|
where
|
|
|
|
T: DeserializeOwned + 'static,
|
|
|
|
{
|
2019-04-13 23:35:25 +00:00
|
|
|
type Config = FormConfig;
|
2019-03-10 17:53:56 +00:00
|
|
|
type Error = Error;
|
2019-11-20 17:33:22 +00:00
|
|
|
type Future = LocalBoxFuture<'static, Result<Self, Error>>;
|
2019-03-10 17:53:56 +00:00
|
|
|
|
|
|
|
#[inline]
|
2019-04-13 21:50:54 +00:00
|
|
|
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
|
2019-04-07 21:43:07 +00:00
|
|
|
let req2 = req.clone();
|
2019-03-10 17:53:56 +00:00
|
|
|
let (limit, err) = req
|
2019-05-05 02:43:49 +00:00
|
|
|
.app_data::<FormConfig>()
|
2019-03-10 17:53:56 +00:00
|
|
|
.map(|c| (c.limit, c.ehandler.clone()))
|
|
|
|
.unwrap_or((16384, None));
|
|
|
|
|
2019-11-20 17:33:22 +00:00
|
|
|
UrlEncoded::new(req, payload)
|
|
|
|
.limit(limit)
|
|
|
|
.map(move |res| match res {
|
|
|
|
Err(e) => {
|
2019-03-10 17:53:56 +00:00
|
|
|
if let Some(err) = err {
|
2019-11-20 17:33:22 +00:00
|
|
|
Err((*err)(e, &req2))
|
2019-03-10 17:53:56 +00:00
|
|
|
} else {
|
2019-11-20 17:33:22 +00:00
|
|
|
Err(e.into())
|
2019-03-10 17:53:56 +00:00
|
|
|
}
|
2019-11-20 17:33:22 +00:00
|
|
|
}
|
|
|
|
Ok(item) => Ok(Form(item)),
|
|
|
|
})
|
|
|
|
.boxed_local()
|
2019-03-10 17:53:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: fmt::Debug> fmt::Debug for Form<T> {
|
2019-12-07 18:46:51 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-03-10 17:53:56 +00:00
|
|
|
self.0.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: fmt::Display> fmt::Display for Form<T> {
|
2019-12-07 18:46:51 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-03-10 17:53:56 +00:00
|
|
|
self.0.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-20 11:55:52 +00:00
|
|
|
impl<T: Serialize> Responder for Form<T> {
|
|
|
|
type Error = Error;
|
2019-11-20 17:33:22 +00:00
|
|
|
type Future = Ready<Result<Response, Error>>;
|
2019-07-20 11:55:52 +00:00
|
|
|
|
|
|
|
fn respond_to(self, _: &HttpRequest) -> Self::Future {
|
|
|
|
let body = match serde_urlencoded::to_string(&self.0) {
|
|
|
|
Ok(body) => body,
|
2019-11-20 17:33:22 +00:00
|
|
|
Err(e) => return err(e.into()),
|
2019-07-20 11:55:52 +00:00
|
|
|
};
|
|
|
|
|
2019-11-20 17:33:22 +00:00
|
|
|
ok(Response::build(StatusCode::OK)
|
2019-07-20 11:55:52 +00:00
|
|
|
.set(ContentType::form_url_encoded())
|
|
|
|
.body(body))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-10 17:53:56 +00:00
|
|
|
/// Form extractor configuration
|
|
|
|
///
|
|
|
|
/// ```rust
|
2019-04-13 23:35:25 +00:00
|
|
|
/// use actix_web::{web, App, FromRequest, Result};
|
2019-10-07 05:29:11 +00:00
|
|
|
/// use serde_derive::Deserialize;
|
2019-03-10 17:53:56 +00:00
|
|
|
///
|
|
|
|
/// #[derive(Deserialize)]
|
|
|
|
/// struct FormData {
|
|
|
|
/// username: String,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// /// Extract form data using serde.
|
|
|
|
/// /// Custom configuration is used for this handler, max payload size is 4k
|
2019-11-21 15:34:04 +00:00
|
|
|
/// async fn index(form: web::Form<FormData>) -> Result<String> {
|
2019-03-10 17:53:56 +00:00
|
|
|
/// Ok(format!("Welcome {}!", form.username))
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let app = App::new().service(
|
|
|
|
/// web::resource("/index.html")
|
2019-05-05 02:43:49 +00:00
|
|
|
/// // change `Form` extractor configuration
|
2019-12-20 11:13:09 +00:00
|
|
|
/// .app_data(
|
2019-05-05 02:43:49 +00:00
|
|
|
/// web::Form::<FormData>::configure(|cfg| cfg.limit(4097))
|
|
|
|
/// )
|
|
|
|
/// .route(web::get().to(index))
|
2019-03-10 17:53:56 +00:00
|
|
|
/// );
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct FormConfig {
|
|
|
|
limit: usize,
|
2019-07-17 09:48:37 +00:00
|
|
|
ehandler: Option<Rc<dyn Fn(UrlencodedError, &HttpRequest) -> Error>>,
|
2019-03-10 17:53:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FormConfig {
|
|
|
|
/// Change max size of payload. By default max size is 16Kb
|
|
|
|
pub fn limit(mut self, limit: usize) -> Self {
|
|
|
|
self.limit = limit;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set custom error handler
|
|
|
|
pub fn error_handler<F>(mut self, f: F) -> Self
|
|
|
|
where
|
|
|
|
F: Fn(UrlencodedError, &HttpRequest) -> Error + 'static,
|
|
|
|
{
|
|
|
|
self.ehandler = Some(Rc::new(f));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for FormConfig {
|
|
|
|
fn default() -> Self {
|
|
|
|
FormConfig {
|
|
|
|
limit: 16384,
|
|
|
|
ehandler: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-17 07:48:40 +00:00
|
|
|
/// Future that resolves to a parsed urlencoded values.
|
|
|
|
///
|
|
|
|
/// 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*.
|
|
|
|
///
|
|
|
|
/// Returns error:
|
|
|
|
///
|
|
|
|
/// * content type is not `application/x-www-form-urlencoded`
|
|
|
|
/// * content-length is greater than 32k
|
|
|
|
///
|
2019-04-13 21:50:54 +00:00
|
|
|
pub struct UrlEncoded<U> {
|
2019-12-15 07:28:54 +00:00
|
|
|
#[cfg(feature = "compress")]
|
2019-04-13 21:50:54 +00:00
|
|
|
stream: Option<Decompress<Payload>>,
|
2019-12-15 07:28:54 +00:00
|
|
|
#[cfg(not(feature = "compress"))]
|
|
|
|
stream: Option<Payload>,
|
2019-03-17 07:48:40 +00:00
|
|
|
limit: usize,
|
|
|
|
length: Option<usize>,
|
2019-06-18 06:43:25 +00:00
|
|
|
encoding: &'static Encoding,
|
2019-03-17 07:48:40 +00:00
|
|
|
err: Option<UrlencodedError>,
|
2019-11-20 17:33:22 +00:00
|
|
|
fut: Option<LocalBoxFuture<'static, Result<U, UrlencodedError>>>,
|
2019-03-17 07:48:40 +00:00
|
|
|
}
|
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
impl<U> UrlEncoded<U> {
|
2019-03-17 07:48:40 +00:00
|
|
|
/// Create a new future to URL encode a request
|
2019-04-13 21:50:54 +00:00
|
|
|
pub fn new(req: &HttpRequest, payload: &mut Payload) -> UrlEncoded<U> {
|
2019-03-17 07:48:40 +00:00
|
|
|
// check content type
|
|
|
|
if req.content_type().to_lowercase() != "application/x-www-form-urlencoded" {
|
|
|
|
return Self::err(UrlencodedError::ContentType);
|
|
|
|
}
|
|
|
|
let encoding = match req.encoding() {
|
|
|
|
Ok(enc) => enc,
|
|
|
|
Err(_) => return Self::err(UrlencodedError::ContentType),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut len = None;
|
2019-04-06 22:02:02 +00:00
|
|
|
if let Some(l) = req.headers().get(&CONTENT_LENGTH) {
|
2019-03-17 07:48:40 +00:00
|
|
|
if let Ok(s) = l.to_str() {
|
|
|
|
if let Ok(l) = s.parse::<usize>() {
|
|
|
|
len = Some(l)
|
|
|
|
} else {
|
|
|
|
return Self::err(UrlencodedError::UnknownLength);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Self::err(UrlencodedError::UnknownLength);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-12-15 07:28:54 +00:00
|
|
|
#[cfg(feature = "compress")]
|
2019-04-13 21:50:54 +00:00
|
|
|
let payload = Decompress::from_headers(payload.take(), req.headers());
|
2019-12-15 07:28:54 +00:00
|
|
|
#[cfg(not(feature = "compress"))]
|
|
|
|
let payload = payload.take();
|
|
|
|
|
2019-03-17 07:48:40 +00:00
|
|
|
UrlEncoded {
|
|
|
|
encoding,
|
2019-04-13 21:50:54 +00:00
|
|
|
stream: Some(payload),
|
2019-03-17 07:48:40 +00:00
|
|
|
limit: 32_768,
|
|
|
|
length: len,
|
|
|
|
fut: None,
|
|
|
|
err: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn err(e: UrlencodedError) -> Self {
|
|
|
|
UrlEncoded {
|
2019-04-13 21:50:54 +00:00
|
|
|
stream: None,
|
2019-03-17 07:48:40 +00:00
|
|
|
limit: 32_768,
|
|
|
|
fut: None,
|
|
|
|
err: Some(e),
|
|
|
|
length: None,
|
|
|
|
encoding: UTF_8,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Change max size of payload. By default max size is 256Kb
|
|
|
|
pub fn limit(mut self, limit: usize) -> Self {
|
|
|
|
self.limit = limit;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
impl<U> Future for UrlEncoded<U>
|
2019-03-17 07:48:40 +00:00
|
|
|
where
|
|
|
|
U: DeserializeOwned + 'static,
|
|
|
|
{
|
2019-11-20 17:33:22 +00:00
|
|
|
type Output = Result<U, UrlencodedError>;
|
2019-03-17 07:48:40 +00:00
|
|
|
|
2019-12-07 18:46:51 +00:00
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
2019-03-17 07:48:40 +00:00
|
|
|
if let Some(ref mut fut) = self.fut {
|
2019-11-20 17:33:22 +00:00
|
|
|
return Pin::new(fut).poll(cx);
|
2019-03-17 07:48:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(err) = self.err.take() {
|
2019-11-20 17:33:22 +00:00
|
|
|
return Poll::Ready(Err(err));
|
2019-03-17 07:48:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// payload size
|
|
|
|
let limit = self.limit;
|
|
|
|
if let Some(len) = self.length.take() {
|
|
|
|
if len > limit {
|
2019-11-20 17:33:22 +00:00
|
|
|
return Poll::Ready(Err(UrlencodedError::Overflow { size: len, limit }));
|
2019-03-17 07:48:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// future
|
|
|
|
let encoding = self.encoding;
|
2019-11-20 17:33:22 +00:00
|
|
|
let mut stream = self.stream.take().unwrap();
|
|
|
|
|
|
|
|
self.fut = Some(
|
|
|
|
async move {
|
|
|
|
let mut body = BytesMut::with_capacity(8192);
|
|
|
|
|
|
|
|
while let Some(item) = stream.next().await {
|
|
|
|
let chunk = item?;
|
|
|
|
if (body.len() + chunk.len()) > limit {
|
|
|
|
return Err(UrlencodedError::Overflow {
|
|
|
|
size: body.len() + chunk.len(),
|
|
|
|
limit,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
body.extend_from_slice(&chunk);
|
|
|
|
}
|
2019-03-17 07:48:40 +00:00
|
|
|
}
|
2019-11-20 17:33:22 +00:00
|
|
|
|
2019-06-18 06:43:25 +00:00
|
|
|
if encoding == UTF_8 {
|
2019-03-17 07:48:40 +00:00
|
|
|
serde_urlencoded::from_bytes::<U>(&body)
|
|
|
|
.map_err(|_| UrlencodedError::Parse)
|
|
|
|
} else {
|
|
|
|
let body = encoding
|
2019-06-18 06:43:25 +00:00
|
|
|
.decode_without_bom_handling_and_without_replacement(&body)
|
|
|
|
.map(|s| s.into_owned())
|
|
|
|
.ok_or(UrlencodedError::Parse)?;
|
2019-03-17 07:48:40 +00:00
|
|
|
serde_urlencoded::from_str::<U>(&body)
|
|
|
|
.map_err(|_| UrlencodedError::Parse)
|
|
|
|
}
|
2019-11-20 17:33:22 +00:00
|
|
|
}
|
2019-12-25 16:13:52 +00:00
|
|
|
.boxed_local(),
|
2019-11-20 17:33:22 +00:00
|
|
|
);
|
|
|
|
self.poll(cx)
|
2019-03-17 07:48:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-10 17:53:56 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use bytes::Bytes;
|
2019-07-20 11:55:52 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2019-03-10 17:53:56 +00:00
|
|
|
|
|
|
|
use super::*;
|
2019-07-20 11:55:52 +00:00
|
|
|
use crate::http::header::{HeaderValue, CONTENT_TYPE};
|
2019-11-26 05:25:50 +00:00
|
|
|
use crate::test::TestRequest;
|
2019-03-10 17:53:56 +00:00
|
|
|
|
2019-07-20 11:55:52 +00:00
|
|
|
#[derive(Deserialize, Serialize, Debug, PartialEq)]
|
2019-03-10 17:53:56 +00:00
|
|
|
struct Info {
|
|
|
|
hello: String,
|
2019-07-20 11:55:52 +00:00
|
|
|
counter: i64,
|
2019-03-10 17:53:56 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_form() {
|
|
|
|
let (req, mut pl) =
|
|
|
|
TestRequest::with_header(CONTENT_TYPE, "application/x-www-form-urlencoded")
|
|
|
|
.header(CONTENT_LENGTH, "11")
|
|
|
|
.set_payload(Bytes::from_static(b"hello=world&counter=123"))
|
|
|
|
.to_http_parts();
|
2019-03-17 07:48:40 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let Form(s) = Form::<Info>::from_request(&req, &mut pl).await.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
s,
|
|
|
|
Info {
|
|
|
|
hello: "world".into(),
|
|
|
|
counter: 123
|
|
|
|
}
|
|
|
|
);
|
2019-03-17 07:48:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn eq(err: UrlencodedError, other: UrlencodedError) -> bool {
|
|
|
|
match err {
|
2019-09-17 00:58:04 +00:00
|
|
|
UrlencodedError::Overflow { .. } => match other {
|
|
|
|
UrlencodedError::Overflow { .. } => true,
|
2019-03-17 07:48:40 +00:00
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
UrlencodedError::UnknownLength => match other {
|
|
|
|
UrlencodedError::UnknownLength => true,
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
UrlencodedError::ContentType => match other {
|
|
|
|
UrlencodedError::ContentType => true,
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_urlencoded_error() {
|
|
|
|
let (req, mut pl) =
|
|
|
|
TestRequest::with_header(CONTENT_TYPE, "application/x-www-form-urlencoded")
|
|
|
|
.header(CONTENT_LENGTH, "xxxx")
|
2019-11-20 17:33:22 +00:00
|
|
|
.to_http_parts();
|
2019-11-26 05:25:50 +00:00
|
|
|
let info = UrlEncoded::<Info>::new(&req, &mut pl).await;
|
|
|
|
assert!(eq(info.err().unwrap(), UrlencodedError::UnknownLength));
|
2019-03-17 07:48:40 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let (req, mut pl) =
|
|
|
|
TestRequest::with_header(CONTENT_TYPE, "application/x-www-form-urlencoded")
|
|
|
|
.header(CONTENT_LENGTH, "1000000")
|
|
|
|
.to_http_parts();
|
|
|
|
let info = UrlEncoded::<Info>::new(&req, &mut pl).await;
|
|
|
|
assert!(eq(
|
|
|
|
info.err().unwrap(),
|
|
|
|
UrlencodedError::Overflow { size: 0, limit: 0 }
|
|
|
|
));
|
|
|
|
|
|
|
|
let (req, mut pl) = TestRequest::with_header(CONTENT_TYPE, "text/plain")
|
|
|
|
.header(CONTENT_LENGTH, "10")
|
2019-11-20 17:33:22 +00:00
|
|
|
.to_http_parts();
|
2019-11-26 05:25:50 +00:00
|
|
|
let info = UrlEncoded::<Info>::new(&req, &mut pl).await;
|
|
|
|
assert!(eq(info.err().unwrap(), UrlencodedError::ContentType));
|
|
|
|
}
|
2019-03-17 07:48:40 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_urlencoded() {
|
|
|
|
let (req, mut pl) =
|
|
|
|
TestRequest::with_header(CONTENT_TYPE, "application/x-www-form-urlencoded")
|
|
|
|
.header(CONTENT_LENGTH, "11")
|
|
|
|
.set_payload(Bytes::from_static(b"hello=world&counter=123"))
|
|
|
|
.to_http_parts();
|
2019-03-17 07:48:40 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let info = UrlEncoded::<Info>::new(&req, &mut pl).await.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
info,
|
|
|
|
Info {
|
|
|
|
hello: "world".to_owned(),
|
|
|
|
counter: 123
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
let (req, mut pl) = TestRequest::with_header(
|
|
|
|
CONTENT_TYPE,
|
|
|
|
"application/x-www-form-urlencoded; charset=utf-8",
|
|
|
|
)
|
|
|
|
.header(CONTENT_LENGTH, "11")
|
|
|
|
.set_payload(Bytes::from_static(b"hello=world&counter=123"))
|
|
|
|
.to_http_parts();
|
|
|
|
|
|
|
|
let info = UrlEncoded::<Info>::new(&req, &mut pl).await.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
info,
|
|
|
|
Info {
|
|
|
|
hello: "world".to_owned(),
|
|
|
|
counter: 123
|
|
|
|
}
|
|
|
|
);
|
2019-03-10 17:53:56 +00:00
|
|
|
}
|
2019-07-20 11:55:52 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_responder() {
|
|
|
|
let req = TestRequest::default().to_http_request();
|
|
|
|
|
|
|
|
let form = Form(Info {
|
|
|
|
hello: "world".to_string(),
|
|
|
|
counter: 123,
|
|
|
|
});
|
|
|
|
let resp = form.respond_to(&req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("application/x-www-form-urlencoded")
|
|
|
|
);
|
|
|
|
|
|
|
|
use crate::responder::tests::BodyTest;
|
|
|
|
assert_eq!(resp.body().bin_ref(), b"hello=world&counter=123");
|
2019-07-20 11:55:52 +00:00
|
|
|
}
|
2019-03-10 17:53:56 +00:00
|
|
|
}
|