http_scrap/
lib.rs

1use std::{collections::HashMap, error::Error, io};
2
3pub struct Response<'a> {
4    response: std::borrow::Cow<'a, str>,
5}
6
7impl<'a> Response<'a> {
8    pub fn new(response: &[u8]) -> Response {
9        let billionaire: std::borrow::Cow<'_, str> = String::from_utf8_lossy(response);
10        // * gets the response from client in lossy format
11        // ! lossy format is used to convert the binary to readable string
12        let parse = Response {
13            response: billionaire,
14        };
15        Response {
16            response: parse.response,
17        }
18    }
19    pub fn method(&self) -> &str {
20        //* extract the methods like GET, POST, PUT, DELETE */
21        //* splits the response and get the 0th index element */
22        //*  */
23        let method = self.response.split(" ").nth(0);
24        match method {
25            Some(method) => method,
26            None => "path not found",
27        }
28    }
29    pub fn path(&self) -> &str {
30        let path = self.response.split(" ").nth(1);
31        // println!("{}", path);
32        match path {
33            Some(path) => path,
34            None => "path not found",
35        }
36    }
37
38    pub fn query(&self) -> QMap<&str, &str> {
39        let path = &self.response.split(" ").nth(1);
40        let mut billionaire = HashMap::new();
41        let value = match path {
42            None => {
43                billionaire.insert("id not found", "value not found");
44                billionaire
45            }
46            Some(path) => {
47                let query = path.split("?").nth(1);
48                // print!("{:?}", query);
49                match query {
50                    None => {
51                        billionaire.insert("id not found", "value not found");
52                        billionaire
53                    }
54                    Some(query) => {
55                        let b: Vec<&str> = query.split("&").collect();
56                        for query in b.iter() {
57                            let mut param = query.split("=");
58                            let key = param.nth(0);
59                            let value = param.nth(0);
60                            if let Some(b) = key {
61                                if let Some(value) = value {
62                                    billionaire.insert(b, value);
63                                } else {
64                                    billionaire.insert(b, "value not found");
65                                }
66                            } else {
67                                billionaire.insert("key not found", "value not found");
68                            }
69                        }
70                        billionaire
71                    }
72                }
73            }
74        };
75        let b = QMap::new(value);
76        b
77    }
78    pub fn encoding_type(&self) -> Vec<&str> {
79        let responses = self.response.find("Accept-Encoding").unwrap();
80        let accept: Vec<&str> = self.response[responses..]
81            .trim_end_matches("\0")
82            .trim_end_matches("\r\n")
83            .split("\r\n")
84            .nth(0)
85            .unwrap()
86            .split(":")
87            .nth(1)
88            .unwrap()
89            .split(",")
90            .collect();
91        accept
92    }
93    pub fn content(&self) -> &str {
94        let content = self
95            .response
96            .find("\r\n\r\n")
97            .unwrap_or(self.response.len());
98        let response = self.response[content..].trim().trim_end_matches("\0");
99        response
100    }
101    pub fn token(&self) -> &str {
102        match self.response.find("Authorization : Bearer") {
103            Some(size) => {
104                let response = self.response[size..]
105                    .trim()
106                    .trim_end_matches("\r\n")
107                    .trim_end_matches("\r\n\r\n");
108                response
109            }
110            None => "Authorization Not Found",
111        }
112    }
113    pub fn cookie(&self) -> String {
114        // println!("{}", self.response);
115        match self.response.find("Cookie: ") {
116            Some(size) => {
117                let size = size + "Cookie :".len();
118                let cookie = self.response[size..]
119                    .trim()
120                    .trim_end_matches("\r\n\r\n")
121                    .trim_end_matches("\0")
122                    .replace("\r\n\r\n", "");
123                cookie
124            }
125            None => String::from("Cookie Not Found"),
126        }
127    }
128    pub fn header(&self) -> HMap<&str, &str> {
129        let b: Vec<&str> = self.response.split("\r\n").collect();
130        let mut billionaire = HashMap::new();
131        let header_section = &b[1..b.len() - 1];
132        for b in header_section {
133            let mut value = b.split(":");
134            let key = value.nth(0);
135            let value = value.nth(0);
136            if let Some(key) = key {
137                if let Some(value) = value {
138                    billionaire.insert(key, value);
139                }
140            }
141        }
142        let map = HMap::new(billionaire);
143        map
144    }
145}
146
147#[derive(Debug)]
148pub struct HMap<K, V> {
149    headers: HashMap<K, V>,
150}
151pub struct QMap<K, V> {
152    query: HashMap<K, V>,
153}
154
155impl<K: std::cmp::Eq + std::hash::Hash, V> HMap<K, V> {
156    pub(crate) fn new(headers: HashMap<K, V>) -> Self {
157        HMap { headers: headers }
158    }
159    pub fn get(&self, param: K) -> Option<&V> {
160        self.headers.get(&param)
161    }
162}
163
164impl<K: std::cmp::Eq + std::hash::Hash, V> QMap<K, V> {
165    pub(crate) fn new(headers: HashMap<K, V>) -> Self {
166        QMap { query: headers }
167    }
168    pub fn get(&self, param: K) -> Option<&V> {
169        self.query.get(&param)
170    }
171}