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
use std::fmt;

const PROTOCOL : &str= "HTTP/1.1";

pub struct HTTPResponse {
    code: i32,
    acces_control: String,
    content_type : String,
    body: String,
    headers: Vec<String>
}

impl HTTPResponse {
    fn new(code: i32,headers: Vec<String>,  body: Option<&str>) -> HTTPResponse {
        return HTTPResponse {
            code: code, 
            headers: headers,  
            body: String::from(body.unwrap_or("")), acces_control: "*".to_string(), content_type: "application/json".to_string()};
    }    
}


impl From<Response> for HTTPResponse {
    fn from(response: Response) -> Self {
        HTTPResponse::new(response.code,Vec::new() ,response.body.as_deref())
    }
}


impl From<Vec<String>> for HTTPResponse {
    fn from(headers: Vec<String>) -> Self {
        return HTTPResponse::new(200, headers, None);
    }
}

fn construct_status_line(code : i32) -> String {
    format!("{} {} {}", PROTOCOL, code, message_from_code(code))
}

fn message_from_code(code : i32) -> String {
    match code {
        200 =>  String::from("OK"),
        500 => "INTERNAL".to_string(),
        404 => "NOT FOUND".to_string(),
        400 => "MALFORMED".to_string(),
        _  => "".to_string()
    }
}

impl fmt::Display for HTTPResponse {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

        return write!(f,
            "{}\r\n{}Access-Control-Allow-Origin: {}\r\nContent-Type: {}\r\nContent-Length: {}\r\n\r\n{}",
            construct_status_line(self.code),
            self.headers.join("\r\n"),
            self.acces_control,
            self.content_type,
            self.body.len(),
            self.body
        );
    }
}



pub struct Response {
    pub code: i32,
    pub headers: Vec<String>,
    pub body: Option<String>
}


impl Response {
    fn new(code: i32,headers: Vec<String>,  body: Option<String>) -> Response {
        return Response {code: code, headers: headers, body: body};
    }    
}


impl From<i32> for Response {
    fn from(code: i32) -> Self {
        return Response::new(code, Vec::new(), None);
    }
}


impl From<(i32, &str)> for Response {
    fn from(code: (i32, &str)) -> Self {
        return Response::new(code.0, Vec::new(),Some(code.1.to_string()));
    }
}

impl From<Vec<String>> for Response {
    fn from(headers: Vec<String>) -> Self {
        return Response::new(200, headers, None);
    }
}

impl From<(i32, String)> for Response {
    fn from(code: (i32, String)) -> Self {
        return Response::new(code.0, Vec::new(),Some(code.1));
    }
}