nutt_web/http/
mod.rs

1use serde::Serialize;
2use serde_json::Value;
3use std::collections::HashMap;
4use std::fmt::{Display, Formatter};
5
6pub mod cookie;
7pub mod method;
8pub mod request;
9pub mod response;
10pub mod status;
11
12pub struct HttpHeader {
13    headers: HashMap<String, String>,
14}
15
16impl HttpHeader {
17    pub fn new<T: Serialize + Clone + Send>(response: T) -> Self {
18        let content = serde_json::to_string(&response).unwrap();
19        let mut base_headers = HashMap::new();
20        base_headers.insert("Content-Type".to_string(), "application/json".to_string());
21        base_headers.insert("Content-Length".to_string(), content.len().to_string());
22        base_headers.insert("Connection".to_string(), "keep-alive".to_string());
23
24        Self {
25            headers: base_headers,
26        }
27    }
28}
29
30impl Display for HttpHeader {
31    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
32        let mut headers = String::new();
33        for (key, value) in &self.headers {
34            headers.push_str(&format!("{}: {}\r\n", key, value))
35        }
36        write!(f, "{}", headers)
37    }
38}
39
40#[derive(Clone, Debug)]
41pub struct HttpBody {
42    body: Value,
43}
44
45impl HttpBody {
46    pub fn new(value: Value) -> HttpBody {
47        Self { body: value }
48    }
49}