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