change error bound to into response

This commit is contained in:
Rob Ede 2021-05-17 13:09:18 +01:00
parent ba9950d81c
commit 0448bbf8ab
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
12 changed files with 258 additions and 206 deletions

View File

@ -5,15 +5,14 @@ use std::{fmt, net};
use actix_codec::Framed;
use actix_service::{IntoServiceFactory, Service, ServiceFactory};
use crate::body::MessageBody;
use crate::config::{KeepAlive, ServiceConfig};
use crate::error::Error;
use crate::h1::{Codec, ExpectHandler, H1Service, UpgradeHandler};
use crate::h2::H2Service;
use crate::request::Request;
use crate::response::Response;
use crate::service::HttpService;
use crate::{ConnectCallback, Extensions};
use crate::{
body::{AnyBody, MessageBody},
config::{KeepAlive, ServiceConfig},
h1::{self, ExpectHandler, H1Service, UpgradeHandler},
h2::H2Service,
service::HttpService,
ConnectCallback, Extensions, Request, Response,
};
/// A HTTP service builder
///
@ -34,7 +33,7 @@ pub struct HttpServiceBuilder<T, S, X = ExpectHandler, U = UpgradeHandler> {
impl<T, S> HttpServiceBuilder<T, S, ExpectHandler, UpgradeHandler>
where
S: ServiceFactory<Request, Config = ()>,
S::Error: Into<Error> + 'static,
S::Error: Into<Response<AnyBody>> + 'static,
S::InitError: fmt::Debug,
<S::Service as Service<Request>>::Future: 'static,
{
@ -57,13 +56,13 @@ where
impl<T, S, X, U> HttpServiceBuilder<T, S, X, U>
where
S: ServiceFactory<Request, Config = ()>,
S::Error: Into<Error> + 'static,
S::Error: Into<Response<AnyBody>> + 'static,
S::InitError: fmt::Debug,
<S::Service as Service<Request>>::Future: 'static,
X: ServiceFactory<Request, Config = (), Response = Request>,
X::Error: Into<Error>,
X::Error: Into<Response<AnyBody>>,
X::InitError: fmt::Debug,
U: ServiceFactory<(Request, Framed<T, Codec>), Config = (), Response = ()>,
U: ServiceFactory<(Request, Framed<T, h1::Codec>), Config = (), Response = ()>,
U::Error: fmt::Display,
U::InitError: fmt::Debug,
{
@ -123,7 +122,7 @@ where
where
F: IntoServiceFactory<X1, Request>,
X1: ServiceFactory<Request, Config = (), Response = Request>,
X1::Error: Into<Error>,
X1::Error: Into<Response<AnyBody>>,
X1::InitError: fmt::Debug,
{
HttpServiceBuilder {
@ -145,8 +144,8 @@ where
/// and this service get called with original request and framed object.
pub fn upgrade<F, U1>(self, upgrade: F) -> HttpServiceBuilder<T, S, X, U1>
where
F: IntoServiceFactory<U1, (Request, Framed<T, Codec>)>,
U1: ServiceFactory<(Request, Framed<T, Codec>), Config = (), Response = ()>,
F: IntoServiceFactory<U1, (Request, Framed<T, h1::Codec>)>,
U1: ServiceFactory<(Request, Framed<T, h1::Codec>), Config = (), Response = ()>,
U1::Error: fmt::Display,
U1::InitError: fmt::Debug,
{
@ -181,7 +180,7 @@ where
where
B: MessageBody,
F: IntoServiceFactory<S, Request>,
S::Error: Into<Error>,
S::Error: Into<Response<AnyBody>>,
S::InitError: fmt::Debug,
S::Response: Into<Response<B>>,
{
@ -203,12 +202,12 @@ where
pub fn h2<F, B>(self, service: F) -> H2Service<T, S, B>
where
F: IntoServiceFactory<S, Request>,
S::Error: Into<Error> + 'static,
S::Error: Into<Response<AnyBody>> + 'static,
S::InitError: fmt::Debug,
S::Response: Into<Response<B>> + 'static,
B: MessageBody + 'static,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
{
let cfg = ServiceConfig::new(
self.keep_alive,
@ -226,12 +225,12 @@ where
pub fn finish<F, B>(self, service: F) -> HttpService<T, S, B, X, U>
where
F: IntoServiceFactory<S, Request>,
S::Error: Into<Error> + 'static,
S::Error: Into<Response<AnyBody>> + 'static,
S::InitError: fmt::Debug,
S::Response: Into<Response<B>> + 'static,
B: MessageBody + 'static,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
{
let cfg = ServiceConfig::new(
self.keep_alive,

View File

@ -13,7 +13,11 @@ use derive_more::{Display, Error, From};
use http::{header, uri::InvalidUri, StatusCode};
use serde::de::value::Error as DeError;
use crate::{body::Body, helpers::Writer, Response};
use crate::{
body::{AnyBody, Body},
helpers::Writer,
Response,
};
pub use http::Error as HttpError;
@ -99,9 +103,8 @@ impl From<()> for Error {
}
impl From<std::convert::Infallible> for Error {
fn from(_: std::convert::Infallible) -> Self {
// hint that an error that will never happen
unreachable!()
fn from(val: std::convert::Infallible) -> Self {
match val {}
}
}
@ -123,7 +126,7 @@ impl<T: ResponseError + 'static> From<T> for Error {
#[derive(Debug, Display, Error)]
#[display(fmt = "Unknown Error")]
struct UnitError;
pub(crate) struct UnitError;
impl ResponseError for Box<dyn StdError + 'static> {}
@ -363,13 +366,14 @@ impl ResponseError for PayloadError {
#[non_exhaustive]
pub enum DispatchError {
/// Service error
Service(Error),
// FIXME: display and error type
#[display(fmt = "Service Error")]
Service(#[error(not(source))] Response<AnyBody>),
/// Upgrade service error
Upgrade,
/// An `io::Error` that occurred while trying to read or write to a network
/// stream.
/// An `io::Error` that occurred while trying to read or write to a network stream.
#[display(fmt = "IO error: {}", _0)]
Io(io::Error),

View File

@ -17,19 +17,19 @@ use futures_core::ready;
use log::{error, trace};
use pin_project::pin_project;
use crate::body::{Body, BodySize, MessageBody};
use crate::config::ServiceConfig;
use crate::error::{DispatchError, Error};
use crate::error::{ParseError, PayloadError};
use crate::http::StatusCode;
use crate::request::Request;
use crate::response::Response;
use crate::service::HttpFlow;
use crate::OnConnectData;
use crate::{
body::{AnyBody, BodySize, MessageBody},
config::ServiceConfig,
error::{DispatchError, ParseError, PayloadError},
service::HttpFlow,
OnConnectData, Request, Response, StatusCode,
};
use super::codec::Codec;
use super::payload::{Payload, PayloadSender, PayloadStatus};
use super::{Message, MessageType};
use super::{
codec::Codec,
payload::{Payload, PayloadSender, PayloadStatus},
Message, MessageType,
};
const LW_BUFFER_SIZE: usize = 1024;
const HW_BUFFER_SIZE: usize = 1024 * 8;
@ -50,13 +50,13 @@ bitflags! {
pub struct Dispatcher<T, S, B, X, U>
where
S: Service<Request>,
S::Error: Into<Error>,
S::Error: Into<Response<AnyBody>>,
B: MessageBody,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
X: Service<Request, Response = Request>,
X::Error: Into<Error>,
X::Error: Into<Response<AnyBody>>,
U: Service<(Request, Framed<T, Codec>), Response = ()>,
U::Error: fmt::Display,
@ -72,13 +72,13 @@ where
enum DispatcherState<T, S, B, X, U>
where
S: Service<Request>,
S::Error: Into<Error>,
S::Error: Into<Response<AnyBody>>,
B: MessageBody,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
X: Service<Request, Response = Request>,
X::Error: Into<Error>,
X::Error: Into<Response<AnyBody>>,
U: Service<(Request, Framed<T, Codec>), Response = ()>,
U::Error: fmt::Display,
@ -91,13 +91,13 @@ where
struct InnerDispatcher<T, S, B, X, U>
where
S: Service<Request>,
S::Error: Into<Error>,
S::Error: Into<Response<AnyBody>>,
B: MessageBody,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
X: Service<Request, Response = Request>,
X::Error: Into<Error>,
X::Error: Into<Response<AnyBody>>,
U: Service<(Request, Framed<T, Codec>), Response = ()>,
U::Error: fmt::Display,
@ -136,13 +136,13 @@ where
X: Service<Request, Response = Request>,
B: MessageBody,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
{
None,
ExpectCall(#[pin] X::Future),
ServiceCall(#[pin] S::Future),
SendPayload(#[pin] B),
SendErrorPayload(#[pin] Body),
SendErrorPayload(#[pin] AnyBody),
}
impl<S, B, X> State<S, B, X>
@ -152,7 +152,7 @@ where
X: Service<Request, Response = Request>,
B: MessageBody,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
{
fn is_empty(&self) -> bool {
matches!(self, State::None)
@ -170,14 +170,14 @@ where
T: AsyncRead + AsyncWrite + Unpin,
S: Service<Request>,
S::Error: Into<Error>,
S::Error: Into<Response<AnyBody>>,
S::Response: Into<Response<B>>,
B: MessageBody,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
X: Service<Request, Response = Request>,
X::Error: Into<Error>,
X::Error: Into<Response<AnyBody>>,
U: Service<(Request, Framed<T, Codec>), Response = ()>,
U::Error: fmt::Display,
@ -231,14 +231,14 @@ where
T: AsyncRead + AsyncWrite + Unpin,
S: Service<Request>,
S::Error: Into<Error>,
S::Error: Into<Response<AnyBody>>,
S::Response: Into<Response<B>>,
B: MessageBody,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
X: Service<Request, Response = Request>,
X::Error: Into<Error>,
X::Error: Into<Response<AnyBody>>,
U: Service<(Request, Framed<T, Codec>), Response = ()>,
U::Error: fmt::Display,
@ -334,7 +334,7 @@ where
fn send_error_response(
mut self: Pin<&mut Self>,
message: Response<()>,
body: Body,
body: AnyBody,
) -> Result<(), DispatchError> {
let size = self.as_mut().send_response_inner(message, &body)?;
let state = match size {
@ -379,7 +379,7 @@ where
// send_response would update InnerDispatcher state to SendPayload or
// None(If response body is empty).
// continue loop to poll it.
self.as_mut().send_error_response(res, Body::Empty)?;
self.as_mut().send_error_response(res, AnyBody::Empty)?;
}
// return with upgrade request and poll it exclusively.
@ -399,7 +399,7 @@ where
// send service call error as response
Poll::Ready(Err(err)) => {
let res = Response::from_error(err.into());
let res: Response<AnyBody> = err.into();
let (res, body) = res.replace_body(());
self.as_mut().send_error_response(res, body)?;
}
@ -473,7 +473,7 @@ where
}
Poll::Ready(Some(Err(err))) => {
return Err(DispatchError::Service(err))
return Err(DispatchError::Service(err.into()))
}
Poll::Pending => return Ok(PollResponse::DoNothing),
@ -496,7 +496,7 @@ where
// send expect error as response
Poll::Ready(Err(err)) => {
let res = Response::from_error(err.into());
let res: Response<AnyBody> = err.into();
let (res, body) = res.replace_body(());
self.as_mut().send_error_response(res, body)?;
}
@ -546,7 +546,7 @@ where
// to notify the dispatcher a new state is set and the outer loop
// should be continue.
Poll::Ready(Err(err)) => {
let res = Response::from_error(err.into());
let res: Response<AnyBody> = err.into();
let (res, body) = res.replace_body(());
return self.send_error_response(res, body);
}
@ -566,7 +566,7 @@ where
Poll::Pending => Ok(()),
// see the comment on ExpectCall state branch's Ready(Err(err)).
Poll::Ready(Err(err)) => {
let res = Response::from_error(err.into());
let res: Response<AnyBody> = err.into();
let (res, body) = res.replace_body(());
self.send_error_response(res, body)
}
@ -772,7 +772,7 @@ where
trace!("Slow request timeout");
let _ = self.as_mut().send_error_response(
Response::with_body(StatusCode::REQUEST_TIMEOUT, ()),
Body::Empty,
AnyBody::Empty,
);
this = self.project();
this.flags.insert(Flags::STARTED | Flags::SHUTDOWN);
@ -909,14 +909,14 @@ where
T: AsyncRead + AsyncWrite + Unpin,
S: Service<Request>,
S::Error: Into<Error>,
S::Error: Into<Response<AnyBody>>,
S::Response: Into<Response<B>>,
B: MessageBody,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
X: Service<Request, Response = Request>,
X::Error: Into<Error>,
X::Error: Into<Response<AnyBody>>,
U: Service<(Request, Framed<T, Codec>), Response = ()>,
U::Error: fmt::Display,
@ -1067,16 +1067,17 @@ mod tests {
}
}
fn ok_service() -> impl Service<Request, Response = Response<Body>, Error = Error> {
fn ok_service() -> impl Service<Request, Response = Response<AnyBody>, Error = Error>
{
fn_service(|_req: Request| ready(Ok::<_, Error>(Response::ok())))
}
fn echo_path_service(
) -> impl Service<Request, Response = Response<Body>, Error = Error> {
) -> impl Service<Request, Response = Response<AnyBody>, Error = Error> {
fn_service(|req: Request| {
let path = req.path().as_bytes();
ready(Ok::<_, Error>(
Response::ok().set_body(Body::from_slice(path)),
Response::ok().set_body(AnyBody::from_slice(path)),
))
})
}

View File

@ -11,13 +11,13 @@ use actix_service::{
use actix_utils::future::ready;
use futures_core::future::LocalBoxFuture;
use crate::body::MessageBody;
use crate::config::ServiceConfig;
use crate::error::{DispatchError, Error};
use crate::request::Request;
use crate::response::Response;
use crate::service::HttpServiceHandler;
use crate::{ConnectCallback, OnConnectData};
use crate::{
body::{AnyBody, MessageBody},
config::ServiceConfig,
error::DispatchError,
service::HttpServiceHandler,
ConnectCallback, OnConnectData, Request, Response,
};
use super::codec::Codec;
use super::dispatcher::Dispatcher;
@ -36,7 +36,7 @@ pub struct H1Service<T, S, B, X = ExpectHandler, U = UpgradeHandler> {
impl<T, S, B> H1Service<T, S, B>
where
S: ServiceFactory<Request, Config = ()>,
S::Error: Into<Error>,
S::Error: Into<Response<AnyBody>>,
S::InitError: fmt::Debug,
S::Response: Into<Response<B>>,
B: MessageBody,
@ -61,21 +61,21 @@ impl<S, B, X, U> H1Service<TcpStream, S, B, X, U>
where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
S::Error: Into<Error>,
S::Error: Into<Response<AnyBody>>,
S::InitError: fmt::Debug,
S::Response: Into<Response<B>>,
B: MessageBody,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
X: ServiceFactory<Request, Config = (), Response = Request>,
X::Future: 'static,
X::Error: Into<Error>,
X::Error: Into<Response<AnyBody>>,
X::InitError: fmt::Debug,
U: ServiceFactory<(Request, Framed<TcpStream, Codec>), Config = (), Response = ()>,
U::Future: 'static,
U::Error: fmt::Display + Into<Error>,
U::Error: fmt::Display + Into<Response<AnyBody>>,
U::InitError: fmt::Debug,
{
/// Create simple tcp stream service
@ -110,16 +110,16 @@ mod openssl {
where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
S::Error: Into<Error>,
S::Error: Into<Response<AnyBody>>,
S::InitError: fmt::Debug,
S::Response: Into<Response<B>>,
B: MessageBody,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
X: ServiceFactory<Request, Config = (), Response = Request>,
X::Future: 'static,
X::Error: Into<Error>,
X::Error: Into<Response<AnyBody>>,
X::InitError: fmt::Debug,
U: ServiceFactory<
@ -128,7 +128,7 @@ mod openssl {
Response = (),
>,
U::Future: 'static,
U::Error: fmt::Display + Into<Error>,
U::Error: fmt::Display + Into<Response<AnyBody>>,
U::InitError: fmt::Debug,
{
/// Create openssl based service
@ -170,16 +170,16 @@ mod rustls {
where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
S::Error: Into<Error>,
S::Error: Into<Response<AnyBody>>,
S::InitError: fmt::Debug,
S::Response: Into<Response<B>>,
B: MessageBody,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
X: ServiceFactory<Request, Config = (), Response = Request>,
X::Future: 'static,
X::Error: Into<Error>,
X::Error: Into<Response<AnyBody>>,
X::InitError: fmt::Debug,
U: ServiceFactory<
@ -188,7 +188,7 @@ mod rustls {
Response = (),
>,
U::Future: 'static,
U::Error: fmt::Display + Into<Error>,
U::Error: fmt::Display + Into<Response<AnyBody>>,
U::InitError: fmt::Debug,
{
/// Create rustls based service
@ -217,7 +217,7 @@ mod rustls {
impl<T, S, B, X, U> H1Service<T, S, B, X, U>
where
S: ServiceFactory<Request, Config = ()>,
S::Error: Into<Error>,
S::Error: Into<Response<AnyBody>>,
S::Response: Into<Response<B>>,
S::InitError: fmt::Debug,
B: MessageBody,
@ -225,7 +225,7 @@ where
pub fn expect<X1>(self, expect: X1) -> H1Service<T, S, B, X1, U>
where
X1: ServiceFactory<Request, Response = Request>,
X1::Error: Into<Error>,
X1::Error: Into<Response<AnyBody>>,
X1::InitError: fmt::Debug,
{
H1Service {
@ -268,21 +268,21 @@ where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
S::Error: Into<Error>,
S::Error: Into<Response<AnyBody>>,
S::Response: Into<Response<B>>,
S::InitError: fmt::Debug,
B: MessageBody,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
X: ServiceFactory<Request, Config = (), Response = Request>,
X::Future: 'static,
X::Error: Into<Error>,
X::Error: Into<Response<AnyBody>>,
X::InitError: fmt::Debug,
U: ServiceFactory<(Request, Framed<T, Codec>), Config = (), Response = ()>,
U::Future: 'static,
U::Error: fmt::Display + Into<Error>,
U::Error: fmt::Display + Into<Response<AnyBody>>,
U::InitError: fmt::Debug,
{
type Response = ();
@ -338,17 +338,17 @@ where
T: AsyncRead + AsyncWrite + Unpin,
S: Service<Request>,
S::Error: Into<Error>,
S::Error: Into<Response<AnyBody>>,
S::Response: Into<Response<B>>,
B: MessageBody,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
X: Service<Request, Response = Request>,
X::Error: Into<Error>,
X::Error: Into<Response<AnyBody>>,
U: Service<(Request, Framed<T, Codec>), Response = ()>,
U::Error: fmt::Display + Into<Error>,
U::Error: fmt::Display + Into<Response<AnyBody>>,
{
type Response = ();
type Error = DispatchError;

View File

@ -12,15 +12,13 @@ use h2::{
use http::header::{HeaderValue, CONNECTION, CONTENT_LENGTH, DATE, TRANSFER_ENCODING};
use log::{error, trace};
use crate::body::{Body, BodySize, MessageBody};
use crate::config::ServiceConfig;
use crate::error::{DispatchError, Error};
use crate::message::ResponseHead;
use crate::payload::Payload;
use crate::request::Request;
use crate::response::Response;
use crate::service::HttpFlow;
use crate::OnConnectData;
use crate::{
body::{AnyBody, Body, BodySize, MessageBody},
config::ServiceConfig,
error::DispatchError,
service::HttpFlow,
OnConnectData, Payload, Request, Response, ResponseHead,
};
const CHUNK_SIZE: usize = 16_384;
@ -44,7 +42,7 @@ 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::Error: Into<Response<AnyBody>>,
S::Response: Into<Response<B>>,
B: MessageBody,
{
@ -71,12 +69,12 @@ where
T: AsyncRead + AsyncWrite + Unpin,
S: Service<Request>,
S::Error: Into<Error> + 'static,
S::Error: Into<Response<AnyBody>> + 'static,
S::Future: 'static,
S::Response: Into<Response<B>> + 'static,
B: MessageBody + 'static,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
{
type Output = Result<(), DispatchError>;
@ -142,11 +140,11 @@ enum ServiceResponseState<F, B> {
impl<F, I, E, B> ServiceResponse<F, I, E, B>
where
F: Future<Output = Result<I, E>>,
E: Into<Error>,
E: Into<Response<AnyBody>>,
I: Into<Response<B>>,
B: MessageBody,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
{
fn prepare_response(
&self,
@ -220,11 +218,11 @@ where
impl<F, I, E, B> Future for ServiceResponse<F, I, E, B>
where
F: Future<Output = Result<I, E>>,
E: Into<Error>,
E: Into<Response<AnyBody>>,
I: Into<Response<B>>,
B: MessageBody,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
{
type Output = ();
@ -261,7 +259,7 @@ where
}
Err(err) => {
let res = Response::from_error(err.into());
let res: Response<AnyBody> = err.into();
let (res, body) = res.replace_body(());
let mut send = send.take().unwrap();

View File

@ -13,16 +13,16 @@ use actix_service::{
use actix_utils::future::ready;
use bytes::Bytes;
use futures_core::{future::LocalBoxFuture, ready};
use h2::server::{handshake, Handshake};
use h2::server::{handshake as h2_handshake, Handshake as H2Handshake};
use log::error;
use crate::body::MessageBody;
use crate::config::ServiceConfig;
use crate::error::{DispatchError, Error};
use crate::request::Request;
use crate::response::Response;
use crate::service::HttpFlow;
use crate::{ConnectCallback, OnConnectData};
use crate::{
body::{AnyBody, MessageBody},
config::ServiceConfig,
error::DispatchError,
service::HttpFlow,
ConnectCallback, OnConnectData, Request, Response,
};
use super::dispatcher::Dispatcher;
@ -37,12 +37,12 @@ pub struct H2Service<T, S, B> {
impl<T, S, B> H2Service<T, S, B>
where
S: ServiceFactory<Request, Config = ()>,
S::Error: Into<Error> + 'static,
S::Error: Into<Response<AnyBody>> + 'static,
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
B: MessageBody + 'static,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
{
/// Create new `H2Service` instance with config.
pub(crate) fn with_config<F: IntoServiceFactory<S, Request>>(
@ -68,12 +68,12 @@ impl<S, B> H2Service<TcpStream, S, B>
where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
S::Error: Into<Error> + 'static,
S::Error: Into<Response<AnyBody>> + 'static,
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
B: MessageBody + 'static,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
{
/// Create plain TCP based service
pub fn tcp(
@ -107,12 +107,12 @@ mod openssl {
where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
S::Error: Into<Error> + 'static,
S::Error: Into<Response<AnyBody>> + 'static,
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
B: MessageBody + 'static,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
{
/// Create OpenSSL based service
pub fn openssl(
@ -153,12 +153,12 @@ mod rustls {
where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
S::Error: Into<Error> + 'static,
S::Error: Into<Response<AnyBody>> + 'static,
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
B: MessageBody + 'static,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
{
/// Create Rustls based service
pub fn rustls(
@ -196,12 +196,12 @@ where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
S::Error: Into<Error> + 'static,
S::Error: Into<Response<AnyBody>> + 'static,
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
B: MessageBody + 'static,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
{
type Response = ();
type Error = DispatchError;
@ -236,7 +236,7 @@ where
impl<T, S, B> H2ServiceHandler<T, S, B>
where
S: Service<Request>,
S::Error: Into<Error> + 'static,
S::Error: Into<Response<AnyBody>> + 'static,
S::Future: 'static,
S::Response: Into<Response<B>> + 'static,
B: MessageBody + 'static,
@ -259,11 +259,11 @@ impl<T, S, B> Service<(T, Option<net::SocketAddr>)> for H2ServiceHandler<T, S, B
where
T: AsyncRead + AsyncWrite + Unpin,
S: Service<Request>,
S::Error: Into<Error> + 'static,
S::Error: Into<Response<AnyBody>> + 'static,
S::Future: 'static,
S::Response: Into<Response<B>> + 'static,
B: MessageBody + 'static,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
{
type Response = ();
type Error = DispatchError;
@ -287,7 +287,7 @@ where
Some(self.cfg.clone()),
addr,
on_connect_data,
handshake(io),
h2_handshake(io),
),
}
}
@ -304,7 +304,7 @@ where
Option<ServiceConfig>,
Option<net::SocketAddr>,
OnConnectData,
Handshake<T, Bytes>,
H2Handshake<T, Bytes>,
),
}
@ -312,7 +312,7 @@ pub struct H2ServiceHandlerResponse<T, S, B>
where
T: AsyncRead + AsyncWrite + Unpin,
S: Service<Request>,
S::Error: Into<Error> + 'static,
S::Error: Into<Response<AnyBody>> + 'static,
S::Future: 'static,
S::Response: Into<Response<B>> + 'static,
B: MessageBody + 'static,
@ -324,11 +324,11 @@ impl<T, S, B> Future for H2ServiceHandlerResponse<T, S, B>
where
T: AsyncRead + AsyncWrite + Unpin,
S: Service<Request>,
S::Error: Into<Error> + 'static,
S::Error: Into<Response<AnyBody>> + 'static,
S::Future: 'static,
S::Response: Into<Response<B>> + 'static,
B: MessageBody,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
{
type Output = Result<(), DispatchError>;

View File

@ -249,6 +249,20 @@ impl From<ResponseBuilder> for Response<Body> {
}
}
impl From<()> for Response<Body> {
fn from(_: ()) -> Self {
Error::from(crate::error::UnitError)
.as_response_error()
.error_response()
}
}
impl From<std::convert::Infallible> for Response<Body> {
fn from(val: std::convert::Infallible) -> Self {
match val {}
}
}
impl From<&'static str> for Response<Body> {
fn from(val: &'static str) -> Self {
Response::build(StatusCode::OK)

View File

@ -8,6 +8,7 @@ use std::{
task::{Context, Poll},
};
use ::h2::server::{handshake as h2_handshake, Handshake as H2Handshake};
use actix_codec::{AsyncRead, AsyncWrite, Framed};
use actix_rt::net::TcpStream;
use actix_service::{
@ -15,16 +16,15 @@ use actix_service::{
};
use bytes::Bytes;
use futures_core::{future::LocalBoxFuture, ready};
use h2::server::{handshake, Handshake};
use pin_project::pin_project;
use crate::body::MessageBody;
use crate::builder::HttpServiceBuilder;
use crate::config::{KeepAlive, ServiceConfig};
use crate::error::{DispatchError, Error};
use crate::request::Request;
use crate::response::Response;
use crate::{h1, h2::Dispatcher, ConnectCallback, OnConnectData, Protocol};
use crate::{
body::{AnyBody, MessageBody},
builder::HttpServiceBuilder,
config::{KeepAlive, ServiceConfig},
error::DispatchError,
h1, h2, ConnectCallback, OnConnectData, Protocol, Request, Response,
};
/// A `ServiceFactory` for HTTP/1.1 or HTTP/2 protocol.
pub struct HttpService<T, S, B, X = h1::ExpectHandler, U = h1::UpgradeHandler> {
@ -39,7 +39,7 @@ pub struct HttpService<T, S, B, X = h1::ExpectHandler, U = h1::UpgradeHandler> {
impl<T, S, B> HttpService<T, S, B>
where
S: ServiceFactory<Request, Config = ()>,
S::Error: Into<Error> + 'static,
S::Error: Into<Response<AnyBody>> + 'static,
S::InitError: fmt::Debug,
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
@ -54,12 +54,12 @@ where
impl<T, S, B> HttpService<T, S, B>
where
S: ServiceFactory<Request, Config = ()>,
S::Error: Into<Error> + 'static,
S::Error: Into<Response<AnyBody>> + 'static,
S::InitError: fmt::Debug,
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
B: MessageBody + 'static,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
{
/// Create new `HttpService` instance.
pub fn new<F: IntoServiceFactory<S, Request>>(service: F) -> Self {
@ -94,7 +94,7 @@ where
impl<T, S, B, X, U> HttpService<T, S, B, X, U>
where
S: ServiceFactory<Request, Config = ()>,
S::Error: Into<Error> + 'static,
S::Error: Into<Response<AnyBody>> + 'static,
S::InitError: fmt::Debug,
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
@ -108,7 +108,7 @@ where
pub fn expect<X1>(self, expect: X1) -> HttpService<T, S, B, X1, U>
where
X1: ServiceFactory<Request, Config = (), Response = Request>,
X1::Error: Into<Error>,
X1::Error: Into<Response<AnyBody>>,
X1::InitError: fmt::Debug,
{
HttpService {
@ -152,17 +152,17 @@ impl<S, B, X, U> HttpService<TcpStream, S, B, X, U>
where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
S::Error: Into<Error> + 'static,
S::Error: Into<Response<AnyBody>> + 'static,
S::InitError: fmt::Debug,
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
B: MessageBody + 'static,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
X: ServiceFactory<Request, Config = (), Response = Request>,
X::Future: 'static,
X::Error: Into<Error>,
X::Error: Into<Response<AnyBody>>,
X::InitError: fmt::Debug,
U: ServiceFactory<
@ -171,7 +171,7 @@ where
Response = (),
>,
U::Future: 'static,
U::Error: fmt::Display + Into<Error>,
U::Error: fmt::Display + Into<Response<AnyBody>>,
U::InitError: fmt::Debug,
{
/// Create simple tcp stream service
@ -204,17 +204,17 @@ mod openssl {
where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
S::Error: Into<Error> + 'static,
S::Error: Into<Response<AnyBody>> + 'static,
S::InitError: fmt::Debug,
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
B: MessageBody + 'static,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
X: ServiceFactory<Request, Config = (), Response = Request>,
X::Future: 'static,
X::Error: Into<Error>,
X::Error: Into<Response<AnyBody>>,
X::InitError: fmt::Debug,
U: ServiceFactory<
@ -223,7 +223,7 @@ mod openssl {
Response = (),
>,
U::Future: 'static,
U::Error: fmt::Display + Into<Error>,
U::Error: fmt::Display + Into<Response<AnyBody>>,
U::InitError: fmt::Debug,
{
/// Create openssl based service
@ -272,17 +272,17 @@ mod rustls {
where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
S::Error: Into<Error> + 'static,
S::Error: Into<Response<AnyBody>> + 'static,
S::InitError: fmt::Debug,
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
B: MessageBody + 'static,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
X: ServiceFactory<Request, Config = (), Response = Request>,
X::Future: 'static,
X::Error: Into<Error>,
X::Error: Into<Response<AnyBody>>,
X::InitError: fmt::Debug,
U: ServiceFactory<
@ -291,7 +291,7 @@ mod rustls {
Response = (),
>,
U::Future: 'static,
U::Error: fmt::Display + Into<Error>,
U::Error: fmt::Display + Into<Response<AnyBody>>,
U::InitError: fmt::Debug,
{
/// Create rustls based service
@ -337,22 +337,22 @@ where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
S::Error: Into<Error> + 'static,
S::Error: Into<Response<AnyBody>> + 'static,
S::InitError: fmt::Debug,
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
B: MessageBody + 'static,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
X: ServiceFactory<Request, Config = (), Response = Request>,
X::Future: 'static,
X::Error: Into<Error>,
X::Error: Into<Response<AnyBody>>,
X::InitError: fmt::Debug,
U: ServiceFactory<(Request, Framed<T, h1::Codec>), Config = (), Response = ()>,
U::Future: 'static,
U::Error: fmt::Display + Into<Error>,
U::Error: fmt::Display + Into<Response<AnyBody>>,
U::InitError: fmt::Debug,
{
type Response = ();
@ -415,11 +415,11 @@ where
impl<T, S, B, X, U> HttpServiceHandler<T, S, B, X, U>
where
S: Service<Request>,
S::Error: Into<Error>,
S::Error: Into<Response<AnyBody>>,
X: Service<Request>,
X::Error: Into<Error>,
X::Error: Into<Response<AnyBody>>,
U: Service<(Request, Framed<T, h1::Codec>)>,
U::Error: Into<Error>,
U::Error: Into<Response<AnyBody>>,
{
pub(super) fn new(
cfg: ServiceConfig,
@ -436,7 +436,10 @@ where
}
}
pub(super) fn _poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Error>> {
pub(super) fn _poll_ready(
&self,
cx: &mut Context<'_>,
) -> Poll<Result<(), Response<AnyBody>>> {
ready!(self.flow.expect.poll_ready(cx).map_err(Into::into))?;
ready!(self.flow.service.poll_ready(cx).map_err(Into::into))?;
@ -472,18 +475,18 @@ where
T: AsyncRead + AsyncWrite + Unpin,
S: Service<Request>,
S::Error: Into<Error> + 'static,
S::Error: Into<Response<AnyBody>> + 'static,
S::Future: 'static,
S::Response: Into<Response<B>> + 'static,
B: MessageBody + 'static,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
X: Service<Request, Response = Request>,
X::Error: Into<Error>,
X::Error: Into<Response<AnyBody>>,
U: Service<(Request, Framed<T, h1::Codec>), Response = ()>,
U::Error: fmt::Display + Into<Error>,
U::Error: fmt::Display + Into<Response<AnyBody>>,
{
type Response = ();
type Error = DispatchError;
@ -506,7 +509,7 @@ where
match proto {
Protocol::Http2 => HttpServiceHandlerResponse {
state: State::H2Handshake(Some((
handshake(io),
h2_handshake(io),
self.cfg.clone(),
self.flow.clone(),
on_connect_data,
@ -536,22 +539,22 @@ where
S: Service<Request>,
S::Future: 'static,
S::Error: Into<Error>,
S::Error: Into<Response<AnyBody>>,
B: MessageBody,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
X: Service<Request, Response = Request>,
X::Error: Into<Error>,
X::Error: Into<Response<AnyBody>>,
U: Service<(Request, Framed<T, h1::Codec>), Response = ()>,
U::Error: fmt::Display,
{
H1(#[pin] h1::Dispatcher<T, S, B, X, U>),
H2(#[pin] Dispatcher<T, S, B, X, U>),
H2(#[pin] h2::Dispatcher<T, S, B, X, U>),
H2Handshake(
Option<(
Handshake<T, Bytes>,
H2Handshake<T, Bytes>,
ServiceConfig,
Rc<HttpFlow<S, X, U>>,
OnConnectData,
@ -566,15 +569,15 @@ where
T: AsyncRead + AsyncWrite + Unpin,
S: Service<Request>,
S::Error: Into<Error> + 'static,
S::Error: Into<Response<AnyBody>> + 'static,
S::Future: 'static,
S::Response: Into<Response<B>> + 'static,
B: MessageBody,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
X: Service<Request, Response = Request>,
X::Error: Into<Error>,
X::Error: Into<Response<AnyBody>>,
U: Service<(Request, Framed<T, h1::Codec>), Response = ()>,
U::Error: fmt::Display,
@ -588,15 +591,15 @@ where
T: AsyncRead + AsyncWrite + Unpin,
S: Service<Request>,
S::Error: Into<Error> + 'static,
S::Error: Into<Response<AnyBody>> + 'static,
S::Future: 'static,
S::Response: Into<Response<B>> + 'static,
B: MessageBody + 'static,
B::Error: Into<Error>,
B::Error: Into<Response<AnyBody>>,
X: Service<Request, Response = Request>,
X::Error: Into<Error>,
X::Error: Into<Response<AnyBody>>,
U: Service<(Request, Framed<T, h1::Codec>), Response = ()>,
U::Error: fmt::Display,
@ -612,13 +615,15 @@ where
Ok(conn) => {
let (_, cfg, srv, on_connect_data, peer_addr) =
data.take().unwrap();
self.as_mut().project().state.set(State::H2(Dispatcher::new(
srv,
conn,
on_connect_data,
cfg,
peer_addr,
)));
self.as_mut().project().state.set(State::H2(
h2::Dispatcher::new(
srv,
conn,
on_connect_data,
cfg,
peer_addr,
),
));
self.poll(cx)
}
Err(err) => {

View File

@ -1,5 +1,6 @@
use actix_http::{
http, http::StatusCode, HttpMessage, HttpService, Request, Response, ResponseError,
body::AnyBody, http, http::StatusCode, HttpMessage, HttpService, Request, Response,
ResponseError,
};
use actix_http_test::test_server;
use actix_service::ServiceFactoryExt;
@ -103,6 +104,12 @@ impl ResponseError for ExpectFailed {
}
}
impl From<ExpectFailed> for Response<AnyBody> {
fn from(res: ExpectFailed) -> Self {
res.error_response()
}
}
#[actix_rt::test]
async fn test_h1_expect() {
let srv = test_server(move || {

View File

@ -5,7 +5,7 @@ extern crate tls_openssl as openssl;
use std::io;
use actix_http::{
body::{Body, SizedStream},
body::{AnyBody, Body, SizedStream},
error::PayloadError,
http::{
header::{self, HeaderName, HeaderValue},
@ -412,6 +412,12 @@ impl ResponseError for BadRequest {
}
}
impl From<BadRequest> for Response<AnyBody> {
fn from(res: BadRequest) -> Self {
res.error_response()
}
}
#[actix_rt::test]
async fn test_h2_service_error() {
let mut srv = test_server(move || {

View File

@ -3,7 +3,7 @@
extern crate tls_rustls as rustls;
use actix_http::{
body::{Body, SizedStream},
body::{AnyBody, Body, SizedStream},
error::PayloadError,
http::{
header::{self, HeaderName, HeaderValue},
@ -427,6 +427,12 @@ impl ResponseError for BadRequest {
}
}
impl From<BadRequest> for Response<AnyBody> {
fn from(res: BadRequest) -> Self {
res.error_response()
}
}
#[actix_rt::test]
async fn test_h2_service_error() {
let mut srv = test_server(move || {

View File

@ -3,7 +3,7 @@ use std::time::Duration;
use std::{net, thread};
use actix_http::{
body::{Body, SizedStream},
body::{AnyBody, Body, SizedStream},
http::{self, header, StatusCode},
Error, HttpService, KeepAlive, Request, Response,
};
@ -67,6 +67,12 @@ impl ResponseError for ExpectFailed {
}
}
impl From<ExpectFailed> for Response<AnyBody> {
fn from(res: ExpectFailed) -> Self {
res.error_response()
}
}
#[actix_rt::test]
async fn test_expect_continue() {
let srv = test_server(|| {
@ -666,6 +672,12 @@ impl ResponseError for BadRequest {
}
}
impl From<BadRequest> for Response<AnyBody> {
fn from(res: BadRequest) -> Self {
res.error_response()
}
}
#[actix_rt::test]
async fn test_h1_service_error() {
let mut srv = test_server(|| {