From fe378c20c645ca1cb6cc04e95c9afd4c2de5c0e8 Mon Sep 17 00:00:00 2001 From: omagdy7 Date: Wed, 5 Jun 2024 22:42:05 +0300 Subject: feat: Handled Gzip compression --- src/response.rs | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'src/response.rs') diff --git a/src/response.rs b/src/response.rs index c3540d2..80424a3 100644 --- a/src/response.rs +++ b/src/response.rs @@ -5,7 +5,7 @@ pub struct Response { version: String, status: StatusCode, headers: Option, - body: Option, + body: Option>, } impl Response { @@ -13,7 +13,7 @@ impl Response { version: String, status: StatusCode, headers: Option, - body: Option, + body: Option>, ) -> Self { Response { version, @@ -24,18 +24,25 @@ impl Response { } } -impl Into for Response { - fn into(self) -> String { - let status_line = format!("HTTP/{} {}", self.version, String::from(self.status)); +impl Into> for Response { + fn into(self) -> Vec { + let status_line = format!("HTTP/{} {}", self.version, String::from(self.status)) + .as_bytes() + .to_owned(); let headers = self .headers .unwrap_or(Headers(HashMap::new())) .0 .iter() .map(|(key, value)| format!("{key}: {value}\r\n")) - .collect::(); - let body = self.body.unwrap_or("".to_string()); - format!("{status_line}\r\n{headers}\r\n{body}") + .collect::() + .as_bytes() + .to_owned(); + let body = self.body.unwrap_or(vec![]); + let crlf = "\r\n".as_bytes().to_owned(); + vec![status_line, crlf.clone(), headers, crlf.clone(), body] + .concat() + .to_owned() } } @@ -74,12 +81,12 @@ impl Into for &str { }; // Parse body - let body: Option = { + let body: Option> = { let remaining_lines: Vec<&str> = lines.collect(); if remaining_lines.is_empty() { None } else { - Some(remaining_lines.join("\n")) + Some(remaining_lines.join("\n").as_bytes().to_owned()) } }; -- cgit v1.2.3