1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2025-01-02 05:18:44 +00:00

big clean up and docs improvmenet of types mod (#1894)

This commit is contained in:
Rob Ede 2021-01-09 13:17:19 +00:00 committed by GitHub
parent 530d03791d
commit 6575ee93f2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 674 additions and 743 deletions

View file

@ -1,11 +1,21 @@
# Changes # Changes
## Unreleased - 2021-xx-xx ## Unreleased - 2021-xx-xx
### Added
* The method `Either<web::Json<T>, web::Form<T>>::into_inner()` which returns the inner type for
whichever variant was created. Also works for `Either<web::Form<T>, web::Json<T>>`. [#1894]
### Changed ### Changed
* Rework `Responder` trait to be sync and returns `Response`/`HttpResponse` directly. * Rework `Responder` trait to be sync and returns `Response`/`HttpResponse` directly.
Making it more simple and performant. [#1891] Making it more simple and performant. [#1891]
* Our `Either` type now uses `Left`/`Right` variants (instead of `A`/`B`) [#1894]
### Removed
* Public field of `web::Path` has been made private. [#1894]
* Public field of `web::Query` has been made private. [#1894]
[#1891]: https://github.com/actix/actix-web/pull/1891 [#1891]: https://github.com/actix/actix-web/pull/1891
[#1894]: https://github.com/actix/actix-web/pull/1894
## 4.0.0-beta.1 - 2021-01-07 ## 4.0.0-beta.1 - 2021-01-07
### Added ### Added

View file

@ -90,6 +90,7 @@ awc = { version = "3.0.0-beta.1", default-features = false }
ahash = "0.6" ahash = "0.6"
bytes = "1" bytes = "1"
derive_more = "0.99.5" derive_more = "0.99.5"
either = "1.5.3"
encoding_rs = "0.8" encoding_rs = "0.8"
futures-core = { version = "0.3.7", default-features = false } futures-core = { version = "0.3.7", default-features = false }
futures-util = { version = "0.3.7", default-features = false } futures-util = { version = "0.3.7", default-features = false }
@ -142,4 +143,4 @@ harness = false
[[bench]] [[bench]]
name = "responder" name = "responder"
harness = false harness = false

View file

@ -1,6 +1,9 @@
# Changes # Changes
## Unreleased - 2021-xx-xx ## Unreleased - 2021-xx-xx
* `Response::content_type` now takes an `impl IntoHeaderValue` to support `mime` types. [#1894]
[#1894]: https://github.com/actix/actix-web/pull/1894
## 3.0.0-beta.1 - 2021-01-07 ## 3.0.0-beta.1 - 2021-01-07

View file

@ -100,10 +100,6 @@ impl fmt::Debug for Error {
} }
impl std::error::Error for Error { impl std::error::Error for Error {
fn cause(&self) -> Option<&dyn std::error::Error> {
None
}
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None None
} }
@ -309,28 +305,45 @@ impl From<httparse::Error> for ParseError {
pub enum PayloadError { pub enum PayloadError {
/// A payload reached EOF, but is not complete. /// A payload reached EOF, but is not complete.
#[display( #[display(
fmt = "A payload reached EOF, but is not complete. With error: {:?}", fmt = "A payload reached EOF, but is not complete. Inner error: {:?}",
_0 _0
)] )]
Incomplete(Option<io::Error>), Incomplete(Option<io::Error>),
/// Content encoding stream corruption
/// Content encoding stream corruption.
#[display(fmt = "Can not decode content-encoding.")] #[display(fmt = "Can not decode content-encoding.")]
EncodingCorrupted, EncodingCorrupted,
/// A payload reached size limit.
#[display(fmt = "A payload reached size limit.")] /// Payload reached size limit.
#[display(fmt = "Payload reached size limit.")]
Overflow, Overflow,
/// A payload length is unknown.
#[display(fmt = "A payload length is unknown.")] /// Payload length is unknown.
#[display(fmt = "Payload length is unknown.")]
UnknownLength, UnknownLength,
/// Http2 payload error
/// HTTP/2 payload error.
#[display(fmt = "{}", _0)] #[display(fmt = "{}", _0)]
Http2Payload(h2::Error), Http2Payload(h2::Error),
/// Io error
/// Generic I/O error.
#[display(fmt = "{}", _0)] #[display(fmt = "{}", _0)]
Io(io::Error), Io(io::Error),
} }
impl std::error::Error for PayloadError {} impl std::error::Error for PayloadError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
PayloadError::Incomplete(None) => None,
PayloadError::Incomplete(Some(err)) => Some(err as &dyn std::error::Error),
PayloadError::EncodingCorrupted => None,
PayloadError::Overflow => None,
PayloadError::UnknownLength => None,
PayloadError::Http2Payload(err) => Some(err as &dyn std::error::Error),
PayloadError::Io(err) => Some(err as &dyn std::error::Error),
}
}
}
impl From<h2::Error> for PayloadError { impl From<h2::Error> for PayloadError {
fn from(err: h2::Error) -> Self { fn from(err: h2::Error) -> Self {
@ -1009,22 +1022,22 @@ mod tests {
fn test_payload_error() { fn test_payload_error() {
let err: PayloadError = let err: PayloadError =
io::Error::new(io::ErrorKind::Other, "ParseError").into(); io::Error::new(io::ErrorKind::Other, "ParseError").into();
assert!(format!("{}", err).contains("ParseError")); assert!(err.to_string().contains("ParseError"));
let err = PayloadError::Incomplete(None); let err = PayloadError::Incomplete(None);
assert_eq!( assert_eq!(
format!("{}", err), err.to_string(),
"A payload reached EOF, but is not complete. With error: None" "A payload reached EOF, but is not complete. Inner error: None"
); );
} }
macro_rules! from { macro_rules! from {
($from:expr => $error:pat) => { ($from:expr => $error:pat) => {
match ParseError::from($from) { match ParseError::from($from) {
e @ $error => { err @ $error => {
assert!(format!("{}", e).len() >= 5); assert!(err.to_string().len() >= 5);
} }
e => unreachable!("{:?}", e), err => unreachable!("{:?}", err),
} }
}; };
} }
@ -1067,7 +1080,7 @@ mod tests {
let err = PayloadError::Overflow; let err = PayloadError::Overflow;
let resp_err: &dyn ResponseError = &err; let resp_err: &dyn ResponseError = &err;
let err = resp_err.downcast_ref::<PayloadError>().unwrap(); let err = resp_err.downcast_ref::<PayloadError>().unwrap();
assert_eq!(err.to_string(), "A payload reached size limit."); assert_eq!(err.to_string(), "Payload reached size limit.");
let not_err = resp_err.downcast_ref::<ContentTypeError>(); let not_err = resp_err.downcast_ref::<ContentTypeError>();
assert!(not_err.is_none()); assert!(not_err.is_none());
} }

View file

@ -481,15 +481,14 @@ impl ResponseBuilder {
self self
} }
/// Set response content type /// Set response content type.
#[inline] #[inline]
pub fn content_type<V>(&mut self, value: V) -> &mut Self pub fn content_type<V>(&mut self, value: V) -> &mut Self
where where
HeaderValue: TryFrom<V>, V: IntoHeaderValue,
<HeaderValue as TryFrom<V>>::Error: Into<HttpError>,
{ {
if let Some(parts) = parts(&mut self.head, &self.err) { if let Some(parts) = parts(&mut self.head, &self.err) {
match HeaderValue::try_from(value) { match value.try_into() {
Ok(value) => { Ok(value) => {
parts.headers.insert(header::CONTENT_TYPE, value); parts.headers.insert(header::CONTENT_TYPE, value);
} }
@ -802,7 +801,7 @@ impl From<ResponseBuilder> for Response {
impl From<&'static str> for Response { impl From<&'static str> for Response {
fn from(val: &'static str) -> Self { fn from(val: &'static str) -> Self {
Response::Ok() Response::Ok()
.content_type("text/plain; charset=utf-8") .content_type(mime::TEXT_PLAIN_UTF_8)
.body(val) .body(val)
} }
} }
@ -810,7 +809,7 @@ impl From<&'static str> for Response {
impl From<&'static [u8]> for Response { impl From<&'static [u8]> for Response {
fn from(val: &'static [u8]) -> Self { fn from(val: &'static [u8]) -> Self {
Response::Ok() Response::Ok()
.content_type("application/octet-stream") .content_type(mime::APPLICATION_OCTET_STREAM)
.body(val) .body(val)
} }
} }
@ -818,7 +817,7 @@ impl From<&'static [u8]> for Response {
impl From<String> for Response { impl From<String> for Response {
fn from(val: String) -> Self { fn from(val: String) -> Self {
Response::Ok() Response::Ok()
.content_type("text/plain; charset=utf-8") .content_type(mime::TEXT_PLAIN_UTF_8)
.body(val) .body(val)
} }
} }
@ -826,7 +825,7 @@ impl From<String> for Response {
impl<'a> From<&'a String> for Response { impl<'a> From<&'a String> for Response {
fn from(val: &'a String) -> Self { fn from(val: &'a String) -> Self {
Response::Ok() Response::Ok()
.content_type("text/plain; charset=utf-8") .content_type(mime::TEXT_PLAIN_UTF_8)
.body(val) .body(val)
} }
} }
@ -834,7 +833,7 @@ impl<'a> From<&'a String> for Response {
impl From<Bytes> for Response { impl From<Bytes> for Response {
fn from(val: Bytes) -> Self { fn from(val: Bytes) -> Self {
Response::Ok() Response::Ok()
.content_type("application/octet-stream") .content_type(mime::APPLICATION_OCTET_STREAM)
.body(val) .body(val)
} }
} }
@ -842,7 +841,7 @@ impl From<Bytes> for Response {
impl From<BytesMut> for Response { impl From<BytesMut> for Response {
fn from(val: BytesMut) -> Self { fn from(val: BytesMut) -> Self {
Response::Ok() Response::Ok()
.content_type("application/octet-stream") .content_type(mime::APPLICATION_OCTET_STREAM)
.body(val) .body(val)
} }
} }

View file

@ -89,7 +89,7 @@ impl Codec {
/// Set max frame size. /// Set max frame size.
/// ///
/// By default max size is set to 64kb. /// By default max size is set to 64kB.
pub fn max_size(mut self, size: usize) -> Self { pub fn max_size(mut self, size: usize) -> Self {
self.max_size = size; self.max_size = size;
self self

View file

@ -184,7 +184,7 @@ where
} }
} }
/// Change max size of payload. By default max size is 256Kb /// Change max size of payload. By default max size is 256kB
pub fn limit(mut self, limit: usize) -> Self { pub fn limit(mut self, limit: usize) -> Self {
if let Some(ref mut fut) = self.fut { if let Some(ref mut fut) = self.fut {
fut.limit = limit; fut.limit = limit;
@ -276,7 +276,7 @@ where
} }
} }
/// Change max size of payload. By default max size is 64Kb /// Change max size of payload. By default max size is 64kB
pub fn limit(mut self, limit: usize) -> Self { pub fn limit(mut self, limit: usize) -> Self {
if let Some(ref mut fut) = self.fut { if let Some(ref mut fut) = self.fut {
fut.limit = limit; fut.limit = limit;

View file

@ -147,7 +147,7 @@ impl WebsocketsRequest {
/// Set max frame size /// Set max frame size
/// ///
/// By default max size is set to 64kb /// By default max size is set to 64kB
pub fn max_frame_size(mut self, size: usize) -> Self { pub fn max_frame_size(mut self, size: usize) -> Self {
self.max_size = size; self.max_size = size;
self self

View file

@ -1,12 +1,11 @@
//! Error and Result module //! Error and Result module
pub use actix_http::error::*; pub use actix_http::error::*;
use derive_more::{Display, From}; use derive_more::{Display, Error, From};
use serde_json::error::Error as JsonError; use serde_json::error::Error as JsonError;
use url::ParseError as UrlParseError; use url::ParseError as UrlParseError;
use crate::http::StatusCode; use crate::{http::StatusCode, HttpResponse};
use crate::HttpResponse;
/// Errors which can occur when attempting to generate resource uri. /// Errors which can occur when attempting to generate resource uri.
#[derive(Debug, PartialEq, Display, From)] #[derive(Debug, PartialEq, Display, From)]
@ -28,34 +27,37 @@ impl std::error::Error for UrlGenerationError {}
impl ResponseError for UrlGenerationError {} impl ResponseError for UrlGenerationError {}
/// A set of errors that can occur during parsing urlencoded payloads /// A set of errors that can occur during parsing urlencoded payloads
#[derive(Debug, Display, From)] #[derive(Debug, Display, Error, From)]
pub enum UrlencodedError { pub enum UrlencodedError {
/// Can not decode chunked transfer encoding /// Can not decode chunked transfer encoding.
#[display(fmt = "Can not decode chunked transfer encoding")] #[display(fmt = "Can not decode chunked transfer encoding.")]
Chunked, Chunked,
/// Payload size is bigger than allowed. (default: 256kB)
/// Payload size is larger than allowed. (default limit: 256kB).
#[display( #[display(
fmt = "Urlencoded payload size is bigger ({} bytes) than allowed (default: {} bytes)", fmt = "URL encoded payload is larger ({} bytes) than allowed (limit: {} bytes).",
size, size,
limit limit
)] )]
Overflow { size: usize, limit: usize }, Overflow { size: usize, limit: usize },
/// Payload size is now known
#[display(fmt = "Payload size is now known")] /// Payload size is now known.
#[display(fmt = "Payload size is now known.")]
UnknownLength, UnknownLength,
/// Content type error
#[display(fmt = "Content type error")] /// Content type error.
#[display(fmt = "Content type error.")]
ContentType, ContentType,
/// Parse error
#[display(fmt = "Parse error")] /// Parse error.
#[display(fmt = "Parse error.")]
Parse, Parse,
/// Payload error
#[display(fmt = "Error that occur during reading payload: {}", _0)] /// Payload error.
#[display(fmt = "Error that occur during reading payload: {}.", _0)]
Payload(PayloadError), Payload(PayloadError),
} }
impl std::error::Error for UrlencodedError {}
/// Return `BadRequest` for `UrlencodedError` /// Return `BadRequest` for `UrlencodedError`
impl ResponseError for UrlencodedError { impl ResponseError for UrlencodedError {
fn status_code(&self) -> StatusCode { fn status_code(&self) -> StatusCode {
@ -115,16 +117,14 @@ impl ResponseError for PathError {
} }
} }
/// A set of errors that can occur during parsing query strings /// A set of errors that can occur during parsing query strings.
#[derive(Debug, Display, From)] #[derive(Debug, Display, Error, From)]
pub enum QueryPayloadError { pub enum QueryPayloadError {
/// Deserialize error /// Query deserialize error.
#[display(fmt = "Query deserialize error: {}", _0)] #[display(fmt = "Query deserialize error: {}", _0)]
Deserialize(serde::de::value::Error), Deserialize(serde::de::value::Error),
} }
impl std::error::Error for QueryPayloadError {}
/// Return `BadRequest` for `QueryPayloadError` /// Return `BadRequest` for `QueryPayloadError`
impl ResponseError for QueryPayloadError { impl ResponseError for QueryPayloadError {
fn status_code(&self) -> StatusCode { fn status_code(&self) -> StatusCode {

View file

@ -1,34 +1,37 @@
//! Request extractors //! Request extractors
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use actix_http::error::Error; use std::{
use futures_util::future::{ready, Ready}; future::Future,
use futures_util::ready; pin::Pin,
task::{Context, Poll},
};
use crate::dev::Payload; use futures_util::{
use crate::request::HttpRequest; future::{ready, Ready},
ready,
};
use crate::{dev::Payload, Error, HttpRequest};
/// Trait implemented by types that can be extracted from request. /// Trait implemented by types that can be extracted from request.
/// ///
/// Types that implement this trait can be used with `Route` handlers. /// Types that implement this trait can be used with `Route` handlers.
pub trait FromRequest: Sized { pub trait FromRequest: Sized {
/// Configuration for this extractor.
type Config: Default + 'static;
/// The associated error which can be returned. /// The associated error which can be returned.
type Error: Into<Error>; type Error: Into<Error>;
/// Future that resolves to a Self /// Future that resolves to a Self.
type Future: Future<Output = Result<Self, Self::Error>>; type Future: Future<Output = Result<Self, Self::Error>>;
/// Configuration for this extractor /// Create a Self from request parts asynchronously.
type Config: Default + 'static;
/// Convert request to a Self
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future; fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future;
/// Convert request to a Self /// Create a Self from request head asynchronously.
/// ///
/// This method uses `Payload::None` as payload stream. /// This method is short for `T::from_request(req, &mut Payload::None)`.
fn extract(req: &HttpRequest) -> Self::Future { fn extract(req: &HttpRequest) -> Self::Future {
Self::from_request(req, &mut Payload::None) Self::from_request(req, &mut Payload::None)
} }

View file

@ -6,7 +6,8 @@
//! use actix_web::{get, web, App, HttpServer, Responder}; //! use actix_web::{get, web, App, HttpServer, Responder};
//! //!
//! #[get("/{id}/{name}/index.html")] //! #[get("/{id}/{name}/index.html")]
//! async fn index(web::Path((id, name)): web::Path<(u32, String)>) -> impl Responder { //! async fn index(path: web::Path<(u32, String)>) -> impl Responder {
//! let (id, name) = path.into_inner();
//! format!("Hello {}! id:{}", name, id) //! format!("Hello {}! id:{}", name, id)
//! } //! }
//! //!
@ -90,7 +91,7 @@ mod scope;
mod server; mod server;
mod service; mod service;
pub mod test; pub mod test;
mod types; pub(crate) mod types;
pub mod web; pub mod web;
pub use actix_http::Response as HttpResponse; pub use actix_http::Response as HttpResponse;
@ -106,6 +107,7 @@ pub use crate::responder::Responder;
pub use crate::route::Route; pub use crate::route::Route;
pub use crate::scope::Scope; pub use crate::scope::Scope;
pub use crate::server::HttpServer; pub use crate::server::HttpServer;
// TODO: is exposing the error directly really needed
pub use crate::types::{Either, EitherExtractError}; pub use crate::types::{Either, EitherExtractError};
pub mod dev { pub mod dev {

View file

@ -15,7 +15,7 @@ use std::{
use actix_service::{Service, Transform}; use actix_service::{Service, Transform};
use bytes::Bytes; use bytes::Bytes;
use futures_util::future::{ok, Ready}; use futures_util::future::{ok, Ready};
use log::debug; use log::{debug, warn};
use regex::{Regex, RegexSet}; use regex::{Regex, RegexSet};
use time::OffsetDateTime; use time::OffsetDateTime;
@ -188,9 +188,8 @@ where
for unit in &self.0.format.0 { for unit in &self.0.format.0 {
// missing request replacement function diagnostic // missing request replacement function diagnostic
if let FormatText::CustomRequest(label, None) = unit { if let FormatText::CustomRequest(label, None) = unit {
debug!( warn!(
"No custom request replacement function was registered for label {} in\ "No custom request replacement function was registered for label \"{}\".",
logger format.",
label label
); );
} }

View file

@ -17,9 +17,10 @@ use crate::rmap::ResourceMap;
#[derive(Clone)] #[derive(Clone)]
/// An HTTP Request /// An HTTP Request
pub struct HttpRequest { pub struct HttpRequest {
// *. Rc<HttpRequestInner> is used exclusively and NO Weak<HttpRequestInner> /// # Panics
// is allowed anywhere in the code. Weak pointer is purposely ignored when /// `Rc<HttpRequestInner>` is used exclusively and NO `Weak<HttpRequestInner>`
// doing Rc's ref counter check. /// is allowed anywhere in the code. Weak pointer is purposely ignored when
/// doing `Rc`'s ref counter check. Expect panics if this invariant is violated.
pub(crate) inner: Rc<HttpRequestInner>, pub(crate) inner: Rc<HttpRequestInner>,
} }

View file

@ -4,17 +4,17 @@ use actix_http::error::InternalError;
use actix_http::http::{ use actix_http::http::{
header::IntoHeaderValue, Error as HttpError, HeaderMap, HeaderName, StatusCode, header::IntoHeaderValue, Error as HttpError, HeaderMap, HeaderName, StatusCode,
}; };
use actix_http::{Error, Response, ResponseBuilder}; use actix_http::ResponseBuilder;
use bytes::{Bytes, BytesMut}; use bytes::{Bytes, BytesMut};
use crate::request::HttpRequest; use crate::{Error, HttpRequest, HttpResponse};
/// Trait implemented by types that can be converted to a http response. /// Trait implemented by types that can be converted to a http response.
/// ///
/// Types that implement this trait can be used as the return type of a handler. /// Types that implement this trait can be used as the return type of a handler.
pub trait Responder { pub trait Responder {
/// Convert self to `Response`. /// Convert self to `HttpResponse`.
fn respond_to(self, req: &HttpRequest) -> Response; fn respond_to(self, req: &HttpRequest) -> HttpResponse;
/// Override a status code for a Responder. /// Override a status code for a Responder.
/// ///
@ -63,18 +63,18 @@ pub trait Responder {
} }
} }
impl Responder for Response { impl Responder for HttpResponse {
#[inline] #[inline]
fn respond_to(self, _: &HttpRequest) -> Response { fn respond_to(self, _: &HttpRequest) -> HttpResponse {
self self
} }
} }
impl<T: Responder> Responder for Option<T> { impl<T: Responder> Responder for Option<T> {
fn respond_to(self, req: &HttpRequest) -> Response { fn respond_to(self, req: &HttpRequest) -> HttpResponse {
match self { match self {
Some(t) => t.respond_to(req), Some(t) => t.respond_to(req),
None => Response::build(StatusCode::NOT_FOUND).finish(), None => HttpResponse::build(StatusCode::NOT_FOUND).finish(),
} }
} }
} }
@ -84,23 +84,23 @@ where
T: Responder, T: Responder,
E: Into<Error>, E: Into<Error>,
{ {
fn respond_to(self, req: &HttpRequest) -> Response { fn respond_to(self, req: &HttpRequest) -> HttpResponse {
match self { match self {
Ok(val) => val.respond_to(req), Ok(val) => val.respond_to(req),
Err(e) => Response::from_error(e.into()), Err(e) => HttpResponse::from_error(e.into()),
} }
} }
} }
impl Responder for ResponseBuilder { impl Responder for ResponseBuilder {
#[inline] #[inline]
fn respond_to(mut self, _: &HttpRequest) -> Response { fn respond_to(mut self, _: &HttpRequest) -> HttpResponse {
self.finish() self.finish()
} }
} }
impl<T: Responder> Responder for (T, StatusCode) { impl<T: Responder> Responder for (T, StatusCode) {
fn respond_to(self, req: &HttpRequest) -> Response { fn respond_to(self, req: &HttpRequest) -> HttpResponse {
let mut res = self.0.respond_to(req); let mut res = self.0.respond_to(req);
*res.status_mut() = self.1; *res.status_mut() = self.1;
res res
@ -108,49 +108,49 @@ impl<T: Responder> Responder for (T, StatusCode) {
} }
impl Responder for &'static str { impl Responder for &'static str {
fn respond_to(self, _: &HttpRequest) -> Response { fn respond_to(self, _: &HttpRequest) -> HttpResponse {
Response::build(StatusCode::OK) HttpResponse::Ok()
.content_type("text/plain; charset=utf-8") .content_type(mime::TEXT_PLAIN_UTF_8)
.body(self) .body(self)
} }
} }
impl Responder for &'static [u8] { impl Responder for &'static [u8] {
fn respond_to(self, _: &HttpRequest) -> Response { fn respond_to(self, _: &HttpRequest) -> HttpResponse {
Response::build(StatusCode::OK) HttpResponse::Ok()
.content_type("application/octet-stream") .content_type(mime::APPLICATION_OCTET_STREAM)
.body(self) .body(self)
} }
} }
impl Responder for String { impl Responder for String {
fn respond_to(self, _: &HttpRequest) -> Response { fn respond_to(self, _: &HttpRequest) -> HttpResponse {
Response::build(StatusCode::OK) HttpResponse::Ok()
.content_type("text/plain; charset=utf-8") .content_type(mime::TEXT_PLAIN_UTF_8)
.body(self) .body(self)
} }
} }
impl<'a> Responder for &'a String { impl<'a> Responder for &'a String {
fn respond_to(self, _: &HttpRequest) -> Response { fn respond_to(self, _: &HttpRequest) -> HttpResponse {
Response::build(StatusCode::OK) HttpResponse::Ok()
.content_type("text/plain; charset=utf-8") .content_type(mime::TEXT_PLAIN_UTF_8)
.body(self) .body(self)
} }
} }
impl Responder for Bytes { impl Responder for Bytes {
fn respond_to(self, _: &HttpRequest) -> Response { fn respond_to(self, _: &HttpRequest) -> HttpResponse {
Response::build(StatusCode::OK) HttpResponse::Ok()
.content_type("application/octet-stream") .content_type(mime::APPLICATION_OCTET_STREAM)
.body(self) .body(self)
} }
} }
impl Responder for BytesMut { impl Responder for BytesMut {
fn respond_to(self, _: &HttpRequest) -> Response { fn respond_to(self, _: &HttpRequest) -> HttpResponse {
Response::build(StatusCode::OK) HttpResponse::Ok()
.content_type("application/octet-stream") .content_type(mime::APPLICATION_OCTET_STREAM)
.body(self) .body(self)
} }
} }
@ -231,7 +231,7 @@ impl<T: Responder> CustomResponder<T> {
} }
impl<T: Responder> Responder for CustomResponder<T> { impl<T: Responder> Responder for CustomResponder<T> {
fn respond_to(self, req: &HttpRequest) -> Response { fn respond_to(self, req: &HttpRequest) -> HttpResponse {
let mut res = self.responder.respond_to(req); let mut res = self.responder.respond_to(req);
if let Some(status) = self.status { if let Some(status) = self.status {
@ -252,8 +252,8 @@ impl<T> Responder for InternalError<T>
where where
T: std::fmt::Debug + std::fmt::Display + 'static, T: std::fmt::Debug + std::fmt::Display + 'static,
{ {
fn respond_to(self, _: &HttpRequest) -> Response { fn respond_to(self, _: &HttpRequest) -> HttpResponse {
Response::from_error(self.into()) HttpResponse::from_error(self.into())
} }
} }

View file

@ -1,93 +1,170 @@
use actix_http::{Error, Response}; //! For either helper, see [`Either`].
use bytes::Bytes; use bytes::Bytes;
use futures_util::{future::LocalBoxFuture, FutureExt, TryFutureExt}; use futures_util::{future::LocalBoxFuture, FutureExt, TryFutureExt};
use crate::{dev, request::HttpRequest, FromRequest, Responder}; use crate::{
dev,
web::{Form, Json},
Error, FromRequest, HttpRequest, HttpResponse, Responder,
};
/// Combines two different responder types into a single type /// Combines two extractor or responder types into a single type.
/// ///
/// ```rust /// Can be converted to and from an [`either::Either`].
/// use actix_web::{Either, Error, HttpResponse};
/// ///
/// type RegisterResult = Either<HttpResponse, Result<HttpResponse, Error>>; /// # Extractor
/// Provides a mechanism for trying two extractors, a primary and a fallback. Useful for
/// "polymorphic payloads" where, for example, a form might be JSON or URL encoded.
/// ///
/// fn index() -> RegisterResult { /// It is important to note that this extractor, by necessity, buffers the entire request payload
/// if is_a_variant() { /// as part of its implementation. Though, it does respect any `PayloadConfig` maximum size limits.
/// // <- choose left variant ///
/// Either::A(HttpResponse::BadRequest().body("Bad data")) /// ```
/// use actix_web::{post, web, Either};
/// use serde::Deserialize;
///
/// #[derive(Deserialize)]
/// struct Info {
/// name: String,
/// }
///
/// // handler that accepts form as JSON or form-urlencoded.
/// #[post("/")]
/// async fn index(form: Either<web::Json<Info>, web::Form<Info>>) -> String {
/// let name: String = match form {
/// Either::Left(json) => json.name.to_owned(),
/// Either::Right(form) => form.name.to_owned(),
/// };
///
/// format!("Welcome {}!", name)
/// }
/// ```
///
/// # Responder
/// It may be desireable to use a concrete type for a response with multiple branches. As long as
/// both types implement `Responder`, so will the `Either` type, enabling it to be used as a
/// handler's return type.
///
/// All properties of a response are determined by the Responder branch returned.
///
/// ```
/// use actix_web::{get, Either, Error, HttpResponse};
///
/// #[get("/")]
/// async fn index() -> Either<&'static str, Result<HttpResponse, Error>> {
/// if 1 == 2 {
/// // respond with Left variant
/// Either::Left("Bad data")
/// } else { /// } else {
/// Either::B( /// // respond with Right variant
/// // <- Right variant /// Either::Right(
/// Ok(HttpResponse::Ok() /// Ok(HttpResponse::Ok()
/// .content_type("text/html") /// .content_type(mime::TEXT_HTML)
/// .body("Hello!")) /// .body("<p>Hello!</p>"))
/// ) /// )
/// } /// }
/// } /// }
/// # fn is_a_variant() -> bool { true }
/// # fn main() {}
/// ``` /// ```
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum Either<A, B> { pub enum Either<L, R> {
/// First branch of the type /// A value of type `L`.
A(A), Left(L),
/// Second branch of the type
B(B), /// A value of type `R`.
Right(R),
}
impl<T> Either<Form<T>, Json<T>> {
pub fn into_inner(self) -> T {
match self {
Either::Left(form) => form.into_inner(),
Either::Right(form) => form.into_inner(),
}
}
}
impl<T> Either<Json<T>, Form<T>> {
pub fn into_inner(self) -> T {
match self {
Either::Left(form) => form.into_inner(),
Either::Right(form) => form.into_inner(),
}
}
}
impl<L, R> From<either::Either<L, R>> for Either<L, R> {
fn from(val: either::Either<L, R>) -> Self {
match val {
either::Either::Left(l) => Either::Left(l),
either::Either::Right(r) => Either::Right(r),
}
}
}
impl<L, R> From<Either<L, R>> for either::Either<L, R> {
fn from(val: Either<L, R>) -> Self {
match val {
Either::Left(l) => either::Either::Left(l),
Either::Right(r) => either::Either::Right(r),
}
}
} }
#[cfg(test)] #[cfg(test)]
impl<A, B> Either<A, B> { impl<L, R> Either<L, R> {
pub(self) fn unwrap_left(self) -> A { pub(self) fn unwrap_left(self) -> L {
match self { match self {
Either::A(data) => data, Either::Left(data) => data,
Either::B(_) => { Either::Right(_) => {
panic!("Cannot unwrap left branch. Either contains a right branch.") panic!("Cannot unwrap Left branch. Either contains an `R` type.")
} }
} }
} }
pub(self) fn unwrap_right(self) -> B { pub(self) fn unwrap_right(self) -> R {
match self { match self {
Either::A(_) => { Either::Left(_) => {
panic!("Cannot unwrap right branch. Either contains a left branch.") panic!("Cannot unwrap Right branch. Either contains an `L` type.")
} }
Either::B(data) => data, Either::Right(data) => data,
} }
} }
} }
impl<A, B> Responder for Either<A, B> /// See [here](#responder) for example of usage as a handler return type.
impl<L, R> Responder for Either<L, R>
where where
A: Responder, L: Responder,
B: Responder, R: Responder,
{ {
fn respond_to(self, req: &HttpRequest) -> Response { fn respond_to(self, req: &HttpRequest) -> HttpResponse {
match self { match self {
Either::A(a) => a.respond_to(req), Either::Left(a) => a.respond_to(req),
Either::B(b) => b.respond_to(req), Either::Right(b) => b.respond_to(req),
} }
} }
} }
/// A composite error resulting from failure to extract an `Either<A, B>`. /// A composite error resulting from failure to extract an `Either<L, R>`.
/// ///
/// The implementation of `Into<actix_web::Error>` will return the payload buffering error or the /// The implementation of `Into<actix_web::Error>` will return the payload buffering error or the
/// error from the primary extractor. To access the fallback error, use a match clause. /// error from the primary extractor. To access the fallback error, use a match clause.
#[derive(Debug)] #[derive(Debug)]
pub enum EitherExtractError<A, B> { pub enum EitherExtractError<L, R> {
/// Error from payload buffering, such as exceeding payload max size limit. /// Error from payload buffering, such as exceeding payload max size limit.
Bytes(Error), Bytes(Error),
/// Error from primary extractor. /// Error from primary extractor.
Extract(A, B), Extract(L, R),
} }
impl<A, B> From<EitherExtractError<A, B>> for Error impl<L, R> From<EitherExtractError<L, R>> for Error
where where
A: Into<Error>, L: Into<Error>,
B: Into<Error>, R: Into<Error>,
{ {
fn from(err: EitherExtractError<A, B>) -> Error { fn from(err: EitherExtractError<L, R>) -> Error {
match err { match err {
EitherExtractError::Bytes(err) => err, EitherExtractError::Bytes(err) => err,
EitherExtractError::Extract(a_err, _b_err) => a_err.into(), EitherExtractError::Extract(a_err, _b_err) => a_err.into(),
@ -95,17 +172,13 @@ where
} }
} }
/// Provides a mechanism for trying two extractors, a primary and a fallback. Useful for /// See [here](#extractor) for example of usage as an extractor.
/// "polymorphic payloads" where, for example, a form might be JSON or URL encoded. impl<L, R> FromRequest for Either<L, R>
///
/// It is important to note that this extractor, by necessity, buffers the entire request payload
/// as part of its implementation. Though, it does respect a `PayloadConfig`'s maximum size limit.
impl<A, B> FromRequest for Either<A, B>
where where
A: FromRequest + 'static, L: FromRequest + 'static,
B: FromRequest + 'static, R: FromRequest + 'static,
{ {
type Error = EitherExtractError<A::Error, B::Error>; type Error = EitherExtractError<L::Error, R::Error>;
type Future = LocalBoxFuture<'static, Result<Self, Self::Error>>; type Future = LocalBoxFuture<'static, Result<Self, Self::Error>>;
type Config = (); type Config = ();
@ -114,32 +187,32 @@ where
Bytes::from_request(req, payload) Bytes::from_request(req, payload)
.map_err(EitherExtractError::Bytes) .map_err(EitherExtractError::Bytes)
.and_then(|bytes| bytes_to_a_or_b(req2, bytes)) .and_then(|bytes| bytes_to_l_or_r(req2, bytes))
.boxed_local() .boxed_local()
} }
} }
async fn bytes_to_a_or_b<A, B>( async fn bytes_to_l_or_r<L, R>(
req: HttpRequest, req: HttpRequest,
bytes: Bytes, bytes: Bytes,
) -> Result<Either<A, B>, EitherExtractError<A::Error, B::Error>> ) -> Result<Either<L, R>, EitherExtractError<L::Error, R::Error>>
where where
A: FromRequest + 'static, L: FromRequest + 'static,
B: FromRequest + 'static, R: FromRequest + 'static,
{ {
let fallback = bytes.clone(); let fallback = bytes.clone();
let a_err; let a_err;
let mut pl = payload_from_bytes(bytes); let mut pl = payload_from_bytes(bytes);
match A::from_request(&req, &mut pl).await { match L::from_request(&req, &mut pl).await {
Ok(a_data) => return Ok(Either::A(a_data)), Ok(a_data) => return Ok(Either::Left(a_data)),
// store A's error for returning if B also fails // store A's error for returning if B also fails
Err(err) => a_err = err, Err(err) => a_err = err,
}; };
let mut pl = payload_from_bytes(fallback); let mut pl = payload_from_bytes(fallback);
match B::from_request(&req, &mut pl).await { match R::from_request(&req, &mut pl).await {
Ok(b_data) => return Ok(Either::B(b_data)), Ok(b_data) => return Ok(Either::Right(b_data)),
Err(b_err) => Err(EitherExtractError::Extract(a_err, b_err)), Err(b_err) => Err(EitherExtractError::Extract(a_err, b_err)),
} }
} }

View file

@ -1,72 +1,68 @@
//! Form extractor //! For URL encoded form helper documentation, see [`Form`].
use std::future::Future; use std::{
use std::pin::Pin; fmt,
use std::rc::Rc; future::Future,
use std::task::{Context, Poll}; ops,
use std::{fmt, ops}; pin::Pin,
rc::Rc,
task::{Context, Poll},
};
use actix_http::{Error, HttpMessage, Payload, Response}; use actix_http::Payload;
use bytes::BytesMut; use bytes::BytesMut;
use encoding_rs::{Encoding, UTF_8}; use encoding_rs::{Encoding, UTF_8};
use futures_util::future::{FutureExt, LocalBoxFuture}; use futures_util::{
use futures_util::StreamExt; future::{FutureExt, LocalBoxFuture},
use serde::de::DeserializeOwned; StreamExt,
use serde::Serialize; };
use serde::{de::DeserializeOwned, Serialize};
#[cfg(feature = "compress")] #[cfg(feature = "compress")]
use crate::dev::Decompress; use crate::dev::Decompress;
use crate::error::UrlencodedError; use crate::{
use crate::extract::FromRequest; error::UrlencodedError, extract::FromRequest, http::header::CONTENT_LENGTH, web,
use crate::http::{ Error, HttpMessage, HttpRequest, HttpResponse, Responder,
header::{ContentType, CONTENT_LENGTH},
StatusCode,
}; };
use crate::request::HttpRequest;
use crate::{responder::Responder, web};
/// Form data helper (`application/x-www-form-urlencoded`) /// URL encoded payload extractor and responder.
/// ///
/// Can be use to extract url-encoded data from the request body, /// `Form` has two uses: URL encoded responses, and extracting typed data from URL request payloads.
/// or send url-encoded data as the response.
/// ///
/// ## Extract /// # Extractor
/// To extract typed data from a request body, the inner type `T` must implement the
/// [`serde::Deserialize`] trait.
/// ///
/// To extract typed information from request's body, the type `T` must /// Use [`FormConfig`] to configure extraction process.
/// implement the `Deserialize` trait from *serde*.
/// ///
/// [**FormConfig**](FormConfig) allows to configure extraction /// ```
/// process. /// use actix_web::{post, web};
/// /// use serde::Deserialize;
/// ### Example
/// ```rust
/// use actix_web::web;
/// use serde_derive::Deserialize;
/// ///
/// #[derive(Deserialize)] /// #[derive(Deserialize)]
/// struct FormData { /// struct Info {
/// username: String, /// name: String,
/// } /// }
/// ///
/// /// Extract form data using serde. /// // This handler is only called if:
/// /// This handler get called only if content type is *x-www-form-urlencoded* /// // - request headers declare the content type as `application/x-www-form-urlencoded`
/// /// and content of the request could be deserialized to a `FormData` struct /// // - request payload is deserialized into a `Info` struct from the URL encoded format
/// fn index(form: web::Form<FormData>) -> String { /// #[post("/")]
/// format!("Welcome {}!", form.username) /// async fn index(form: web::Form<Info>) -> String {
/// format!("Welcome {}!", form.name)
/// } /// }
/// # fn main() {}
/// ``` /// ```
/// ///
/// ## Respond /// # Responder
/// The `Form` type also allows you to create URL encoded responses:
/// simply return a value of type Form<T> where T is the type to be URL encoded.
/// The type must implement [`serde::Serialize`].
/// ///
/// The `Form` type also allows you to respond with well-formed url-encoded data: /// Responses use
/// 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 /// use actix_web::{get, web};
/// use actix_web::*; /// use serde::Serialize;
/// use serde_derive::Serialize;
/// ///
/// #[derive(Serialize)] /// #[derive(Serialize)]
/// struct SomeForm { /// struct SomeForm {
@ -74,22 +70,23 @@ use crate::{responder::Responder, web};
/// age: u8 /// age: u8
/// } /// }
/// ///
/// // Will return a 200 response with header /// // Response will have:
/// // `Content-Type: application/x-www-form-urlencoded` /// // - status: 200 OK
/// // and body "name=actix&age=123" /// // - header: `Content-Type: application/x-www-form-urlencoded`
/// fn index() -> web::Form<SomeForm> { /// // - body: `name=actix&age=123`
/// #[get("/")]
/// async fn index() -> web::Form<SomeForm> {
/// web::Form(SomeForm { /// web::Form(SomeForm {
/// name: "actix".into(), /// name: "actix".into(),
/// age: 123 /// age: 123
/// }) /// })
/// } /// }
/// # fn main() {}
/// ``` /// ```
#[derive(PartialEq, Eq, PartialOrd, Ord)] #[derive(PartialEq, Eq, PartialOrd, Ord)]
pub struct Form<T>(pub T); pub struct Form<T>(pub T);
impl<T> Form<T> { impl<T> Form<T> {
/// Deconstruct to an inner value /// Unwrap into inner `T` value.
pub fn into_inner(self) -> T { pub fn into_inner(self) -> T {
self.0 self.0
} }
@ -109,6 +106,7 @@ impl<T> ops::DerefMut for Form<T> {
} }
} }
/// See [here](#extractor) for example of usage as an extractor.
impl<T> FromRequest for Form<T> impl<T> FromRequest for Form<T>
where where
T: DeserializeOwned + 'static, T: DeserializeOwned + 'static,
@ -120,7 +118,7 @@ where
#[inline] #[inline]
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future { fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
let req2 = req.clone(); let req2 = req.clone();
let (limit, err) = req let (limit, err_handler) = req
.app_data::<Self::Config>() .app_data::<Self::Config>()
.or_else(|| { .or_else(|| {
req.app_data::<web::Data<Self::Config>>() req.app_data::<web::Data<Self::Config>>()
@ -132,13 +130,10 @@ where
UrlEncoded::new(req, payload) UrlEncoded::new(req, payload)
.limit(limit) .limit(limit)
.map(move |res| match res { .map(move |res| match res {
Err(e) => { Err(err) => match err_handler {
if let Some(err) = err { Some(err_handler) => Err((err_handler)(err, &req2)),
Err((*err)(e, &req2)) None => Err(err.into()),
} else { },
Err(e.into())
}
}
Ok(item) => Ok(Form(item)), Ok(item) => Ok(Form(item)),
}) })
.boxed_local() .boxed_local()
@ -157,44 +152,39 @@ impl<T: fmt::Display> fmt::Display for Form<T> {
} }
} }
/// See [here](#responder) for example of usage as a handler return type.
impl<T: Serialize> Responder for Form<T> { impl<T: Serialize> Responder for Form<T> {
fn respond_to(self, _: &HttpRequest) -> Response { fn respond_to(self, _: &HttpRequest) -> HttpResponse {
match serde_urlencoded::to_string(&self.0) { match serde_urlencoded::to_string(&self.0) {
Ok(body) => Response::build(StatusCode::OK) Ok(body) => HttpResponse::Ok()
.set(ContentType::form_url_encoded()) .content_type(mime::APPLICATION_WWW_FORM_URLENCODED)
.body(body), .body(body),
Err(e) => Response::from_error(e.into()), Err(err) => HttpResponse::from_error(err.into()),
} }
} }
} }
/// Form extractor configuration /// [`Form`] extractor configuration.
/// ///
/// ```rust /// ```
/// use actix_web::{web, App, FromRequest, Result}; /// use actix_web::{post, web, App, FromRequest, Result};
/// use serde_derive::Deserialize; /// use serde::Deserialize;
/// ///
/// #[derive(Deserialize)] /// #[derive(Deserialize)]
/// struct FormData { /// struct Info {
/// username: String, /// username: String,
/// } /// }
/// ///
/// /// Extract form data using serde. /// // Custom `FormConfig` is applied to App.
/// /// Custom configuration is used for this handler, max payload size is 4k /// // Max payload size for URL encoded forms is set to 4kB.
/// async fn index(form: web::Form<FormData>) -> Result<String> { /// #[post("/")]
/// async fn index(form: web::Form<Info>) -> Result<String> {
/// Ok(format!("Welcome {}!", form.username)) /// Ok(format!("Welcome {}!", form.username))
/// } /// }
/// ///
/// fn main() { /// App::new()
/// let app = App::new().service( /// .app_data(web::FormConfig::default().limit(4096))
/// web::resource("/index.html") /// .service(index);
/// // change `Form` extractor configuration
/// .app_data(
/// web::FormConfig::default().limit(4097)
/// )
/// .route(web::get().to(index))
/// );
/// }
/// ``` /// ```
#[derive(Clone)] #[derive(Clone)]
pub struct FormConfig { pub struct FormConfig {
@ -203,7 +193,7 @@ pub struct FormConfig {
} }
impl FormConfig { impl FormConfig {
/// Change max size of payload. By default max size is 16Kb /// Set maximum accepted payload size. By default this limit is 16kB.
pub fn limit(mut self, limit: usize) -> Self { pub fn limit(mut self, limit: usize) -> Self {
self.limit = limit; self.limit = limit;
self self
@ -228,33 +218,30 @@ impl Default for FormConfig {
} }
} }
/// Future that resolves to a parsed urlencoded values. /// Future that resolves to some `T` when parsed from a URL encoded payload.
/// ///
/// Parse `application/x-www-form-urlencoded` encoded request's body. /// Form can be deserialized from any type `T` that implements [`serde::Deserialize`].
/// Return `UrlEncoded` future. Form can be deserialized to any type that
/// implements `Deserialize` trait from *serde*.
/// ///
/// Returns error: /// Returns error if:
/// /// - content type is not `application/x-www-form-urlencoded`
/// * content type is not `application/x-www-form-urlencoded` /// - content length is greater than [limit](UrlEncoded::limit())
/// * content-length is greater than 32k pub struct UrlEncoded<T> {
///
pub struct UrlEncoded<U> {
#[cfg(feature = "compress")] #[cfg(feature = "compress")]
stream: Option<Decompress<Payload>>, stream: Option<Decompress<Payload>>,
#[cfg(not(feature = "compress"))] #[cfg(not(feature = "compress"))]
stream: Option<Payload>, stream: Option<Payload>,
limit: usize, limit: usize,
length: Option<usize>, length: Option<usize>,
encoding: &'static Encoding, encoding: &'static Encoding,
err: Option<UrlencodedError>, err: Option<UrlencodedError>,
fut: Option<LocalBoxFuture<'static, Result<U, UrlencodedError>>>, fut: Option<LocalBoxFuture<'static, Result<T, UrlencodedError>>>,
} }
#[allow(clippy::borrow_interior_mutable_const)] #[allow(clippy::borrow_interior_mutable_const)]
impl<U> UrlEncoded<U> { impl<T> UrlEncoded<T> {
/// Create a new future to URL encode a request /// Create a new future to decode a URL encoded request payload.
pub fn new(req: &HttpRequest, payload: &mut Payload) -> UrlEncoded<U> { pub fn new(req: &HttpRequest, payload: &mut Payload) -> Self {
// check content type // check content type
if req.content_type().to_lowercase() != "application/x-www-form-urlencoded" { if req.content_type().to_lowercase() != "application/x-www-form-urlencoded" {
return Self::err(UrlencodedError::ContentType); return Self::err(UrlencodedError::ContentType);
@ -292,29 +279,29 @@ impl<U> UrlEncoded<U> {
} }
} }
fn err(e: UrlencodedError) -> Self { fn err(err: UrlencodedError) -> Self {
UrlEncoded { UrlEncoded {
stream: None, stream: None,
limit: 32_768, limit: 32_768,
fut: None, fut: None,
err: Some(e), err: Some(err),
length: None, length: None,
encoding: UTF_8, encoding: UTF_8,
} }
} }
/// Change max size of payload. By default max size is 256Kb /// Set maximum accepted payload size. The default limit is 256kB.
pub fn limit(mut self, limit: usize) -> Self { pub fn limit(mut self, limit: usize) -> Self {
self.limit = limit; self.limit = limit;
self self
} }
} }
impl<U> Future for UrlEncoded<U> impl<T> Future for UrlEncoded<T>
where where
U: DeserializeOwned + 'static, T: DeserializeOwned + 'static,
{ {
type Output = Result<U, UrlencodedError>; type Output = Result<T, UrlencodedError>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if let Some(ref mut fut) = self.fut { if let Some(ref mut fut) = self.fut {
@ -343,6 +330,7 @@ where
while let Some(item) = stream.next().await { while let Some(item) = stream.next().await {
let chunk = item?; let chunk = item?;
if (body.len() + chunk.len()) > limit { if (body.len() + chunk.len()) > limit {
return Err(UrlencodedError::Overflow { return Err(UrlencodedError::Overflow {
size: body.len() + chunk.len(), size: body.len() + chunk.len(),
@ -354,19 +342,21 @@ where
} }
if encoding == UTF_8 { if encoding == UTF_8 {
serde_urlencoded::from_bytes::<U>(&body) serde_urlencoded::from_bytes::<T>(&body)
.map_err(|_| UrlencodedError::Parse) .map_err(|_| UrlencodedError::Parse)
} else { } else {
let body = encoding let body = encoding
.decode_without_bom_handling_and_without_replacement(&body) .decode_without_bom_handling_and_without_replacement(&body)
.map(|s| s.into_owned()) .map(|s| s.into_owned())
.ok_or(UrlencodedError::Parse)?; .ok_or(UrlencodedError::Parse)?;
serde_urlencoded::from_str::<U>(&body)
serde_urlencoded::from_str::<T>(&body)
.map_err(|_| UrlencodedError::Parse) .map_err(|_| UrlencodedError::Parse)
} }
} }
.boxed_local(), .boxed_local(),
); );
self.poll(cx) self.poll(cx)
} }
} }
@ -377,7 +367,10 @@ mod tests {
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use super::*; use super::*;
use crate::http::header::{HeaderValue, CONTENT_LENGTH, CONTENT_TYPE}; use crate::http::{
header::{HeaderValue, CONTENT_LENGTH, CONTENT_TYPE},
StatusCode,
};
use crate::test::TestRequest; use crate::test::TestRequest;
#[derive(Deserialize, Serialize, Debug, PartialEq)] #[derive(Deserialize, Serialize, Debug, PartialEq)]
@ -514,6 +507,6 @@ mod tests {
assert!(s.is_err()); assert!(s.is_err());
let err_str = s.err().unwrap().to_string(); let err_str = s.err().unwrap().to_string();
assert!(err_str.contains("Urlencoded payload size is bigger")); assert!(err_str.starts_with("URL encoded payload is larger"));
} }
} }

View file

@ -1,45 +1,44 @@
//! Json extractor/responder //! For JSON helper documentation, see [`Json`].
use std::future::Future; use std::{
use std::marker::PhantomData; fmt,
use std::pin::Pin; future::Future,
use std::sync::Arc; marker::PhantomData,
use std::task::{Context, Poll}; ops,
use std::{fmt, ops}; pin::Pin,
sync::Arc,
task::{Context, Poll},
};
use bytes::BytesMut; use bytes::BytesMut;
use futures_util::ready; use futures_util::{ready, stream::Stream};
use futures_util::stream::Stream; use serde::{de::DeserializeOwned, Serialize};
use serde::de::DeserializeOwned;
use serde::Serialize;
use actix_http::http::{header::CONTENT_LENGTH, StatusCode}; use actix_http::Payload;
use actix_http::{HttpMessage, Payload, Response};
#[cfg(feature = "compress")] #[cfg(feature = "compress")]
use crate::dev::Decompress; use crate::dev::Decompress;
use crate::error::{Error, JsonPayloadError}; use crate::{
use crate::extract::FromRequest; error::{Error, JsonPayloadError},
use crate::request::HttpRequest; extract::FromRequest,
use crate::{responder::Responder, web}; http::header::CONTENT_LENGTH,
request::HttpRequest,
web, HttpMessage, HttpResponse, Responder,
};
/// Json helper /// JSON extractor and responder.
/// ///
/// Json can be used for two different purpose. First is for json response /// `Json` has two uses: JSON responses, and extracting typed data from JSON request payloads.
/// generation and second is for extracting typed information from request's
/// payload.
/// ///
/// To extract typed information from request's body, the type `T` must /// # Extractor
/// implement the `Deserialize` trait from *serde*. /// To extract typed data from a request body, the inner type `T` must implement the
/// [`serde::Deserialize`] trait.
/// ///
/// [**JsonConfig**](JsonConfig) allows to configure extraction /// Use [`JsonConfig`] to configure extraction process.
/// process.
/// ///
/// ## Example /// ```
/// /// use actix_web::{post, web, App};
/// ```rust /// use serde::Deserialize;
/// use actix_web::{web, App};
/// use serde_derive::Deserialize;
/// ///
/// #[derive(Deserialize)] /// #[derive(Deserialize)]
/// struct Info { /// struct Info {
@ -47,43 +46,37 @@ use crate::{responder::Responder, web};
/// } /// }
/// ///
/// /// deserialize `Info` from request's body /// /// deserialize `Info` from request's body
/// #[post("/")]
/// async fn index(info: web::Json<Info>) -> String { /// async fn index(info: web::Json<Info>) -> String {
/// format!("Welcome {}!", info.username) /// format!("Welcome {}!", info.username)
/// } /// }
///
/// fn main() {
/// let app = App::new().service(
/// web::resource("/index.html").route(
/// web::post().to(index))
/// );
/// }
/// ``` /// ```
/// ///
/// The `Json` type allows you to respond with well-formed JSON data: simply /// # Responder
/// return a value of type Json<T> where T is the type of a structure /// The `Json` type JSON formatted responses. A handler may return a value of type
/// to serialize into *JSON*. The type `T` must implement the `Serialize` /// `Json<T>` where `T` is the type of a structure to serialize into JSON. The type `T` must
/// trait from *serde*. /// implement [`serde::Serialize`].
/// ///
/// ```rust /// ```
/// use actix_web::*; /// use actix_web::{post, web, HttpRequest};
/// use serde_derive::Serialize; /// use serde::Serialize;
/// ///
/// #[derive(Serialize)] /// #[derive(Serialize)]
/// struct MyObj { /// struct Info {
/// name: String, /// name: String,
/// } /// }
/// ///
/// fn index(req: HttpRequest) -> Result<web::Json<MyObj>> { /// #[post("/{name}")]
/// Ok(web::Json(MyObj { /// async fn index(req: HttpRequest) -> web::Json<Info> {
/// name: req.match_info().get("name").unwrap().to_string(), /// web::Json(Info {
/// })) /// name: req.match_info().get("name").unwrap().to_owned(),
/// })
/// } /// }
/// # fn main() {}
/// ``` /// ```
pub struct Json<T>(pub T); pub struct Json<T>(pub T);
impl<T> Json<T> { impl<T> Json<T> {
/// Deconstruct to an inner value /// Unwrap into inner `T` value.
pub fn into_inner(self) -> T { pub fn into_inner(self) -> T {
self.0 self.0
} }
@ -121,49 +114,21 @@ where
} }
} }
/// Creates response with OK status code, correct content type header, and serialized JSON payload.
///
/// If serialization failed
impl<T: Serialize> Responder for Json<T> { impl<T: Serialize> Responder for Json<T> {
fn respond_to(self, _: &HttpRequest) -> Response { fn respond_to(self, _: &HttpRequest) -> HttpResponse {
match serde_json::to_string(&self.0) { match serde_json::to_string(&self.0) {
Ok(body) => Response::build(StatusCode::OK) Ok(body) => HttpResponse::Ok()
.content_type("application/json") .content_type(mime::APPLICATION_JSON)
.body(body), .body(body),
Err(e) => Response::from_error(e.into()), Err(err) => HttpResponse::from_error(err.into()),
} }
} }
} }
/// Json extractor. Allow to extract typed information from request's /// See [here](#extractor) for example of usage as an extractor.
/// payload.
///
/// To extract typed information from request's body, the type `T` must
/// implement the `Deserialize` trait from *serde*.
///
/// [**JsonConfig**](JsonConfig) allows to configure extraction
/// process.
///
/// ## Example
///
/// ```rust
/// use actix_web::{web, App};
/// use serde_derive::Deserialize;
///
/// #[derive(Deserialize)]
/// struct Info {
/// username: String,
/// }
///
/// /// deserialize `Info` from request's body
/// async fn index(info: web::Json<Info>) -> String {
/// format!("Welcome {}!", info.username)
/// }
///
/// fn main() {
/// let app = App::new().service(
/// web::resource("/index.html").route(
/// web::post().to(index))
/// );
/// }
/// ```
impl<T> FromRequest for Json<T> impl<T> FromRequest for Json<T>
where where
T: DeserializeOwned + 'static, T: DeserializeOwned + 'static,
@ -209,7 +174,7 @@ where
let res = ready!(Pin::new(&mut this.fut).poll(cx)); let res = ready!(Pin::new(&mut this.fut).poll(cx));
let res = match res { let res = match res {
Err(e) => { Err(err) => {
let req = this.req.take().unwrap(); let req = this.req.take().unwrap();
log::debug!( log::debug!(
"Failed to deserialize Json from payload. \ "Failed to deserialize Json from payload. \
@ -217,10 +182,10 @@ where
req.path() req.path()
); );
if let Some(err) = this.err_handler.as_ref() { if let Some(err_handler) = this.err_handler.as_ref() {
Err((*err)(e, &req)) Err((*err_handler)(err, &req))
} else { } else {
Err(e.into()) Err(err.into())
} }
} }
Ok(data) => Ok(Json(data)), Ok(data) => Ok(Json(data)),
@ -230,44 +195,39 @@ where
} }
} }
/// Json extractor configuration /// `Json` extractor configuration.
/// ///
/// # Example /// # Usage
/// /// ```
/// ```rust /// use actix_web::{error, post, web, App, FromRequest, HttpResponse};
/// use actix_web::{error, web, App, FromRequest, HttpResponse}; /// use serde::Deserialize;
/// use serde_derive::Deserialize;
/// ///
/// #[derive(Deserialize)] /// #[derive(Deserialize)]
/// struct Info { /// struct Info {
/// username: String, /// name: String,
/// } /// }
/// ///
/// /// deserialize `Info` from request's body, max payload size is 4kb /// // `Json` extraction is bound by custom `JsonConfig` applied to App.
/// #[post("/")]
/// async fn index(info: web::Json<Info>) -> String { /// async fn index(info: web::Json<Info>) -> String {
/// format!("Welcome {}!", info.username) /// format!("Welcome {}!", info.name)
/// } /// }
/// ///
/// fn main() { /// // custom `Json` extractor configuration
/// let app = App::new().service( /// let json_cfg = web::JsonConfig::default()
/// web::resource("/index.html") /// // limit request payload size
/// .app_data( /// .limit(4096)
/// // Json extractor configuration for this resource. /// // only accept text/plain content type
/// web::JsonConfig::default() /// .content_type(|mime| mime == mime::TEXT_PLAIN)
/// .limit(4096) // Limit request payload size /// // use custom error handler
/// .content_type(|mime| { // <- accept text/plain content type /// .error_handler(|err, req| {
/// mime.type_() == mime::TEXT && mime.subtype() == mime::PLAIN /// error::InternalError::from_response(err, HttpResponse::Conflict().finish()).into()
/// }) /// });
/// .error_handler(|err, req| { // <- create custom error response ///
/// error::InternalError::from_response( /// App::new()
/// err, HttpResponse::Conflict().finish()).into() /// .app_data(json_cfg)
/// }) /// .service(index);
/// )
/// .route(web::post().to(index))
/// );
/// }
/// ``` /// ```
///
#[derive(Clone)] #[derive(Clone)]
pub struct JsonConfig { pub struct JsonConfig {
limit: usize, limit: usize,
@ -276,13 +236,13 @@ pub struct JsonConfig {
} }
impl JsonConfig { impl JsonConfig {
/// Change max size of payload. By default max size is 32Kb /// Set maximum accepted payload size. By default this limit is 32kB.
pub fn limit(mut self, limit: usize) -> Self { pub fn limit(mut self, limit: usize) -> Self {
self.limit = limit; self.limit = limit;
self self
} }
/// Set custom error handler /// Set custom error handler.
pub fn error_handler<F>(mut self, f: F) -> Self pub fn error_handler<F>(mut self, f: F) -> Self
where where
F: Fn(JsonPayloadError, &HttpRequest) -> Error + Send + Sync + 'static, F: Fn(JsonPayloadError, &HttpRequest) -> Error + Send + Sync + 'static,
@ -291,7 +251,7 @@ impl JsonConfig {
self self
} }
/// Set predicate for allowed content types /// Set predicate for allowed content types.
pub fn content_type<F>(mut self, predicate: F) -> Self pub fn content_type<F>(mut self, predicate: F) -> Self
where where
F: Fn(mime::Mime) -> bool + Send + Sync + 'static, F: Fn(mime::Mime) -> bool + Send + Sync + 'static,
@ -322,15 +282,14 @@ impl Default for JsonConfig {
} }
} }
/// Request's payload json parser, it resolves to a deserialized `T` value. /// Future that resolves to some `T` when parsed from a JSON payload.
/// This future could be used with `ServiceRequest` and `ServiceFromRequest`.
/// ///
/// Returns error: /// Form can be deserialized from any type `T` that implements [`serde::Deserialize`].
/// ///
/// * content type is not `application/json` /// Returns error if:
/// (unless specified in [`JsonConfig`]) /// - content type is not `application/json`
/// * content length is greater than 256k /// - content length is greater than [limit](JsonBody::limit())
pub enum JsonBody<U> { pub enum JsonBody<T> {
Error(Option<JsonPayloadError>), Error(Option<JsonPayloadError>),
Body { Body {
limit: usize, limit: usize,
@ -340,17 +299,17 @@ pub enum JsonBody<U> {
#[cfg(not(feature = "compress"))] #[cfg(not(feature = "compress"))]
payload: Payload, payload: Payload,
buf: BytesMut, buf: BytesMut,
_res: PhantomData<U>, _res: PhantomData<T>,
}, },
} }
impl<U> Unpin for JsonBody<U> {} impl<T> Unpin for JsonBody<T> {}
impl<U> JsonBody<U> impl<T> JsonBody<T>
where where
U: DeserializeOwned + 'static, T: DeserializeOwned + 'static,
{ {
/// Create `JsonBody` for request. /// Create a new future to decode a JSON request payload.
#[allow(clippy::borrow_interior_mutable_const)] #[allow(clippy::borrow_interior_mutable_const)]
pub fn new( pub fn new(
req: &HttpRequest, req: &HttpRequest,
@ -394,7 +353,7 @@ where
} }
} }
/// Change max size of payload. By default max size is 256Kb /// Set maximum accepted payload size. The default limit is 256kB.
pub fn limit(self, limit: usize) -> Self { pub fn limit(self, limit: usize) -> Self {
match self { match self {
JsonBody::Body { JsonBody::Body {
@ -422,11 +381,11 @@ where
} }
} }
impl<U> Future for JsonBody<U> impl<T> Future for JsonBody<T>
where where
U: DeserializeOwned + 'static, T: DeserializeOwned + 'static,
{ {
type Output = Result<U, JsonPayloadError>; type Output = Result<T, JsonPayloadError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut(); let this = self.get_mut();
@ -449,7 +408,7 @@ where
} }
} }
None => { None => {
let json = serde_json::from_slice::<U>(&buf)?; let json = serde_json::from_slice::<T>(&buf)?;
return Poll::Ready(Ok(json)); return Poll::Ready(Ok(json));
} }
} }
@ -462,13 +421,17 @@ where
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use bytes::Bytes; use bytes::Bytes;
use serde_derive::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use super::*; use super::*;
use crate::error::InternalError; use crate::{
use crate::http::header::{self, HeaderValue, CONTENT_LENGTH, CONTENT_TYPE}; error::InternalError,
use crate::test::{load_stream, TestRequest}; http::{
use crate::HttpResponse; header::{self, HeaderValue, CONTENT_LENGTH, CONTENT_TYPE},
StatusCode,
},
test::{load_stream, TestRequest},
};
#[derive(Serialize, Deserialize, PartialEq, Debug)] #[derive(Serialize, Deserialize, PartialEq, Debug)]
struct MyObject { struct MyObject {
@ -526,7 +489,7 @@ mod tests {
.to_http_parts(); .to_http_parts();
let s = Json::<MyObject>::from_request(&req, &mut pl).await; let s = Json::<MyObject>::from_request(&req, &mut pl).await;
let mut resp = Response::from_error(s.err().unwrap()); let mut resp = HttpResponse::from_error(s.err().unwrap());
assert_eq!(resp.status(), StatusCode::BAD_REQUEST); assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
let body = load_stream(resp.take_body()).await.unwrap(); let body = load_stream(resp.take_body()).await.unwrap();

View file

@ -1,5 +1,6 @@
//! Helper types //! Common extractors and responders.
// TODO: review visibility
mod either; mod either;
pub(crate) mod form; pub(crate) mod form;
pub(crate) mod json; pub(crate) mod json;

View file

@ -1,70 +1,55 @@
//! Path extractor //! For path segment extractor documentation, see [`Path`].
use std::sync::Arc;
use std::{fmt, ops}; use std::{fmt, ops, sync::Arc};
use actix_http::error::{Error, ErrorNotFound}; use actix_http::error::{Error, ErrorNotFound};
use actix_router::PathDeserializer; use actix_router::PathDeserializer;
use futures_util::future::{ready, Ready}; use futures_util::future::{ready, Ready};
use serde::de; use serde::de;
use crate::dev::Payload; use crate::{dev::Payload, error::PathError, FromRequest, HttpRequest};
use crate::error::PathError;
use crate::request::HttpRequest;
use crate::FromRequest;
#[derive(PartialEq, Eq, PartialOrd, Ord)] /// Extract typed data from request path segments.
/// Extract typed information from the request's path.
/// ///
/// [**PathConfig**](PathConfig) allows to configure extraction process. /// Use [`PathConfig`] to configure extraction process.
/// ///
/// ## Example /// # Usage
/// ```
/// use actix_web::{get, web};
/// ///
/// ```rust /// // extract path info from "/{name}/{count}/index.html" into tuple
/// use actix_web::{web, App}; /// // {name} - deserialize a String
/// /// // {count} - deserialize a u32
/// /// extract path info from "/{username}/{count}/index.html" url /// #[get("/")]
/// /// {username} - deserializes to a String /// async fn index(path: web::Path<(String, u32)>) -> String {
/// /// {count} - - deserializes to a u32 /// let (name, count) = path.into_inner();
/// async fn index(web::Path((username, count)): web::Path<(String, u32)>) -> String { /// format!("Welcome {}! {}", name, count)
/// format!("Welcome {}! {}", username, count)
/// }
///
/// fn main() {
/// let app = App::new().service(
/// web::resource("/{username}/{count}/index.html") // <- define path parameters
/// .route(web::get().to(index)) // <- register handler with `Path` extractor
/// );
/// } /// }
/// ``` /// ```
/// ///
/// It is possible to extract path information to a specific type that /// Path segments also can be deserialized into any type that implements [`serde::Deserialize`].
/// implements `Deserialize` trait from *serde*. /// Path segment labels will be matched with struct field names.
/// ///
/// ```rust /// ```
/// use actix_web::{web, App, Error}; /// use actix_web::{get, web};
/// use serde_derive::Deserialize; /// use serde::Deserialize;
/// ///
/// #[derive(Deserialize)] /// #[derive(Deserialize)]
/// struct Info { /// struct Info {
/// username: String, /// name: String,
/// } /// }
/// ///
/// /// extract `Info` from a path using serde /// // extract `Info` from a path using serde
/// async fn index(info: web::Path<Info>) -> Result<String, Error> { /// #[get("/")]
/// Ok(format!("Welcome {}!", info.username)) /// async fn index(info: web::Path<Info>) -> String {
/// } /// format!("Welcome {}!", info.name)
///
/// fn main() {
/// let app = App::new().service(
/// web::resource("/{username}/index.html") // <- define path parameters
/// .route(web::get().to(index)) // <- use handler with Path` extractor
/// );
/// } /// }
/// ``` /// ```
pub struct Path<T>(pub T); #[derive(PartialEq, Eq, PartialOrd, Ord)]
pub struct Path<T>(T);
impl<T> Path<T> { impl<T> Path<T> {
/// Deconstruct to an inner value /// Unwrap into inner `T` value.
pub fn into_inner(self) -> T { pub fn into_inner(self) -> T {
self.0 self.0
} }
@ -108,52 +93,7 @@ impl<T: fmt::Display> fmt::Display for Path<T> {
} }
} }
/// Extract typed information from the request's path. /// See [here](#usage) for example of usage as an extractor.
///
/// ## Example
///
/// ```rust
/// use actix_web::{web, App};
///
/// /// extract path info from "/{username}/{count}/index.html" url
/// /// {username} - deserializes to a String
/// /// {count} - - deserializes to a u32
/// async fn index(web::Path((username, count)): web::Path<(String, u32)>) -> String {
/// format!("Welcome {}! {}", username, count)
/// }
///
/// fn main() {
/// let app = App::new().service(
/// web::resource("/{username}/{count}/index.html") // <- define path parameters
/// .route(web::get().to(index)) // <- register handler with `Path` extractor
/// );
/// }
/// ```
///
/// It is possible to extract path information to a specific type that
/// implements `Deserialize` trait from *serde*.
///
/// ```rust
/// use actix_web::{web, App, Error};
/// use serde_derive::Deserialize;
///
/// #[derive(Deserialize)]
/// struct Info {
/// username: String,
/// }
///
/// /// extract `Info` from a path using serde
/// async fn index(info: web::Path<Info>) -> Result<String, Error> {
/// Ok(format!("Welcome {}!", info.username))
/// }
///
/// fn main() {
/// let app = App::new().service(
/// web::resource("/{username}/index.html") // <- define path parameters
/// .route(web::get().to(index)) // <- use handler with Path` extractor
/// );
/// }
/// ```
impl<T> FromRequest for Path<T> impl<T> FromRequest for Path<T>
where where
T: de::DeserializeOwned, T: de::DeserializeOwned,
@ -191,10 +131,10 @@ where
/// Path extractor configuration /// Path extractor configuration
/// ///
/// ```rust /// ```
/// use actix_web::web::PathConfig; /// use actix_web::web::PathConfig;
/// use actix_web::{error, web, App, FromRequest, HttpResponse}; /// use actix_web::{error, web, App, FromRequest, HttpResponse};
/// use serde_derive::Deserialize; /// use serde::Deserialize;
/// ///
/// #[derive(Deserialize, Debug)] /// #[derive(Deserialize, Debug)]
/// enum Folder { /// enum Folder {
@ -249,7 +189,7 @@ impl Default for PathConfig {
mod tests { mod tests {
use actix_router::ResourceDef; use actix_router::ResourceDef;
use derive_more::Display; use derive_more::Display;
use serde_derive::Deserialize; use serde::Deserialize;
use super::*; use super::*;
use crate::test::TestRequest; use crate::test::TestRequest;

View file

@ -1,57 +1,51 @@
//! Payload/Bytes/String extractors //! Basic binary and string payload extractors.
use std::future::Future;
use std::pin::Pin;
use std::str;
use std::task::{Context, Poll};
use actix_http::error::{Error, ErrorBadRequest, PayloadError}; use std::{
use actix_http::HttpMessage; future::Future,
pin::Pin,
str,
task::{Context, Poll},
};
use actix_http::error::{ErrorBadRequest, PayloadError};
use bytes::{Bytes, BytesMut}; use bytes::{Bytes, BytesMut};
use encoding_rs::{Encoding, UTF_8}; use encoding_rs::{Encoding, UTF_8};
use futures_core::stream::Stream; use futures_core::stream::Stream;
use futures_util::{ use futures_util::{
future::{err, ok, Either, ErrInto, Ready, TryFutureExt as _}, future::{ready, Either, ErrInto, Ready, TryFutureExt as _},
ready, ready,
}; };
use mime::Mime; use mime::Mime;
use crate::extract::FromRequest; use crate::{dev, http::header, web, Error, FromRequest, HttpMessage, HttpRequest};
use crate::http::header;
use crate::request::HttpRequest;
use crate::{dev, web};
/// Payload extractor returns request 's payload stream. /// Extract a request's raw payload stream.
/// ///
/// ## Example /// See [`PayloadConfig`] for important notes when using this advanced extractor.
/// ///
/// ```rust /// # Usage
/// use actix_web::{web, error, App, Error, HttpResponse}; /// ```
/// use std::future::Future; /// use std::future::Future;
/// use futures_core::stream::Stream; /// use futures_util::stream::{Stream, StreamExt};
/// use futures_util::StreamExt; /// use actix_web::{post, web};
/// /// extract binary data from request ///
/// async fn index(mut body: web::Payload) -> Result<HttpResponse, Error> /// // `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
/// let mut bytes = web::BytesMut::new(); /// let mut bytes = web::BytesMut::new();
/// while let Some(item) = body.next().await { /// while let Some(item) = body.next().await {
/// bytes.extend_from_slice(&item?); /// bytes.extend_from_slice(&item?);
/// } /// }
/// ///
/// format!("Body {:?}!", bytes); /// Ok(format!("Request Body Bytes:\n{:?}", bytes))
/// Ok(HttpResponse::Ok().finish())
/// }
///
/// fn main() {
/// let app = App::new().service(
/// web::resource("/index.html").route(
/// web::get().to(index))
/// );
/// } /// }
/// ``` /// ```
pub struct Payload(pub crate::dev::Payload); pub struct Payload(pub crate::dev::Payload);
impl Payload { impl Payload {
/// Deconstruct to a inner value /// Unwrap to inner Payload type.
pub fn into_inner(self) -> crate::dev::Payload { pub fn into_inner(self) -> crate::dev::Payload {
self.0 self.0
} }
@ -69,35 +63,7 @@ impl Stream for Payload {
} }
} }
/// Get request's payload stream /// See [here](#usage) for example of usage as an extractor.
///
/// ## Example
///
/// ```rust
/// use actix_web::{web, error, App, Error, HttpResponse};
/// use std::future::Future;
/// use futures_core::stream::Stream;
/// use futures_util::StreamExt;
///
/// /// extract binary data from request
/// async fn index(mut body: web::Payload) -> Result<HttpResponse, Error>
/// {
/// let mut bytes = web::BytesMut::new();
/// while let Some(item) = body.next().await {
/// bytes.extend_from_slice(&item?);
/// }
///
/// format!("Body {:?}!", bytes);
/// Ok(HttpResponse::Ok().finish())
/// }
///
/// fn main() {
/// let app = App::new().service(
/// web::resource("/index.html").route(
/// web::get().to(index))
/// );
/// }
/// ```
impl FromRequest for Payload { impl FromRequest for Payload {
type Config = PayloadConfig; type Config = PayloadConfig;
type Error = Error; type Error = Error;
@ -105,34 +71,25 @@ impl FromRequest for Payload {
#[inline] #[inline]
fn from_request(_: &HttpRequest, payload: &mut dev::Payload) -> Self::Future { fn from_request(_: &HttpRequest, payload: &mut dev::Payload) -> Self::Future {
ok(Payload(payload.take())) ready(Ok(Payload(payload.take())))
} }
} }
/// Request binary data from a request's payload. /// Extract binary data from a request's payload.
/// ///
/// Loads request's payload and construct Bytes instance. /// Collects request payload stream into a [Bytes] instance.
/// ///
/// [**PayloadConfig**](PayloadConfig) allows to configure /// Use [`PayloadConfig`] to configure extraction process.
/// extraction process.
/// ///
/// ## Example /// # Usage
/// /// ```
/// ```rust /// use actix_web::{post, web};
/// use bytes::Bytes;
/// use actix_web::{web, App};
/// ///
/// /// extract binary data from request /// /// extract binary data from request
/// async fn index(body: Bytes) -> String { /// #[post("/")]
/// async fn index(body: web::Bytes) -> String {
/// format!("Body {:?}!", body) /// format!("Body {:?}!", body)
/// } /// }
///
/// fn main() {
/// let app = App::new().service(
/// web::resource("/index.html").route(
/// web::get().to(index))
/// );
/// }
/// ``` /// ```
impl FromRequest for Bytes { impl FromRequest for Bytes {
type Config = PayloadConfig; type Config = PayloadConfig;
@ -144,8 +101,8 @@ impl FromRequest for Bytes {
// allow both Config and Data<Config> // allow both Config and Data<Config>
let cfg = PayloadConfig::from_req(req); let cfg = PayloadConfig::from_req(req);
if let Err(e) = cfg.check_mimetype(req) { if let Err(err) = cfg.check_mimetype(req) {
return Either::Right(err(e)); return Either::Right(ready(Err(err)));
} }
let limit = cfg.limit; let limit = cfg.limit;
@ -161,26 +118,15 @@ impl FromRequest for Bytes {
/// [**PayloadConfig**](PayloadConfig) allows to configure /// [**PayloadConfig**](PayloadConfig) allows to configure
/// extraction process. /// extraction process.
/// ///
/// ## Example /// # Usage
/// ```
/// use actix_web::{post, web, FromRequest};
/// ///
/// ```rust /// // extract text data from request
/// use actix_web::{web, App, FromRequest}; /// #[post("/")]
///
/// /// extract text data from request
/// async fn index(text: String) -> String { /// async fn index(text: String) -> String {
/// format!("Body {}!", text) /// format!("Body {}!", text)
/// } /// }
///
/// fn main() {
/// let app = App::new().service(
/// web::resource("/index.html")
/// .app_data(String::configure(|cfg| { // <- limit size of the payload
/// cfg.limit(4096)
/// }))
/// .route(web::get().to(index)) // <- register handler with extractor params
/// );
/// }
/// ```
impl FromRequest for String { impl FromRequest for String {
type Config = PayloadConfig; type Config = PayloadConfig;
type Error = Error; type Error = Error;
@ -191,14 +137,14 @@ impl FromRequest for String {
let cfg = PayloadConfig::from_req(req); let cfg = PayloadConfig::from_req(req);
// check content-type // check content-type
if let Err(e) = cfg.check_mimetype(req) { if let Err(err) = cfg.check_mimetype(req) {
return Either::Right(err(e)); return Either::Right(ready(Err(err)));
} }
// check charset // check charset
let encoding = match req.encoding() { let encoding = match req.encoding() {
Ok(enc) => enc, Ok(enc) => enc,
Err(e) => return Either::Right(err(e.into())), Err(err) => return Either::Right(ready(Err(err.into()))),
}; };
let limit = cfg.limit; let limit = cfg.limit;
let body_fut = HttpMessageBody::new(req, payload).limit(limit); let body_fut = HttpMessageBody::new(req, payload).limit(limit);
@ -238,11 +184,13 @@ fn bytes_to_string(body: Bytes, encoding: &'static Encoding) -> Result<String, E
} }
} }
/// Configuration for request's payload. /// Configuration for request payloads.
/// ///
/// Applies to the built-in `Bytes` and `String` extractors. Note that the Payload extractor does /// Applies to the built-in `Bytes` and `String` extractors. Note that the `Payload` extractor does
/// not automatically check conformance with this configuration to allow more flexibility when /// not automatically check conformance with this configuration to allow more flexibility when
/// building extractors on top of `Payload`. /// building extractors on top of `Payload`.
///
/// By default, the payload size limit is 256kB and there is no mime type condition.
#[derive(Clone)] #[derive(Clone)]
pub struct PayloadConfig { pub struct PayloadConfig {
limit: usize, limit: usize,
@ -250,7 +198,7 @@ pub struct PayloadConfig {
} }
impl PayloadConfig { impl PayloadConfig {
/// Create `PayloadConfig` instance and set max size of payload. /// Create new instance with a size limit and no mime type condition.
pub fn new(limit: usize) -> Self { pub fn new(limit: usize) -> Self {
Self { Self {
limit, limit,
@ -258,14 +206,13 @@ impl PayloadConfig {
} }
} }
/// Change max size of payload. By default max size is 256Kb /// Set maximum accepted payload size. The default limit is 256kB.
pub fn limit(mut self, limit: usize) -> Self { pub fn limit(mut self, limit: usize) -> Self {
self.limit = limit; self.limit = limit;
self self
} }
/// Set required mime-type of the request. By default mime type is not /// Set required mime type of the request. By default mime type is not enforced.
/// enforced.
pub fn mimetype(mut self, mt: Mime) -> Self { pub fn mimetype(mut self, mt: Mime) -> Self {
self.mimetype = Some(mt); self.mimetype = Some(mt);
self self
@ -292,7 +239,7 @@ impl PayloadConfig {
} }
/// Extract payload config from app data. Check both `T` and `Data<T>`, in that order, and fall /// Extract payload config from app data. Check both `T` and `Data<T>`, in that order, and fall
/// back to the default payload config. /// back to the default payload config if neither is found.
fn from_req(req: &HttpRequest) -> &Self { fn from_req(req: &HttpRequest) -> &Self {
req.app_data::<Self>() req.app_data::<Self>()
.or_else(|| req.app_data::<web::Data<Self>>().map(|d| d.as_ref())) .or_else(|| req.app_data::<web::Data<Self>>().map(|d| d.as_ref()))
@ -314,13 +261,10 @@ impl Default for PayloadConfig {
} }
} }
/// Future that resolves to a complete http message body. /// Future that resolves to a complete HTTP body payload.
/// ///
/// Load http message body. /// By default only 256kB payload is accepted before `PayloadError::Overflow` is returned.
/// /// Use `MessageBody::limit()` method to change upper limit.
/// By default only 256Kb payload reads to a memory, then
/// `PayloadError::Overflow` get returned. Use `MessageBody::limit()`
/// method to change upper limit.
pub struct HttpMessageBody { pub struct HttpMessageBody {
limit: usize, limit: usize,
length: Option<usize>, length: Option<usize>,
@ -366,7 +310,7 @@ impl HttpMessageBody {
} }
} }
/// Change max size of payload. By default max size is 256Kb /// Change max size of payload. By default max size is 256kB
pub fn limit(mut self, limit: usize) -> Self { pub fn limit(mut self, limit: usize) -> Self {
if let Some(l) = self.length { if let Some(l) = self.length {
if l > limit { if l > limit {
@ -384,8 +328,8 @@ impl Future for HttpMessageBody {
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut(); let this = self.get_mut();
if let Some(e) = this.err.take() { if let Some(err) = this.err.take() {
return Poll::Ready(Err(e)); return Poll::Ready(Err(err));
} }
loop { loop {

View file

@ -1,30 +1,27 @@
//! Query extractor //! For query parameter extractor documentation, see [`Query`].
use std::sync::Arc; use std::{fmt, ops, sync::Arc};
use std::{fmt, ops};
use actix_http::error::Error;
use futures_util::future::{err, ok, Ready}; use futures_util::future::{err, ok, Ready};
use serde::de; use serde::de;
use crate::dev::Payload; use crate::{dev::Payload, error::QueryPayloadError, Error, FromRequest, HttpRequest};
use crate::error::QueryPayloadError;
use crate::extract::FromRequest;
use crate::request::HttpRequest;
/// Extract typed information from the request's query. /// Extract typed information from the request's query.
/// ///
/// **Note**: A query string consists of unordered `key=value` pairs, therefore it cannot /// To extract typed data from the URL query string, the inner type `T` must implement the
/// be decoded into any type which depends upon data ordering e.g. tuples or tuple-structs. /// [`serde::Deserialize`] trait.
/// Attempts to do so will *fail at runtime*.
/// ///
/// [**QueryConfig**](QueryConfig) allows to configure extraction process. /// Use [`QueryConfig`] to configure extraction process.
/// ///
/// ## Example /// # Panics
/// A query string consists of unordered `key=value` pairs, therefore it cannot be decoded into any
/// type which depends upon data ordering (eg. tuples). Trying to do so will result in a panic.
/// ///
/// ```rust /// # Usage
/// use actix_web::{web, App}; /// ```
/// use serde_derive::Deserialize; /// use actix_web::{get, web};
/// use serde::Deserialize;
/// ///
/// #[derive(Debug, Deserialize)] /// #[derive(Debug, Deserialize)]
/// pub enum ResponseType { /// pub enum ResponseType {
@ -38,35 +35,40 @@ use crate::request::HttpRequest;
/// response_type: ResponseType, /// response_type: ResponseType,
/// } /// }
/// ///
/// // Use `Query` extractor for query information (and destructure it within the signature). /// // Deserialize `AuthRequest` struct from query string.
/// // This handler gets called only if the request's query string contains `id` and `response_type` fields. /// // This handler gets called only if the request's query parameters contain both fields.
/// // The correct request for this handler would be `/index.html?id=64&response_type=Code"`. /// // A valid request path for this handler would be `/?id=64&response_type=Code"`.
/// async fn index(web::Query(info): web::Query<AuthRequest>) -> String { /// #[get("/")]
/// format!("Authorization request for client with id={} and type={:?}!", info.id, info.response_type) /// async fn index(info: web::Query<AuthRequest>) -> String {
/// } /// format!("Authorization request for id={} and type={:?}!", info.id, info.response_type)
///
/// fn main() {
/// let app = App::new().service(
/// web::resource("/index.html").route(web::get().to(index))); // <- use `Query` extractor
/// } /// }
/// ``` /// ```
#[derive(PartialEq, Eq, PartialOrd, Ord)] #[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Query<T>(pub T); pub struct Query<T>(T);
impl<T> Query<T> { impl<T> Query<T> {
/// Deconstruct to a inner value /// Unwrap into inner `T` value.
pub fn into_inner(self) -> T { pub fn into_inner(self) -> T {
self.0 self.0
} }
/// Get query parameters from the path /// Deserialize `T` from a URL encoded query parameter string.
///
/// ```
/// # use std::collections::HashMap;
/// # use actix_web::web::Query;
/// let numbers = Query::<HashMap<String, u32>>::from_query("one=1&two=2").unwrap();
/// assert_eq!(numbers.get("one"), Some(&1));
/// assert_eq!(numbers.get("two"), Some(&2));
/// assert!(numbers.get("three").is_none());
/// ```
pub fn from_query(query_str: &str) -> Result<Self, QueryPayloadError> pub fn from_query(query_str: &str) -> Result<Self, QueryPayloadError>
where where
T: de::DeserializeOwned, T: de::DeserializeOwned,
{ {
serde_urlencoded::from_str::<T>(query_str) serde_urlencoded::from_str::<T>(query_str)
.map(|val| Ok(Query(val))) .map(Self)
.unwrap_or_else(move |e| Err(QueryPayloadError::Deserialize(e))) .map_err(QueryPayloadError::Deserialize)
} }
} }
@ -96,39 +98,7 @@ impl<T: fmt::Display> fmt::Display for Query<T> {
} }
} }
/// Extract typed information from the request's query. /// See [here](#usage) for example of usage as an extractor.
///
/// ## Example
///
/// ```rust
/// use actix_web::{web, App};
/// use serde_derive::Deserialize;
///
/// #[derive(Debug, Deserialize)]
/// pub enum ResponseType {
/// Token,
/// Code
/// }
///
/// #[derive(Deserialize)]
/// pub struct AuthRequest {
/// id: u64,
/// response_type: ResponseType,
/// }
///
/// // Use `Query` extractor for query information.
/// // This handler get called only if request's query contains `id` and `response_type` fields.
/// // The correct request for this handler would be `/index.html?id=64&response_type=Code"`
/// async fn index(info: web::Query<AuthRequest>) -> String {
/// format!("Authorization request for client with id={} and type={:?}!", info.id, info.response_type)
/// }
///
/// fn main() {
/// let app = App::new().service(
/// web::resource("/index.html")
/// .route(web::get().to(index))); // <- use `Query` extractor
/// }
/// ```
impl<T> FromRequest for Query<T> impl<T> FromRequest for Query<T>
where where
T: de::DeserializeOwned, T: de::DeserializeOwned,
@ -141,7 +111,7 @@ where
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future { fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
let error_handler = req let error_handler = req
.app_data::<Self::Config>() .app_data::<Self::Config>()
.map(|c| c.ehandler.clone()) .map(|c| c.err_handler.clone())
.unwrap_or(None); .unwrap_or(None);
serde_urlencoded::from_str::<T>(req.query_string()) serde_urlencoded::from_str::<T>(req.query_string())
@ -166,13 +136,12 @@ where
} }
} }
/// Query extractor configuration /// Query extractor configuration.
/// ///
/// ## Example /// # Usage
/// /// ```
/// ```rust /// use actix_web::{error, get, web, App, FromRequest, HttpResponse};
/// use actix_web::{error, web, App, FromRequest, HttpResponse}; /// use serde::Deserialize;
/// use serde_derive::Deserialize;
/// ///
/// #[derive(Deserialize)] /// #[derive(Deserialize)]
/// struct Info { /// struct Info {
@ -180,27 +149,25 @@ where
/// } /// }
/// ///
/// /// deserialize `Info` from request's querystring /// /// deserialize `Info` from request's querystring
/// #[get("/")]
/// async fn index(info: web::Query<Info>) -> String { /// async fn index(info: web::Query<Info>) -> String {
/// format!("Welcome {}!", info.username) /// format!("Welcome {}!", info.username)
/// } /// }
/// ///
/// fn main() { /// // custom `Query` extractor configuration
/// let app = App::new().service( /// let query_cfg = web::QueryConfig::default()
/// web::resource("/index.html").app_data( /// // use custom error handler
/// // change query extractor configuration /// .error_handler(|err, req| {
/// web::QueryConfig::default() /// error::InternalError::from_response(err, HttpResponse::Conflict().finish()).into()
/// .error_handler(|err, req| { // <- create custom error response /// });
/// error::InternalError::from_response( ///
/// err, HttpResponse::Conflict().finish()).into() /// App::new()
/// }) /// .app_data(query_cfg)
/// ) /// .service(index);
/// .route(web::post().to(index))
/// );
/// }
/// ``` /// ```
#[derive(Clone)] #[derive(Clone)]
pub struct QueryConfig { pub struct QueryConfig {
ehandler: err_handler:
Option<Arc<dyn Fn(QueryPayloadError, &HttpRequest) -> Error + Send + Sync>>, Option<Arc<dyn Fn(QueryPayloadError, &HttpRequest) -> Error + Send + Sync>>,
} }
@ -210,14 +177,14 @@ impl QueryConfig {
where where
F: Fn(QueryPayloadError, &HttpRequest) -> Error + Send + Sync + 'static, F: Fn(QueryPayloadError, &HttpRequest) -> Error + Send + Sync + 'static,
{ {
self.ehandler = Some(Arc::new(f)); self.err_handler = Some(Arc::new(f));
self self
} }
} }
impl Default for QueryConfig { impl Default for QueryConfig {
fn default() -> Self { fn default() -> Self {
QueryConfig { ehandler: None } QueryConfig { err_handler: None }
} }
} }
@ -225,7 +192,7 @@ impl Default for QueryConfig {
mod tests { mod tests {
use actix_http::http::StatusCode; use actix_http::http::StatusCode;
use derive_more::Display; use derive_more::Display;
use serde_derive::Deserialize; use serde::Deserialize;
use super::*; use super::*;
use crate::error::InternalError; use crate::error::InternalError;
@ -271,6 +238,17 @@ mod tests {
assert_eq!(s.id, "test1"); assert_eq!(s.id, "test1");
} }
#[actix_rt::test]
#[should_panic]
async fn test_tuple_panic() {
let req = TestRequest::with_uri("/?one=1&two=2").to_srv_request();
let (req, mut pl) = req.into_parts();
Query::<(u32, u32)>::from_request(&req, &mut pl)
.await
.unwrap();
}
#[actix_rt::test] #[actix_rt::test]
async fn test_custom_error_responder() { async fn test_custom_error_responder() {
let req = TestRequest::with_uri("/name/user1/") let req = TestRequest::with_uri("/name/user1/")

View file

@ -1,17 +1,23 @@
use std::borrow::Cow; //! For request line reader documentation, see [`Readlines`].
use std::pin::Pin;
use std::str; use std::{
use std::task::{Context, Poll}; borrow::Cow,
pin::Pin,
str,
task::{Context, Poll},
};
use bytes::{Bytes, BytesMut}; use bytes::{Bytes, BytesMut};
use encoding_rs::{Encoding, UTF_8}; use encoding_rs::{Encoding, UTF_8};
use futures_util::stream::Stream; use futures_core::{ready, stream::Stream};
use crate::dev::Payload; use crate::{
use crate::error::{PayloadError, ReadlinesError}; dev::Payload,
use crate::HttpMessage; error::{PayloadError, ReadlinesError},
HttpMessage,
};
/// Stream to read request line by line. /// Stream that reads request line by line.
pub struct Readlines<T: HttpMessage> { pub struct Readlines<T: HttpMessage> {
stream: Payload<T::Stream>, stream: Payload<T::Stream>,
buff: BytesMut, buff: BytesMut,
@ -43,7 +49,7 @@ where
} }
} }
/// Change max line size. By default max size is 256Kb /// Set maximum accepted payload size. The default limit is 256kB.
pub fn limit(mut self, limit: usize) -> Self { pub fn limit(mut self, limit: usize) -> Self {
self.limit = limit; self.limit = limit;
self self
@ -108,9 +114,10 @@ where
} }
this.checked_buff = true; this.checked_buff = true;
} }
// poll req for more bytes // poll req for more bytes
match Pin::new(&mut this.stream).poll_next(cx) { match ready!(Pin::new(&mut this.stream).poll_next(cx)) {
Poll::Ready(Some(Ok(mut bytes))) => { Some(Ok(mut bytes)) => {
// check if there is a newline in bytes // check if there is a newline in bytes
let mut found: Option<usize> = None; let mut found: Option<usize> = None;
for (ind, b) in bytes.iter().enumerate() { for (ind, b) in bytes.iter().enumerate() {
@ -144,8 +151,8 @@ where
this.buff.extend_from_slice(&bytes); this.buff.extend_from_slice(&bytes);
Poll::Pending Poll::Pending
} }
Poll::Pending => Poll::Pending,
Poll::Ready(None) => { None => {
if this.buff.is_empty() { if this.buff.is_empty() {
return Poll::Ready(None); return Poll::Ready(None);
} }
@ -165,7 +172,8 @@ where
this.buff.clear(); this.buff.clear();
Poll::Ready(Some(Ok(line))) Poll::Ready(Some(Ok(line)))
} }
Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(ReadlinesError::from(e)))),
Some(Err(err)) => Poll::Ready(Some(Err(ReadlinesError::from(err)))),
} }
} }
} }