mirror of https://github.com/fafhrd91/actix-web
Merge branch 'master' of github.com:actix/actix-web
This commit is contained in:
commit
437a15d323
|
@ -82,6 +82,7 @@ language-tags = "0.3"
|
||||||
once_cell = "1.5"
|
once_cell = "1.5"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
mime = "0.3"
|
mime = "0.3"
|
||||||
|
paste = "1"
|
||||||
pin-project = "1.0.0"
|
pin-project = "1.0.0"
|
||||||
regex = "1.4"
|
regex = "1.4"
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
|
|
@ -38,7 +38,7 @@ pub struct Files {
|
||||||
mime_override: Option<Rc<MimeOverride>>,
|
mime_override: Option<Rc<MimeOverride>>,
|
||||||
file_flags: named::Flags,
|
file_flags: named::Flags,
|
||||||
use_guards: Option<Rc<dyn Guard>>,
|
use_guards: Option<Rc<dyn Guard>>,
|
||||||
guards: Vec<Box<Rc<dyn Guard>>>,
|
guards: Vec<Rc<dyn Guard>>,
|
||||||
hidden_files: bool,
|
hidden_files: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -199,7 +199,7 @@ impl Files {
|
||||||
/// );
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
pub fn guard<G: Guard + 'static>(mut self, guard: G) -> Self {
|
pub fn guard<G: Guard + 'static>(mut self, guard: G) -> Self {
|
||||||
self.guards.push(Box::new(Rc::new(guard)));
|
self.guards.push(Rc::new(guard));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -276,7 +276,7 @@ impl HttpServiceFactory for Files {
|
||||||
Some(
|
Some(
|
||||||
guards
|
guards
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|guard| -> Box<dyn Guard> { guard })
|
.map(|guard| -> Box<dyn Guard> { Box::new(guard) })
|
||||||
.collect::<Vec<_>>(),
|
.collect::<Vec<_>>(),
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
## Unreleased - 2021-xx-xx
|
## Unreleased - 2021-xx-xx
|
||||||
### Added
|
### Added
|
||||||
|
* Alias `body::Body` as `body::AnyBody`. [#2215]
|
||||||
* `BoxAnyBody`: a boxed message body with boxed errors. [#2183]
|
* `BoxAnyBody`: a boxed message body with boxed errors. [#2183]
|
||||||
* Re-export `http` crate's `Error` type as `error::HttpError`. [#2171]
|
* Re-export `http` crate's `Error` type as `error::HttpError`. [#2171]
|
||||||
* Re-export `StatusCode`, `Method`, `Version` and `Uri` at the crate root. [#2171]
|
* Re-export `StatusCode`, `Method`, `Version` and `Uri` at the crate root. [#2171]
|
||||||
|
@ -25,12 +26,15 @@
|
||||||
* Error field from `Response` and `Response::error`. [#2205]
|
* Error field from `Response` and `Response::error`. [#2205]
|
||||||
* `impl Future` for `Response`. [#2201]
|
* `impl Future` for `Response`. [#2201]
|
||||||
* `Response::take_body` and old `Response::into_body` method that casted body type. [#2201]
|
* `Response::take_body` and old `Response::into_body` method that casted body type. [#2201]
|
||||||
|
* `InternalError` and all the error types it constructed. [#2215]
|
||||||
|
* Conversion (`impl Into`) of `Response<Body>` and `ResponseBuilder` to `Error`. [#2215]
|
||||||
|
|
||||||
[#2171]: https://github.com/actix/actix-web/pull/2171
|
[#2171]: https://github.com/actix/actix-web/pull/2171
|
||||||
[#2183]: https://github.com/actix/actix-web/pull/2183
|
[#2183]: https://github.com/actix/actix-web/pull/2183
|
||||||
[#2196]: https://github.com/actix/actix-web/pull/2196
|
[#2196]: https://github.com/actix/actix-web/pull/2196
|
||||||
[#2201]: https://github.com/actix/actix-web/pull/2201
|
[#2201]: https://github.com/actix/actix-web/pull/2201
|
||||||
[#2205]: https://github.com/actix/actix-web/pull/2205
|
[#2205]: https://github.com/actix/actix-web/pull/2205
|
||||||
|
[#2215]: https://github.com/actix/actix-web/pull/2215
|
||||||
|
|
||||||
|
|
||||||
## 3.0.0-beta.6 - 2021-04-17
|
## 3.0.0-beta.6 - 2021-04-17
|
||||||
|
|
|
@ -62,7 +62,6 @@ local-channel = "0.1"
|
||||||
once_cell = "1.5"
|
once_cell = "1.5"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
mime = "0.3"
|
mime = "0.3"
|
||||||
paste = "1"
|
|
||||||
percent-encoding = "2.1"
|
percent-encoding = "2.1"
|
||||||
pin-project = "1.0.0"
|
pin-project = "1.0.0"
|
||||||
pin-project-lite = "0.2"
|
pin-project-lite = "0.2"
|
||||||
|
|
|
@ -13,9 +13,10 @@ use crate::error::Error;
|
||||||
|
|
||||||
use super::{BodySize, BodyStream, MessageBody, MessageBodyMapErr, SizedStream};
|
use super::{BodySize, BodyStream, MessageBody, MessageBodyMapErr, SizedStream};
|
||||||
|
|
||||||
|
pub type Body = AnyBody;
|
||||||
|
|
||||||
/// Represents various types of HTTP message body.
|
/// Represents various types of HTTP message body.
|
||||||
// #[deprecated(since = "4.0.0", note = "Use body types directly.")]
|
pub enum AnyBody {
|
||||||
pub enum Body {
|
|
||||||
/// Empty response. `Content-Length` header is not set.
|
/// Empty response. `Content-Length` header is not set.
|
||||||
None,
|
None,
|
||||||
|
|
||||||
|
@ -29,14 +30,14 @@ pub enum Body {
|
||||||
Message(BoxAnyBody),
|
Message(BoxAnyBody),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Body {
|
impl AnyBody {
|
||||||
/// Create body from slice (copy)
|
/// Create body from slice (copy)
|
||||||
pub fn from_slice(s: &[u8]) -> Body {
|
pub fn from_slice(s: &[u8]) -> Self {
|
||||||
Body::Bytes(Bytes::copy_from_slice(s))
|
Self::Bytes(Bytes::copy_from_slice(s))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create body from generic message body.
|
/// Create body from generic message body.
|
||||||
pub fn from_message<B>(body: B) -> Body
|
pub fn from_message<B>(body: B) -> Self
|
||||||
where
|
where
|
||||||
B: MessageBody + 'static,
|
B: MessageBody + 'static,
|
||||||
B::Error: Into<Box<dyn StdError + 'static>>,
|
B::Error: Into<Box<dyn StdError + 'static>>,
|
||||||
|
@ -45,15 +46,15 @@ impl Body {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MessageBody for Body {
|
impl MessageBody for AnyBody {
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn size(&self) -> BodySize {
|
fn size(&self) -> BodySize {
|
||||||
match self {
|
match self {
|
||||||
Body::None => BodySize::None,
|
AnyBody::None => BodySize::None,
|
||||||
Body::Empty => BodySize::Empty,
|
AnyBody::Empty => BodySize::Empty,
|
||||||
Body::Bytes(ref bin) => BodySize::Sized(bin.len() as u64),
|
AnyBody::Bytes(ref bin) => BodySize::Sized(bin.len() as u64),
|
||||||
Body::Message(ref body) => body.size(),
|
AnyBody::Message(ref body) => body.size(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,9 +63,9 @@ impl MessageBody for Body {
|
||||||
cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
) -> Poll<Option<Result<Bytes, Self::Error>>> {
|
) -> Poll<Option<Result<Bytes, Self::Error>>> {
|
||||||
match self.get_mut() {
|
match self.get_mut() {
|
||||||
Body::None => Poll::Ready(None),
|
AnyBody::None => Poll::Ready(None),
|
||||||
Body::Empty => Poll::Ready(None),
|
AnyBody::Empty => Poll::Ready(None),
|
||||||
Body::Bytes(ref mut bin) => {
|
AnyBody::Bytes(ref mut bin) => {
|
||||||
let len = bin.len();
|
let len = bin.len();
|
||||||
if len == 0 {
|
if len == 0 {
|
||||||
Poll::Ready(None)
|
Poll::Ready(None)
|
||||||
|
@ -74,7 +75,7 @@ impl MessageBody for Body {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: MSRV 1.51: poll_map_err
|
// TODO: MSRV 1.51: poll_map_err
|
||||||
Body::Message(body) => match ready!(body.as_pin_mut().poll_next(cx)) {
|
AnyBody::Message(body) => match ready!(body.as_pin_mut().poll_next(cx)) {
|
||||||
Some(Err(err)) => Poll::Ready(Some(Err(err.into()))),
|
Some(Err(err)) => Poll::Ready(Some(Err(err.into()))),
|
||||||
Some(Ok(val)) => Poll::Ready(Some(Ok(val))),
|
Some(Ok(val)) => Poll::Ready(Some(Ok(val))),
|
||||||
None => Poll::Ready(None),
|
None => Poll::Ready(None),
|
||||||
|
@ -83,100 +84,100 @@ impl MessageBody for Body {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq for Body {
|
impl PartialEq for AnyBody {
|
||||||
fn eq(&self, other: &Body) -> bool {
|
fn eq(&self, other: &Body) -> bool {
|
||||||
match *self {
|
match *self {
|
||||||
Body::None => matches!(*other, Body::None),
|
AnyBody::None => matches!(*other, AnyBody::None),
|
||||||
Body::Empty => matches!(*other, Body::Empty),
|
AnyBody::Empty => matches!(*other, AnyBody::Empty),
|
||||||
Body::Bytes(ref b) => match *other {
|
AnyBody::Bytes(ref b) => match *other {
|
||||||
Body::Bytes(ref b2) => b == b2,
|
AnyBody::Bytes(ref b2) => b == b2,
|
||||||
_ => false,
|
_ => false,
|
||||||
},
|
},
|
||||||
Body::Message(_) => false,
|
AnyBody::Message(_) => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for Body {
|
impl fmt::Debug for AnyBody {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
match *self {
|
match *self {
|
||||||
Body::None => write!(f, "Body::None"),
|
AnyBody::None => write!(f, "AnyBody::None"),
|
||||||
Body::Empty => write!(f, "Body::Empty"),
|
AnyBody::Empty => write!(f, "AnyBody::Empty"),
|
||||||
Body::Bytes(ref b) => write!(f, "Body::Bytes({:?})", b),
|
AnyBody::Bytes(ref b) => write!(f, "AnyBody::Bytes({:?})", b),
|
||||||
Body::Message(_) => write!(f, "Body::Message(_)"),
|
AnyBody::Message(_) => write!(f, "AnyBody::Message(_)"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&'static str> for Body {
|
impl From<&'static str> for AnyBody {
|
||||||
fn from(s: &'static str) -> Body {
|
fn from(s: &'static str) -> Body {
|
||||||
Body::Bytes(Bytes::from_static(s.as_ref()))
|
AnyBody::Bytes(Bytes::from_static(s.as_ref()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&'static [u8]> for Body {
|
impl From<&'static [u8]> for AnyBody {
|
||||||
fn from(s: &'static [u8]) -> Body {
|
fn from(s: &'static [u8]) -> Body {
|
||||||
Body::Bytes(Bytes::from_static(s))
|
AnyBody::Bytes(Bytes::from_static(s))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Vec<u8>> for Body {
|
impl From<Vec<u8>> for AnyBody {
|
||||||
fn from(vec: Vec<u8>) -> Body {
|
fn from(vec: Vec<u8>) -> Body {
|
||||||
Body::Bytes(Bytes::from(vec))
|
AnyBody::Bytes(Bytes::from(vec))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<String> for Body {
|
impl From<String> for AnyBody {
|
||||||
fn from(s: String) -> Body {
|
fn from(s: String) -> Body {
|
||||||
s.into_bytes().into()
|
s.into_bytes().into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&'_ String> for Body {
|
impl From<&'_ String> for AnyBody {
|
||||||
fn from(s: &String) -> Body {
|
fn from(s: &String) -> Body {
|
||||||
Body::Bytes(Bytes::copy_from_slice(AsRef::<[u8]>::as_ref(&s)))
|
AnyBody::Bytes(Bytes::copy_from_slice(AsRef::<[u8]>::as_ref(&s)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Cow<'_, str>> for Body {
|
impl From<Cow<'_, str>> for AnyBody {
|
||||||
fn from(s: Cow<'_, str>) -> Body {
|
fn from(s: Cow<'_, str>) -> Body {
|
||||||
match s {
|
match s {
|
||||||
Cow::Owned(s) => Body::from(s),
|
Cow::Owned(s) => AnyBody::from(s),
|
||||||
Cow::Borrowed(s) => {
|
Cow::Borrowed(s) => {
|
||||||
Body::Bytes(Bytes::copy_from_slice(AsRef::<[u8]>::as_ref(s)))
|
AnyBody::Bytes(Bytes::copy_from_slice(AsRef::<[u8]>::as_ref(s)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Bytes> for Body {
|
impl From<Bytes> for AnyBody {
|
||||||
fn from(s: Bytes) -> Body {
|
fn from(s: Bytes) -> Body {
|
||||||
Body::Bytes(s)
|
AnyBody::Bytes(s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<BytesMut> for Body {
|
impl From<BytesMut> for AnyBody {
|
||||||
fn from(s: BytesMut) -> Body {
|
fn from(s: BytesMut) -> Body {
|
||||||
Body::Bytes(s.freeze())
|
AnyBody::Bytes(s.freeze())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S> From<SizedStream<S>> for Body
|
impl<S> From<SizedStream<S>> for AnyBody
|
||||||
where
|
where
|
||||||
S: Stream<Item = Result<Bytes, Error>> + 'static,
|
S: Stream<Item = Result<Bytes, Error>> + 'static,
|
||||||
{
|
{
|
||||||
fn from(s: SizedStream<S>) -> Body {
|
fn from(s: SizedStream<S>) -> Body {
|
||||||
Body::from_message(s)
|
AnyBody::from_message(s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S, E> From<BodyStream<S>> for Body
|
impl<S, E> From<BodyStream<S>> for AnyBody
|
||||||
where
|
where
|
||||||
S: Stream<Item = Result<Bytes, E>> + 'static,
|
S: Stream<Item = Result<Bytes, E>> + 'static,
|
||||||
E: Into<Error> + 'static,
|
E: Into<Error> + 'static,
|
||||||
{
|
{
|
||||||
fn from(s: BodyStream<S>) -> Body {
|
fn from(s: BodyStream<S>) -> Body {
|
||||||
Body::from_message(s)
|
AnyBody::from_message(s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ mod response_body;
|
||||||
mod size;
|
mod size;
|
||||||
mod sized_stream;
|
mod sized_stream;
|
||||||
|
|
||||||
pub use self::body::{Body, BoxAnyBody};
|
pub use self::body::{AnyBody, Body, BoxAnyBody};
|
||||||
pub use self::body_stream::BodyStream;
|
pub use self::body_stream::BodyStream;
|
||||||
pub use self::message_body::MessageBody;
|
pub use self::message_body::MessageBody;
|
||||||
pub(crate) use self::message_body::MessageBodyMapErr;
|
pub(crate) use self::message_body::MessageBodyMapErr;
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
//! Error and Result module
|
//! Error and Result module
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
cell::RefCell,
|
|
||||||
error::Error as StdError,
|
error::Error as StdError,
|
||||||
fmt,
|
fmt,
|
||||||
io::{self, Write as _},
|
io::{self, Write as _},
|
||||||
|
@ -14,7 +13,7 @@ use derive_more::{Display, Error, From};
|
||||||
use http::{header, uri::InvalidUri, StatusCode};
|
use http::{header, uri::InvalidUri, StatusCode};
|
||||||
use serde::de::value::Error as DeError;
|
use serde::de::value::Error as DeError;
|
||||||
|
|
||||||
use crate::{body::Body, helpers::Writer, Response, ResponseBuilder};
|
use crate::{body::Body, helpers::Writer, Response};
|
||||||
|
|
||||||
pub use http::Error as HttpError;
|
pub use http::Error as HttpError;
|
||||||
|
|
||||||
|
@ -121,20 +120,6 @@ impl<T: ResponseError + 'static> From<T> for Error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert Response to a Error
|
|
||||||
impl From<Response<Body>> for Error {
|
|
||||||
fn from(res: Response<Body>) -> Error {
|
|
||||||
InternalError::from_response("", res).into()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Convert ResponseBuilder to a Error
|
|
||||||
impl From<ResponseBuilder> for Error {
|
|
||||||
fn from(mut res: ResponseBuilder) -> Error {
|
|
||||||
InternalError::from_response("", res.finish()).into()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Display, Error)]
|
#[derive(Debug, Display, Error)]
|
||||||
#[display(fmt = "Unknown Error")]
|
#[display(fmt = "Unknown Error")]
|
||||||
struct UnitError;
|
struct UnitError;
|
||||||
|
@ -449,179 +434,12 @@ mod content_type_test_impls {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return `BadRequest` for `ContentTypeError`
|
|
||||||
impl ResponseError for ContentTypeError {
|
impl ResponseError for ContentTypeError {
|
||||||
fn status_code(&self) -> StatusCode {
|
fn status_code(&self) -> StatusCode {
|
||||||
StatusCode::BAD_REQUEST
|
StatusCode::BAD_REQUEST
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helper type that can wrap any error and generate custom response.
|
|
||||||
///
|
|
||||||
/// In following example any `io::Error` will be converted into "BAD REQUEST"
|
|
||||||
/// response as opposite to *INTERNAL SERVER ERROR* which is defined by
|
|
||||||
/// default.
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// # use std::io;
|
|
||||||
/// # use actix_http::{error, Request};
|
|
||||||
/// fn index(req: Request) -> Result<&'static str, actix_http::Error> {
|
|
||||||
/// Err(error::ErrorBadRequest(io::Error::new(io::ErrorKind::Other, "error")))
|
|
||||||
/// }
|
|
||||||
/// ```
|
|
||||||
pub struct InternalError<T> {
|
|
||||||
cause: T,
|
|
||||||
status: InternalErrorType,
|
|
||||||
}
|
|
||||||
|
|
||||||
enum InternalErrorType {
|
|
||||||
Status(StatusCode),
|
|
||||||
Response(RefCell<Option<Response<Body>>>),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> InternalError<T> {
|
|
||||||
/// Create `InternalError` instance
|
|
||||||
pub fn new(cause: T, status: StatusCode) -> Self {
|
|
||||||
InternalError {
|
|
||||||
cause,
|
|
||||||
status: InternalErrorType::Status(status),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create `InternalError` with predefined `Response`.
|
|
||||||
pub fn from_response(cause: T, response: Response<Body>) -> Self {
|
|
||||||
InternalError {
|
|
||||||
cause,
|
|
||||||
status: InternalErrorType::Response(RefCell::new(Some(response))),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> fmt::Debug for InternalError<T>
|
|
||||||
where
|
|
||||||
T: fmt::Debug + 'static,
|
|
||||||
{
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
fmt::Debug::fmt(&self.cause, f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> fmt::Display for InternalError<T>
|
|
||||||
where
|
|
||||||
T: fmt::Display + 'static,
|
|
||||||
{
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
fmt::Display::fmt(&self.cause, f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> ResponseError for InternalError<T>
|
|
||||||
where
|
|
||||||
T: fmt::Debug + fmt::Display + 'static,
|
|
||||||
{
|
|
||||||
fn status_code(&self) -> StatusCode {
|
|
||||||
match self.status {
|
|
||||||
InternalErrorType::Status(st) => st,
|
|
||||||
InternalErrorType::Response(ref resp) => {
|
|
||||||
if let Some(resp) = resp.borrow().as_ref() {
|
|
||||||
resp.head().status
|
|
||||||
} else {
|
|
||||||
StatusCode::INTERNAL_SERVER_ERROR
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn error_response(&self) -> Response<Body> {
|
|
||||||
match self.status {
|
|
||||||
InternalErrorType::Status(st) => {
|
|
||||||
let mut res = Response::new(st);
|
|
||||||
let mut buf = BytesMut::new();
|
|
||||||
let _ = write!(Writer(&mut buf), "{}", self);
|
|
||||||
res.headers_mut().insert(
|
|
||||||
header::CONTENT_TYPE,
|
|
||||||
header::HeaderValue::from_static("text/plain; charset=utf-8"),
|
|
||||||
);
|
|
||||||
res.set_body(Body::from(buf))
|
|
||||||
}
|
|
||||||
InternalErrorType::Response(ref resp) => {
|
|
||||||
if let Some(resp) = resp.borrow_mut().take() {
|
|
||||||
resp
|
|
||||||
} else {
|
|
||||||
Response::new(StatusCode::INTERNAL_SERVER_ERROR)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! error_helper {
|
|
||||||
($name:ident, $status:ident) => {
|
|
||||||
paste::paste! {
|
|
||||||
#[doc = "Helper function that wraps any error and generates a `" $status "` response."]
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
pub fn $name<T>(err: T) -> Error
|
|
||||||
where
|
|
||||||
T: fmt::Debug + fmt::Display + 'static,
|
|
||||||
{
|
|
||||||
InternalError::new(err, StatusCode::$status).into()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
error_helper!(ErrorBadRequest, BAD_REQUEST);
|
|
||||||
error_helper!(ErrorUnauthorized, UNAUTHORIZED);
|
|
||||||
error_helper!(ErrorPaymentRequired, PAYMENT_REQUIRED);
|
|
||||||
error_helper!(ErrorForbidden, FORBIDDEN);
|
|
||||||
error_helper!(ErrorNotFound, NOT_FOUND);
|
|
||||||
error_helper!(ErrorMethodNotAllowed, METHOD_NOT_ALLOWED);
|
|
||||||
error_helper!(ErrorNotAcceptable, NOT_ACCEPTABLE);
|
|
||||||
error_helper!(
|
|
||||||
ErrorProxyAuthenticationRequired,
|
|
||||||
PROXY_AUTHENTICATION_REQUIRED
|
|
||||||
);
|
|
||||||
error_helper!(ErrorRequestTimeout, REQUEST_TIMEOUT);
|
|
||||||
error_helper!(ErrorConflict, CONFLICT);
|
|
||||||
error_helper!(ErrorGone, GONE);
|
|
||||||
error_helper!(ErrorLengthRequired, LENGTH_REQUIRED);
|
|
||||||
error_helper!(ErrorPayloadTooLarge, PAYLOAD_TOO_LARGE);
|
|
||||||
error_helper!(ErrorUriTooLong, URI_TOO_LONG);
|
|
||||||
error_helper!(ErrorUnsupportedMediaType, UNSUPPORTED_MEDIA_TYPE);
|
|
||||||
error_helper!(ErrorRangeNotSatisfiable, RANGE_NOT_SATISFIABLE);
|
|
||||||
error_helper!(ErrorImATeapot, IM_A_TEAPOT);
|
|
||||||
error_helper!(ErrorMisdirectedRequest, MISDIRECTED_REQUEST);
|
|
||||||
error_helper!(ErrorUnprocessableEntity, UNPROCESSABLE_ENTITY);
|
|
||||||
error_helper!(ErrorLocked, LOCKED);
|
|
||||||
error_helper!(ErrorFailedDependency, FAILED_DEPENDENCY);
|
|
||||||
error_helper!(ErrorUpgradeRequired, UPGRADE_REQUIRED);
|
|
||||||
error_helper!(ErrorPreconditionFailed, PRECONDITION_FAILED);
|
|
||||||
error_helper!(ErrorPreconditionRequired, PRECONDITION_REQUIRED);
|
|
||||||
error_helper!(ErrorTooManyRequests, TOO_MANY_REQUESTS);
|
|
||||||
error_helper!(
|
|
||||||
ErrorRequestHeaderFieldsTooLarge,
|
|
||||||
REQUEST_HEADER_FIELDS_TOO_LARGE
|
|
||||||
);
|
|
||||||
error_helper!(
|
|
||||||
ErrorUnavailableForLegalReasons,
|
|
||||||
UNAVAILABLE_FOR_LEGAL_REASONS
|
|
||||||
);
|
|
||||||
error_helper!(ErrorExpectationFailed, EXPECTATION_FAILED);
|
|
||||||
error_helper!(ErrorInternalServerError, INTERNAL_SERVER_ERROR);
|
|
||||||
error_helper!(ErrorNotImplemented, NOT_IMPLEMENTED);
|
|
||||||
error_helper!(ErrorBadGateway, BAD_GATEWAY);
|
|
||||||
error_helper!(ErrorServiceUnavailable, SERVICE_UNAVAILABLE);
|
|
||||||
error_helper!(ErrorGatewayTimeout, GATEWAY_TIMEOUT);
|
|
||||||
error_helper!(ErrorHttpVersionNotSupported, HTTP_VERSION_NOT_SUPPORTED);
|
|
||||||
error_helper!(ErrorVariantAlsoNegotiates, VARIANT_ALSO_NEGOTIATES);
|
|
||||||
error_helper!(ErrorInsufficientStorage, INSUFFICIENT_STORAGE);
|
|
||||||
error_helper!(ErrorLoopDetected, LOOP_DETECTED);
|
|
||||||
error_helper!(ErrorNotExtended, NOT_EXTENDED);
|
|
||||||
error_helper!(
|
|
||||||
ErrorNetworkAuthenticationRequired,
|
|
||||||
NETWORK_AUTHENTICATION_REQUIRED
|
|
||||||
);
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -718,13 +536,6 @@ mod tests {
|
||||||
from!(httparse::Error::Version => ParseError::Version);
|
from!(httparse::Error::Version => ParseError::Version);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_internal_error() {
|
|
||||||
let err = InternalError::from_response(ParseError::Method, Response::ok());
|
|
||||||
let resp: Response<Body> = err.error_response();
|
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_error_casting() {
|
fn test_error_casting() {
|
||||||
let err = PayloadError::Overflow;
|
let err = PayloadError::Overflow;
|
||||||
|
@ -734,124 +545,4 @@ mod tests {
|
||||||
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());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_error_helpers() {
|
|
||||||
let res: Response<Body> = ErrorBadRequest("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorUnauthorized("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::UNAUTHORIZED);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorPaymentRequired("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::PAYMENT_REQUIRED);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorForbidden("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::FORBIDDEN);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorNotFound("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::NOT_FOUND);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorMethodNotAllowed("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::METHOD_NOT_ALLOWED);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorNotAcceptable("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::NOT_ACCEPTABLE);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorProxyAuthenticationRequired("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::PROXY_AUTHENTICATION_REQUIRED);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorRequestTimeout("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::REQUEST_TIMEOUT);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorConflict("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::CONFLICT);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorGone("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::GONE);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorLengthRequired("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::LENGTH_REQUIRED);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorPreconditionFailed("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::PRECONDITION_FAILED);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorPayloadTooLarge("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::PAYLOAD_TOO_LARGE);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorUriTooLong("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::URI_TOO_LONG);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorUnsupportedMediaType("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::UNSUPPORTED_MEDIA_TYPE);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorRangeNotSatisfiable("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::RANGE_NOT_SATISFIABLE);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorExpectationFailed("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::EXPECTATION_FAILED);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorImATeapot("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::IM_A_TEAPOT);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorMisdirectedRequest("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::MISDIRECTED_REQUEST);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorUnprocessableEntity("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::UNPROCESSABLE_ENTITY);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorLocked("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::LOCKED);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorFailedDependency("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::FAILED_DEPENDENCY);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorUpgradeRequired("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::UPGRADE_REQUIRED);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorPreconditionRequired("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::PRECONDITION_REQUIRED);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorTooManyRequests("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::TOO_MANY_REQUESTS);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorRequestHeaderFieldsTooLarge("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::REQUEST_HEADER_FIELDS_TOO_LARGE);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorUnavailableForLegalReasons("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorInternalServerError("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorNotImplemented("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::NOT_IMPLEMENTED);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorBadGateway("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::BAD_GATEWAY);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorServiceUnavailable("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::SERVICE_UNAVAILABLE);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorGatewayTimeout("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::GATEWAY_TIMEOUT);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorHttpVersionNotSupported("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::HTTP_VERSION_NOT_SUPPORTED);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorVariantAlsoNegotiates("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::VARIANT_ALSO_NEGOTIATES);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorInsufficientStorage("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::INSUFFICIENT_STORAGE);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorLoopDetected("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::LOOP_DETECTED);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorNotExtended("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::NOT_EXTENDED);
|
|
||||||
|
|
||||||
let res: Response<Body> = ErrorNetworkAuthenticationRequired("err").into();
|
|
||||||
assert_eq!(res.status(), StatusCode::NETWORK_AUTHENTICATION_REQUIRED);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,20 +1,26 @@
|
||||||
use std::task::{Context, Poll};
|
use std::{
|
||||||
use std::{cmp, future::Future, marker::PhantomData, net, pin::Pin, rc::Rc};
|
cmp,
|
||||||
|
future::Future,
|
||||||
|
marker::PhantomData,
|
||||||
|
net,
|
||||||
|
pin::Pin,
|
||||||
|
rc::Rc,
|
||||||
|
task::{Context, Poll},
|
||||||
|
};
|
||||||
|
|
||||||
use actix_codec::{AsyncRead, AsyncWrite};
|
use actix_codec::{AsyncRead, AsyncWrite};
|
||||||
use actix_service::Service;
|
use actix_service::Service;
|
||||||
|
use actix_utils::future::poll_fn;
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use futures_core::ready;
|
use futures_core::ready;
|
||||||
use h2::{
|
use h2::server::{Connection, SendResponse};
|
||||||
server::{Connection, SendResponse},
|
|
||||||
SendStream,
|
|
||||||
};
|
|
||||||
use http::header::{HeaderValue, CONNECTION, CONTENT_LENGTH, DATE, TRANSFER_ENCODING};
|
use http::header::{HeaderValue, CONNECTION, CONTENT_LENGTH, DATE, TRANSFER_ENCODING};
|
||||||
use log::{error, trace};
|
use log::{error, trace};
|
||||||
|
use pin_project_lite::pin_project;
|
||||||
|
|
||||||
use crate::body::{Body, BodySize, MessageBody};
|
use crate::body::{BodySize, MessageBody};
|
||||||
use crate::config::ServiceConfig;
|
use crate::config::ServiceConfig;
|
||||||
use crate::error::{DispatchError, Error};
|
use crate::error::Error;
|
||||||
use crate::message::ResponseHead;
|
use crate::message::ResponseHead;
|
||||||
use crate::payload::Payload;
|
use crate::payload::Payload;
|
||||||
use crate::request::Request;
|
use crate::request::Request;
|
||||||
|
@ -24,30 +30,19 @@ use crate::OnConnectData;
|
||||||
|
|
||||||
const CHUNK_SIZE: usize = 16_384;
|
const CHUNK_SIZE: usize = 16_384;
|
||||||
|
|
||||||
/// Dispatcher for HTTP/2 protocol.
|
pin_project! {
|
||||||
#[pin_project::pin_project]
|
/// Dispatcher for HTTP/2 protocol.
|
||||||
pub struct Dispatcher<T, S, B, X, U>
|
pub struct Dispatcher<T, S, B, X, U> {
|
||||||
where
|
|
||||||
T: AsyncRead + AsyncWrite + Unpin,
|
|
||||||
S: Service<Request>,
|
|
||||||
B: MessageBody,
|
|
||||||
{
|
|
||||||
flow: Rc<HttpFlow<S, X, U>>,
|
flow: Rc<HttpFlow<S, X, U>>,
|
||||||
connection: Connection<T, Bytes>,
|
connection: Connection<T, Bytes>,
|
||||||
on_connect_data: OnConnectData,
|
on_connect_data: OnConnectData,
|
||||||
config: ServiceConfig,
|
config: ServiceConfig,
|
||||||
peer_addr: Option<net::SocketAddr>,
|
peer_addr: Option<net::SocketAddr>,
|
||||||
_phantom: PhantomData<B>,
|
_phantom: PhantomData<B>,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, S, B, X, U> Dispatcher<T, S, B, X, U>
|
impl<T, S, B, X, U> Dispatcher<T, S, B, X, U> {
|
||||||
where
|
|
||||||
T: AsyncRead + AsyncWrite + Unpin,
|
|
||||||
S: Service<Request>,
|
|
||||||
S::Error: Into<Error>,
|
|
||||||
S::Response: Into<Response<B>>,
|
|
||||||
B: MessageBody,
|
|
||||||
{
|
|
||||||
pub(crate) fn new(
|
pub(crate) fn new(
|
||||||
flow: Rc<HttpFlow<S, X, U>>,
|
flow: Rc<HttpFlow<S, X, U>>,
|
||||||
connection: Connection<T, Bytes>,
|
connection: Connection<T, Bytes>,
|
||||||
|
@ -55,7 +50,7 @@ where
|
||||||
config: ServiceConfig,
|
config: ServiceConfig,
|
||||||
peer_addr: Option<net::SocketAddr>,
|
peer_addr: Option<net::SocketAddr>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Dispatcher {
|
Self {
|
||||||
flow,
|
flow,
|
||||||
config,
|
config,
|
||||||
peer_addr,
|
peer_addr,
|
||||||
|
@ -71,26 +66,22 @@ where
|
||||||
T: AsyncRead + AsyncWrite + Unpin,
|
T: AsyncRead + AsyncWrite + Unpin,
|
||||||
|
|
||||||
S: Service<Request>,
|
S: Service<Request>,
|
||||||
S::Error: Into<Error> + 'static,
|
S::Error: Into<Error>,
|
||||||
S::Future: 'static,
|
S::Future: 'static,
|
||||||
S::Response: Into<Response<B>> + 'static,
|
S::Response: Into<Response<B>>,
|
||||||
|
|
||||||
B: MessageBody + 'static,
|
B: MessageBody,
|
||||||
B::Error: Into<Error>,
|
B::Error: Into<Error>,
|
||||||
{
|
{
|
||||||
type Output = Result<(), DispatchError>;
|
type Output = Result<(), crate::error::DispatchError>;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
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();
|
||||||
|
|
||||||
loop {
|
while let Some((req, tx)) =
|
||||||
match ready!(Pin::new(&mut this.connection).poll_accept(cx)) {
|
ready!(Pin::new(&mut this.connection).poll_accept(cx)?)
|
||||||
None => return Poll::Ready(Ok(())),
|
{
|
||||||
|
|
||||||
Some(Err(err)) => return Poll::Ready(Err(err.into())),
|
|
||||||
|
|
||||||
Some(Ok((req, res))) => {
|
|
||||||
let (parts, body) = req.into_parts();
|
let (parts, body) = req.into_parts();
|
||||||
let pl = crate::h2::Payload::new(body);
|
let pl = crate::h2::Payload::new(body);
|
||||||
let pl = Payload::<crate::payload::PayloadStream>::H2(pl);
|
let pl = Payload::<crate::payload::PayloadStream>::H2(pl);
|
||||||
|
@ -106,53 +97,116 @@ where
|
||||||
// merge on_connect_ext data into request extensions
|
// merge on_connect_ext data into request extensions
|
||||||
this.on_connect_data.merge_into(&mut req);
|
this.on_connect_data.merge_into(&mut req);
|
||||||
|
|
||||||
let svc = ServiceResponse {
|
let fut = this.flow.service.call(req);
|
||||||
state: ServiceResponseState::ServiceCall(
|
let config = this.config.clone();
|
||||||
this.flow.service.call(req),
|
|
||||||
Some(res),
|
// multiplex request handling with spawn task
|
||||||
),
|
actix_rt::spawn(async move {
|
||||||
config: this.config.clone(),
|
// resolve service call and send response.
|
||||||
buffer: None,
|
let res = match fut.await {
|
||||||
_phantom: PhantomData,
|
Ok(res) => handle_response(res.into(), tx, config).await,
|
||||||
|
Err(err) => {
|
||||||
|
let res = Response::from_error(err.into());
|
||||||
|
handle_response(res, tx, config).await
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
actix_rt::spawn(svc);
|
// log error.
|
||||||
|
if let Err(err) = res {
|
||||||
|
match err {
|
||||||
|
DispatchError::SendResponse(err) => {
|
||||||
|
trace!("Error sending HTTP/2 response: {:?}", err)
|
||||||
|
}
|
||||||
|
DispatchError::SendData(err) => warn!("{:?}", err),
|
||||||
|
DispatchError::ResponseBody(err) => {
|
||||||
|
error!("Response payload stream error: {:?}", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[pin_project::pin_project]
|
enum DispatchError {
|
||||||
struct ServiceResponse<F, I, E, B> {
|
SendResponse(h2::Error),
|
||||||
#[pin]
|
SendData(h2::Error),
|
||||||
state: ServiceResponseState<F, B>,
|
ResponseBody(Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle_response<B>(
|
||||||
|
res: Response<B>,
|
||||||
|
mut tx: SendResponse<Bytes>,
|
||||||
config: ServiceConfig,
|
config: ServiceConfig,
|
||||||
buffer: Option<Bytes>,
|
) -> Result<(), DispatchError>
|
||||||
_phantom: PhantomData<(I, E)>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[pin_project::pin_project(project = ServiceResponseStateProj)]
|
|
||||||
enum ServiceResponseState<F, B> {
|
|
||||||
ServiceCall(#[pin] F, Option<SendResponse<Bytes>>),
|
|
||||||
SendPayload(SendStream<Bytes>, #[pin] B),
|
|
||||||
SendErrorPayload(SendStream<Bytes>, #[pin] Body),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<F, I, E, B> ServiceResponse<F, I, E, B>
|
|
||||||
where
|
where
|
||||||
F: Future<Output = Result<I, E>>,
|
|
||||||
E: Into<Error>,
|
|
||||||
I: Into<Response<B>>,
|
|
||||||
|
|
||||||
B: MessageBody,
|
B: MessageBody,
|
||||||
B::Error: Into<Error>,
|
B::Error: Into<Error>,
|
||||||
{
|
{
|
||||||
fn prepare_response(
|
let (res, body) = res.replace_body(());
|
||||||
&self,
|
|
||||||
|
// prepare response.
|
||||||
|
let mut size = body.size();
|
||||||
|
let res = prepare_response(config, res.head(), &mut size);
|
||||||
|
let eof = size.is_eof();
|
||||||
|
|
||||||
|
// send response head and return on eof.
|
||||||
|
let mut stream = tx
|
||||||
|
.send_response(res, eof)
|
||||||
|
.map_err(DispatchError::SendResponse)?;
|
||||||
|
|
||||||
|
if eof {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
// poll response body and send chunks to client.
|
||||||
|
actix_rt::pin!(body);
|
||||||
|
|
||||||
|
while let Some(res) = poll_fn(|cx| body.as_mut().poll_next(cx)).await {
|
||||||
|
let mut chunk = res.map_err(|err| DispatchError::ResponseBody(err.into()))?;
|
||||||
|
|
||||||
|
'send: loop {
|
||||||
|
// reserve enough space and wait for stream ready.
|
||||||
|
stream.reserve_capacity(cmp::min(chunk.len(), CHUNK_SIZE));
|
||||||
|
|
||||||
|
match poll_fn(|cx| stream.poll_capacity(cx)).await {
|
||||||
|
// No capacity left. drop body and return.
|
||||||
|
None => return Ok(()),
|
||||||
|
Some(res) => {
|
||||||
|
// Split chuck to writeable size and send to client.
|
||||||
|
let cap = res.map_err(DispatchError::SendData)?;
|
||||||
|
|
||||||
|
let len = chunk.len();
|
||||||
|
let bytes = chunk.split_to(cmp::min(cap, len));
|
||||||
|
|
||||||
|
stream
|
||||||
|
.send_data(bytes, false)
|
||||||
|
.map_err(DispatchError::SendData)?;
|
||||||
|
|
||||||
|
// Current chuck completely sent. break send loop and poll next one.
|
||||||
|
if chunk.is_empty() {
|
||||||
|
break 'send;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// response body streaming finished. send end of stream and return.
|
||||||
|
stream
|
||||||
|
.send_data(Bytes::new(), true)
|
||||||
|
.map_err(DispatchError::SendData)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn prepare_response(
|
||||||
|
config: ServiceConfig,
|
||||||
head: &ResponseHead,
|
head: &ResponseHead,
|
||||||
size: &mut BodySize,
|
size: &mut BodySize,
|
||||||
) -> http::Response<()> {
|
) -> http::Response<()> {
|
||||||
let mut has_date = false;
|
let mut has_date = false;
|
||||||
let mut skip_len = size != &BodySize::Stream;
|
let mut skip_len = size != &BodySize::Stream;
|
||||||
|
|
||||||
|
@ -205,7 +259,7 @@ where
|
||||||
// set date header
|
// set date header
|
||||||
if !has_date {
|
if !has_date {
|
||||||
let mut bytes = BytesMut::with_capacity(29);
|
let mut bytes = BytesMut::with_capacity(29);
|
||||||
self.config.set_date_header(&mut bytes);
|
config.set_date_header(&mut bytes);
|
||||||
res.headers_mut().insert(
|
res.headers_mut().insert(
|
||||||
DATE,
|
DATE,
|
||||||
// SAFETY: serialized date-times are known ASCII strings
|
// SAFETY: serialized date-times are known ASCII strings
|
||||||
|
@ -214,188 +268,4 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
res
|
res
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<F, I, E, B> Future for ServiceResponse<F, I, E, B>
|
|
||||||
where
|
|
||||||
F: Future<Output = Result<I, E>>,
|
|
||||||
E: Into<Error>,
|
|
||||||
I: Into<Response<B>>,
|
|
||||||
|
|
||||||
B: MessageBody,
|
|
||||||
B::Error: Into<Error>,
|
|
||||||
{
|
|
||||||
type Output = ();
|
|
||||||
|
|
||||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
||||||
let mut this = self.as_mut().project();
|
|
||||||
|
|
||||||
match this.state.project() {
|
|
||||||
ServiceResponseStateProj::ServiceCall(call, send) => {
|
|
||||||
match ready!(call.poll(cx)) {
|
|
||||||
Ok(res) => {
|
|
||||||
let (res, body) = res.into().replace_body(());
|
|
||||||
|
|
||||||
let mut send = send.take().unwrap();
|
|
||||||
let mut size = body.size();
|
|
||||||
let h2_res =
|
|
||||||
self.as_mut().prepare_response(res.head(), &mut size);
|
|
||||||
this = self.as_mut().project();
|
|
||||||
|
|
||||||
let stream = match send.send_response(h2_res, size.is_eof()) {
|
|
||||||
Err(e) => {
|
|
||||||
trace!("Error sending HTTP/2 response: {:?}", e);
|
|
||||||
return Poll::Ready(());
|
|
||||||
}
|
|
||||||
Ok(stream) => stream,
|
|
||||||
};
|
|
||||||
|
|
||||||
if size.is_eof() {
|
|
||||||
Poll::Ready(())
|
|
||||||
} else {
|
|
||||||
this.state
|
|
||||||
.set(ServiceResponseState::SendPayload(stream, body));
|
|
||||||
self.poll(cx)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Err(err) => {
|
|
||||||
let res = Response::from_error(err.into());
|
|
||||||
let (res, body) = res.replace_body(());
|
|
||||||
|
|
||||||
let mut send = send.take().unwrap();
|
|
||||||
let mut size = body.size();
|
|
||||||
let h2_res =
|
|
||||||
self.as_mut().prepare_response(res.head(), &mut size);
|
|
||||||
this = self.as_mut().project();
|
|
||||||
|
|
||||||
let stream = match send.send_response(h2_res, size.is_eof()) {
|
|
||||||
Err(e) => {
|
|
||||||
trace!("Error sending HTTP/2 response: {:?}", e);
|
|
||||||
return Poll::Ready(());
|
|
||||||
}
|
|
||||||
Ok(stream) => stream,
|
|
||||||
};
|
|
||||||
|
|
||||||
if size.is_eof() {
|
|
||||||
Poll::Ready(())
|
|
||||||
} else {
|
|
||||||
this.state.set(ServiceResponseState::SendErrorPayload(
|
|
||||||
stream, body,
|
|
||||||
));
|
|
||||||
self.poll(cx)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ServiceResponseStateProj::SendPayload(ref mut stream, ref mut body) => {
|
|
||||||
loop {
|
|
||||||
match this.buffer {
|
|
||||||
Some(ref mut buffer) => match ready!(stream.poll_capacity(cx)) {
|
|
||||||
None => return Poll::Ready(()),
|
|
||||||
|
|
||||||
Some(Ok(cap)) => {
|
|
||||||
let len = buffer.len();
|
|
||||||
let bytes = buffer.split_to(cmp::min(cap, len));
|
|
||||||
|
|
||||||
if let Err(e) = stream.send_data(bytes, false) {
|
|
||||||
warn!("{:?}", e);
|
|
||||||
return Poll::Ready(());
|
|
||||||
} else if !buffer.is_empty() {
|
|
||||||
let cap = cmp::min(buffer.len(), CHUNK_SIZE);
|
|
||||||
stream.reserve_capacity(cap);
|
|
||||||
} else {
|
|
||||||
this.buffer.take();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Some(Err(e)) => {
|
|
||||||
warn!("{:?}", e);
|
|
||||||
return Poll::Ready(());
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
None => match ready!(body.as_mut().poll_next(cx)) {
|
|
||||||
None => {
|
|
||||||
if let Err(e) = stream.send_data(Bytes::new(), true) {
|
|
||||||
warn!("{:?}", e);
|
|
||||||
}
|
|
||||||
return Poll::Ready(());
|
|
||||||
}
|
|
||||||
|
|
||||||
Some(Ok(chunk)) => {
|
|
||||||
stream
|
|
||||||
.reserve_capacity(cmp::min(chunk.len(), CHUNK_SIZE));
|
|
||||||
*this.buffer = Some(chunk);
|
|
||||||
}
|
|
||||||
|
|
||||||
Some(Err(err)) => {
|
|
||||||
error!(
|
|
||||||
"Response payload stream error: {:?}",
|
|
||||||
err.into()
|
|
||||||
);
|
|
||||||
|
|
||||||
return Poll::Ready(());
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ServiceResponseStateProj::SendErrorPayload(ref mut stream, ref mut body) => {
|
|
||||||
// TODO: de-dupe impl with SendPayload
|
|
||||||
|
|
||||||
loop {
|
|
||||||
match this.buffer {
|
|
||||||
Some(ref mut buffer) => match ready!(stream.poll_capacity(cx)) {
|
|
||||||
None => return Poll::Ready(()),
|
|
||||||
|
|
||||||
Some(Ok(cap)) => {
|
|
||||||
let len = buffer.len();
|
|
||||||
let bytes = buffer.split_to(cmp::min(cap, len));
|
|
||||||
|
|
||||||
if let Err(e) = stream.send_data(bytes, false) {
|
|
||||||
warn!("{:?}", e);
|
|
||||||
return Poll::Ready(());
|
|
||||||
} else if !buffer.is_empty() {
|
|
||||||
let cap = cmp::min(buffer.len(), CHUNK_SIZE);
|
|
||||||
stream.reserve_capacity(cap);
|
|
||||||
} else {
|
|
||||||
this.buffer.take();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Some(Err(e)) => {
|
|
||||||
warn!("{:?}", e);
|
|
||||||
return Poll::Ready(());
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
None => match ready!(body.as_mut().poll_next(cx)) {
|
|
||||||
None => {
|
|
||||||
if let Err(e) = stream.send_data(Bytes::new(), true) {
|
|
||||||
warn!("{:?}", e);
|
|
||||||
}
|
|
||||||
return Poll::Ready(());
|
|
||||||
}
|
|
||||||
|
|
||||||
Some(Ok(chunk)) => {
|
|
||||||
stream
|
|
||||||
.reserve_capacity(cmp::min(chunk.len(), CHUNK_SIZE));
|
|
||||||
*this.buffer = Some(chunk);
|
|
||||||
}
|
|
||||||
|
|
||||||
Some(Err(err)) => {
|
|
||||||
error!("Response payload stream error: {:?}", err);
|
|
||||||
|
|
||||||
return Poll::Ready(());
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,7 +41,8 @@ pub fn write_content_length<B: BufMut>(n: u64, buf: &mut B) {
|
||||||
buf.put_slice(b"\r\n");
|
buf.put_slice(b"\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: bench why this is needed
|
// TODO: bench why this is needed vs Buf::writer
|
||||||
|
/// An `io` writer for a `BufMut` that should only be used once and on an empty buffer.
|
||||||
pub(crate) struct Writer<'a, B>(pub &'a mut B);
|
pub(crate) struct Writer<'a, B>(pub &'a mut B);
|
||||||
|
|
||||||
impl<'a, B> io::Write for Writer<'a, B>
|
impl<'a, B> io::Write for Writer<'a, B>
|
||||||
|
|
|
@ -84,7 +84,7 @@ impl<B> Response<B> {
|
||||||
pub fn with_body(status: StatusCode, body: B) -> Response<B> {
|
pub fn with_body(status: StatusCode, body: B) -> Response<B> {
|
||||||
Response {
|
Response {
|
||||||
head: BoxedResponseHead::new(status),
|
head: BoxedResponseHead::new(status),
|
||||||
body: body,
|
body,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
use actix_http::{
|
use actix_http::{
|
||||||
error, http, http::StatusCode, HttpMessage, HttpService, Request, Response,
|
http, http::StatusCode, HttpMessage, HttpService, Request, Response, ResponseError,
|
||||||
};
|
};
|
||||||
use actix_http_test::test_server;
|
use actix_http_test::test_server;
|
||||||
use actix_service::ServiceFactoryExt;
|
use actix_service::ServiceFactoryExt;
|
||||||
use actix_utils::future;
|
use actix_utils::future;
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
|
use derive_more::{Display, Error};
|
||||||
use futures_util::StreamExt as _;
|
use futures_util::StreamExt as _;
|
||||||
|
|
||||||
const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
|
const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
|
||||||
|
@ -92,6 +93,16 @@ async fn test_with_query_parameter() {
|
||||||
assert!(response.status().is_success());
|
assert!(response.status().is_success());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Display, Error)]
|
||||||
|
#[display(fmt = "expect failed")]
|
||||||
|
struct ExpectFailed;
|
||||||
|
|
||||||
|
impl ResponseError for ExpectFailed {
|
||||||
|
fn status_code(&self) -> StatusCode {
|
||||||
|
StatusCode::EXPECTATION_FAILED
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_h1_expect() {
|
async fn test_h1_expect() {
|
||||||
let srv = test_server(move || {
|
let srv = test_server(move || {
|
||||||
|
@ -100,7 +111,7 @@ async fn test_h1_expect() {
|
||||||
if req.headers().contains_key("AUTH") {
|
if req.headers().contains_key("AUTH") {
|
||||||
Ok(req)
|
Ok(req)
|
||||||
} else {
|
} else {
|
||||||
Err(error::ErrorExpectationFailed("expect failed"))
|
Err(ExpectFailed)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.h1(|req: Request| async move {
|
.h1(|req: Request| async move {
|
||||||
|
@ -134,7 +145,7 @@ async fn test_h1_expect() {
|
||||||
let response = request.send_body("expect body").await.unwrap();
|
let response = request.send_body("expect body").await.unwrap();
|
||||||
assert_eq!(response.status(), StatusCode::EXPECTATION_FAILED);
|
assert_eq!(response.status(), StatusCode::EXPECTATION_FAILED);
|
||||||
|
|
||||||
// test exepct would continue
|
// test expect would continue
|
||||||
let request = srv
|
let request = srv
|
||||||
.request(http::Method::GET, srv.url("/"))
|
.request(http::Method::GET, srv.url("/"))
|
||||||
.insert_header(("Expect", "100-continue"))
|
.insert_header(("Expect", "100-continue"))
|
||||||
|
|
|
@ -6,17 +6,18 @@ use std::io;
|
||||||
|
|
||||||
use actix_http::{
|
use actix_http::{
|
||||||
body::{Body, SizedStream},
|
body::{Body, SizedStream},
|
||||||
error::{ErrorBadRequest, PayloadError},
|
error::PayloadError,
|
||||||
http::{
|
http::{
|
||||||
header::{self, HeaderName, HeaderValue},
|
header::{self, HeaderName, HeaderValue},
|
||||||
Method, StatusCode, Version,
|
Method, StatusCode, Version,
|
||||||
},
|
},
|
||||||
Error, HttpMessage, HttpService, Request, Response,
|
Error, HttpMessage, HttpService, Request, Response, ResponseError,
|
||||||
};
|
};
|
||||||
use actix_http_test::test_server;
|
use actix_http_test::test_server;
|
||||||
use actix_service::{fn_service, ServiceFactoryExt};
|
use actix_service::{fn_service, ServiceFactoryExt};
|
||||||
use actix_utils::future::{err, ok, ready};
|
use actix_utils::future::{err, ok, ready};
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
|
use derive_more::{Display, Error};
|
||||||
use futures_core::Stream;
|
use futures_core::Stream;
|
||||||
use futures_util::stream::{once, StreamExt as _};
|
use futures_util::stream::{once, StreamExt as _};
|
||||||
use openssl::{
|
use openssl::{
|
||||||
|
@ -401,11 +402,21 @@ async fn test_h2_response_http_error_handling() {
|
||||||
assert_eq!(bytes, Bytes::from_static(b"failed to parse header value"));
|
assert_eq!(bytes, Bytes::from_static(b"failed to parse header value"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Display, Error)]
|
||||||
|
#[display(fmt = "error")]
|
||||||
|
struct BadRequest;
|
||||||
|
|
||||||
|
impl ResponseError for BadRequest {
|
||||||
|
fn status_code(&self) -> StatusCode {
|
||||||
|
StatusCode::BAD_REQUEST
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_h2_service_error() {
|
async fn test_h2_service_error() {
|
||||||
let mut srv = test_server(move || {
|
let mut srv = test_server(move || {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h2(|_| err::<Response<Body>, Error>(ErrorBadRequest("error")))
|
.h2(|_| err::<Response<Body>, _>(BadRequest))
|
||||||
.openssl(tls_config())
|
.openssl(tls_config())
|
||||||
.map_err(|_| ())
|
.map_err(|_| ())
|
||||||
})
|
})
|
||||||
|
|
|
@ -4,18 +4,18 @@ extern crate tls_rustls as rustls;
|
||||||
|
|
||||||
use actix_http::{
|
use actix_http::{
|
||||||
body::{Body, SizedStream},
|
body::{Body, SizedStream},
|
||||||
error::{self, PayloadError},
|
error::PayloadError,
|
||||||
http::{
|
http::{
|
||||||
header::{self, HeaderName, HeaderValue},
|
header::{self, HeaderName, HeaderValue},
|
||||||
Method, StatusCode, Version,
|
Method, StatusCode, Version,
|
||||||
},
|
},
|
||||||
Error, HttpService, Request, Response,
|
Error, HttpService, Request, Response, ResponseError,
|
||||||
};
|
};
|
||||||
use actix_http_test::test_server;
|
use actix_http_test::test_server;
|
||||||
use actix_service::{fn_factory_with_config, fn_service};
|
use actix_service::{fn_factory_with_config, fn_service};
|
||||||
use actix_utils::future::{err, ok};
|
use actix_utils::future::{err, ok};
|
||||||
|
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
|
use derive_more::{Display, Error};
|
||||||
use futures_core::Stream;
|
use futures_core::Stream;
|
||||||
use futures_util::stream::{once, StreamExt as _};
|
use futures_util::stream::{once, StreamExt as _};
|
||||||
use rustls::{
|
use rustls::{
|
||||||
|
@ -417,11 +417,21 @@ async fn test_h2_response_http_error_handling() {
|
||||||
assert_eq!(bytes, Bytes::from_static(b"failed to parse header value"));
|
assert_eq!(bytes, Bytes::from_static(b"failed to parse header value"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Display, Error)]
|
||||||
|
#[display(fmt = "error")]
|
||||||
|
struct BadRequest;
|
||||||
|
|
||||||
|
impl ResponseError for BadRequest {
|
||||||
|
fn status_code(&self) -> StatusCode {
|
||||||
|
StatusCode::BAD_REQUEST
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_h2_service_error() {
|
async fn test_h2_service_error() {
|
||||||
let mut srv = test_server(move || {
|
let mut srv = test_server(move || {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h2(|_| err::<Response<Body>, Error>(error::ErrorBadRequest("error")))
|
.h2(|_| err::<Response<Body>, _>(BadRequest))
|
||||||
.rustls(tls_config())
|
.rustls(tls_config())
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -438,7 +448,7 @@ async fn test_h2_service_error() {
|
||||||
async fn test_h1_service_error() {
|
async fn test_h1_service_error() {
|
||||||
let mut srv = test_server(move || {
|
let mut srv = test_server(move || {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h1(|_| err::<Response<Body>, Error>(error::ErrorBadRequest("error")))
|
.h1(|_| err::<Response<Body>, _>(BadRequest))
|
||||||
.rustls(tls_config())
|
.rustls(tls_config())
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
|
|
@ -2,23 +2,22 @@ use std::io::{Read, Write};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::{net, thread};
|
use std::{net, thread};
|
||||||
|
|
||||||
|
use actix_http::{
|
||||||
|
body::{Body, SizedStream},
|
||||||
|
http::{self, header, StatusCode},
|
||||||
|
Error, HttpService, KeepAlive, Request, Response,
|
||||||
|
};
|
||||||
|
use actix_http::{HttpMessage, ResponseError};
|
||||||
use actix_http_test::test_server;
|
use actix_http_test::test_server;
|
||||||
use actix_rt::time::sleep;
|
use actix_rt::time::sleep;
|
||||||
use actix_service::fn_service;
|
use actix_service::fn_service;
|
||||||
use actix_utils::future::{err, ok, ready};
|
use actix_utils::future::{err, ok, ready};
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
|
use derive_more::{Display, Error};
|
||||||
use futures_util::stream::{once, StreamExt as _};
|
use futures_util::stream::{once, StreamExt as _};
|
||||||
use futures_util::FutureExt as _;
|
use futures_util::FutureExt as _;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
|
||||||
use actix_http::HttpMessage;
|
|
||||||
use actix_http::{
|
|
||||||
body::{Body, SizedStream},
|
|
||||||
error,
|
|
||||||
http::{self, header, StatusCode},
|
|
||||||
Error, HttpService, KeepAlive, Request, Response,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_h1() {
|
async fn test_h1() {
|
||||||
let srv = test_server(|| {
|
let srv = test_server(|| {
|
||||||
|
@ -58,6 +57,16 @@ async fn test_h1_2() {
|
||||||
assert!(response.status().is_success());
|
assert!(response.status().is_success());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Display, Error)]
|
||||||
|
#[display(fmt = "expect failed")]
|
||||||
|
struct ExpectFailed;
|
||||||
|
|
||||||
|
impl ResponseError for ExpectFailed {
|
||||||
|
fn status_code(&self) -> StatusCode {
|
||||||
|
StatusCode::PRECONDITION_FAILED
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_expect_continue() {
|
async fn test_expect_continue() {
|
||||||
let srv = test_server(|| {
|
let srv = test_server(|| {
|
||||||
|
@ -66,7 +75,7 @@ async fn test_expect_continue() {
|
||||||
if req.head().uri.query() == Some("yes=") {
|
if req.head().uri.query() == Some("yes=") {
|
||||||
ok(req)
|
ok(req)
|
||||||
} else {
|
} else {
|
||||||
err(error::ErrorPreconditionFailed("error"))
|
err(ExpectFailed)
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
.finish(|_| ok::<_, ()>(Response::ok()))
|
.finish(|_| ok::<_, ()>(Response::ok()))
|
||||||
|
@ -96,7 +105,7 @@ async fn test_expect_continue_h1() {
|
||||||
if req.head().uri.query() == Some("yes=") {
|
if req.head().uri.query() == Some("yes=") {
|
||||||
ok(req)
|
ok(req)
|
||||||
} else {
|
} else {
|
||||||
err(error::ErrorPreconditionFailed("error"))
|
err(ExpectFailed)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}))
|
}))
|
||||||
|
@ -647,11 +656,21 @@ async fn test_h1_response_http_error_handling() {
|
||||||
assert_eq!(bytes, Bytes::from_static(b"failed to parse header value"));
|
assert_eq!(bytes, Bytes::from_static(b"failed to parse header value"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Display, Error)]
|
||||||
|
#[display(fmt = "error")]
|
||||||
|
struct BadRequest;
|
||||||
|
|
||||||
|
impl ResponseError for BadRequest {
|
||||||
|
fn status_code(&self) -> StatusCode {
|
||||||
|
StatusCode::BAD_REQUEST
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_h1_service_error() {
|
async fn test_h1_service_error() {
|
||||||
let mut srv = test_server(|| {
|
let mut srv = test_server(|| {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h1(|_| err::<Response<Body>, _>(error::ErrorBadRequest("error")))
|
.h1(|_| err::<Response<Body>, _>(BadRequest))
|
||||||
.tcp()
|
.tcp()
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
#[rustversion::stable(1.46)] // MSRV
|
||||||
#[test]
|
#[test]
|
||||||
fn compile_macros() {
|
fn compile_macros() {
|
||||||
let t = trybuild::TestCases::new();
|
let t = trybuild::TestCases::new();
|
||||||
|
@ -12,11 +13,3 @@ fn compile_macros() {
|
||||||
|
|
||||||
t.pass("tests/trybuild/docstring-ok.rs");
|
t.pass("tests/trybuild/docstring-ok.rs");
|
||||||
}
|
}
|
||||||
|
|
||||||
// #[rustversion::not(nightly)]
|
|
||||||
// fn skip_on_nightly(t: &trybuild::TestCases) {
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
|
|
||||||
// #[rustversion::nightly]
|
|
||||||
// fn skip_on_nightly(_t: &trybuild::TestCases) {}
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use actix_web::{test, web, App, HttpResponse};
|
use actix_web::{web, App, HttpResponse};
|
||||||
use awc::Client;
|
use awc::Client;
|
||||||
use criterion::{criterion_group, criterion_main, Criterion};
|
use criterion::{criterion_group, criterion_main, Criterion};
|
||||||
use futures_util::future::join_all;
|
use futures_util::future::join_all;
|
||||||
|
|
13
src/data.rs
13
src/data.rs
|
@ -1,16 +1,13 @@
|
||||||
use std::any::type_name;
|
use std::{any::type_name, ops::Deref, sync::Arc};
|
||||||
use std::ops::Deref;
|
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use actix_http::error::{Error, ErrorInternalServerError};
|
use actix_http::{error::Error, Extensions};
|
||||||
use actix_http::Extensions;
|
|
||||||
use actix_utils::future::{err, ok, Ready};
|
use actix_utils::future::{err, ok, Ready};
|
||||||
use futures_core::future::LocalBoxFuture;
|
use futures_core::future::LocalBoxFuture;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
||||||
use crate::dev::Payload;
|
use crate::{
|
||||||
use crate::extract::FromRequest;
|
dev::Payload, error::ErrorInternalServerError, extract::FromRequest, request::HttpRequest,
|
||||||
use crate::request::HttpRequest;
|
};
|
||||||
|
|
||||||
/// Data factory.
|
/// Data factory.
|
||||||
pub(crate) trait DataFactory {
|
pub(crate) trait DataFactory {
|
||||||
|
|
|
@ -0,0 +1,304 @@
|
||||||
|
use std::{cell::RefCell, fmt, io::Write as _};
|
||||||
|
|
||||||
|
use actix_http::{body::Body, header, Response, StatusCode};
|
||||||
|
use bytes::{BufMut as _, BytesMut};
|
||||||
|
|
||||||
|
use crate::{Error, HttpResponse, ResponseError};
|
||||||
|
|
||||||
|
/// Wraps errors to alter the generated response status code.
|
||||||
|
///
|
||||||
|
/// In following example, the `io::Error` is wrapped into `ErrorBadRequest` which will generate a
|
||||||
|
/// response with the 400 Bad Request status code instead of the usual status code generated by
|
||||||
|
/// an `io::Error`.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
/// ```
|
||||||
|
/// # use std::io;
|
||||||
|
/// # use actix_web::{error, HttpRequest};
|
||||||
|
/// async fn handler_error() -> Result<String, actix_web::Error> {
|
||||||
|
/// let err = io::Error::new(io::ErrorKind::Other, "error");
|
||||||
|
/// Err(error::ErrorBadRequest(err))
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
pub struct InternalError<T> {
|
||||||
|
cause: T,
|
||||||
|
status: InternalErrorType,
|
||||||
|
}
|
||||||
|
|
||||||
|
enum InternalErrorType {
|
||||||
|
Status(StatusCode),
|
||||||
|
Response(RefCell<Option<HttpResponse>>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> InternalError<T> {
|
||||||
|
/// Constructs an `InternalError` with given status code.
|
||||||
|
pub fn new(cause: T, status: StatusCode) -> Self {
|
||||||
|
InternalError {
|
||||||
|
cause,
|
||||||
|
status: InternalErrorType::Status(status),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Constructs an `InternalError` with pre-defined response.
|
||||||
|
pub fn from_response(cause: T, response: HttpResponse) -> Self {
|
||||||
|
InternalError {
|
||||||
|
cause,
|
||||||
|
status: InternalErrorType::Response(RefCell::new(Some(response))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: fmt::Debug> fmt::Debug for InternalError<T> {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
self.cause.fmt(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: fmt::Display> fmt::Display for InternalError<T> {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
self.cause.fmt(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> ResponseError for InternalError<T>
|
||||||
|
where
|
||||||
|
T: fmt::Debug + fmt::Display,
|
||||||
|
{
|
||||||
|
fn status_code(&self) -> StatusCode {
|
||||||
|
match self.status {
|
||||||
|
InternalErrorType::Status(st) => st,
|
||||||
|
InternalErrorType::Response(ref resp) => {
|
||||||
|
if let Some(resp) = resp.borrow().as_ref() {
|
||||||
|
resp.head().status
|
||||||
|
} else {
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn error_response(&self) -> Response<Body> {
|
||||||
|
match self.status {
|
||||||
|
InternalErrorType::Status(status) => {
|
||||||
|
let mut res = Response::new(status);
|
||||||
|
let mut buf = BytesMut::new().writer();
|
||||||
|
let _ = write!(buf, "{}", self);
|
||||||
|
|
||||||
|
res.headers_mut().insert(
|
||||||
|
header::CONTENT_TYPE,
|
||||||
|
header::HeaderValue::from_static("text/plain; charset=utf-8"),
|
||||||
|
);
|
||||||
|
res.set_body(Body::from(buf.into_inner())).into()
|
||||||
|
}
|
||||||
|
|
||||||
|
InternalErrorType::Response(ref resp) => {
|
||||||
|
if let Some(resp) = resp.borrow_mut().take() {
|
||||||
|
resp.into()
|
||||||
|
} else {
|
||||||
|
Response::new(StatusCode::INTERNAL_SERVER_ERROR)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! error_helper {
|
||||||
|
($name:ident, $status:ident) => {
|
||||||
|
paste::paste! {
|
||||||
|
#[doc = "Helper function that wraps any error and generates a `" $status "` response."]
|
||||||
|
#[allow(non_snake_case)]
|
||||||
|
pub fn $name<T>(err: T) -> Error
|
||||||
|
where
|
||||||
|
T: fmt::Debug + fmt::Display + 'static,
|
||||||
|
{
|
||||||
|
InternalError::new(err, StatusCode::$status).into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
error_helper!(ErrorBadRequest, BAD_REQUEST);
|
||||||
|
error_helper!(ErrorUnauthorized, UNAUTHORIZED);
|
||||||
|
error_helper!(ErrorPaymentRequired, PAYMENT_REQUIRED);
|
||||||
|
error_helper!(ErrorForbidden, FORBIDDEN);
|
||||||
|
error_helper!(ErrorNotFound, NOT_FOUND);
|
||||||
|
error_helper!(ErrorMethodNotAllowed, METHOD_NOT_ALLOWED);
|
||||||
|
error_helper!(ErrorNotAcceptable, NOT_ACCEPTABLE);
|
||||||
|
error_helper!(
|
||||||
|
ErrorProxyAuthenticationRequired,
|
||||||
|
PROXY_AUTHENTICATION_REQUIRED
|
||||||
|
);
|
||||||
|
error_helper!(ErrorRequestTimeout, REQUEST_TIMEOUT);
|
||||||
|
error_helper!(ErrorConflict, CONFLICT);
|
||||||
|
error_helper!(ErrorGone, GONE);
|
||||||
|
error_helper!(ErrorLengthRequired, LENGTH_REQUIRED);
|
||||||
|
error_helper!(ErrorPayloadTooLarge, PAYLOAD_TOO_LARGE);
|
||||||
|
error_helper!(ErrorUriTooLong, URI_TOO_LONG);
|
||||||
|
error_helper!(ErrorUnsupportedMediaType, UNSUPPORTED_MEDIA_TYPE);
|
||||||
|
error_helper!(ErrorRangeNotSatisfiable, RANGE_NOT_SATISFIABLE);
|
||||||
|
error_helper!(ErrorImATeapot, IM_A_TEAPOT);
|
||||||
|
error_helper!(ErrorMisdirectedRequest, MISDIRECTED_REQUEST);
|
||||||
|
error_helper!(ErrorUnprocessableEntity, UNPROCESSABLE_ENTITY);
|
||||||
|
error_helper!(ErrorLocked, LOCKED);
|
||||||
|
error_helper!(ErrorFailedDependency, FAILED_DEPENDENCY);
|
||||||
|
error_helper!(ErrorUpgradeRequired, UPGRADE_REQUIRED);
|
||||||
|
error_helper!(ErrorPreconditionFailed, PRECONDITION_FAILED);
|
||||||
|
error_helper!(ErrorPreconditionRequired, PRECONDITION_REQUIRED);
|
||||||
|
error_helper!(ErrorTooManyRequests, TOO_MANY_REQUESTS);
|
||||||
|
error_helper!(
|
||||||
|
ErrorRequestHeaderFieldsTooLarge,
|
||||||
|
REQUEST_HEADER_FIELDS_TOO_LARGE
|
||||||
|
);
|
||||||
|
error_helper!(
|
||||||
|
ErrorUnavailableForLegalReasons,
|
||||||
|
UNAVAILABLE_FOR_LEGAL_REASONS
|
||||||
|
);
|
||||||
|
error_helper!(ErrorExpectationFailed, EXPECTATION_FAILED);
|
||||||
|
error_helper!(ErrorInternalServerError, INTERNAL_SERVER_ERROR);
|
||||||
|
error_helper!(ErrorNotImplemented, NOT_IMPLEMENTED);
|
||||||
|
error_helper!(ErrorBadGateway, BAD_GATEWAY);
|
||||||
|
error_helper!(ErrorServiceUnavailable, SERVICE_UNAVAILABLE);
|
||||||
|
error_helper!(ErrorGatewayTimeout, GATEWAY_TIMEOUT);
|
||||||
|
error_helper!(ErrorHttpVersionNotSupported, HTTP_VERSION_NOT_SUPPORTED);
|
||||||
|
error_helper!(ErrorVariantAlsoNegotiates, VARIANT_ALSO_NEGOTIATES);
|
||||||
|
error_helper!(ErrorInsufficientStorage, INSUFFICIENT_STORAGE);
|
||||||
|
error_helper!(ErrorLoopDetected, LOOP_DETECTED);
|
||||||
|
error_helper!(ErrorNotExtended, NOT_EXTENDED);
|
||||||
|
error_helper!(
|
||||||
|
ErrorNetworkAuthenticationRequired,
|
||||||
|
NETWORK_AUTHENTICATION_REQUIRED
|
||||||
|
);
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use actix_http::{error::ParseError, Response};
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_internal_error() {
|
||||||
|
let err = InternalError::from_response(ParseError::Method, HttpResponse::Ok().finish());
|
||||||
|
let resp: Response<Body> = err.error_response();
|
||||||
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_error_helpers() {
|
||||||
|
let res: Response<Body> = ErrorBadRequest("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorUnauthorized("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::UNAUTHORIZED);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorPaymentRequired("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::PAYMENT_REQUIRED);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorForbidden("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::FORBIDDEN);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorNotFound("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::NOT_FOUND);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorMethodNotAllowed("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::METHOD_NOT_ALLOWED);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorNotAcceptable("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::NOT_ACCEPTABLE);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorProxyAuthenticationRequired("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::PROXY_AUTHENTICATION_REQUIRED);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorRequestTimeout("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::REQUEST_TIMEOUT);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorConflict("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::CONFLICT);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorGone("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::GONE);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorLengthRequired("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::LENGTH_REQUIRED);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorPreconditionFailed("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::PRECONDITION_FAILED);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorPayloadTooLarge("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::PAYLOAD_TOO_LARGE);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorUriTooLong("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::URI_TOO_LONG);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorUnsupportedMediaType("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::UNSUPPORTED_MEDIA_TYPE);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorRangeNotSatisfiable("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::RANGE_NOT_SATISFIABLE);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorExpectationFailed("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::EXPECTATION_FAILED);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorImATeapot("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::IM_A_TEAPOT);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorMisdirectedRequest("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::MISDIRECTED_REQUEST);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorUnprocessableEntity("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::UNPROCESSABLE_ENTITY);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorLocked("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::LOCKED);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorFailedDependency("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::FAILED_DEPENDENCY);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorUpgradeRequired("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::UPGRADE_REQUIRED);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorPreconditionRequired("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::PRECONDITION_REQUIRED);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorTooManyRequests("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::TOO_MANY_REQUESTS);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorRequestHeaderFieldsTooLarge("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::REQUEST_HEADER_FIELDS_TOO_LARGE);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorUnavailableForLegalReasons("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorInternalServerError("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorNotImplemented("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::NOT_IMPLEMENTED);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorBadGateway("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::BAD_GATEWAY);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorServiceUnavailable("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::SERVICE_UNAVAILABLE);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorGatewayTimeout("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::GATEWAY_TIMEOUT);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorHttpVersionNotSupported("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::HTTP_VERSION_NOT_SUPPORTED);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorVariantAlsoNegotiates("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::VARIANT_ALSO_NEGOTIATES);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorInsufficientStorage("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::INSUFFICIENT_STORAGE);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorLoopDetected("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::LOOP_DETECTED);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorNotExtended("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::NOT_EXTENDED);
|
||||||
|
|
||||||
|
let res: Response<Body> = ErrorNetworkAuthenticationRequired("err").into();
|
||||||
|
assert_eq!(res.status(), StatusCode::NETWORK_AUTHENTICATION_REQUIRED);
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,6 +9,10 @@ use url::ParseError as UrlParseError;
|
||||||
|
|
||||||
use crate::http::StatusCode;
|
use crate::http::StatusCode;
|
||||||
|
|
||||||
|
mod internal;
|
||||||
|
|
||||||
|
pub use self::internal::*;
|
||||||
|
|
||||||
/// A convenience [`Result`](std::result::Result) for Actix Web operations.
|
/// A convenience [`Result`](std::result::Result) for Actix Web operations.
|
||||||
///
|
///
|
||||||
/// This type alias is generally used to avoid writing out `actix_http::Error` directly.
|
/// This type alias is generally used to avoid writing out `actix_http::Error` directly.
|
|
@ -1,9 +1,9 @@
|
||||||
use std::{any::type_name, ops::Deref};
|
use std::{any::type_name, ops::Deref};
|
||||||
|
|
||||||
use actix_http::error::{Error, ErrorInternalServerError};
|
use actix_http::error::Error;
|
||||||
use actix_utils::future::{err, ok, Ready};
|
use actix_utils::future::{err, ok, Ready};
|
||||||
|
|
||||||
use crate::{dev::Payload, FromRequest, HttpRequest};
|
use crate::{dev::Payload, error::ErrorInternalServerError, FromRequest, HttpRequest};
|
||||||
|
|
||||||
/// Request-local data extractor.
|
/// Request-local data extractor.
|
||||||
///
|
///
|
||||||
|
|
|
@ -2,12 +2,11 @@ use std::{borrow::Cow, fmt};
|
||||||
|
|
||||||
use actix_http::{
|
use actix_http::{
|
||||||
body::Body,
|
body::Body,
|
||||||
error::InternalError,
|
|
||||||
http::{header::IntoHeaderPair, Error as HttpError, HeaderMap, StatusCode},
|
http::{header::IntoHeaderPair, Error as HttpError, HeaderMap, StatusCode},
|
||||||
};
|
};
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
|
|
||||||
use crate::{Error, HttpRequest, HttpResponse, HttpResponseBuilder};
|
use crate::{error::InternalError, Error, HttpRequest, HttpResponse, HttpResponseBuilder};
|
||||||
|
|
||||||
/// Trait implemented by types that can be converted to an HTTP response.
|
/// Trait implemented by types that can be converted to an HTTP response.
|
||||||
///
|
///
|
||||||
|
|
|
@ -2,12 +2,16 @@
|
||||||
|
|
||||||
use std::{fmt, ops, sync::Arc};
|
use std::{fmt, ops, sync::Arc};
|
||||||
|
|
||||||
use actix_http::error::{Error, ErrorNotFound};
|
use actix_http::error::Error;
|
||||||
use actix_router::PathDeserializer;
|
use actix_router::PathDeserializer;
|
||||||
use actix_utils::future::{ready, Ready};
|
use actix_utils::future::{ready, Ready};
|
||||||
use serde::de;
|
use serde::de;
|
||||||
|
|
||||||
use crate::{dev::Payload, error::PathError, FromRequest, HttpRequest};
|
use crate::{
|
||||||
|
dev::Payload,
|
||||||
|
error::{ErrorNotFound, PathError},
|
||||||
|
FromRequest, HttpRequest,
|
||||||
|
};
|
||||||
|
|
||||||
/// Extract typed data from request path segments.
|
/// Extract typed data from request path segments.
|
||||||
///
|
///
|
||||||
|
|
|
@ -7,14 +7,17 @@ use std::{
|
||||||
task::{Context, Poll},
|
task::{Context, Poll},
|
||||||
};
|
};
|
||||||
|
|
||||||
use actix_http::error::{ErrorBadRequest, PayloadError};
|
use actix_http::error::PayloadError;
|
||||||
use actix_utils::future::{ready, Either, Ready};
|
use actix_utils::future::{ready, Either, Ready};
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use encoding_rs::{Encoding, UTF_8};
|
use encoding_rs::{Encoding, UTF_8};
|
||||||
use futures_core::{ready, stream::Stream};
|
use futures_core::{ready, stream::Stream};
|
||||||
use mime::Mime;
|
use mime::Mime;
|
||||||
|
|
||||||
use crate::{dev, http::header, web, Error, FromRequest, HttpMessage, HttpRequest};
|
use crate::{
|
||||||
|
dev, error::ErrorBadRequest, http::header, web, Error, FromRequest, HttpMessage,
|
||||||
|
HttpRequest,
|
||||||
|
};
|
||||||
|
|
||||||
/// Extract a request's raw payload stream.
|
/// Extract a request's raw payload stream.
|
||||||
///
|
///
|
||||||
|
|
|
@ -901,7 +901,7 @@ async fn test_normalize() {
|
||||||
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
||||||
App::new()
|
App::new()
|
||||||
.wrap(NormalizePath::new(TrailingSlash::Trim))
|
.wrap(NormalizePath::new(TrailingSlash::Trim))
|
||||||
.service(web::resource("/one").route(web::to(|| HttpResponse::Ok())))
|
.service(web::resource("/one").route(web::to(HttpResponse::Ok)))
|
||||||
});
|
});
|
||||||
|
|
||||||
let response = srv.get("/one/").send().await.unwrap();
|
let response = srv.get("/one/").send().await.unwrap();
|
||||||
|
|
Loading…
Reference in New Issue