2017-10-13 23:43:17 +02:00
|
|
|
use std::{io, net, mem};
|
2017-10-07 06:48:14 +02:00
|
|
|
use std::rc::Rc;
|
2017-10-14 01:33:23 +02:00
|
|
|
use std::time::Duration;
|
2017-10-07 06:48:14 +02:00
|
|
|
use std::collections::VecDeque;
|
|
|
|
|
|
|
|
use actix::dev::*;
|
|
|
|
use futures::{Future, Poll, Async};
|
2017-10-14 01:33:23 +02:00
|
|
|
use tokio_core::reactor::Timeout;
|
2017-10-07 06:48:14 +02:00
|
|
|
use tokio_core::net::{TcpListener, TcpStream};
|
|
|
|
|
2017-10-13 23:43:17 +02:00
|
|
|
use task::{Task, RequestInfo};
|
2017-10-14 01:33:23 +02:00
|
|
|
use reader::{Reader, ReaderError};
|
2017-10-07 06:48:14 +02:00
|
|
|
use router::{Router, RoutingMap};
|
|
|
|
|
2017-10-08 23:56:51 +02:00
|
|
|
/// An HTTP Server
|
2017-10-07 06:48:14 +02:00
|
|
|
pub struct HttpServer {
|
|
|
|
router: Rc<Router>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Actor for HttpServer {
|
|
|
|
type Context = Context<Self>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HttpServer {
|
2017-10-07 08:14:13 +02:00
|
|
|
/// Create new http server with specified `RoutingMap`
|
2017-10-07 06:48:14 +02:00
|
|
|
pub fn new(routes: RoutingMap) -> Self {
|
|
|
|
HttpServer {router: Rc::new(routes.into_router())}
|
|
|
|
}
|
|
|
|
|
2017-10-07 08:14:13 +02:00
|
|
|
/// Start listening for incomming connections.
|
2017-10-07 06:48:14 +02:00
|
|
|
pub fn serve<Addr>(self, addr: &net::SocketAddr) -> io::Result<Addr>
|
|
|
|
where Self: ActorAddress<Self, Addr>
|
|
|
|
{
|
|
|
|
let tcp = TcpListener::bind(addr, Arbiter::handle())?;
|
|
|
|
|
|
|
|
Ok(HttpServer::create(move |ctx| {
|
|
|
|
ctx.add_stream(tcp.incoming());
|
|
|
|
self
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ResponseType<(TcpStream, net::SocketAddr)> for HttpServer {
|
|
|
|
type Item = ();
|
|
|
|
type Error = ();
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StreamHandler<(TcpStream, net::SocketAddr), io::Error> for HttpServer {}
|
|
|
|
|
|
|
|
impl Handler<(TcpStream, net::SocketAddr), io::Error> for HttpServer {
|
|
|
|
|
|
|
|
fn handle(&mut self, msg: (TcpStream, net::SocketAddr), _: &mut Context<Self>)
|
|
|
|
-> Response<Self, (TcpStream, net::SocketAddr)>
|
|
|
|
{
|
|
|
|
Arbiter::handle().spawn(
|
|
|
|
HttpChannel{router: Rc::clone(&self.router),
|
|
|
|
addr: msg.1,
|
|
|
|
stream: msg.0,
|
|
|
|
reader: Reader::new(),
|
2017-10-13 23:43:17 +02:00
|
|
|
error: false,
|
2017-10-07 06:48:14 +02:00
|
|
|
items: VecDeque::new(),
|
|
|
|
inactive: Vec::new(),
|
2017-10-14 01:33:23 +02:00
|
|
|
keepalive: true,
|
|
|
|
keepalive_timer: None,
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct Entry {
|
|
|
|
task: Task,
|
2017-10-13 23:43:17 +02:00
|
|
|
req: RequestInfo,
|
2017-10-07 06:48:14 +02:00
|
|
|
eof: bool,
|
|
|
|
error: bool,
|
|
|
|
finished: bool,
|
|
|
|
}
|
|
|
|
|
2017-10-14 01:33:23 +02:00
|
|
|
const KEEPALIVE_PERIOD: u64 = 15; // seconds
|
|
|
|
const MAX_PIPELINED_MESSAGES: usize = 16;
|
|
|
|
|
2017-10-07 06:48:14 +02:00
|
|
|
pub struct HttpChannel {
|
|
|
|
router: Rc<Router>,
|
|
|
|
#[allow(dead_code)]
|
|
|
|
addr: net::SocketAddr,
|
|
|
|
stream: TcpStream,
|
|
|
|
reader: Reader,
|
2017-10-13 23:43:17 +02:00
|
|
|
error: bool,
|
2017-10-07 06:48:14 +02:00
|
|
|
items: VecDeque<Entry>,
|
|
|
|
inactive: Vec<Entry>,
|
2017-10-14 01:33:23 +02:00
|
|
|
keepalive: bool,
|
|
|
|
keepalive_timer: Option<Timeout>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for HttpChannel {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
println!("Drop http channel");
|
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Actor for HttpChannel {
|
|
|
|
type Context = Context<Self>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Future for HttpChannel {
|
|
|
|
type Item = ();
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
2017-10-14 01:33:23 +02:00
|
|
|
// keep-alive timer
|
|
|
|
if let Some(ref mut timeout) = self.keepalive_timer {
|
|
|
|
match timeout.poll() {
|
|
|
|
Ok(Async::Ready(_)) =>
|
|
|
|
return Ok(Async::Ready(())),
|
|
|
|
Ok(Async::NotReady) => (),
|
|
|
|
Err(_) => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-07 06:48:14 +02:00
|
|
|
loop {
|
|
|
|
// check in-flight messages
|
|
|
|
let mut idx = 0;
|
|
|
|
while idx < self.items.len() {
|
|
|
|
if idx == 0 {
|
|
|
|
if self.items[idx].error {
|
|
|
|
return Err(())
|
|
|
|
}
|
2017-10-13 23:43:17 +02:00
|
|
|
|
|
|
|
// this is anoying
|
|
|
|
let req: &RequestInfo = unsafe {
|
|
|
|
mem::transmute(&self.items[idx].req)
|
|
|
|
};
|
|
|
|
match self.items[idx].task.poll_io(&mut self.stream, req)
|
|
|
|
{
|
2017-10-07 06:48:14 +02:00
|
|
|
Ok(Async::Ready(val)) => {
|
|
|
|
let mut item = self.items.pop_front().unwrap();
|
2017-10-14 01:33:23 +02:00
|
|
|
|
|
|
|
// overide keep-alive state
|
|
|
|
if self.keepalive {
|
|
|
|
self.keepalive = item.task.keepalive();
|
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
if !val {
|
|
|
|
item.eof = true;
|
|
|
|
self.inactive.push(item);
|
|
|
|
}
|
2017-10-14 01:33:23 +02:00
|
|
|
|
|
|
|
// no keep-alive
|
|
|
|
if !self.keepalive && self.items.is_empty() {
|
|
|
|
return Ok(Async::Ready(()))
|
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
continue
|
|
|
|
},
|
|
|
|
Ok(Async::NotReady) => (),
|
2017-10-13 23:43:17 +02:00
|
|
|
Err(_) => {
|
|
|
|
// it is not possible to recover from error
|
|
|
|
// during task handling, so just drop connection
|
|
|
|
return Err(())
|
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
} else if !self.items[idx].finished {
|
|
|
|
match self.items[idx].task.poll() {
|
|
|
|
Ok(Async::Ready(_)) =>
|
|
|
|
self.items[idx].finished = true,
|
|
|
|
Ok(Async::NotReady) => (),
|
|
|
|
Err(_) =>
|
|
|
|
self.items[idx].error = true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
idx += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read incoming data
|
2017-10-14 01:33:23 +02:00
|
|
|
if !self.error && self.items.len() < MAX_PIPELINED_MESSAGES {
|
2017-10-13 23:43:17 +02:00
|
|
|
match self.reader.parse(&mut self.stream) {
|
|
|
|
Ok(Async::Ready((req, payload))) => {
|
2017-10-14 01:33:23 +02:00
|
|
|
// stop keepalive timer
|
|
|
|
self.keepalive_timer.take();
|
|
|
|
|
|
|
|
// start request processing
|
2017-10-13 23:43:17 +02:00
|
|
|
let info = RequestInfo::new(&req);
|
|
|
|
self.items.push_back(
|
|
|
|
Entry {task: self.router.call(req, payload),
|
|
|
|
req: info,
|
|
|
|
eof: false,
|
|
|
|
error: false,
|
|
|
|
finished: false});
|
|
|
|
}
|
2017-10-14 01:33:23 +02:00
|
|
|
Err(err) => {
|
|
|
|
// kill keepalive
|
|
|
|
self.keepalive = false;
|
|
|
|
self.keepalive_timer.take();
|
|
|
|
|
|
|
|
// on parse error, stop reading stream but
|
|
|
|
// complete tasks
|
|
|
|
self.error = true;
|
|
|
|
|
|
|
|
if let ReaderError::Error(err) = err {
|
|
|
|
self.items.push_back(
|
|
|
|
Entry {task: Task::reply(err),
|
|
|
|
req: RequestInfo::for_error(),
|
|
|
|
eof: false,
|
|
|
|
error: false,
|
|
|
|
finished: false});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(Async::NotReady) => {
|
|
|
|
// start keep-alive timer, this is also slow request timeout
|
|
|
|
if self.items.is_empty() {
|
|
|
|
if self.keepalive {
|
|
|
|
if self.keepalive_timer.is_none() {
|
|
|
|
trace!("Start keep-alive timer");
|
|
|
|
let mut timeout = Timeout::new(
|
|
|
|
Duration::new(KEEPALIVE_PERIOD, 0),
|
|
|
|
Arbiter::handle()).unwrap();
|
|
|
|
// register timeout
|
|
|
|
let _ = timeout.poll();
|
|
|
|
self.keepalive_timer = Some(timeout);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// keep-alive disable, drop connection
|
|
|
|
return Ok(Async::Ready(()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Ok(Async::NotReady)
|
|
|
|
}
|
2017-10-13 23:43:17 +02:00
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
2017-10-14 01:33:23 +02:00
|
|
|
|
|
|
|
// check for parse error
|
|
|
|
if self.items.is_empty() && self.error {
|
|
|
|
return Ok(Async::Ready(()))
|
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|