Skip to main content

lava_http/response/
res.rs

1use crate::{response::valid, func::hashmapper::stringify_hashmapped_headers};
2use core::panic;
3use std::{fs, collections::HashMap};
4
5pub struct Response{
6    pub status:     i32,
7    pub body:       String,
8    pub headers:    String
9}
10
11const MAX_CONTENT_LEN: usize = 1000000000;
12
13impl Response{
14    /// Set response-status
15    /// 
16    /// Example
17    /// ```
18    /// res.set_status(404);
19    /// ```
20    pub fn set_status(&mut self, status: i32){
21        
22        if valid::is_valid_status(status){
23            self.status = status;
24        }
25        else{
26            panic!("Invalid response status");
27        }
28    }
29
30    /// Get response-status
31    /// 
32    /// Example
33    /// ```
34    /// res.get_status();
35    /// ```
36    pub fn get_status(&self) -> i32{
37        return self.status;
38    }
39
40    /// Send response-body as text
41    /// 
42    /// Example
43    /// ```
44    /// res.send_body("body content");
45    /// ```
46    pub fn send_body(&mut self, body: &str){
47
48        let string_body: String = String::from(body);
49
50        if body.len() < MAX_CONTENT_LEN{
51            self.body = string_body;
52        }
53        else{
54            panic!("Response-body is too large! Max is {}", MAX_CONTENT_LEN);
55        }
56    }
57    
58    /// Send response-body as file
59    /// 
60    /// Example
61    /// ```
62    /// res.send_file("path.html");
63    /// ```
64    pub fn send_file(&mut self, path: &str){
65
66        let file_content = fs::read_to_string(path);
67
68        match file_content {
69            Ok(_) => {
70
71                let content: String = file_content.unwrap();
72                
73                if content.len() > MAX_CONTENT_LEN{
74                    panic!("Response-body is too large! Max is {}", MAX_CONTENT_LEN);
75                }
76
77                self.body = content;
78            }
79            Err(err) => {
80                panic!("{}", err);     
81            }
82        }
83    }
84
85    /// Get response-body
86    /// 
87    /// Example
88    /// ```
89    /// res.get_body();
90    /// ```
91    pub fn get_body(&self) -> String{
92        return String::from(&self.body);
93    }
94
95    fn get_header_len(&self, content: &str) -> String{
96        if self.headers.len() > 0 {
97
98            return format!("\r\nLocation: {}", content);
99        }
100        else {
101
102            return format!("Location: {}", content);            
103        }
104    }
105
106
107    /// Send response-headers in the form of a hashmap (this will overwrite an already set header for the response)
108    /// 
109    /// Example
110    /// ```
111    /// let mut headers: HashMap<&str, &str> = HashMap::new();
112    /// 
113    /// headers.insert("key", "value");
114    /// 
115    /// res.set_header(&headers);
116    /// ```
117    pub fn set_header(&mut self, headers: &HashMap<&str, &str>){
118
119        let stringified_header: String = stringify_hashmapped_headers(headers);
120
121        self.headers = stringified_header;
122    }
123
124    /// Append response-headers in the form of a hashmap (this will append to the already set header for the response)
125    /// 
126    /// Example
127    /// ```
128    /// let mut headers: HashMap<&str, &str> = HashMap::new();
129    /// 
130    /// headers.insert("key", "value");
131    /// 
132    /// res.append_header(&headers);
133    /// ```
134    pub fn append_header(&mut self, headers: &HashMap<&str, &str>){
135
136        let stringified_header: String = stringify_hashmapped_headers(headers);
137
138        self.headers.push_str(
139            &self.get_header_len(&stringified_header)
140        );
141    }
142
143    /// Clear set response-header
144    /// 
145    /// Example
146    /// ```
147    /// res.clear_header();
148    /// ```
149    pub fn clear_header(&mut self){
150        self.headers = String::from("");
151    }
152
153    /// Set response-header in the form of a string (this will overwrite an already set header for the response)
154    /// 
155    /// Example
156    /// ```
157    /// res.set_string_header("Key: Value");
158    /// ```
159    pub fn set_string_header(&mut self, header: String){
160        self.headers = header;
161    }
162
163    /// Append response-header in the form of a string (this will append to the already set header for the response)
164    /// 
165    /// Example
166    /// ```
167    /// res.append_string_header("Key: Value");
168    /// ```
169    pub fn append_string_header(&mut self, header: String){
170        self.headers.push_str(
171            &self.get_header_len(&header)
172        );
173    }
174
175    /// Get response-header
176    /// 
177    /// Example
178    /// ```
179    /// res.get_header();
180    /// ```
181    pub fn get_header(&self) -> String{
182        return String::from(&self.headers);
183    }
184    /// Redirects to given path
185    /// 
186    /// Example
187    /// ```
188    /// res.redirect("/path");
189    /// ```
190    pub fn redirect(&mut self, path: &str){
191
192        self.headers.push_str(
193            &self.get_header_len(path)
194        );
195
196        self.set_status(303);
197    }
198}