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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use crate::{response::valid, func::hashmapper::stringify_hashmapped_headers};
use core::panic;
use std::{fs, collections::HashMap};

pub struct Response{
    pub status:     i32,
    pub body:       String,
    pub headers:    String
}

const MAX_CONTENT_LEN: usize = 1000000000;

impl Response{

    pub fn set_status(&mut self, status: i32){
        
        if valid::is_valid_status(status){
            self.status = status;
        }
        else{
            panic!("Invalid response status");
        }
    }
    pub fn get_status(&self) -> i32{
        return self.status;
    }

    pub fn send_body(&mut self, body: String){

        if body.len() < MAX_CONTENT_LEN{
            self.body = body;
        }
        else{
            panic!("Response-body is too large! Max is {}", MAX_CONTENT_LEN);
        }
    }
    
    pub fn send_file(&mut self, path: &str){

        let file_content = fs::read_to_string(path);

        match file_content {
            Ok(_) => {

                let content: String = file_content.unwrap();
                
                if content.len() > MAX_CONTENT_LEN{
                    panic!("Response-body is too large! Max is {}", MAX_CONTENT_LEN);
                }

                self.body = content;
            }
            Err(err) => {
                panic!("{}", err);     
            }
        }
    }

    fn get_header_len(&self, content: &str) -> String{
        if self.headers.len() > 0 {

            return format!("\r\nLocation: {}", content);
        }
        else {

            return format!("Location: {}", content);            
        }
    }

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

    pub fn set_header(&mut self, headers: &HashMap<&str, &str>){

        let stringified_header: String = stringify_hashmapped_headers(headers);

        self.headers = stringified_header;
    }

    pub fn append_header(&mut self, headers: &HashMap<&str, &str>){

        let stringified_header: String = stringify_hashmapped_headers(headers);

        self.headers.push_str(
            &self.get_header_len(&stringified_header)
        );
    }

    pub fn clear_header(&mut self){
        self.headers = String::from("");
    }

    pub fn set_string_header(&mut self, header: String){
        self.headers = header;
    }
    
    pub fn append_string_header(&mut self, header: String){
        self.headers.push_str(
            &self.get_header_len(&header)
        );
    }

    pub fn get_header(&self) -> String{
        return String::from(&self.headers);
    }

    pub fn redirect(&mut self, path: &str){

        self.headers.push_str(
            &self.get_header_len(path)
        );

        self.set_status(303);
    }
}