2017-11-03 19:29:44 +01:00
|
|
|
use std::{io, net, mem};
|
2017-10-07 06:48:14 +02:00
|
|
|
use std::rc::Rc;
|
2017-10-16 22:13:32 +02:00
|
|
|
use std::marker::PhantomData;
|
2017-10-07 06:48:14 +02:00
|
|
|
|
|
|
|
use actix::dev::*;
|
2017-10-16 22:13:32 +02:00
|
|
|
use futures::{Future, Poll, Async, Stream};
|
2017-10-07 06:48:14 +02:00
|
|
|
use tokio_core::net::{TcpListener, TcpStream};
|
2017-10-16 22:13:32 +02:00
|
|
|
use tokio_io::{AsyncRead, AsyncWrite};
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-11-02 00:34:58 +01:00
|
|
|
#[cfg(feature="tls")]
|
|
|
|
use native_tls::TlsAcceptor;
|
|
|
|
#[cfg(feature="tls")]
|
|
|
|
use tokio_tls::{TlsStream, TlsAcceptorExt};
|
|
|
|
|
2017-11-02 20:54:09 +01:00
|
|
|
use h1;
|
2017-11-03 19:29:44 +01:00
|
|
|
use h2;
|
2017-10-22 07:59:09 +02:00
|
|
|
use task::Task;
|
2017-10-22 03:54:24 +02:00
|
|
|
use payload::Payload;
|
|
|
|
use httprequest::HttpRequest;
|
|
|
|
|
|
|
|
/// Low level http request handler
|
|
|
|
pub trait HttpHandler: 'static {
|
|
|
|
/// Http handler prefix
|
|
|
|
fn prefix(&self) -> &str;
|
|
|
|
/// Handle request
|
2017-10-22 07:59:09 +02:00
|
|
|
fn handle(&self, req: &mut HttpRequest, payload: Payload) -> Task;
|
2017-10-22 03:54:24 +02:00
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-10-08 23:56:51 +02:00
|
|
|
/// An HTTP Server
|
2017-10-16 22:13:32 +02:00
|
|
|
///
|
|
|
|
/// `T` - async stream, anything that implements `AsyncRead` + `AsyncWrite`.
|
|
|
|
///
|
|
|
|
/// `A` - peer address
|
2017-10-22 03:54:24 +02:00
|
|
|
///
|
|
|
|
/// `H` - request handler
|
|
|
|
pub struct HttpServer<T, A, H> {
|
|
|
|
h: Rc<Vec<H>>,
|
2017-10-16 22:13:32 +02:00
|
|
|
io: PhantomData<T>,
|
|
|
|
addr: PhantomData<A>,
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2017-10-22 03:54:24 +02:00
|
|
|
impl<T: 'static, A: 'static, H: 'static> Actor for HttpServer<T, A, H> {
|
2017-10-07 06:48:14 +02:00
|
|
|
type Context = Context<Self>;
|
|
|
|
}
|
|
|
|
|
2017-10-22 03:54:24 +02:00
|
|
|
impl<T, A, H> HttpServer<T, A, H> where H: HttpHandler
|
|
|
|
{
|
|
|
|
/// Create new http server with vec of http handlers
|
|
|
|
pub fn new<U: IntoIterator<Item=H>>(handler: U) -> Self {
|
2017-10-23 02:33:24 +02:00
|
|
|
let apps: Vec<_> = handler.into_iter().collect();
|
2017-10-22 03:54:24 +02:00
|
|
|
|
|
|
|
HttpServer {h: Rc::new(apps),
|
|
|
|
io: PhantomData,
|
|
|
|
addr: PhantomData}
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
2017-10-16 22:13:32 +02:00
|
|
|
}
|
|
|
|
|
2017-10-22 03:54:24 +02:00
|
|
|
impl<T, A, H> HttpServer<T, A, H>
|
2017-10-16 22:13:32 +02:00
|
|
|
where T: AsyncRead + AsyncWrite + 'static,
|
2017-10-22 03:54:24 +02:00
|
|
|
A: 'static,
|
|
|
|
H: HttpHandler,
|
2017-10-16 22:13:32 +02:00
|
|
|
{
|
|
|
|
/// Start listening for incomming connections from stream.
|
|
|
|
pub fn serve_incoming<S, Addr>(self, stream: S) -> io::Result<Addr>
|
|
|
|
where Self: ActorAddress<Self, Addr>,
|
|
|
|
S: Stream<Item=(T, A), Error=io::Error> + 'static
|
|
|
|
{
|
|
|
|
Ok(HttpServer::create(move |ctx| {
|
2017-10-22 00:21:16 +02:00
|
|
|
ctx.add_stream(stream.map(|(t, a)| IoStream(t, a)));
|
2017-10-16 22:13:32 +02:00
|
|
|
self
|
|
|
|
}))
|
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-11-02 00:34:58 +01:00
|
|
|
fn bind<S: net::ToSocketAddrs>(&self, addr: S)
|
|
|
|
-> io::Result<Vec<(net::SocketAddr, TcpListener)>>
|
2017-10-07 06:48:14 +02:00
|
|
|
{
|
2017-10-15 23:53:03 +02:00
|
|
|
let mut err = None;
|
|
|
|
let mut addrs = Vec::new();
|
2017-10-16 10:19:23 +02:00
|
|
|
if let Ok(iter) = addr.to_socket_addrs() {
|
2017-10-15 23:53:03 +02:00
|
|
|
for addr in iter {
|
|
|
|
match TcpListener::bind(&addr, Arbiter::handle()) {
|
2017-10-27 08:14:33 +02:00
|
|
|
Ok(tcp) => addrs.push((addr, tcp)),
|
2017-10-15 23:53:03 +02:00
|
|
|
Err(e) => err = Some(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if addrs.is_empty() {
|
|
|
|
if let Some(e) = err.take() {
|
|
|
|
Err(e)
|
|
|
|
} else {
|
|
|
|
Err(io::Error::new(io::ErrorKind::Other, "Can not bind to address."))
|
|
|
|
}
|
|
|
|
} else {
|
2017-11-02 00:34:58 +01:00
|
|
|
Ok(addrs)
|
2017-10-15 23:53:03 +02:00
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-02 00:34:58 +01:00
|
|
|
impl<H: HttpHandler> HttpServer<TcpStream, net::SocketAddr, H> {
|
|
|
|
|
|
|
|
/// Start listening for incomming connections.
|
|
|
|
///
|
|
|
|
/// This methods converts address to list of `SocketAddr`
|
|
|
|
/// then binds to all available addresses.
|
|
|
|
pub fn serve<S, Addr>(self, addr: S) -> io::Result<Addr>
|
|
|
|
where Self: ActorAddress<Self, Addr>,
|
|
|
|
S: net::ToSocketAddrs,
|
|
|
|
{
|
|
|
|
let addrs = self.bind(addr)?;
|
|
|
|
|
|
|
|
Ok(HttpServer::create(move |ctx| {
|
|
|
|
for (addr, tcp) in addrs {
|
|
|
|
info!("Starting http server on {}", addr);
|
|
|
|
ctx.add_stream(tcp.incoming().map(|(t, a)| IoStream(t, a)));
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature="tls")]
|
|
|
|
impl<H: HttpHandler> HttpServer<TlsStream<TcpStream>, net::SocketAddr, H> {
|
|
|
|
|
|
|
|
/// Start listening for incomming tls connections.
|
|
|
|
///
|
|
|
|
/// This methods converts address to list of `SocketAddr`
|
|
|
|
/// then binds to all available addresses.
|
|
|
|
pub fn serve_tls<S, Addr>(self, addr: S, pkcs12: ::Pkcs12) -> io::Result<Addr>
|
|
|
|
where Self: ActorAddress<Self, Addr>,
|
|
|
|
S: net::ToSocketAddrs,
|
|
|
|
{
|
|
|
|
let addrs = self.bind(addr)?;
|
|
|
|
let acceptor = match TlsAcceptor::builder(pkcs12) {
|
|
|
|
Ok(builder) => {
|
|
|
|
match builder.build() {
|
|
|
|
Ok(acceptor) => Rc::new(acceptor),
|
|
|
|
Err(err) => return Err(io::Error::new(io::ErrorKind::Other, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err) => return Err(io::Error::new(io::ErrorKind::Other, err))
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(HttpServer::create(move |ctx| {
|
|
|
|
for (addr, tcp) in addrs {
|
|
|
|
info!("Starting tls http server on {}", addr);
|
|
|
|
|
|
|
|
let acc = acceptor.clone();
|
|
|
|
ctx.add_stream(tcp.incoming().and_then(move |(stream, addr)| {
|
2017-11-02 20:54:09 +01:00
|
|
|
println!("SSL");
|
2017-11-02 00:34:58 +01:00
|
|
|
TlsAcceptorExt::accept_async(acc.as_ref(), stream)
|
2017-11-02 20:54:09 +01:00
|
|
|
.map(move |t| {
|
|
|
|
IoStream(t, addr)
|
|
|
|
})
|
|
|
|
.map_err(|err| {
|
2017-11-03 19:29:44 +01:00
|
|
|
trace!("Error during handling tls connection: {}", err);
|
2017-11-02 20:54:09 +01:00
|
|
|
io::Error::new(io::ErrorKind::Other, err)
|
|
|
|
})
|
2017-11-02 00:34:58 +01:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-22 00:21:16 +02:00
|
|
|
struct IoStream<T, A>(T, A);
|
|
|
|
|
|
|
|
impl<T, A> ResponseType for IoStream<T, A>
|
2017-10-16 22:13:32 +02:00
|
|
|
where T: AsyncRead + AsyncWrite + 'static,
|
|
|
|
A: 'static
|
|
|
|
{
|
2017-10-07 06:48:14 +02:00
|
|
|
type Item = ();
|
|
|
|
type Error = ();
|
|
|
|
}
|
|
|
|
|
2017-10-22 03:54:24 +02:00
|
|
|
impl<T, A, H> StreamHandler<IoStream<T, A>, io::Error> for HttpServer<T, A, H>
|
|
|
|
where T: AsyncRead + AsyncWrite + 'static,
|
|
|
|
A: 'static,
|
|
|
|
H: HttpHandler + 'static {}
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-10-22 03:54:24 +02:00
|
|
|
impl<T, A, H> Handler<IoStream<T, A>, io::Error> for HttpServer<T, A, H>
|
2017-10-16 22:13:32 +02:00
|
|
|
where T: AsyncRead + AsyncWrite + 'static,
|
2017-10-22 03:54:24 +02:00
|
|
|
A: 'static,
|
|
|
|
H: HttpHandler + 'static,
|
2017-10-16 22:13:32 +02:00
|
|
|
{
|
2017-11-02 00:34:58 +01:00
|
|
|
fn error(&mut self, err: io::Error, _: &mut Context<Self>) {
|
2017-11-02 20:54:09 +01:00
|
|
|
println!("Error handling request: {}", err)
|
2017-11-02 00:34:58 +01:00
|
|
|
}
|
|
|
|
|
2017-10-22 00:21:16 +02:00
|
|
|
fn handle(&mut self, msg: IoStream<T, A>, _: &mut Context<Self>)
|
|
|
|
-> Response<Self, IoStream<T, A>>
|
2017-10-07 06:48:14 +02:00
|
|
|
{
|
|
|
|
Arbiter::handle().spawn(
|
2017-11-03 19:29:44 +01:00
|
|
|
HttpChannel{
|
|
|
|
proto: Protocol::H1(h1::Http1::new(msg.0, msg.1, Rc::clone(&self.h)))
|
2017-10-07 06:48:14 +02:00
|
|
|
});
|
2017-10-07 09:22:09 +02:00
|
|
|
Self::empty()
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-03 19:29:44 +01:00
|
|
|
enum Protocol<T, A, H>
|
|
|
|
where T: AsyncRead + AsyncWrite + 'static, A: 'static, H: 'static
|
|
|
|
{
|
|
|
|
H1(h1::Http1<T, A, H>),
|
|
|
|
H2(h2::Http2<T, A, H>),
|
|
|
|
None,
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2017-11-03 19:29:44 +01:00
|
|
|
pub struct HttpChannel<T, A, H>
|
|
|
|
where T: AsyncRead + AsyncWrite + 'static, A: 'static, H: 'static
|
|
|
|
{
|
|
|
|
proto: Protocol<T, A, H>,
|
2017-10-14 01:33:23 +02:00
|
|
|
}
|
|
|
|
|
2017-10-30 04:54:52 +01:00
|
|
|
/*impl<T: 'static, A: 'static, H: 'static> Drop for HttpChannel<T, A, H> {
|
2017-10-14 01:33:23 +02:00
|
|
|
fn drop(&mut self) {
|
|
|
|
println!("Drop http channel");
|
|
|
|
}
|
2017-10-30 04:54:52 +01:00
|
|
|
}*/
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-10-22 03:54:24 +02:00
|
|
|
impl<T, A, H> Actor for HttpChannel<T, A, H>
|
2017-11-03 19:29:44 +01:00
|
|
|
where T: AsyncRead + AsyncWrite + 'static, A: 'static, H: HttpHandler + 'static
|
2017-10-16 22:13:32 +02:00
|
|
|
{
|
2017-10-07 06:48:14 +02:00
|
|
|
type Context = Context<Self>;
|
|
|
|
}
|
|
|
|
|
2017-10-22 03:54:24 +02:00
|
|
|
impl<T, A, H> Future for HttpChannel<T, A, H>
|
2017-11-03 19:29:44 +01:00
|
|
|
where T: AsyncRead + AsyncWrite + 'static, A: 'static, H: HttpHandler + 'static
|
2017-10-16 22:13:32 +02:00
|
|
|
{
|
2017-10-07 06:48:14 +02:00
|
|
|
type Item = ();
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
2017-11-03 19:29:44 +01:00
|
|
|
match self.proto {
|
|
|
|
Protocol::H1(ref mut h1) => {
|
|
|
|
match h1.poll() {
|
|
|
|
Ok(Async::Ready(h1::Http1Result::Done)) =>
|
|
|
|
return Ok(Async::Ready(())),
|
|
|
|
Ok(Async::Ready(h1::Http1Result::Upgrade)) => (),
|
|
|
|
Ok(Async::NotReady) =>
|
|
|
|
return Ok(Async::NotReady),
|
|
|
|
Err(_) =>
|
|
|
|
return Err(()),
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
2017-10-14 01:33:23 +02:00
|
|
|
}
|
2017-11-03 19:29:44 +01:00
|
|
|
Protocol::H2(ref mut h2) =>
|
|
|
|
return h2.poll(),
|
|
|
|
Protocol::None =>
|
|
|
|
unreachable!()
|
|
|
|
}
|
2017-10-26 01:25:26 +02:00
|
|
|
|
2017-11-03 19:29:44 +01:00
|
|
|
// upgrade to h2
|
|
|
|
let proto = mem::replace(&mut self.proto, Protocol::None);
|
|
|
|
match proto {
|
|
|
|
Protocol::H1(h1) => {
|
|
|
|
let (stream, addr, router, buf) = h1.into_inner();
|
|
|
|
self.proto = Protocol::H2(h2::Http2::new(stream, addr, router, buf));
|
|
|
|
return self.poll()
|
2017-10-26 01:25:26 +02:00
|
|
|
}
|
2017-11-03 19:29:44 +01:00
|
|
|
_ => unreachable!()
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|