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 ///
128 /// Gets the HTTP status code of the response.
129 ///
130 /// # Returns
131 ///
132 /// - `ResponseStatusCode` - The status code.
133 pub fn get_status_code(&self) -> ResponseStatusCode {
134 self.status_code
135 }
136
137 /// Retrieves the status text associated with the HTTP status code.
138 ///
139 /// # Returns
140 /// - `String`: The human-readable status text.
141 ///
142 /// Gets the HTTP status text of the response.
143 ///
144 /// # Returns
145 ///
146 /// - `String` - The status text.
147 pub fn get_status_text(&self) -> String {
148 if let Ok(status_text) = self.status_text.read() {
149 return status_text.to_string();
150 }
151 HttpStatus::default().to_string()
152 }
153
154 /// Retrieves the headers of the HTTP response.
155 ///
156 /// # Returns
157 /// - `ResponseHeaders`: A map of header names and their corresponding values as key-value pairs.
158 ///
159 /// Gets the HTTP response headers.
160 ///
161 /// # Returns
162 ///
163 /// - `ResponseHeaders` - The response headers.
164 pub fn get_headers(&self) -> ResponseHeaders {
165 if let Ok(headers) = self.headers.read() {
166 return headers.clone();
167 }
168 hash_map_xx_hash3_64()
169 }
170
171 /// Retrieves the body content of the HTTP response as a `String`.
172 ///
173 /// This method attempts to read the body of the response. If the body can be successfully read,
174 /// it is converted into a `String` and returned. If reading the body fails, an empty string is returned.
175 ///
176 /// # Returns
177 /// - `RequestBodyString`: The body of the response as a string. If the body could not be read,
178 /// an empty string is returned.
179 ///
180 /// Gets the HTTP response body.
181 ///
182 /// # Returns
183 ///
184 /// - `RequestBodyString` - The response body.
185 pub fn get_body(&self) -> RequestBodyString {
186 if let Ok(body) = self.body.read() {
187 return body.to_string();
188 }
189 RequestBodyString::new()
190 }
191}
192
193/// Default implementation for HttpResponseText.
194///
195/// # Returns
196///
197/// - `HttpResponseText` - Default initialized HttpResponseText.
198impl Default for HttpResponseText {
199 fn default() -> Self {
200 Self {
201 http_version: Arc::new(RwLock::new(HttpVersion::Unknown(String::new()))),
202 status_code: HttpStatus::Unknown.code(),
203 status_text: Arc::new(RwLock::new(HttpStatus::Unknown.to_string())),
204 headers: Arc::new(RwLock::new(hash_map_xx_hash3_64())),
205 body: Arc::new(RwLock::new(String::new())),
206 }
207 }
208}