1use std::{collections::HashMap, io::Cursor, str::FromStr};
2
3use tiny_http::Response as Res;
4
5
6pub struct ResponseConstruction {
7 headers: HashMap<String, String>,
8 content: String,
9 data: Vec<u8>,
10}
11
12impl ResponseConstruction {
13 pub fn generate_response(res: Response) -> Self {
14 Self {
15 headers: res.headers,
16 content: "".to_owned(),
17 data: vec![]
18 }
19 }
20
21 pub fn add_header(&mut self, header: &str, value: &str) {
22 self.headers.insert(header.to_owned(), value.to_owned());
23 }
24 pub fn add_content(&mut self, content: &str) {
25 self.content += content;
26 }
27 pub fn set_raw_data(&mut self, data: Vec<u8>) {
28 self.data = data;
29 }
30 pub fn render_response(&self) -> Res<Cursor<Vec<u8>>> {
31 let new_data: Vec<u8>;
32 if self.data.len() < 1 {
33 new_data = format!("{}", self.content).as_bytes().to_vec()
34 } else {
35 new_data = self.data.clone();
36 }
37 let mut response = Res::from_data(new_data);
38 for i in self.headers.iter() {
39 response.add_header(tiny_http::Header::from_str(&format!("{}: {}", i.0,i.1)).unwrap());
40 }
41 response
42 }
43}
44
45
46pub struct Response {
47 headers: HashMap<String, String>,
48 status_code: i32
49}
50impl Response {
51 pub fn new() -> Self {
52 Self {
53 headers: HashMap::new(),
54 status_code: 200
55 }
56 }
57 pub fn add_header(&mut self, header: &str, value: &str) {
58 self.headers.insert(header.to_owned(), value.to_owned());
59 }
60 pub fn set_status_code(&mut self, status_code: i32) {
61 self.status_code = status_code;
62 }
63}
64
65
66pub enum ReturnData {
67 RawData(Vec<u8>),
68 Text(String),
69 Json(String)
70}