2017-11-04 20:33:14 +01:00
|
|
|
use std::{io, net};
|
2017-10-07 06:48:14 +02:00
|
|
|
use std::rc::Rc;
|
2017-11-10 22:08:15 +01:00
|
|
|
use std::net::SocketAddr;
|
2017-10-16 22:13:32 +02:00
|
|
|
use std::marker::PhantomData;
|
2017-10-07 06:48:14 +02:00
|
|
|
|
|
|
|
use actix::dev::*;
|
2017-11-04 20:33:14 +01:00
|
|
|
use futures::Stream;
|
2017-10-16 22:13:32 +02:00
|
|
|
use tokio_io::{AsyncRead, AsyncWrite};
|
2017-11-04 20:33:14 +01:00
|
|
|
use tokio_core::net::{TcpListener, TcpStream};
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-11-10 22:08:15 +01:00
|
|
|
#[cfg(feature="tls")]
|
|
|
|
use futures::Future;
|
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-04 21:24:57 +01:00
|
|
|
#[cfg(feature="alpn")]
|
|
|
|
use futures::Future;
|
2017-11-04 20:33:14 +01:00
|
|
|
#[cfg(feature="alpn")]
|
|
|
|
use openssl::ssl::{SslMethod, SslAcceptorBuilder};
|
|
|
|
#[cfg(feature="alpn")]
|
|
|
|
use openssl::pkcs12::ParsedPkcs12;
|
|
|
|
#[cfg(feature="alpn")]
|
|
|
|
use tokio_openssl::{SslStream, SslAcceptorExt};
|
|
|
|
|
2017-12-06 20:00:39 +01:00
|
|
|
use channel::{HttpChannel, HttpHandler, IntoHttpHandler};
|
2017-10-22 03:54:24 +02:00
|
|
|
|
2017-12-08 07:54:44 +01:00
|
|
|
/// Various server settings
|
|
|
|
pub struct ServerSettings<H> (Rc<InnerServerSettings<H>>);
|
|
|
|
|
|
|
|
struct InnerServerSettings<H> {
|
|
|
|
h: Vec<H>,
|
|
|
|
addr: Option<SocketAddr>,
|
|
|
|
secure: bool,
|
|
|
|
sethost: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<H> Clone for ServerSettings<H> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
ServerSettings(Rc::clone(&self.0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<H> ServerSettings<H> {
|
|
|
|
/// Crate server settings instance
|
|
|
|
fn new(h: Vec<H>, addr: Option<SocketAddr>, secure: bool, sethost: bool) -> Self {
|
|
|
|
ServerSettings(
|
|
|
|
Rc::new(InnerServerSettings {
|
|
|
|
h: h,
|
|
|
|
addr: addr,
|
|
|
|
secure: secure,
|
|
|
|
sethost: sethost }))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns list of http handlers
|
|
|
|
pub fn handlers(&self) -> &Vec<H> {
|
|
|
|
&self.0.h
|
|
|
|
}
|
|
|
|
/// Returns the socket address of the local half of this TCP connection
|
|
|
|
pub fn local_addr(&self) -> Option<SocketAddr> {
|
|
|
|
self.0.addr
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if connection is secure(https)
|
|
|
|
pub fn secure(&self) -> bool {
|
|
|
|
self.0.secure
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Should http channel set *HOST* header
|
|
|
|
pub fn set_host_header(&self) -> bool {
|
|
|
|
self.0.sethost
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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> {
|
2017-12-08 07:54:44 +01:00
|
|
|
h: Option<Vec<H>>,
|
2017-10-16 22:13:32 +02:00
|
|
|
io: PhantomData<T>,
|
|
|
|
addr: PhantomData<A>,
|
2017-12-08 07:54:44 +01:00
|
|
|
sethost: bool,
|
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
|
2017-12-06 20:00:39 +01:00
|
|
|
pub fn new<V, U: IntoIterator<Item=V>>(handler: U) -> Self
|
|
|
|
where V: IntoHttpHandler<Handler=H>
|
|
|
|
{
|
|
|
|
let apps: Vec<_> = handler.into_iter().map(|h| h.into_handler()).collect();
|
2017-10-22 03:54:24 +02:00
|
|
|
|
2017-12-08 07:54:44 +01:00
|
|
|
HttpServer {h: Some(apps),
|
2017-10-22 03:54:24 +02:00
|
|
|
io: PhantomData,
|
2017-12-08 07:54:44 +01:00
|
|
|
addr: PhantomData,
|
|
|
|
sethost: false}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set *HOST* header if not set
|
|
|
|
pub fn set_host_header(mut self) -> Self {
|
|
|
|
self.sethost = true;
|
|
|
|
self
|
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.
|
2017-12-08 07:54:44 +01:00
|
|
|
pub fn serve_incoming<S, Addr>(mut self, stream: S, secure: bool) -> io::Result<Addr>
|
2017-10-16 22:13:32 +02:00
|
|
|
where Self: ActorAddress<Self, Addr>,
|
|
|
|
S: Stream<Item=(T, A), Error=io::Error> + 'static
|
|
|
|
{
|
2017-12-08 07:54:44 +01:00
|
|
|
let addr: SocketAddr = "127.0.0.1:8080".parse().unwrap();
|
|
|
|
let settings = ServerSettings::new(
|
|
|
|
self.h.take().unwrap(), Some(addr), secure, self.sethost);
|
|
|
|
|
2017-10-16 22:13:32 +02:00
|
|
|
Ok(HttpServer::create(move |ctx| {
|
2017-12-08 06:52:46 +01:00
|
|
|
ctx.add_stream(stream.map(
|
2017-12-08 07:54:44 +01:00
|
|
|
move |(t, _)| IoStream{settings: settings.clone(),
|
|
|
|
io: t, peer: None, http2: false}));
|
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.
|
2017-12-08 07:54:44 +01:00
|
|
|
pub fn serve<S, Addr>(mut self, addr: S) -> io::Result<Addr>
|
2017-11-02 00:34:58 +01:00
|
|
|
where Self: ActorAddress<Self, Addr>,
|
|
|
|
S: net::ToSocketAddrs,
|
|
|
|
{
|
|
|
|
let addrs = self.bind(addr)?;
|
2017-12-08 07:54:44 +01:00
|
|
|
let settings = ServerSettings::new(
|
|
|
|
self.h.take().unwrap(), Some(addrs[0].0), false, self.sethost);
|
2017-11-02 00:34:58 +01:00
|
|
|
|
|
|
|
Ok(HttpServer::create(move |ctx| {
|
|
|
|
for (addr, tcp) in addrs {
|
|
|
|
info!("Starting http server on {}", addr);
|
2017-12-08 07:54:44 +01:00
|
|
|
let s = settings.clone();
|
2017-12-08 06:52:46 +01:00
|
|
|
ctx.add_stream(tcp.incoming().map(
|
2017-12-08 07:54:44 +01:00
|
|
|
move |(t, a)| IoStream{settings: s.clone(),
|
|
|
|
io: t, peer: Some(a), http2: false}));
|
2017-11-02 00:34:58 +01:00
|
|
|
}
|
|
|
|
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.
|
2017-12-08 07:54:44 +01:00
|
|
|
pub fn serve_tls<S, Addr>(mut self, addr: S, pkcs12: ::Pkcs12) -> io::Result<Addr>
|
2017-11-02 00:34:58 +01:00
|
|
|
where Self: ActorAddress<Self, Addr>,
|
|
|
|
S: net::ToSocketAddrs,
|
|
|
|
{
|
|
|
|
let addrs = self.bind(addr)?;
|
2017-12-08 07:54:44 +01:00
|
|
|
let settings = ServerSettings::new(
|
|
|
|
self.h.take().unwrap(), Some(addrs[0].0.clone()), true, self.sethost);
|
|
|
|
|
2017-11-02 00:34:58 +01:00
|
|
|
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| {
|
2017-12-08 06:52:46 +01:00
|
|
|
for (srv, tcp) in addrs {
|
|
|
|
info!("Starting tls http server on {}", srv);
|
2017-11-02 00:34:58 +01:00
|
|
|
|
2017-12-08 07:54:44 +01:00
|
|
|
let st = settings.clone();
|
2017-11-02 00:34:58 +01:00
|
|
|
let acc = acceptor.clone();
|
|
|
|
ctx.add_stream(tcp.incoming().and_then(move |(stream, addr)| {
|
2017-12-08 07:54:44 +01:00
|
|
|
let st2 = st.clone();
|
2017-11-02 00:34:58 +01:00
|
|
|
TlsAcceptorExt::accept_async(acc.as_ref(), stream)
|
2017-12-08 06:52:46 +01:00
|
|
|
.map(move |t|
|
2017-12-08 07:54:44 +01:00
|
|
|
IoStream{settings: st2.clone(),
|
|
|
|
io: t, peer: Some(addr), http2: false})
|
2017-11-02 20:54:09 +01:00
|
|
|
.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-11-04 20:33:14 +01:00
|
|
|
#[cfg(feature="alpn")]
|
|
|
|
impl<H: HttpHandler> HttpServer<SslStream<TcpStream>, net::SocketAddr, H> {
|
|
|
|
|
|
|
|
/// Start listening for incomming tls connections.
|
|
|
|
///
|
|
|
|
/// This methods converts address to list of `SocketAddr`
|
|
|
|
/// then binds to all available addresses.
|
2017-12-08 07:54:44 +01:00
|
|
|
pub fn serve_tls<S, Addr>(mut self, addr: S, identity: ParsedPkcs12) -> io::Result<Addr>
|
2017-11-04 20:33:14 +01:00
|
|
|
where Self: ActorAddress<Self, Addr>,
|
|
|
|
S: net::ToSocketAddrs,
|
|
|
|
{
|
|
|
|
let addrs = self.bind(addr)?;
|
2017-12-08 07:54:44 +01:00
|
|
|
let settings = ServerSettings::new(
|
|
|
|
self.h.take().unwrap(), Some(addrs[0].0.clone()), true, self.sethost);
|
|
|
|
|
|
|
|
let acceptor = match SslAcceptorBuilder::mozilla_intermediate(
|
|
|
|
SslMethod::tls(), &identity.pkey, &identity.cert, &identity.chain)
|
2017-11-04 20:33:14 +01:00
|
|
|
{
|
|
|
|
Ok(mut builder) => {
|
|
|
|
match builder.builder_mut().set_alpn_protocols(&[b"h2", b"http/1.1"]) {
|
|
|
|
Ok(_) => builder.build(),
|
|
|
|
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| {
|
2017-12-08 06:52:46 +01:00
|
|
|
for (srv, tcp) in addrs {
|
|
|
|
info!("Starting tls http server on {}", srv);
|
2017-11-04 20:33:14 +01:00
|
|
|
|
2017-12-08 07:54:44 +01:00
|
|
|
let st = settings.clone();
|
2017-11-04 20:33:14 +01:00
|
|
|
let acc = acceptor.clone();
|
|
|
|
ctx.add_stream(tcp.incoming().and_then(move |(stream, addr)| {
|
2017-12-08 07:54:44 +01:00
|
|
|
let st2 = st.clone();
|
2017-11-04 20:33:14 +01:00
|
|
|
SslAcceptorExt::accept_async(&acc, stream)
|
|
|
|
.map(move |stream| {
|
|
|
|
let http2 = if let Some(p) =
|
|
|
|
stream.get_ref().ssl().selected_alpn_protocol()
|
|
|
|
{
|
|
|
|
p.len() == 2 && &p == b"h2"
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
2017-12-08 07:54:44 +01:00
|
|
|
IoStream{settings: st2.clone(),
|
|
|
|
io: stream, peer: Some(addr), http2: http2}
|
2017-11-04 20:33:14 +01:00
|
|
|
})
|
|
|
|
.map_err(|err| {
|
|
|
|
trace!("Error during handling tls connection: {}", err);
|
|
|
|
io::Error::new(io::ErrorKind::Other, err)
|
|
|
|
})
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-08 07:54:44 +01:00
|
|
|
struct IoStream<T, H> {
|
2017-12-08 06:52:46 +01:00
|
|
|
io: T,
|
|
|
|
peer: Option<SocketAddr>,
|
|
|
|
http2: bool,
|
2017-12-08 07:54:44 +01:00
|
|
|
settings: ServerSettings<H>,
|
2017-12-08 06:52:46 +01:00
|
|
|
}
|
2017-10-22 00:21:16 +02:00
|
|
|
|
2017-12-08 07:54:44 +01:00
|
|
|
impl<T, H> ResponseType for IoStream<T, H>
|
2017-11-10 22:08:15 +01:00
|
|
|
where T: AsyncRead + AsyncWrite + 'static
|
2017-10-16 22:13:32 +02:00
|
|
|
{
|
2017-10-07 06:48:14 +02:00
|
|
|
type Item = ();
|
|
|
|
type Error = ();
|
|
|
|
}
|
|
|
|
|
2017-12-08 07:54:44 +01:00
|
|
|
impl<T, A, H> StreamHandler<IoStream<T, H>, io::Error> for HttpServer<T, A, H>
|
2017-10-22 03:54:24 +02:00
|
|
|
where T: AsyncRead + AsyncWrite + 'static,
|
2017-11-10 22:08:15 +01:00
|
|
|
H: HttpHandler + 'static,
|
|
|
|
A: 'static {}
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-12-08 07:54:44 +01:00
|
|
|
impl<T, A, H> Handler<IoStream<T, H>, 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
|
|
|
H: HttpHandler + 'static,
|
2017-11-10 22:08:15 +01:00
|
|
|
A: '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-04 17:07:44 +01:00
|
|
|
debug!("Error handling request: {}", err)
|
2017-11-02 00:34:58 +01:00
|
|
|
}
|
|
|
|
|
2017-12-08 07:54:44 +01:00
|
|
|
fn handle(&mut self, msg: IoStream<T, H>, _: &mut Context<Self>)
|
|
|
|
-> Response<Self, IoStream<T, H>>
|
2017-10-07 06:48:14 +02:00
|
|
|
{
|
|
|
|
Arbiter::handle().spawn(
|
2017-12-08 07:54:44 +01:00
|
|
|
HttpChannel::new(msg.settings, msg.io, msg.peer, msg.http2));
|
2017-10-07 09:22:09 +02:00
|
|
|
Self::empty()
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|