use crate::*;
impl ResponseTrait for HttpResponseText {
type OutputText = HttpResponseText;
type OutputBinary = HttpResponseBinary;
#[inline]
fn from(response: &[u8]) -> Self::OutputText
where
Self: Sized,
{
<HttpResponseBinary as ResponseTrait>::from(response).text()
}
#[inline]
fn text(&self) -> Self::OutputText {
self.clone()
}
#[inline]
fn binary(&self) -> HttpResponseBinary {
let body: Vec<u8> = self
.body
.read()
.map_or(Vec::new(), |body| body.clone().into_bytes());
HttpResponseBinary {
http_version: self.http_version.clone(),
status_code: self.status_code,
status_text: self.status_text.clone(),
headers: self.headers.clone(),
body: Arc::new(RwLock::new(body)),
}
}
#[inline]
fn decode(&self, buffer_size: usize) -> HttpResponseBinary {
let http_response: HttpResponseText = self.clone();
let tmp_body: Vec<u8> = self
.body
.read()
.map_or(Vec::new(), |body| body.as_bytes().to_vec())
.to_vec();
let body: Vec<u8> = Compress::from(
&self
.headers
.read()
.map_or(dash_map(), |headers| headers.clone()),
)
.decode(&tmp_body, buffer_size)
.into_owned();
HttpResponseBinary {
http_version: http_response.http_version,
status_code: http_response.status_code,
status_text: http_response.status_text,
headers: http_response.headers,
body: Arc::new(RwLock::new(body)),
}
}
}
impl HttpResponseText {
#[inline]
pub fn get_http_version(&self) -> HttpVersion {
if let Ok(http_version) = self.http_version.read() {
return http_version
.to_string()
.parse::<HttpVersion>()
.unwrap_or_default();
}
return HttpVersion::default();
}
#[inline]
pub fn get_status_code(&self) -> ResponseStatusCode {
self.status_code
}
#[inline]
pub fn get_status_text(&self) -> String {
if let Ok(status_text) = self.status_text.read() {
return status_text.to_string();
}
return HttpStatus::default().to_string();
}
#[inline]
pub fn get_headers(&self) -> RequestHeaders {
if let Ok(headers) = self.headers.read() {
return headers.clone();
}
return RequestHeaders::new();
}
#[inline]
pub fn get_body(&self) -> RequestBodyString {
if let Ok(body) = self.body.read() {
return body.to_string();
}
return RequestBodyString::new();
}
}
impl Default for HttpResponseText {
#[inline]
fn default() -> Self {
Self {
http_version: Arc::new(RwLock::new(HttpVersion::Unknown(String::new()))),
status_code: HttpStatus::Unknown.code(),
status_text: Arc::new(RwLock::new(HttpStatus::Unknown.to_string())),
headers: Arc::new(RwLock::new(dash_map())),
body: Arc::new(RwLock::new(String::new())),
}
}
}