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

use crate::http::{Header, IntoHeader, MimeType, StatusCode, Version};
use anyhow;

pub type ResponseBody = Vec<u8>;

#[derive(Debug, PartialEq)]
pub struct Response {
    version: Version,
    status: StatusCode,
    body: ResponseBody,
    mime: MimeType,
    headers: Vec<Header>,
}

pub trait IntoResponse {
    fn into_response(self) -> Response;
}

/// All types that implent into response already get IntoResponse for free ... does that make this
/// trait redudant ?
impl<T> IntoResponse for T
where
    T: Into<Response>,
{
    fn into_response(self) -> Response {
        self.into()
    }
}

impl Response {
    pub fn new(status: StatusCode, body: ResponseBody, mime: MimeType) -> Response {
        let version = Version::V1_1;
        Response {
            status,
            mime,
            body,
            version,
            headers: vec![],
        }
    }

    pub fn error(status: StatusCode, body: ResponseBody) -> Response {
        let version = Version::V1_1;
        let mime = MimeType::PlainText;
        Response {
            status,
            body,
            version,
            mime,
            headers: vec![],
        }
    }

    pub fn set_mime(&mut self, mime: MimeType) {
        self.mime = mime;
    }

    pub fn to_send_buffer(&self) -> Vec<u8> {
        //transform response to array of bytes to be sent
        let status: &str = &self.status.to_string();
        let length = self.body.len();
        let version: &str = self.version.into();
        let content_type: String = String::from(&self.mime);
        let mut headers_string = "".to_string();
        for header in &self.headers {
            let header_string: String = String::from(header);
            headers_string.push_str(&header_string);
            headers_string.push_str("\r\n");
        }
        let mut buffer: Vec<u8> = Vec::new();
        let response = format!(
            "{version} {status}\r\n\
            Content-Length: {length}\r\n\
            Content-Type: {content_type}\r\n\
            {headers_string}\r\n"
        );
        buffer.append(&mut response.into_bytes());
        for byte in &self.body {
            buffer.push(*byte);
        }
        buffer
    }

    pub fn add_header(&mut self, value: impl IntoHeader) {
        let header = value.into_header();
        self.headers.push(header);
    }

    pub fn version(&self) -> Version {
        self.version
    }

    pub fn status(&self) -> StatusCode {
        self.status
    }

    pub fn mime(&self) -> MimeType {
        self.mime
    }
}

impl From<Vec<u8>> for Response {
    fn from(bytes: Vec<u8>) -> Self {
        Response {
            status: StatusCode::OK,
            body: bytes,
            mime: MimeType::Binary,
            version: Version::V1_1,
            headers: vec![],
        }
    }
}

impl From<String> for Response {
    fn from(string: String) -> Self {
        Response {
            status: StatusCode::OK,
            body: string.into(),
            mime: MimeType::HTML,
            version: Version::V1_1,
            headers: vec![],
        }
    }
}

impl From<&str> for Response {
    fn from(string: &str) -> Self {
        Response {
            status: StatusCode::OK,
            body: string.into(),
            mime: MimeType::HTML,
            version: Version::V1_1,
            headers: vec![],
        }
    }
}

impl From<anyhow::Error> for Response {
    fn from(value: anyhow::Error) -> Self {
        tracing::error!(?value);
        let message = "<h1>Error 500 Internal Server Error</h1>\r\n".to_string();
        Response {
            status: StatusCode::INTERNAL_SERVER_ERROR, //FIXME: make this smarter
            body: message.into(),
            mime: MimeType::HTML,
            version: Version::V1_1,
            headers: vec![],
        }
    }
}

impl From<Infallible> for Response {
    fn from(_: Infallible) -> Self {
        panic!("tried to conver from infalliable");
    }
}

impl From<Response> for String {
    fn from(response: Response) -> String {
        let status: &str = response.status.as_str();
        let length = response.body.len();
        let version: &str = response.version.into();
        let body: &str = &String::from_utf8_lossy(&response.body);
        let content_type: String = response.mime.into();
        let mut headers_string = "".to_string();
        for header in response.headers {
            let header_string: String = header.into();
            headers_string.push_str(&header_string);
            headers_string.push_str("\r\n");
        }
        let response = format!(
            "{version} {status}\r\n\
            Content-Length: {length}\r\n\
            Content-Type: {content_type}\r\n\
            {headers_string}\r\n\
            {body}"
        );
        response
    }
}

impl From<std::io::Error> for Response {
    fn from(error: std::io::Error) -> Self {
        Response {
            status: StatusCode::INTERNAL_SERVER_ERROR,
            body: error.to_string().into(),
            mime: MimeType::HTML,
            version: Version::V1_1,
            headers: vec![],
        }
    }
}

impl From<StatusCode> for Response {
    fn from(value: StatusCode) -> Self {
        let body = if let Some(reason) = value.canonical_reason() {
            reason
        } else {
            "Invalid Status Code"
        };
        let body = body.as_bytes().to_vec();
        Response {
            version: Version::V1_1,
            status: value,
            body,
            mime: MimeType::HTML,
            headers: vec![],
        }
    }
}