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