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