http_request/response/response_text/
impl.rs1use crate::*;
2
3impl 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 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 pub fn get_status_code(&self) -> ResponseStatusCode {
85 self.status_code
86 }
87
88 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 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 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}