http_request/response/response_text/
impl.rs1use crate::*;
2
3impl 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 #[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 #[inline]
91 pub fn get_status_code(&self) -> StatusCodeUsize {
92 self.status_code
93 }
94
95 #[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 #[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 #[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}