1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-10-11 04:32:28 +00:00
actix-web/src/channel.rs

118 lines
3.1 KiB
Rust
Raw Normal View History

use std::net::SocketAddr;
use actix::dev::*;
use bytes::Bytes;
use futures::{Future, Poll, Async};
use tokio_io::{AsyncRead, AsyncWrite};
use h1;
use h2;
2017-11-25 06:15:52 +00:00
use pipeline::Pipeline;
use httprequest::HttpRequest;
2017-12-08 06:54:44 +00:00
use server::ServerSettings;
/// Low level http request handler
pub trait HttpHandler: 'static {
/// Handle request
2017-11-29 21:53:52 +00:00
fn handle(&self, req: HttpRequest) -> Result<Pipeline, HttpRequest>;
}
2017-12-06 19:00:39 +00:00
/// Conversion helper trait
pub trait IntoHttpHandler {
/// The associated type which is result of conversion.
type Handler: HttpHandler;
/// Convert into `HttpHandler` object.
fn into_handler(self) -> Self::Handler;
}
impl<T: HttpHandler> IntoHttpHandler for T {
type Handler = T;
fn into_handler(self) -> Self::Handler {
self
}
}
enum HttpProtocol<T, H>
where T: AsyncRead + AsyncWrite + 'static, H: 'static
{
H1(h1::Http1<T, H>),
H2(h2::Http2<T, H>),
}
2017-11-27 06:53:28 +00:00
#[doc(hidden)]
pub struct HttpChannel<T, H>
where T: AsyncRead + AsyncWrite + 'static, H: 'static
{
proto: Option<HttpProtocol<T, H>>,
}
impl<T, H> HttpChannel<T, H>
where T: AsyncRead + AsyncWrite + 'static, H: HttpHandler + 'static
{
2017-12-08 06:54:44 +00:00
pub fn new(settings: ServerSettings<H>,
stream: T, peer: Option<SocketAddr>, http2: bool) -> HttpChannel<T, H>
{
if http2 {
HttpChannel {
proto: Some(HttpProtocol::H2(
2017-12-08 06:54:44 +00:00
h2::Http2::new(settings, stream, peer, Bytes::new()))) }
} else {
HttpChannel {
proto: Some(HttpProtocol::H1(
2017-12-08 06:54:44 +00:00
h1::Http1::new(settings, stream, peer))) }
}
}
}
/*impl<T: 'static, A: 'static, H: 'static> Drop for HttpChannel<T, A, H> {
fn drop(&mut self) {
println!("Drop http channel");
}
}*/
impl<T, H> Actor for HttpChannel<T, H>
where T: AsyncRead + AsyncWrite + 'static, H: HttpHandler + 'static
{
type Context = Context<Self>;
}
impl<T, H> Future for HttpChannel<T, H>
where T: AsyncRead + AsyncWrite + 'static, H: HttpHandler + 'static
{
type Item = ();
type Error = ();
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
match self.proto {
Some(HttpProtocol::H1(ref mut h1)) => {
match h1.poll() {
Ok(Async::Ready(h1::Http1Result::Done)) =>
return Ok(Async::Ready(())),
2017-11-04 20:49:05 +00:00
Ok(Async::Ready(h1::Http1Result::Switch)) => (),
Ok(Async::NotReady) =>
return Ok(Async::NotReady),
Err(_) =>
return Err(()),
}
}
Some(HttpProtocol::H2(ref mut h2)) =>
return h2.poll(),
None => unreachable!(),
}
// upgrade to h2
let proto = self.proto.take().unwrap();
match proto {
HttpProtocol::H1(h1) => {
2017-12-08 06:54:44 +00:00
let (settings, stream, addr, buf) = h1.into_inner();
self.proto = Some(
HttpProtocol::H2(h2::Http2::new(settings, stream, addr, buf)));
self.poll()
}
_ => unreachable!()
}
}
}