titan_http/response/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use http::{HeaderMap, HeaderValue, StatusCode};

pub type Response<B = Body> = http::Response<B>;

use crate::body::Body;
pub type ResponseBuilder = http::response::Builder;

pub struct Head {
  /// The response's status
  pub status: StatusCode,

  /// The response's headers
  pub headers: HeaderMap<HeaderValue>,
}

pub struct HttpResponseExt(pub Response<Body>);

impl HttpResponseExt {
  pub fn parse_parts(self) -> (String, Body) {
    let (parts, body) = self.0.into_parts();
    let mut res = format!(
      "HTTP/1.1 {status} {text}\r\n",
      status = parts.status.as_u16(),
      text = parts.status.canonical_reason().unwrap()
    );

    for (name, value) in parts.headers {
      let header_line =
        format!("{}: {}\r\n", name.unwrap().as_str(), value.to_str().unwrap());
      res.push_str(&header_line);
    }
    (res, body)
  }
}

//impl From<HttpResponseExt> for String {
//  fn from(res: HttpResponseExt) -> Self {
//    let (parts, body) = res.0.into_parts();
//    let mut res = format!(
//      "HTTP/1.1 {status} {text}\r\n",
//      status = parts.status.as_u16(),
//      text = parts.status.canonical_reason().unwrap()
//    );
//
//    for (name, value) in parts.headers {
//      let header_line =
//        format!("{}: {}\r\n", name.unwrap().as_str(), value.to_str().unwrap());
//      res.push_str(&header_line);
//    }
//
//    let body_str: String = body.into();
//    res.push_str(&format!("\r\n{body_str}"));
//    res
//  }
//}