blob: dad68132c69f544bbe34a3c12aae1c0519662d6d (
plain)
| 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
 | use std::io;
use std::io::Write;
use conduit::Handler;
use crate::request::FastCgiRequest;
pub struct Server;
impl Server {
    pub fn start<H: Handler + 'static + Sync>(handler: H) -> io::Result<Server> {
        fastcgi::run(move |mut raw_request| {
            let mut request = FastCgiRequest::new(&mut raw_request).unwrap();
            let response = handler.call(&mut request);
            let mut stdout = raw_request.stdout();
            let (head, body) = response.unwrap().into_parts();
            write!(
                &mut stdout,
                "HTTP/1.1 {} {}\r\n",
                head.status.as_str(),
                head.status.canonical_reason().unwrap_or("UNKNOWN"),
            );
            for (name, value) in head.headers.iter() {
                write!(&mut stdout, "{}: ", name).unwrap();
                stdout.write(value.as_bytes()).unwrap();
                stdout.write(b"\r\n").unwrap();
            }
            stdout.write(b"\r\n").unwrap();
            match body {
                conduit::Body::Static(slice) => stdout.write(slice).map(|_| ()).unwrap(),
                conduit::Body::Owned(vec) => stdout.write(&vec).map(|_| ()).unwrap(),
                conduit::Body::File(mut file) => io::copy(&mut file, &mut stdout).map(|_| ()).unwrap(),
            };
        });
        Ok(Server{})
    }
}
 |