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    fn from(response: &[u8]) -> Self::OutputText
17    where
18        Self: Sized,
19    {
20        <HttpResponseBinary as ResponseTrait>::from(response).text()
21    }
22
23    fn text(&self) -> Self::OutputText {
24        self.clone()
25    }
26
27    fn binary(&self) -> HttpResponseBinary {
28        let body: Vec<u8> = self
29            .body
30            .read()
31            .map_or(Vec::new(), |body| body.clone().into_bytes());
32        HttpResponseBinary {
33            http_version: self.http_version.clone(),
34            status_code: self.status_code,
35            status_text: self.status_text.clone(),
36            headers: self.headers.clone(),
37            body: Arc::new(RwLock::new(body)),
38        }
39    }
40
41    fn decode(&self, buffer_size: usize) -> HttpResponseBinary {
42        let http_response: HttpResponseText = self.clone();
43        let tmp_body: Vec<u8> = self
44            .body
45            .read()
46            .map_or(Vec::new(), |body| body.as_bytes().to_vec())
47            .to_vec();
48        let headers: HashMapXxHash3_64<String, String> = self
49            .headers
50            .read()
51            .map_or(hash_map_xx_hash3_64(), |headers| headers.clone());
52        let body: Vec<u8> = Compress::from(&headers)
53            .decode(&tmp_body, buffer_size)
54            .into_owned();
55        HttpResponseBinary {
56            http_version: http_response.http_version,
57            status_code: http_response.status_code,
58            status_text: http_response.status_text,
59            headers: http_response.headers,
60            body: Arc::new(RwLock::new(body)),
61        }
62    }
63}
64
65impl HttpResponseText {
66    /// Retrieves the HTTP version associated with this response.
67    ///
68    /// # Returns
69    /// - `HttpVersion`: The HTTP version (e.g., HTTP/1.1, HTTP/2, etc.) used for the response.
70    pub fn get_http_version(&self) -> HttpVersion {
71        if let Ok(http_version) = self.http_version.read() {
72            return http_version
73                .to_string()
74                .parse::<HttpVersion>()
75                .unwrap_or_default();
76        }
77        return HttpVersion::default();
78    }
79
80    /// Retrieves the HTTP status code associated with this response.
81    ///
82    /// # Returns
83    /// - `ResponseStatusCode`: The HTTP status code as a usize (e.g., 200 for OK, 404 for Not Found).
84    pub fn get_status_code(&self) -> ResponseStatusCode {
85        self.status_code
86    }
87
88    /// Retrieves the status text associated with the HTTP status code.
89    ///
90    /// # Returns
91    /// - `String`: The human-readable status text (e.g., "OK" for status code 200, "Not Found" for status code 404).
92    pub fn get_status_text(&self) -> String {
93        if let Ok(status_text) = self.status_text.read() {
94            return status_text.to_string();
95        }
96        return HttpStatus::default().to_string();
97    }
98
99    /// Retrieves the headers of the HTTP response.
100    ///
101    /// # Returns
102    /// - `ResponseHeaders`: A map of header names and their corresponding values as key-value pairs.
103    pub fn get_headers(&self) -> ResponseHeaders {
104        if let Ok(headers) = self.headers.read() {
105            return headers.clone();
106        }
107        return hash_map_xx_hash3_64();
108    }
109
110    /// Retrieves the body content of the HTTP response as a `String`.
111    ///
112    /// This method attempts to read the body of the response. If the body can be successfully read,
113    /// it is converted into a `String` and returned. If reading the body fails, an empty string is returned.
114    ///
115    /// # Returns
116    /// - `RequestBodyString`: The body of the response as a string. If the body could not be read,
117    ///   an empty string is returned.
118    pub fn get_body(&self) -> RequestBodyString {
119        if let Ok(body) = self.body.read() {
120            return body.to_string();
121        }
122        return RequestBodyString::new();
123    }
124}
125
126impl Default for HttpResponseText {
127    fn default() -> Self {
128        Self {
129            http_version: Arc::new(RwLock::new(HttpVersion::Unknown(String::new()))),
130            status_code: HttpStatus::Unknown.code(),
131            status_text: Arc::new(RwLock::new(HttpStatus::Unknown.to_string())),
132            headers: Arc::new(RwLock::new(hash_map_xx_hash3_64())),
133            body: Arc::new(RwLock::new(String::new())),
134        }
135    }
136}