1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-22 08:07:18 +00:00

Merge pull request #308 from eddomuke/master

Allow to override Form extractor error
This commit is contained in:
Özgür Akkurt 2018-06-13 01:53:32 +03:00 committed by GitHub
commit c8528e8920
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 7 deletions

View file

@ -6,6 +6,8 @@
* Add `ClientRequestBuilder::form()` for sending `application/x-www-form-urlencoded` requests.
* Add method to configure custom error handler to Form extractor.
* Add methods to `HttpResponse` to retrieve, add, and delete cookies
* Add `.set_content_type()` and `.set_content_disposition()` methods

View file

@ -1,6 +1,7 @@
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
use std::{fmt, str};
use std::rc::Rc;
use bytes::Bytes;
use encoding::all::UTF_8;
@ -11,7 +12,7 @@ use serde::de::{self, DeserializeOwned};
use serde_urlencoded;
use de::PathDeserializer;
use error::{Error, ErrorBadRequest, ErrorNotFound};
use error::{Error, ErrorBadRequest, ErrorNotFound, UrlencodedError};
use handler::{AsyncResult, FromRequest};
use httpmessage::{HttpMessage, MessageBody, UrlEncoded};
use httprequest::HttpRequest;
@ -261,15 +262,17 @@ where
T: DeserializeOwned + 'static,
S: 'static,
{
type Config = FormConfig;
type Config = FormConfig<S>;
type Result = Box<Future<Item = Self, Error = Error>>;
#[inline]
fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result {
let req = req.clone();
let err = Rc::clone(&cfg.ehandler);
Box::new(
UrlEncoded::new(req.clone())
.limit(cfg.limit)
.from_err()
.map_err(move |e| (*err)(e, req))
.map(Form),
)
}
@ -314,21 +317,34 @@ impl<T: fmt::Display> fmt::Display for Form<T> {
/// );
/// }
/// ```
pub struct FormConfig {
pub struct FormConfig<S> {
limit: usize,
ehandler: Rc<Fn(UrlencodedError, HttpRequest<S>) -> Error>,
}
impl FormConfig {
impl<S> FormConfig<S> {
/// Change max size of payload. By default max size is 256Kb
pub fn limit(&mut self, limit: usize) -> &mut Self {
self.limit = limit;
self
}
/// Set custom error handler
pub fn error_handler<F>(&mut self, f: F) -> &mut Self
where
F: Fn(UrlencodedError, HttpRequest<S>) -> Error + 'static,
{
self.ehandler = Rc::new(f);
self
}
}
impl Default for FormConfig {
impl<S> Default for FormConfig<S> {
fn default() -> Self {
FormConfig { limit: 262_144 }
FormConfig {
limit: 262_144,
ehandler: Rc::new(|e, _| e.into()),
}
}
}

View file

@ -125,6 +125,30 @@ fn test_form_extractor() {
assert_eq!(bytes, Bytes::from_static(b"test"));
}
#[test]
fn test_form_extractor2() {
let mut srv = test::TestServer::new(|app| {
app.resource("/{username}/index.html", |r| {
r.route().with(|form: Form<FormData>| {
format!("{}", form.username)
}).error_handler(|err, res| {
error::InternalError::from_response(
err, HttpResponse::Conflict().finish()).into()
});
});
});
// client request
let request = srv
.post()
.uri(srv.url("/test1/index.html"))
.header("content-type", "application/x-www-form-urlencoded")
.body("918237129hdk:D:D:D:D:D:DjASHDKJhaswkjeq")
.unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_client_error());
}
#[test]
fn test_path_and_query_extractor() {
let mut srv = test::TestServer::new(|app| {