2017-11-04 20:33:14 +01:00
|
|
|
use std::rc::Rc;
|
2017-11-10 22:08:15 +01:00
|
|
|
use std::net::SocketAddr;
|
2017-11-04 20:33:14 +01:00
|
|
|
|
|
|
|
use actix::dev::*;
|
|
|
|
use bytes::Bytes;
|
|
|
|
use futures::{Future, Poll, Async};
|
|
|
|
use tokio_io::{AsyncRead, AsyncWrite};
|
|
|
|
|
|
|
|
use h1;
|
|
|
|
use h2;
|
2017-11-25 07:15:52 +01:00
|
|
|
use pipeline::Pipeline;
|
2017-11-04 20:33:14 +01:00
|
|
|
use httprequest::HttpRequest;
|
|
|
|
|
|
|
|
/// Low level http request handler
|
|
|
|
pub trait HttpHandler: 'static {
|
|
|
|
/// Http handler prefix
|
|
|
|
fn prefix(&self) -> &str;
|
|
|
|
/// Handle request
|
2017-11-27 04:00:57 +01:00
|
|
|
fn handle(&self, req: HttpRequest) -> Pipeline;
|
2017-11-04 20:33:14 +01:00
|
|
|
}
|
|
|
|
|
2017-11-10 22:08:15 +01:00
|
|
|
enum HttpProtocol<T, H>
|
|
|
|
where T: AsyncRead + AsyncWrite + 'static, H: 'static
|
2017-11-04 20:33:14 +01:00
|
|
|
{
|
2017-11-10 22:08:15 +01:00
|
|
|
H1(h1::Http1<T, H>),
|
|
|
|
H2(h2::Http2<T, H>),
|
2017-11-04 20:33:14 +01:00
|
|
|
}
|
|
|
|
|
2017-11-10 22:08:15 +01:00
|
|
|
pub struct HttpChannel<T, H>
|
|
|
|
where T: AsyncRead + AsyncWrite + 'static, H: 'static
|
2017-11-04 20:33:14 +01:00
|
|
|
{
|
2017-11-10 22:08:15 +01:00
|
|
|
proto: Option<HttpProtocol<T, H>>,
|
2017-11-04 20:33:14 +01:00
|
|
|
}
|
|
|
|
|
2017-11-10 22:08:15 +01:00
|
|
|
impl<T, H> HttpChannel<T, H>
|
|
|
|
where T: AsyncRead + AsyncWrite + 'static, H: HttpHandler + 'static
|
2017-11-04 20:33:14 +01:00
|
|
|
{
|
2017-11-10 22:08:15 +01:00
|
|
|
pub fn new(stream: T, addr: Option<SocketAddr>, router: Rc<Vec<H>>, http2: bool)
|
|
|
|
-> HttpChannel<T, H> {
|
2017-11-04 20:33:14 +01:00
|
|
|
if http2 {
|
|
|
|
HttpChannel {
|
|
|
|
proto: Some(HttpProtocol::H2(
|
|
|
|
h2::Http2::new(stream, addr, router, Bytes::new()))) }
|
|
|
|
} else {
|
|
|
|
HttpChannel {
|
|
|
|
proto: Some(HttpProtocol::H1(
|
|
|
|
h1::Http1::new(stream, addr, router))) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*impl<T: 'static, A: 'static, H: 'static> Drop for HttpChannel<T, A, H> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
println!("Drop http channel");
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
|
2017-11-10 22:08:15 +01:00
|
|
|
impl<T, H> Actor for HttpChannel<T, H>
|
|
|
|
where T: AsyncRead + AsyncWrite + 'static, H: HttpHandler + 'static
|
2017-11-04 20:33:14 +01:00
|
|
|
{
|
|
|
|
type Context = Context<Self>;
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:08:15 +01:00
|
|
|
impl<T, H> Future for HttpChannel<T, H>
|
|
|
|
where T: AsyncRead + AsyncWrite + 'static, H: HttpHandler + 'static
|
2017-11-04 20:33:14 +01:00
|
|
|
{
|
|
|
|
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 21:49:05 +01:00
|
|
|
Ok(Async::Ready(h1::Http1Result::Switch)) => (),
|
2017-11-04 20:33:14 +01:00
|
|
|
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) => {
|
|
|
|
let (stream, addr, router, buf) = h1.into_inner();
|
|
|
|
self.proto = Some(HttpProtocol::H2(h2::Http2::new(stream, addr, router, buf)));
|
|
|
|
self.poll()
|
|
|
|
}
|
|
|
|
_ => unreachable!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|