http_type/response/
impl.rs

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
use super::{error::Error, r#type::Response};
use crate::{CloseStreamResult, ResponseData, ResponseResult, StatusCode};
use http_compress::*;
use http_constant::*;
use std::{borrow::Cow, collections::HashMap, io::Write, net::TcpStream};
use tokio::io::AsyncWriteExt;
use tokio::net::TcpStream as TokioTcpStream;

impl Default for Response {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

impl Response {
    /// Creates a new instance of `Response`.
    ///
    /// # Returns
    /// - An initialized `Response` with default values.
    #[inline]
    pub fn new() -> Self {
        Response {
            version: HTTP_VERSION_1_1.to_owned(),
            status_code: 200,
            reason_phrase: EMPTY_STR.to_owned(),
            headers: HashMap::new(),
            body: Vec::new(),
            response: Vec::new(),
        }
    }

    /// Adds a header to the response.
    ///
    /// This function inserts a key-value pair into the response headers.
    /// The key and value are converted into `String`, allowing for efficient handling of both owned and borrowed string data.
    ///
    /// # Parameters
    /// - `key`: The header key, which will be converted into a `String`.
    /// - `value`: The value of the header, which will be converted into a `String`.
    ///
    /// # Returns
    /// - Returns a mutable reference to the current instance (`&mut Self`), allowing for method chaining.
    #[inline]
    pub fn set_header<K, V>(&mut self, key: K, value: V) -> &mut Self
    where
        K: Into<String>,
        V: Into<String>,
    {
        self.headers.insert(key.into(), value.into());
        self
    }

    /// Pushes a header with a key and value into the response string.
    ///
    /// # Parameters
    /// - `response_string`: A mutable reference to the string where the header will be added.
    /// - `key`: The header key as a string slice (`&str`).
    /// - `value`: The header value as a string slice (`&str`).
    pub(super) fn push_header(response_string: &mut String, key: &str, value: &str) {
        response_string.push_str(&format!("{}{}{}{}", key, COLON_SPACE, value, HTTP_BR));
    }

    /// Pushes the first line of an HTTP response (version, status code, and reason phrase) into the response string.
    /// This corresponds to the status line of the HTTP response.
    ///
    /// # Parameters
    /// - `response_string`: A mutable reference to the string where the first line will be added.
    pub(super) fn push_http_response_first_line(&self, response_string: &mut String) {
        response_string.push_str(&format!(
            "{}{}{}{}{}{}",
            self.get_version(),
            SPACE,
            self.get_status_code(),
            SPACE,
            self.get_reason_phrase(),
            HTTP_BR
        ));
    }

    /// Builds the full HTTP response as a byte vector.
    ///
    /// # Returns
    /// - The serialized HTTP response including headers and body.
    #[inline]
    pub fn build(&mut self) -> ResponseData {
        if self.reason_phrase.is_empty() {
            self.set_reason_phrase(StatusCode::phrase(*self.get_status_code()).into());
        }
        let mut response_string: String = String::new();
        self.push_http_response_first_line(&mut response_string);
        let mut compress_type_opt: Option<Compress> = None;
        let mut connection_opt: Option<&str> = None;
        let mut content_type_opt: Option<&str> = None;
        for (key, value) in self.get_headers() {
            if key == CONTENT_LENGTH {
                continue;
            } else if key == CONTENT_ENCODING {
                compress_type_opt = Some(value.parse::<Compress>().unwrap_or_default());
            } else if key == CONNECTION {
                connection_opt = Some(value);
            } else if key == CONTENT_TYPE {
                content_type_opt = Some(value);
            }
            Self::push_header(&mut response_string, key, &value);
        }
        if connection_opt.is_none() {
            Self::push_header(&mut response_string, CONNECTION, CONNECTION_KEEP_ALIVE);
        }
        if content_type_opt.is_none() {
            Self::push_header(
                &mut response_string,
                CONTENT_TYPE,
                &format!("{}{}{}", TEXT_HTML, SEMICOLON_SPACE, CHARSET_UTF_8),
            );
        }
        let mut body: Cow<Vec<u8>> = Cow::Borrowed(self.get_body());
        if let Some(compress_type) = compress_type_opt {
            if !compress_type.is_unknown() {
                let tmp_body: Cow<'_, Vec<u8>> = compress_type.encode(&body, DEFAULT_BUFFER_SIZE);
                body = Cow::Owned(tmp_body.into_owned());
            }
        }
        let len_string: String = body.len().to_string();
        let len_str: &str = len_string.as_str();
        Self::push_header(&mut response_string, CONTENT_LENGTH, len_str);
        response_string.push_str(HTTP_BR);
        let mut response_bytes: Vec<u8> = response_string.into_bytes();
        response_bytes.extend_from_slice(&body);
        self.set_response(response_bytes.clone());
        response_bytes
    }

    /// Sends the HTTP response body over a TCP stream.
    ///
    /// # Parameters
    /// - `stream`: A mutable reference to the `TcpStream` to send the response.
    ///
    /// # Returns
    /// - `Ok`: If the response body is successfully sent.
    /// - `Err`: If an error occurs during sending.
    #[inline]
    pub fn send_body(&mut self, mut stream: &TcpStream) -> ResponseResult {
        let send_res: ResponseResult = stream
            .write_all(self.get_body())
            .and_then(|_| stream.flush())
            .map_err(|err| Error::ResponseError(err.to_string()))
            .and_then(|_| Ok(()));
        send_res
    }

    /// Closes the stream after sending the response.
    ///
    /// This function is responsible for:
    /// - Building the response using the `build()` method.
    /// - Setting the response using the `set_response()` method.
    /// - Shutting down the write half of the TCP stream to indicate no more data will be sent.
    ///
    /// # Parameters
    /// - `stream`: A reference to the `TcpStream` that will be closed after sending the response.
    ///
    /// # Returns
    /// - `CloseStreamResult`: The result of the operation, indicating whether the closure was successful or if an error occurred.
    #[inline]
    pub fn close(&mut self, stream: &TcpStream) -> CloseStreamResult {
        let _ = stream
            .shutdown(std::net::Shutdown::Both)
            .map_err(|err| Error::ResponseError(err.to_string()))?;
        Ok(())
    }

    /// Sends the HTTP response over a TCP stream.
    ///
    /// # Parameters
    /// - `stream`: A mutable reference to the `TcpStream` to send the response.
    ///
    /// # Returns
    /// - `Ok`: If the response is successfully sent.
    /// - `Err`: If an error occurs during sending.
    #[inline]
    pub fn send(&mut self, mut stream: &TcpStream) -> ResponseResult {
        self.build();
        stream
            .write_all(self.get_response())
            .and_then(|_| stream.flush())
            .map_err(|err| Error::ResponseError(err.to_string()))?;
        Ok(())
    }

    /// Sends the HTTP response body over a TCP stream.
    ///
    /// # Parameters
    /// - `stream`: A mutable reference to the `TcpStream` to send the response.
    ///
    /// # Returns
    /// - `Ok`: If the response body is successfully sent.
    /// - `Err`: If an error occurs during sending.
    #[inline]
    pub async fn async_send_body(&mut self, stream: &mut TokioTcpStream) -> ResponseResult {
        stream
            .write_all(self.get_body())
            .await
            .map_err(|err| Error::ResponseError(err.to_string()))?;
        stream
            .flush()
            .await
            .map_err(|err| Error::ResponseError(err.to_string()))?;
        Ok(())
    }

    /// Closes the stream after sending the response.
    ///
    /// This function is responsible for:
    /// - Building the response using the `build()` method.
    /// - Setting the response using the `set_response()` method.
    /// - Shutting down the write half of the TCP stream to indicate no more data will be sent.
    ///
    /// # Parameters
    /// - `stream`: A reference to the `TcpStream` that will be closed after sending the response.
    ///
    /// # Returns
    /// - `CloseStreamResult`: The result of the operation, indicating whether the closure was successful or if an error occurred.
    #[inline]
    pub fn async_close(&mut self, stream: &mut TokioTcpStream) -> CloseStreamResult {
        let _ = stream.shutdown();
        Ok(())
    }

    /// Sends the HTTP response over a TCP stream.
    ///
    /// # Parameters
    /// - `stream`: A mutable reference to the `TcpStream` to send the response.
    ///
    /// # Returns
    /// - `Ok`: If the response is successfully sent.
    /// - `Err`: If an error occurs during sending.
    #[inline]
    pub async fn async_send(&mut self, stream: &mut TokioTcpStream) -> ResponseResult {
        self.build();
        stream
            .write_all(&self.get_response())
            .await
            .map_err(|err| Error::ResponseError(err.to_string()))?;
        stream
            .flush()
            .await
            .map_err(|err| Error::ResponseError(err.to_string()))?;
        Ok(())
    }
}