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
///! Module [`Request`]
use crate::http::headers::Headers;
#[allow(unused_imports)]
use napi_derive::napi;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::{io::Result, str};

/// HTTP request
#[cfg_attr(feature = "napi", napi(object))]
#[derive(Debug, Serialize, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Request {
    pub url: String,
    pub host: String,
    pub peer_addr: String,
    pub protocol: String,
    pub method: String,
    pub content_length: u32,
    pub ttl: u32,
    pub headers: Headers,
    pub body: String,
    pub error: String,
}

pub struct Socket {
    pub host: String,
    pub peer_addr: String,
    pub ttl: u32,
    pub error: String,
}

impl Request {
    pub fn new(socket: Socket, buffer: Vec<u8>) -> Result<Self> {
        let headers = Headers::from_bytes(&buffer)?;
        Ok(Request::create(socket, headers))
    }

    pub fn create(
        Socket {
            host,
            peer_addr,
            ttl,
            error,
        }: Socket,
        headers: Headers,
    ) -> Self {
        let mut content_length: u32 = 0;
        let content_length_op = Headers::get_content_length(&headers.raw);
        if let Some(val) = content_length_op {
            content_length = val;
        }
        Request {
            host,
            peer_addr,
            url: Headers::get_url(&headers.raw),
            protocol: Headers::get_protocol(&headers.raw),
            method: Headers::get_method(&headers.raw),
            content_length,
            ttl,
            body: "".to_string(),
            error,
            headers,
        }
    }

    pub fn change_host(&mut self, target: &str) -> Result<()> {
        let heads = self.headers.change_header("host", target)?;

        self.headers = heads;
        self.host = target.to_string();

        Ok(())
    }

    pub fn set_body(&mut self, body: String) {
        self.body = body;
    }
}

#[allow(dead_code)]
fn get_status(raw: &String) -> u16 {
    let reg = Regex::new(r"\d{3}").unwrap();
    let capts = reg.captures(raw.as_str());
    let mut status: u16 = 500;
    if let None = capts {
        return status;
    }
    let capts = capts.unwrap();
    let status_r = capts.get(0).unwrap().as_str().parse::<u16>();
    if let Ok(val) = status_r {
        status = val;
    }
    status
}

#[allow(dead_code)]
fn get_status_text(raw: &String) -> String {
    let reg = Regex::new(r"\d{3}[ \w\-]+").unwrap();
    let capts = reg.captures(raw.as_str());
    let mut status_text: String = "Internal Server Error".to_string();
    if let None = capts {
        return status_text;
    }
    let capts = capts.unwrap();
    status_text = capts.get(0).unwrap().as_str().to_string();
    status_text = Regex::new(r"^\d{3}\s+")
        .unwrap()
        .replace_all(&status_text, "")
        .to_string();
    status_text = Regex::new(r"^\s+")
        .unwrap()
        .replace_all(&status_text, "")
        .to_string();
    Regex::new(r"\s+$")
        .unwrap()
        .replace_all(&status_text, "")
        .to_string()
}