fp_server/
response.rs

1use std::collections::HashMap;
2
3#[derive(Debug, Clone)]
4pub struct HTTPResponse {
5    msg: String,
6    code: u32,
7    headers: HashMap<String, String>,
8    body: Vec<u8>
9}
10
11impl HTTPResponse {
12    pub fn new(
13        msg: &str,
14        status_code: u32,
15        body: &[u8]
16    ) -> Self {
17        Self {
18            code: status_code,
19            msg: msg.to_string(),
20            headers: HashMap::new(),
21            body: body.to_vec()
22        }
23    }
24
25    /// Adds a header to the request, if the header already exists, it replaces
26    /// it
27    pub fn add_header(&mut self, key: &str, value: &str) {
28        self.headers.insert(key.to_string(), value.to_string());
29    }
30
31    pub fn get_body(&self) -> Vec<u8> {
32        self.body[..].to_vec()
33    }
34
35    pub fn get_headers(&self) -> Vec<(String, String)> {
36        self.headers.iter().map(|(key, value)| {((*key).clone(), (*value).clone())}).collect::<Vec<_>>()
37    }
38
39    pub fn has_header(&self, key: &str) -> bool {
40        self.headers.contains_key(key)
41    }
42
43    pub fn get_msg(&self) -> String {
44        self.msg.clone()
45    }
46
47    pub fn get_code(&self) -> u32 {
48        self.code
49    }
50}