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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
pub mod handler;
pub mod encoding;

use std::{collections::HashMap, env, ffi::OsStr, fmt::Display, hash::Hash, io::{BufRead, BufReader, Read, Write}, net::TcpStream, path::Path, str::FromStr};
use crate::{Result, ServerError};
use crate::request::encoding::Chunked;

/// Request Method
///
/// Represents the method of the HTTP request
#[derive(Debug,Eq,Hash,PartialEq,Clone,Copy)]
pub enum RequestMethod {
    GET, POST, PUT, DELETE,
    HEAD, PATCH, CONNECT,
    OPTIONS, TRACE,
}

impl FromStr for RequestMethod {
    type Err = ServerError;
    fn from_str(t: &str) -> Result<Self> {
        match t {
            "GET" => Ok(Self::GET),
            "POST" => Ok(Self::POST),
            "PUT" => Ok(Self::PUT),
            "DELETE" => Ok(Self::DELETE),
            "HEAD" => Ok(Self::HEAD),
            "PATCH" => Ok(Self::PATCH),
            "CONNECT" => Ok(Self::CONNECT),
            "OPTIONS" => Ok(Self::OPTIONS),
            "TRACE" => Ok(Self::TRACE),
            _ => ServerError::from_string(format!("Couldn't parse request method ({t})")).err()
        }
    }
}

impl Display for RequestMethod {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self)
    }
}

/// HTTP Request
///
/// Represents an HTTP request
pub struct HttpRequest {
    method: RequestMethod,
    url: String,
    headers: HashMap<String,String>,
    params: HashMap<String,String>,
    response_headers: HashMap<String,String>,
    version: f32,
    stream: BufReader<TcpStream>,
    status: u16,
}

fn parse_request(mut stream: BufReader<TcpStream>) -> Result<HttpRequest> {
    let mut line = String::new();
    /* Parse request line */
    stream.read_line(&mut line)?;
    let mut space = line.split_whitespace().take(3);
    let method = space.next().unwrap_or("").parse()?;
    let mut url = space.next().unwrap();
    let mut params = HashMap::new();
    if url.contains("?") {
        /* Parse URL */
        let mut split = url.split("?");
        let new_url = split.next().unwrap();
        let query = split.next().unwrap_or("");
        for arg in query.split("&") {
            let mut arg = arg.split("=");
            let k = arg.next().unwrap_or("");
            let v = arg.next().unwrap_or("");
            let k = url::decode(k)?.into_owned();
            let v = url::decode(v)?.into_owned();
            params.insert(k, v);
        }
        url = new_url;
    }
    let url = url::decode(&url)?.into_owned();
    let version: f32 = space.next().unwrap()
                           .replace("HTTP/", "")
                           .parse()
                           .or_else(|_| ServerError::from_str("Could not parse HTTP Version").err())?;
    line.clear();
    /* Parse Headers */
    let mut headers = HashMap::new();
    while let Ok(_) = stream.read_line(&mut line) {
        if line == "\r\n" { break; }
        let mut splt = line.split(":");
        let key = splt.next().unwrap_or("").to_string();
        let value = splt.next().unwrap_or("")
                        .strip_prefix(" ").unwrap_or("")
                        .strip_suffix("\r\n").unwrap_or("")
                        .to_string();
        headers.insert(key, value);
        line.clear();
    }
    let response_headers = HashMap::new();
    Ok(HttpRequest { method, url, headers, params, response_headers, version, stream, status:200 })
}

impl HttpRequest {
    /// Read and parse an HTTP request from the given [TcpStream]
    pub fn parse(stream: TcpStream) -> Result<Self>  {
        let stream = BufReader::new(stream);
        parse_request(stream)
    }
    #[inline]
    pub fn keep_alive(self) -> Result<Self> {
        parse_request(self.stream)
    }
    #[inline]
    pub fn stream(&self) -> &TcpStream { self.stream.get_ref() }
    /// Url of the request
    #[inline]
    pub fn url(&self) -> &str { &self.url }
    #[inline]
    pub fn set_url(&mut self, url: String) { self.url = url; }
    /// Get the query parameters
    #[inline]
    pub fn params(&self) -> &HashMap<String,String> { &self.params }
    #[inline]
    pub fn param(&self, key: &str) -> Option<&str> { self.params.get(key).map(|s| s.as_str()) }
    /// Get the filename for the request
    ///
    /// It computes the path in the server corresponding to the
    /// request's url.
    ///
    pub fn filename(&self) -> Result<String> {
        let mut cwd = env::current_dir()?;
        cwd.push(
            Path::new(
                OsStr::new(&self.url[1..])
            )
        );
        let cwd = cwd.to_str().ok_or_else(|| ServerError::from_str("Error getting cwd"))?;
        Ok(cwd.to_owned())
    }
    #[inline]
    pub fn method(&self) -> &RequestMethod { &self.method }
    #[inline]
    pub fn status(&self) -> u16 { self.status }
    #[inline]
    pub fn set_status(&mut self, status: u16) -> &mut Self {
        self.status = status;
        self
    }
    #[inline]
    pub fn version(&self) -> f32 { self.version }
    /// Get a human-readable description of the request's status code
    pub fn status_msg(&self) -> &'static str {
        match self.status {
            200 => "OK",
            201 => "CREATED",
            202 => "ACCEPTED",
            203 => "NON-AUTHORITATIVE INFORMATION",
            204 => "NO CONTENT",
            205 => "RESET CONTENT",
            206 => "PARTIAL CONTENT",
            300 => "MULTIPLE CHOICES",
            301 => "MOVED PERMANENTLY",
            302 => "FOUND",
            303 => "SEE OTHER",
            304 => "NOT MODIFIED",
            307 => "TEMPORARY REDIRECT",
            308 => "PERMANENT REDIRECT",
            400 => "BAD REQUEST",
            401 => "UNAUTHORIZED",
            403 => "FORBIDDEN",
            404 => "NOT FOUND",
            405 => "METHOD NOT ALLOWED",
            406 => "NOT ACCEPTABLE",
            407 => "PROXY AUTHENTICATION REQUIRED",
            408 => "REQUEST TIMEOUT",
            409 => "CONFLICT",
            501 => "NOT IMPLEMENTED",
            500 => "INTERNAL SERVER ERROR",
            _ => "?"
        }
    }
    /// Get the value of the *Content-Length* HTTP header
    ///
    /// If the header is not present, or if it fails to parse
    /// it's value, it returns 0
    pub fn content_length(&self) -> usize {
        match self.headers.get("Content-Length") {
            Some(l) => l.parse().unwrap_or(0),
            None => 0,
        }
    }
    /// Get the value of the given header key, if present
    #[inline]
    pub fn header(&self, key: &str) -> Option<&String> {
        self.headers.get(key)
    }
    #[inline]
    pub fn headers(&self) -> &HashMap<String,String> { &self.headers }
    #[inline]
    pub fn set_header<V: ToString>(&mut self, key: &str, value: V) {
        self.response_headers.insert(key.to_string(), value.to_string());
    }
    pub fn data(&mut self) -> Vec<u8> {
        let len = self.content_length();
        let mut buf:Vec<u8> = Vec::with_capacity(len);
        buf.resize(len, 0);
        self.stream.read_exact(&mut buf).unwrap();
        buf
    }
    pub fn read_data(&mut self, writer: &mut dyn Write) -> Result<()> {
        const CHUNK_SIZE:usize = 1024 * 1024;
        let mut buf:[u8;CHUNK_SIZE] = [0;CHUNK_SIZE];
        let len = self.content_length();
        let n = len / CHUNK_SIZE;
        let remainder = len % CHUNK_SIZE;

        for _ in 0..n {
            self.stream.read_exact(&mut buf)?;
            writer.write_all(&buf)?;
        }

        if remainder > 0 {
            self.stream.read_exact(&mut buf[0..remainder])?;
            writer.write_all(&buf[0..remainder])?;
        }

        Ok(())
    }
    /// Respond to the request without a body
    pub fn respond(&mut self) -> Result<()> {
        let response_line = format!("HTTP/{} {} {}\r\n", self.version, self.status, self.status_msg());
        self.stream.get_mut().write_all(response_line.as_bytes())?;
        let stream = self.stream.get_mut();
        for (k,v) in &self.response_headers {
           stream.write_all(k.as_bytes())?;
           stream.write_all(b": ")?;
           stream.write_all(v.as_bytes())?;
           stream.write_all(b"\r\n")?;
        }
        stream.write_all(b"\r\n")?;
        Ok(())
    }
    /// Respond to the request with the data of buf as a body
    pub fn respond_buf(&mut self, mut buf: &[u8]) -> Result<()> {
        self.set_header("Content-Length", buf.len());
        self.respond_reader(&mut buf)
    }
    /// Respond to the request with the data read from reader as a body
    pub fn respond_reader(&mut self, reader: &mut dyn Read) -> Result<()> {
        self.respond()?;
        const CHUNK_SIZE: usize = 1024 * 1024;
        let mut buf: [u8; CHUNK_SIZE] = [0; CHUNK_SIZE];

        let stream = self.stream.get_mut();
        while let Ok(n) = reader.read(&mut buf) {
            if n == 0 { break; }
            stream.write_all(&buf[0..n])?;
        }
        Ok(())
    }
    /// Respond to the request as a chunked transfer
    ///
    /// This means that the Content-Length of the request doen't need to be known.
    pub fn respond_chunked(&mut self, reader: &mut dyn Read) -> Result<()> {
        self.set_header("Transfer-Encoding", "chunked");
        let mut reader = Chunked::new(reader);
        self.respond_reader(&mut reader)
    }
    /// Respond with a basic HTML error page
    pub fn respond_error_page(&mut self) -> Result<()> {
        let mut buf = self.error_page();
        self.respond_buf(&mut buf)
    }
    /// Respond to the request with an 200 OK status
    #[inline]
    pub fn ok(&mut self) -> Result<()> {
        self.set_status(200).respond()
    }
    /// Respond to the request with an 403 FORBIDDEN status
    #[inline]
    pub fn forbidden(&mut self) -> Result<()> {
        self.set_status(403).respond_error_page()
    }
    /// Respond to the request with an 404 NOT FOUND status
    #[inline]
    pub fn not_found(&mut self) -> Result<()> {
        self.set_status(404).respond_error_page()
    }
    /// Respond to the request with an 500 INTERNAL SERVER ERROR status
    #[inline]
    pub fn server_error(&mut self) -> Result<()> {
        self.set_status(500).respond_error_page()
    }
    /// Returns a basic HTML error page of the given status
    pub fn error_page(&mut self) -> Vec<u8> {
        let code = self.status;
        let msg = self.status_msg();
        format!(
"<!DOCTYPE html>
<html lang=\"en\">
    <head>
        <meta charset=\"utf-8\">
        <title>{code} {msg}</title>
    </head>
<body>
    <h1>{code} {msg}</h1>
</body>
</html>").as_bytes().to_vec()
    }
}

#[cfg(test)]
mod test {
    use std::str::FromStr;

    use crate::request::RequestMethod::{self,*};

    #[test]
    fn parse_method() {
        assert!(RequestMethod::from_str("unknown").is_err());
        let strs = vec!["GET","POST","PUT","DELETE"];
        let methods = vec![GET,POST,PUT,DELETE];
        let res:Vec<RequestMethod> =
            strs.iter()
            .map(|m| RequestMethod::from_str(m))
            .map(Result::unwrap).collect();
        assert_eq!(methods,res);
    }
}