titan_http/response/
mod.rs

1use http::{HeaderMap, HeaderValue, StatusCode};
2
3pub type Response<B = Body> = http::Response<B>;
4
5use crate::body::Body;
6pub type ResponseBuilder = http::response::Builder;
7
8pub struct Head {
9  /// The response's status
10  pub status: StatusCode,
11
12  /// The response's headers
13  pub headers: HeaderMap<HeaderValue>,
14}
15
16pub struct HttpResponseExt(pub Response<Body>);
17
18impl HttpResponseExt {
19  pub fn parse_parts(self) -> (String, Body) {
20    let (parts, body) = self.0.into_parts();
21    let mut res = format!(
22      "HTTP/1.1 {status} {text}\r\n",
23      status = parts.status.as_u16(),
24      text = parts.status.canonical_reason().unwrap()
25    );
26
27    for (name, value) in parts.headers {
28      let header_line =
29        format!("{}: {}\r\n", name.unwrap().as_str(), value.to_str().unwrap());
30      res.push_str(&header_line);
31    }
32    (res, body)
33  }
34}
35
36//impl From<HttpResponseExt> for String {
37//  fn from(res: HttpResponseExt) -> Self {
38//    let (parts, body) = res.0.into_parts();
39//    let mut res = format!(
40//      "HTTP/1.1 {status} {text}\r\n",
41//      status = parts.status.as_u16(),
42//      text = parts.status.canonical_reason().unwrap()
43//    );
44//
45//    for (name, value) in parts.headers {
46//      let header_line =
47//        format!("{}: {}\r\n", name.unwrap().as_str(), value.to_str().unwrap());
48//      res.push_str(&header_line);
49//    }
50//
51//    let body_str: String = body.into();
52//    res.push_str(&format!("\r\n{body_str}"));
53//    res
54//  }
55//}