1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-28 01:32:57 +01:00

Merge branch 'master' of github.com:actix/actix-web

This commit is contained in:
Nikolay Kim 2019-09-17 21:46:45 +06:00
commit b3783b403e
9 changed files with 43 additions and 17 deletions

View File

@ -8,6 +8,7 @@
* Allow to re-construct `ServiceRequest` from `HttpRequest` and `Payload` * Allow to re-construct `ServiceRequest` from `HttpRequest` and `Payload`
* Make UrlEncodedError::Overflow more informativve
## [1.0.7] - 2019-08-29 ## [1.0.7] - 2019-08-29

View File

@ -6,6 +6,9 @@
* Add support for sending HTTP requests with `Rc<RequestHead>` in addition to sending HTTP requests with `RequestHead` * Add support for sending HTTP requests with `Rc<RequestHead>` in addition to sending HTTP requests with `RequestHead`
* Allow to use `std::convert::Infallible` as `actix_http::error::Error`
### Fixed ### Fixed
* h2 will use error response #1080 * h2 will use error response #1080

View File

@ -212,7 +212,7 @@ where
pub fn finish( pub fn finish(
self, self,
) -> impl Service<Request = Connect, Response = impl Connection, Error = ConnectError> ) -> impl Service<Request = Connect, Response = impl Connection, Error = ConnectError>
+ Clone { + Clone {
#[cfg(not(any(feature = "ssl", feature = "rust-tls")))] #[cfg(not(any(feature = "ssl", feature = "rust-tls")))]
{ {
let connector = TimeoutService::new( let connector = TimeoutService::new(

View File

@ -132,6 +132,14 @@ impl std::error::Error for Error {
} }
} }
impl From<std::convert::Infallible> for Error {
fn from(_: std::convert::Infallible) -> Self {
// `std::convert::Infallible` indicates an error
// that will never happen
unreachable!()
}
}
/// Convert `Error` to a `Response` instance /// Convert `Error` to a `Response` instance
impl From<Error> for Response { impl From<Error> for Response {
fn from(err: Error) -> Self { fn from(err: Error) -> Self {

View File

@ -1,5 +1,6 @@
use std::io;
use std::marker::PhantomData; use std::marker::PhantomData;
use std::{io, mem}; use std::mem::MaybeUninit;
use actix_codec::Decoder; use actix_codec::Decoder;
use bytes::{Bytes, BytesMut}; use bytes::{Bytes, BytesMut};
@ -186,11 +187,12 @@ impl MessageType for Request {
fn decode(src: &mut BytesMut) -> Result<Option<(Self, PayloadType)>, ParseError> { fn decode(src: &mut BytesMut) -> Result<Option<(Self, PayloadType)>, ParseError> {
// Unsafe: we read only this data only after httparse parses headers into. // Unsafe: we read only this data only after httparse parses headers into.
// performance bump for pipeline benchmarks. // performance bump for pipeline benchmarks.
let mut headers: [HeaderIndex; MAX_HEADERS] = unsafe { mem::uninitialized() }; let mut headers: [HeaderIndex; MAX_HEADERS] =
unsafe { MaybeUninit::uninit().assume_init() };
let (len, method, uri, ver, h_len) = { let (len, method, uri, ver, h_len) = {
let mut parsed: [httparse::Header; MAX_HEADERS] = let mut parsed: [httparse::Header; MAX_HEADERS] =
unsafe { mem::uninitialized() }; unsafe { MaybeUninit::uninit().assume_init() };
let mut req = httparse::Request::new(&mut parsed); let mut req = httparse::Request::new(&mut parsed);
match req.parse(src)? { match req.parse(src)? {
@ -260,11 +262,12 @@ impl MessageType for ResponseHead {
fn decode(src: &mut BytesMut) -> Result<Option<(Self, PayloadType)>, ParseError> { fn decode(src: &mut BytesMut) -> Result<Option<(Self, PayloadType)>, ParseError> {
// Unsafe: we read only this data only after httparse parses headers into. // Unsafe: we read only this data only after httparse parses headers into.
// performance bump for pipeline benchmarks. // performance bump for pipeline benchmarks.
let mut headers: [HeaderIndex; MAX_HEADERS] = unsafe { mem::uninitialized() }; let mut headers: [HeaderIndex; MAX_HEADERS] =
unsafe { MaybeUninit::uninit().assume_init() };
let (len, ver, status, h_len) = { let (len, ver, status, h_len) = {
let mut parsed: [httparse::Header; MAX_HEADERS] = let mut parsed: [httparse::Header; MAX_HEADERS] =
unsafe { mem::uninitialized() }; unsafe { MaybeUninit::uninit().assume_init() };
let mut res = httparse::Response::new(&mut parsed); let mut res = httparse::Response::new(&mut parsed);
match res.parse(src)? { match res.parse(src)? {

View File

@ -115,7 +115,7 @@ pub fn write_content_length(mut n: usize, bytes: &mut BytesMut) {
pub(crate) fn convert_usize(mut n: usize, bytes: &mut BytesMut) { pub(crate) fn convert_usize(mut n: usize, bytes: &mut BytesMut) {
let mut curr: isize = 39; let mut curr: isize = 39;
let mut buf: [u8; 41] = unsafe { mem::uninitialized() }; let mut buf: [u8; 41] = unsafe { mem::MaybeUninit::uninit().assume_init() };
buf[39] = b'\r'; buf[39] = b'\r';
buf[40] = b'\n'; buf[40] = b'\n';
let buf_ptr = buf.as_mut_ptr(); let buf_ptr = buf.as_mut_ptr();

View File

@ -150,7 +150,7 @@ impl TestRequest {
/// Complete request creation and generate `Request` instance /// Complete request creation and generate `Request` instance
pub fn finish(&mut self) -> Request { pub fn finish(&mut self) -> Request {
let inner = self.0.take().expect("cannot reuse test request builder");; let inner = self.0.take().expect("cannot reuse test request builder");
let mut req = if let Some(pl) = inner.payload { let mut req = if let Some(pl) = inner.payload {
Request::with_payload(pl) Request::with_payload(pl)

View File

@ -32,8 +32,12 @@ pub enum UrlencodedError {
#[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 bigger than allowed. (default: 256kB)
#[display(fmt = "Urlencoded payload size is bigger than allowed (default: 256kB)")] #[display(
Overflow, fmt = "Urlencoded payload size is bigger ({} bytes) than allowed (default: {} bytes)",
size,
limit
)]
Overflow { size: usize, limit: usize },
/// Payload size is now known /// Payload size is now known
#[display(fmt = "Payload size is now known")] #[display(fmt = "Payload size is now known")]
UnknownLength, UnknownLength,
@ -52,7 +56,7 @@ pub enum UrlencodedError {
impl ResponseError for UrlencodedError { impl ResponseError for UrlencodedError {
fn error_response(&self) -> HttpResponse { fn error_response(&self) -> HttpResponse {
match *self { match *self {
UrlencodedError::Overflow => { UrlencodedError::Overflow { .. } => {
HttpResponse::new(StatusCode::PAYLOAD_TOO_LARGE) HttpResponse::new(StatusCode::PAYLOAD_TOO_LARGE)
} }
UrlencodedError::UnknownLength => { UrlencodedError::UnknownLength => {
@ -164,7 +168,8 @@ mod tests {
#[test] #[test]
fn test_urlencoded_error() { fn test_urlencoded_error() {
let resp: HttpResponse = UrlencodedError::Overflow.error_response(); let resp: HttpResponse =
UrlencodedError::Overflow { size: 0, limit: 0 }.error_response();
assert_eq!(resp.status(), StatusCode::PAYLOAD_TOO_LARGE); assert_eq!(resp.status(), StatusCode::PAYLOAD_TOO_LARGE);
let resp: HttpResponse = UrlencodedError::UnknownLength.error_response(); let resp: HttpResponse = UrlencodedError::UnknownLength.error_response();
assert_eq!(resp.status(), StatusCode::LENGTH_REQUIRED); assert_eq!(resp.status(), StatusCode::LENGTH_REQUIRED);

View File

@ -318,7 +318,7 @@ where
let limit = self.limit; let limit = self.limit;
if let Some(len) = self.length.take() { if let Some(len) = self.length.take() {
if len > limit { if len > limit {
return Err(UrlencodedError::Overflow); return Err(UrlencodedError::Overflow { size: len, limit });
} }
} }
@ -331,7 +331,10 @@ where
.from_err() .from_err()
.fold(BytesMut::with_capacity(8192), move |mut body, chunk| { .fold(BytesMut::with_capacity(8192), move |mut body, chunk| {
if (body.len() + chunk.len()) > limit { if (body.len() + chunk.len()) > limit {
Err(UrlencodedError::Overflow) Err(UrlencodedError::Overflow {
size: body.len() + chunk.len(),
limit,
})
} else { } else {
body.extend_from_slice(&chunk); body.extend_from_slice(&chunk);
Ok(body) Ok(body)
@ -390,8 +393,8 @@ mod tests {
fn eq(err: UrlencodedError, other: UrlencodedError) -> bool { fn eq(err: UrlencodedError, other: UrlencodedError) -> bool {
match err { match err {
UrlencodedError::Overflow => match other { UrlencodedError::Overflow { .. } => match other {
UrlencodedError::Overflow => true, UrlencodedError::Overflow { .. } => true,
_ => false, _ => false,
}, },
UrlencodedError::UnknownLength => match other { UrlencodedError::UnknownLength => match other {
@ -420,7 +423,10 @@ mod tests {
.header(CONTENT_LENGTH, "1000000") .header(CONTENT_LENGTH, "1000000")
.to_http_parts(); .to_http_parts();
let info = block_on(UrlEncoded::<Info>::new(&req, &mut pl)); let info = block_on(UrlEncoded::<Info>::new(&req, &mut pl));
assert!(eq(info.err().unwrap(), UrlencodedError::Overflow)); assert!(eq(
info.err().unwrap(),
UrlencodedError::Overflow { size: 0, limit: 0 }
));
let (req, mut pl) = TestRequest::with_header(CONTENT_TYPE, "text/plain") let (req, mut pl) = TestRequest::with_header(CONTENT_TYPE, "text/plain")
.header(CONTENT_LENGTH, "10") .header(CONTENT_LENGTH, "10")