http_request/response/response_text/
impl.rs

1use crate::*;
2
3/// Implements the `ResponseTrait` trait for `HttpResponseText`.
4///
5/// This implementation allows `HttpResponseText` to convert between text and binary
6/// representations of HTTP responses. It provides methods for parsing raw responses, as well
7/// as accessing text and binary formats.
8///
9/// # Associated Types
10/// - `OutputText`: Specifies the text representation of an HTTP response (`HttpResponseText`).
11/// - `OutputBinary`: Specifies the binary representation of an HTTP response (`HttpResponseBinary`).
12impl ResponseTrait for HttpResponseText {
13    type OutputText = HttpResponseText;
14    type OutputBinary = HttpResponseBinary;
15
16    /// Creates a new HttpResponseText from raw response bytes.
17    ///
18    /// # Arguments
19    ///
20    /// - `&[u8]` - The raw HTTP response bytes.
21    ///
22    /// # Returns
23    ///
24    /// - `Self::OutputText` - The parsed HttpResponseText.
25    fn from(response: &[u8]) -> Self::OutputText
26    where
27        Self: Sized,
28    {
29        <HttpResponseBinary as ResponseTrait>::from(response).text()
30    }
31
32    /// Converts the response to text format.
33    ///
34    /// # Returns
35    ///
36    /// - `Self::OutputText` - The text representation of the response.
37    fn text(&self) -> Self::OutputText {
38        self.clone()
39    }
40
41    /// Converts the response to binary format.
42    ///
43    /// # Returns
44    ///
45    /// - `HttpResponseBinary` - The binary representation of the response.
46    fn binary(&self) -> HttpResponseBinary {
47        let body: Vec<u8> = self
48            .body
49            .read()
50            .map_or(Vec::new(), |body| body.clone().into_bytes());
51        HttpResponseBinary {
52            http_version: self.http_version.clone(),
53            status_code: self.status_code,
54            status_text: self.status_text.clone(),
55            headers: self.headers.clone(),
56            body: Arc::new(RwLock::new(body)),
57        }
58    }
59
60    /// Decodes the response body using the specified buffer size.
61    ///
62    /// # Arguments
63    ///
64    /// - `usize` - The buffer size for decoding.
65    ///
66    /// # Returns
67    ///
68    /// - `HttpResponseBinary` - The decoded binary response.
69    fn decode(&self, buffer_size: usize) -> HttpResponseBinary {
70        let http_response: HttpResponseText = self.clone();
71        let tmp_body: Vec<u8> = self
72            .body
73            .read()
74            .map_or(Vec::new(), |body| body.as_bytes().to_vec())
75            .to_vec();
76        let headers: HashMapXxHash3_64<String, String> =
77            self.headers
78                .read()
79                .map_or(hash_map_xx_hash3_64(), |headers_ref| {
80                    let mut string_headers: HashMapXxHash3_64<String, String> =
81                        hash_map_xx_hash3_64();
82                    for (key, value_deque) in headers_ref.iter() {
83                        if let Some(first_value) = value_deque.front() {
84                            string_headers.insert(key.clone(), first_value.clone());
85                        }
86                    }
87                    string_headers
88                });
89        let body: Vec<u8> = Compress::from(&headers)
90            .decode(&tmp_body, buffer_size)
91            .into_owned();
92        HttpResponseBinary {
93            http_version: http_response.http_version,
94            status_code: http_response.status_code,
95            status_text: http_response.status_text,
96            headers: http_response.headers,
97            body: Arc::new(RwLock::new(body)),
98        }
99    }
100}
101
102impl HttpResponseText {
103    /// Retrieves the HTTP version associated with this response.
104    ///
105    /// # Returns
106    /// - `HttpVersion`: The HTTP version used for the response.
107    ///
108    /// Gets the HTTP version of the response.
109    ///
110    /// # Returns
111    ///
112    /// - `HttpVersion` - The HTTP version.
113    pub fn get_http_version(&self) -> HttpVersion {
114        if let Ok(http_version) = self.http_version.read() {
115            return http_version
116                .to_string()
117                .parse::<HttpVersion>()
118                .unwrap_or_default();
119        }
120        HttpVersion::default()
121    }
122
123    /// Retrieves the HTTP status code associated with this response.
124    ///
125    /// # Returns
126    /// - `ResponseStatusCode`: The HTTP status code as a usize.
127    /// Gets the HTTP status code of the response.
128    ///
129    /// # Returns
130    ///
131    /// - `ResponseStatusCode` - The status code.
132    pub fn get_status_code(&self) -> ResponseStatusCode {
133        self.status_code
134    }
135
136    /// Retrieves the status text associated with the HTTP status code.
137    ///
138    /// # Returns
139    /// - `String`: The human-readable status text.
140    /// Gets the HTTP status text of the response.
141    ///
142    /// # Returns
143    ///
144    /// - `String` - The status text.
145    pub fn get_status_text(&self) -> String {
146        if let Ok(status_text) = self.status_text.read() {
147            return status_text.to_string();
148        }
149        HttpStatus::default().to_string()
150    }
151
152    /// Retrieves the headers of the HTTP response.
153    ///
154    /// # Returns
155    /// - `ResponseHeaders`: A map of header names and their corresponding values as key-value pairs.
156    /// Gets the HTTP response headers.
157    ///
158    /// # Returns
159    ///
160    /// - `ResponseHeaders` - The response headers.
161    pub fn get_headers(&self) -> ResponseHeaders {
162        if let Ok(headers) = self.headers.read() {
163            return headers.clone();
164        }
165        hash_map_xx_hash3_64()
166    }
167
168    /// Retrieves the body content of the HTTP response as a `String`.
169    ///
170    /// This method attempts to read the body of the response. If the body can be successfully read,
171    /// it is converted into a `String` and returned. If reading the body fails, an empty string is returned.
172    ///
173    /// # Returns
174    /// - `RequestBodyString`: The body of the response as a string. If the body could not be read,
175    ///   an empty string is returned.
176    /// Gets the HTTP response body.
177    ///
178    /// # Returns
179    ///
180    /// - `RequestBodyString` - The response body.
181    pub fn get_body(&self) -> RequestBodyString {
182        if let Ok(body) = self.body.read() {
183            return body.to_string();
184        }
185        RequestBodyString::new()
186    }
187}
188
189/// Default implementation for HttpResponseText.
190///
191/// # Returns
192///
193/// - `HttpResponseText` - Default initialized HttpResponseText.
194impl Default for HttpResponseText {
195    fn default() -> Self {
196        Self {
197            http_version: Arc::new(RwLock::new(HttpVersion::Unknown(String::new()))),
198            status_code: HttpStatus::Unknown.code(),
199            status_text: Arc::new(RwLock::new(HttpStatus::Unknown.to_string())),
200            headers: Arc::new(RwLock::new(hash_map_xx_hash3_64())),
201            body: Arc::new(RwLock::new(String::new())),
202        }
203    }
204}