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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use std::collections::HashMap;
use serde_json::Value;
use crate::kernel::singleton::{new_singleton, Singleton};

pub struct Response {
    code: i32,
    headers: HashMap<String, String>,
    body: String
}

static mut HTTP_HEADERS : Singleton<HashMap<String, String>> = new_singleton();

impl Response {

    fn clone(&self) -> Self {
        return Response{
            code: self.code.clone(), headers: self.headers.clone(), body: self.body.clone()
        };
    }

    pub fn add_default_header(name: &str, value: &str) {
        unsafe {
            let mut headers = HTTP_HEADERS.get_instance().clone();
            headers.insert(name.to_string(), value.to_string());
            HTTP_HEADERS.set_instance(headers);
        }
    }

    pub fn new() -> Self {
        let headers = unsafe {
            HTTP_HEADERS.get_instance().clone()
        };
        return Response{ code: 200, headers, body: String::new() };
    }

    pub fn json(&mut self, body: Value) -> Self {
        self.headers.insert("Content-Type".to_owned(), "application/json".to_owned());
        self.body = body.to_string();
        return self.clone();
    }

    pub fn raw(&mut self, body: &str) -> Self {
        let content_type = "Content-Type".to_owned();
        if !self.headers.contains_key(&content_type) {
            self.headers.insert(content_type, "text/html".to_owned());
        }
        self.body = body.to_string();
        return self.clone();
    }

    pub fn header(&mut self, name: &str, value: &str) -> &mut Self {
        self.headers.insert(name.to_string(), value.to_string());
        return self;
    }

    pub fn status(&mut self, code: i32) -> &mut Self {
        self.code = code;
        return self;
    }

    pub fn get_body(&self) -> String {
        return self.body.clone();
    }

    pub fn get_headers(&self) -> HashMap<String, String> {
        return self.headers.clone();
    }

    pub fn get_code(&self) -> i32 {
        return self.code;
    }

}