1use std::{error::Error, io};
2
3pub struct Response<'a> {
4 response: &'a str,
5}
6
7impl<'a> Response<'a> {
8 pub fn new(response: &str) -> Response {
9 let parse = Response { response: response };
12 Response {
13 response: parse.response,
14 }
15 }
16 pub fn method(&self) -> &str {
17 let method = self.response.split(" ").nth(0);
21 match method {
22 Some(method) => method,
23 None => "path not found",
24 }
25 }
26 pub fn path(&self) -> &str {
27 let path = self.response.split(" ").nth(1);
28 match path {
30 Some(path) => path,
31 None => "path not found",
32 }
33 }
34 pub fn encoding_type(&self) -> Vec<&str> {
35 let responses = self.response.find("Accept-Encoding").unwrap();
36 let accept: Vec<&str> = self.response[responses..]
37 .trim_end_matches("\0")
38 .trim_end_matches("\r\n")
39 .split("\r\n")
40 .nth(0)
41 .unwrap()
42 .split(":")
43 .nth(1)
44 .unwrap()
45 .split(",")
46 .collect();
47 accept
48 }
49 pub fn content(&self) -> &str {
50 let content = self
51 .response
52 .find("\r\n\r\n")
53 .unwrap_or(self.response.len());
54 let response = self.response[content..].trim().trim_end_matches("\0");
55 response
56 }
57 pub fn token(&self) -> &str {
58 match self.response.find("Authorization : Bearer") {
59 Some(size) => {
60 let response = self.response[size..].trim().trim_end_matches("\r\n\r\n");
61 response
62 }
63 None => "Invalid token",
64 }
65 }
66 pub fn cookie(&self) -> Result<&str, Box<dyn Error>> {
67 match self.response.find("Cookie: ") {
69 Some(size) => {
70 let size = size + "Cookie :".len();
71 let cookie = self.response[size..]
72 .trim()
73 .trim_end_matches("\r\n\r\n")
74 .trim_end_matches("\0");
75 println!("{:?}", cookie);
76 Ok(cookie)
77 }
78 None => Err(Box::new(io::Error::new(
79 io::ErrorKind::InvalidData,
80 "Cookie Not found",
81 ))),
82 }
83 }
84}