1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//! HTTP Server
use std::io::{Listener, EndOfFile, BufferedReader, BufferedWriter};
use std::io::net::ip::{IpAddr, Port, SocketAddr};
use std::os;
use std::sync::{Arc, TaskPool};
use std::thread::{Builder, JoinGuard};


pub use self::request::Request;
pub use self::response::Response;

pub use net::{Fresh, Streaming};

use HttpError::HttpIoError;
use {HttpResult};
use header::common::Connection;
use header::common::connection::{KeepAlive, Close};
use net::{NetworkListener, NetworkStream, NetworkAcceptor,
          HttpAcceptor, HttpListener};
use version::HttpVersion::{Http10, Http11};

pub mod request;
pub mod response;

/// A server can listen on a TCP socket.
///
/// Once listening, it will create a `Request`/`Response` pair for each
/// incoming connection, and hand them to the provided handler.
pub struct Server<L = HttpListener> {
    ip: IpAddr,
    port: Port,
    listener: L,
}

macro_rules! try_option(
    ($e:expr) => {{
        match $e {
            Some(v) => v,
            None => return None
        }
    }}
);

impl Server<HttpListener> {
    /// Creates a new server that will handle `HttpStream`s.
    pub fn http(ip: IpAddr, port: Port) -> Server {
        Server::with_listener(ip, port, HttpListener::Http)
    }
    /// Creates a new server that will handler `HttpStreams`s using a TLS connection.
    pub fn https(ip: IpAddr, port: Port, cert: Path, key: Path) -> Server {
        Server::with_listener(ip, port, HttpListener::Https(cert, key))
    }
}

impl<
L: NetworkListener<Acceptor=A> + Send,
A: NetworkAcceptor<Stream=S> + Send,
S: NetworkStream + Clone + Send> Server<L> {
    /// Creates a new server that will handle `HttpStream`s.
    pub fn with_listener(ip: IpAddr, port: Port, listener: L) -> Server<L> {
        Server {
            ip: ip,
            port: port,
            listener: listener,
        }
    }

    /// Binds to a socket, and starts handling connections using a task pool.
    pub fn listen_threads<H: Handler>(mut self, handler: H, threads: usize) -> HttpResult<Listening<L::Acceptor>> {
        debug!("binding to {:?}:{:?}", self.ip, self.port);
        let acceptor = try!(self.listener.listen((self.ip, self.port)));
        let socket = try!(acceptor.socket_name());

        let mut captured = acceptor.clone();
        let guard = Builder::new().name("hyper acceptor".to_string()).scoped(move || {
            let handler = Arc::new(handler);
            debug!("threads = {:?}", threads);
            let pool = TaskPool::new(threads);
            for conn in captured.incoming() {
                match conn {
                    Ok(mut stream) => {
                        debug!("Incoming stream");
                        let handler = handler.clone();
                        pool.execute(move || {
                            let addr = match stream.peer_name() {
                                Ok(addr) => addr,
                                Err(e) => {
                                    error!("Peer Name error: {:?}", e);
                                    return;
                                }
                            };
                            let mut rdr = BufferedReader::new(stream.clone());
                            let mut wrt = BufferedWriter::new(stream);

                            let mut keep_alive = true;
                            while keep_alive {
                                let mut res = Response::new(&mut wrt);
                                let req = match Request::new(&mut rdr, addr) {
                                    Ok(req) => req,
                                    Err(e@HttpIoError(_)) => {
                                        debug!("ioerror in keepalive loop = {:?}", e);
                                        return;
                                    }
                                    Err(e) => {
                                        //TODO: send a 400 response
                                        error!("request error = {:?}", e);
                                        return;
                                    }
                                };

                                keep_alive = match (req.version, req.headers.get::<Connection>()) {
                                    (Http10, Some(conn)) if !conn.contains(&KeepAlive) => false,
                                    (Http11, Some(conn)) if conn.contains(&Close)  => false,
                                    _ => true
                                };
                                res.version = req.version;
                                handler.handle(req, res);
                                debug!("keep_alive = {:?}", keep_alive);
                            }

                        });
                    },
                    Err(ref e) if e.kind == EndOfFile => {
                        debug!("server closed");
                        break;
                    },
                    Err(e) => {
                        error!("Connection failed: {}", e);
                        continue;
                    }
                }
            }
        });

        Ok(Listening {
            acceptor: acceptor,
            guard: Some(guard),
            socket: socket,
        })
    }

    /// Binds to a socket and starts handling connections.
    pub fn listen<H: Handler>(self, handler: H) -> HttpResult<Listening<L::Acceptor>> {
        self.listen_threads(handler, os::num_cpus() * 5 / 4)
    }

}

/// A listening server, which can later be closed.
pub struct Listening<A = HttpAcceptor> {
    acceptor: A,
    guard: Option<JoinGuard<'static, ()>>,
    /// The socket addresses that the server is bound to.
    pub socket: SocketAddr,
}

impl<A: NetworkAcceptor> Listening<A> {
    /// Causes the current thread to wait for this listening to complete.
    pub fn await(&mut self) {
        if let Some(guard) = self.guard.take() {
            let _ = guard.join();
        }
    }

    /// Stop the server from listening to its socket address.
    pub fn close(&mut self) -> HttpResult<()> {
        debug!("closing server");
        try!(self.acceptor.close());
        Ok(())
    }
}

/// A handler that can handle incoming requests for a server.
pub trait Handler: Sync + Send {
    /// Receives a `Request`/`Response` pair, and should perform some action on them.
    ///
    /// This could reading from the request, and writing to the response.
    fn handle(&self, Request, Response<Fresh>);
}

impl<F> Handler for F where F: Fn(Request, Response<Fresh>), F: Sync + Send {
    fn handle(&self, req: Request, res: Response<Fresh>) {
        (*self)(req, res)
    }
}