aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib.rs
blob: a8f55a50cbaa185e3464d3fa041870ab21342d91 (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
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
extern crate conduit;
extern crate fastcgi;
extern crate http;

use std::io;
use std::io::{BufReader, Write};

use conduit::Handler;

use inflector::cases::traincase::to_train_case;

use snafu::{ResultExt, Snafu};


#[derive(Debug, Snafu)]
pub enum RequestError {
    #[snafu(display("{}", source))]
    InvalidMethod { source: http::method::InvalidMethod },

    #[snafu(display("{}", source))]
    InvalidHeaderName { source: conduit::header::InvalidHeaderName },

    #[snafu(display("{}", source))]
    InvalidHeaderValue { source: conduit::header::InvalidHeaderValue },
}

pub type RequestResult<T, E = RequestError> = std::result::Result<T, E>;


struct FastCgiRequest<'a> {
    request: &'a fastcgi::Request,
    method: conduit::Method,
    headers: conduit::HeaderMap,
}

impl<'a> FastCgiRequest<'a> {
    pub fn new(request: &'a fastcgi::Request) -> RequestResult<Self> {
        let method = Self::method(request)
            .context(InvalidMethod)?;

        let headers = Self::headers(request.params())?;

        let r = Self {
            request: request,
            method: method,
            headers: headers,
        };

        Ok(r)
    }

    fn method(
        request: &'a fastcgi::Request
    ) -> Result<conduit::Method, http::method::InvalidMethod> {
        conduit::Method::from_bytes(
            request.param("REQUEST_METHOD")
                .unwrap_or_default()
                .as_bytes()
        )
    }

    fn headers(params: fastcgi::Params) -> RequestResult<conduit::HeaderMap> {
        let mut map = conduit::HeaderMap::new();
        let headers = Self::headers_from_params(params);

        for (name, value) in headers
            .iter()
            .map(|(name, value)| (name.as_bytes(), value.as_bytes()))
        {
            map.append(
                conduit::header::HeaderName::from_bytes(name)
                    .context(InvalidHeaderName)?,
                conduit::header::HeaderValue::from_bytes(value)
                    .context(InvalidHeaderValue)?,
            );
        }

        Ok(map)
    }

    fn headers_from_params(params: fastcgi::Params) -> Vec<(String, String)> {
        return params
            .filter(|(key, _)| key.starts_with("HTTP_"))
            .map(|(key, value)| {
                let key = key.get(5..).unwrap_or_default();
                let key = &key.replace("_", "-");
                let key = &to_train_case(&key);

                (key.to_owned(), value)
            })
            .collect()
    }
}

// impl<'a> conduit::RequestExt for FastCgiRequest {
//    fn http_version(&self) -> conduit::Version { todo!() }
//    fn method(&self) -> &conduit::Method {
//        self.method
//    }
//    fn scheme(&self) -> conduit::Scheme { todo!() }
//    fn host(&'a self) -> conduit::Host<'a> { todo!() }
//    fn virtual_root(&'a self) -> std::option::Option<&'a str> { todo!() }
//    fn path(&'a self) -> &'a str { todo!() }
//    fn query_string(&'a self) -> std::option::Option<&'a str> { todo!() }
//    fn remote_addr(&self) -> std::net::SocketAddr { todo!() }
//    fn content_length(&self) -> std::option::Option<u64> { todo!() }
//    fn headers(&self) -> &conduit::HeaderMap { todo!() }
//    fn body(&'a mut self) -> &'a mut (dyn std::io::Read + 'a) { todo!() }
//    fn extensions(&'a self) -> &'a conduit::TypeMap { todo!() }
//    fn mut_extensions(&'a mut self) -> &'a mut conduit::TypeMap { todo!() }
// }


struct Server;

impl Server {
    pub fn start<H: Handler + 'static + Sync>(handler: H) -> io::Result<Server> {
        Ok(Server{})
    }
}