From fce8dd275ab0c86559a098af3c2d2b58ef17ff1f Mon Sep 17 00:00:00 2001 From: Douman Date: Sat, 2 Jun 2018 22:20:22 +0300 Subject: [PATCH 1/9] Specialize ResponseError for PayloadError Closes #257 --- src/error.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/error.rs b/src/error.rs index 481cf3268..174530cac 100644 --- a/src/error.rs +++ b/src/error.rs @@ -294,7 +294,14 @@ impl From for PayloadError { } /// `InternalServerError` for `PayloadError` -impl ResponseError for PayloadError {} +impl ResponseError for PayloadError { + fn error_response(&self) -> HttpResponse { + match *self { + PayloadError::Overflow => HttpResponse::new(StatusCode::PAYLOAD_TOO_LARGE), + _ => HttpResponse::new(StatusCode::BAD_REQUEST) + } + } +} /// Return `BadRequest` for `cookie::ParseError` impl ResponseError for cookie::ParseError { From 2a9b57f489fa82142d3b7072fa86e8b80c79f18b Mon Sep 17 00:00:00 2001 From: Douman Date: Sat, 2 Jun 2018 22:27:43 +0300 Subject: [PATCH 2/9] Correct docstring --- src/error.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/error.rs b/src/error.rs index 174530cac..6bb94af56 100644 --- a/src/error.rs +++ b/src/error.rs @@ -293,7 +293,10 @@ impl From for PayloadError { } } -/// `InternalServerError` for `PayloadError` +/// `PayloadError` returns two possible results: +/// +/// - `Overflow` returns `PayloadTooLarge` +/// - Other errors returns `BadRequest` impl ResponseError for PayloadError { fn error_response(&self) -> HttpResponse { match *self { From 7ab23d082dcd05d758685130247c80dbc1a05100 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 2 Jun 2018 13:45:29 -0700 Subject: [PATCH 3/9] fix doc test --- src/httprequest.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/httprequest.rs b/src/httprequest.rs index 1d9f6245f..d852bc743 100644 --- a/src/httprequest.rs +++ b/src/httprequest.rs @@ -283,9 +283,9 @@ impl HttpRequest { /// Generate url for named resource /// /// ```rust - /// //#### # extern crate actix_web; - /// //#### # use actix_web::{App, HttpRequest, HttpResponse, http}; - /// //#### # + /// # extern crate actix_web; + /// # use actix_web::{App, HttpRequest, HttpResponse, http}; + /// # /// fn index(req: HttpRequest) -> HttpResponse { /// let url = req.url_for("foo", &["1", "2", "3"]); // <- generate url for "foo" resource /// HttpResponse::Ok().into() From 8b8a3ac01d79d513c78ce4938ab612347a875dd6 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 2 Jun 2018 14:57:36 -0700 Subject: [PATCH 4/9] Support chunked encoding for UrlEncoded body #262 --- src/httpmessage.rs | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/src/httpmessage.rs b/src/httpmessage.rs index 1e57d3867..a9d68d3ab 100644 --- a/src/httpmessage.rs +++ b/src/httpmessage.rs @@ -149,7 +149,6 @@ pub trait HttpMessage { /// Returns error: /// /// * content type is not `application/x-www-form-urlencoded` - /// * transfer encoding is `chunked`. /// * content-length is greater than 256k /// /// ## Server example @@ -367,9 +366,7 @@ where fn poll(&mut self) -> Poll { if let Some(req) = self.req.take() { - if req.chunked().unwrap_or(false) { - return Err(UrlencodedError::Chunked); - } else if let Some(len) = req.headers().get(header::CONTENT_LENGTH) { + if let Some(len) = req.headers().get(header::CONTENT_LENGTH) { if let Ok(s) = len.to_str() { if let Ok(len) = s.parse::() { if len > 262_144 { @@ -577,13 +574,6 @@ mod tests { #[test] fn test_urlencoded_error() { - let req = - TestRequest::with_header(header::TRANSFER_ENCODING, "chunked").finish(); - assert_eq!( - req.urlencoded::().poll().err().unwrap(), - UrlencodedError::Chunked - ); - let req = TestRequest::with_header( header::CONTENT_TYPE, "application/x-www-form-urlencoded", From 698f0a1849eefc5bf53e285c62a5273420695339 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 2 Jun 2018 14:57:56 -0700 Subject: [PATCH 5/9] update changelog --- CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index d1e4e6f6c..367354a9b 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -25,6 +25,8 @@ ### Fixed +* Support chunked encoding for UrlEncoded body #262 + * `HttpRequest::url_for()` for a named route with no variables segments #265 From 7e0706a9426708e60df412558f17f34785c8441e Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 2 Jun 2018 14:58:15 -0700 Subject: [PATCH 6/9] implement Debug for Form, Query, Path extractors --- src/extractor.rs | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/extractor.rs b/src/extractor.rs index 3d77853a5..175e948b8 100644 --- a/src/extractor.rs +++ b/src/extractor.rs @@ -1,6 +1,6 @@ use std::marker::PhantomData; use std::ops::{Deref, DerefMut}; -use std::str; +use std::{fmt, str}; use bytes::Bytes; use encoding::all::UTF_8; @@ -115,6 +115,18 @@ where } } +impl fmt::Debug for Path { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.inner.fmt(f) + } +} + +impl fmt::Display for Path { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.inner.fmt(f) + } +} + /// Extract typed information from from the request's query. /// /// ## Example @@ -175,13 +187,24 @@ where #[inline] fn from_request(req: &HttpRequest, _: &Self::Config) -> Self::Result { - let req = req.clone(); serde_urlencoded::from_str::(req.query_string()) .map_err(|e| e.into()) .map(Query) } } +impl fmt::Debug for Query { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.0.fmt(f) + } +} + +impl fmt::Display for Query { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.0.fmt(f) + } +} + /// Extract typed information from the request's body. /// /// To extract typed information from request's body, the type `T` must @@ -252,6 +275,18 @@ where } } +impl fmt::Debug for Form { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.0.fmt(f) + } +} + +impl fmt::Display for Form { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.0.fmt(f) + } +} + /// Form extractor configuration /// /// ```rust From b799677532e46618704caa47aebd948ae26618ef Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 2 Jun 2018 15:10:48 -0700 Subject: [PATCH 7/9] better error messages for overflow errors --- src/error.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/error.rs b/src/error.rs index 6bb94af56..cfb6a0287 100644 --- a/src/error.rs +++ b/src/error.rs @@ -301,7 +301,7 @@ impl ResponseError for PayloadError { fn error_response(&self) -> HttpResponse { match *self { PayloadError::Overflow => HttpResponse::new(StatusCode::PAYLOAD_TOO_LARGE), - _ => HttpResponse::new(StatusCode::BAD_REQUEST) + _ => HttpResponse::new(StatusCode::BAD_REQUEST), } } } @@ -427,8 +427,10 @@ pub enum UrlencodedError { /// Can not decode chunked transfer encoding #[fail(display = "Can not decode chunked transfer encoding")] Chunked, - /// Payload size is bigger than 256k - #[fail(display = "Payload size is bigger than 256k")] + /// Payload size is bigger than allowed. (default: 256kB) + #[fail( + display = "Urlencoded payload size is bigger than allowed. (default: 256kB)" + )] Overflow, /// Payload size is now known #[fail(display = "Payload size is now known")] @@ -468,8 +470,8 @@ impl From for UrlencodedError { /// A set of errors that can occur during parsing json payloads #[derive(Fail, Debug)] pub enum JsonPayloadError { - /// Payload size is bigger than 256k - #[fail(display = "Payload size is bigger than 256k")] + /// Payload size is bigger than allowed. (default: 256kB) + #[fail(display = "Json payload size is bigger than allowed. (default: 256kB)")] Overflow, /// Content type error #[fail(display = "Content type error")] From ea018e0ad6b45d42e30c023a18da8a15207ff551 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 2 Jun 2018 16:03:23 -0700 Subject: [PATCH 8/9] better examle in doc string --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2772309e0..5d3767a29 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,10 +2,10 @@ //! for Rust. //! //! ```rust -//! use actix_web::{server, App, Path}; +//! use actix_web::{server, App, Path, Responder}; //! # use std::thread; //! -//! fn index(info: Path<(String, u32)>) -> String { +//! fn index(info: Path<(String, u32)>) -> impl Responder { //! format!("Hello {}! id:{}", info.0, info.1) //! } //! From 268c5d9238c2c43e174969605ac8c9a93523fc3a Mon Sep 17 00:00:00 2001 From: Matthijs Brobbel Date: Sun, 3 Jun 2018 20:28:08 +0200 Subject: [PATCH 9/9] Fix typo --- src/middleware/identity.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/middleware/identity.rs b/src/middleware/identity.rs index d1e52d463..706dd9fc8 100644 --- a/src/middleware/identity.rs +++ b/src/middleware/identity.rs @@ -3,7 +3,7 @@ //! [**IdentityService**](struct.IdentityService.html) middleware can be //! used with different policies types to store identity information. //! -//! Bu default, only cookie identity policy is implemented. Other backend +//! By default, only cookie identity policy is implemented. Other backend //! implementations can be added separately. //! //! [**CookieIdentityPolicy**](struct.CookieIdentityPolicy.html)