http-scrap 0.1.1136

HTTP parsing methods made easier to use
Documentation
use std::{collections::HashMap, error::Error, io};

pub struct Response {
    response: String,
}

impl Response {
    pub fn new(response: impl Into<String>) -> Response {
        // * gets the response from client in lossy format
        // ! lossy format is used to convert the binary to readable string
        let parse = Response {
            response: response.into(),
        };
        Response {
            response: parse.response,
        }
    }
    pub fn method(&self) -> &str {
        //* extract the methods like GET, POST, PUT, DELETE */
        //* splits the response and get the 0th index element */
        //*  */
        let method = self.response.split(" ").nth(0);
        match method {
            Some(method) => method,
            None => "path not found",
        }
    }
    pub fn path(&self) -> &str {
        let path = self.response.split(" ").nth(1);
        // println!("{}", path);
        match path {
            Some(path) => path,
            None => "path not found",
        }
    }

    pub fn query(&self) -> QMap<&str, &str> {
        let path = &self.response.split(" ").nth(1);
        let mut billionaire = HashMap::new();
        let value = match path {
            None => {
                billionaire.insert("id not found", "value not found");
                billionaire
            }
            Some(path) => {
                let query = path.split("?").nth(1);
                // print!("{:?}", query);
                match query {
                    None => {
                        billionaire.insert("id not found", "value not found");
                        billionaire
                    }
                    Some(query) => {
                        let b: Vec<&str> = query.split("&").collect();
                        for query in b.iter() {
                            let mut param = query.split("=");
                            let key = param.nth(0);
                            let value = param.nth(0);
                            if let Some(b) = key {
                                if let Some(value) = value {
                                    billionaire.insert(b, value);
                                } else {
                                    billionaire.insert(b, "value not found");
                                }
                            } else {
                                billionaire.insert("key not found", "value not found");
                            }
                        }
                        billionaire
                    }
                }
            }
        };
        let b = QMap::new(value);
        b
    }
    pub fn encoding_type(&self) -> Vec<&str> {
        let responses = self.response.find("Accept-Encoding").unwrap();
        let accept: Vec<&str> = self.response[responses..]
            .trim_end_matches("\0")
            .trim_end_matches("\r\n")
            .split("\r\n")
            .nth(0)
            .unwrap()
            .split(":")
            .nth(1)
            .unwrap()
            .split(",")
            .collect();
        accept
    }
    pub fn content(&self) -> &str {
        let content = self
            .response
            .find("\r\n\r\n")
            .unwrap_or(self.response.len());
        let response = self.response[content..].trim().trim_end_matches("\0");
        response
    }
    pub fn token(&self) -> &str {
        match self.response.find("Authorization : Bearer") {
            Some(size) => {
                let response = self.response[size..]
                    .trim()
                    .trim_end_matches("\r\n")
                    .trim_end_matches("\r\n\r\n");
                response
            }
            None => "Authorization Not Found",
        }
    }
    pub fn cookie(&self) -> String {
        // println!("{}", self.response);
        match self.response.find("Cookie: ") {
            Some(size) => {
                let size = size + "Cookie :".len();
                let cookie = self.response[size..]
                    .trim()
                    .trim_end_matches("\r\n\r\n")
                    .trim_end_matches("\0")
                    .replace("\r\n\r\n", "");
                cookie
            }
            None => String::from("Cookie Not Found"),
        }
    }
    pub fn header(&self) -> HMap<&str, &str> {
        let b: Vec<&str> = self.response.split("\r\n").collect();
        let mut billionaire = HashMap::new();
        let header_section = if b.len() > 1 {
            &b[1..b.len().saturating_sub(1)]
        } else {
            &[]
        };
        for b in header_section {
            let mut value = b.split(":");
            let key = value.nth(0);
            let value = value.nth(0);
            if let Some(key) = key {
                if let Some(value) = value {
                    billionaire.insert(key, value);
                }
            }
        }
        let map = HMap::new(billionaire);
        map
    }
}

#[derive(Debug)]
pub struct HMap<K, V> {
    headers: HashMap<K, V>,
}
#[derive(Debug)]
pub struct QMap<K, V> {
    query: HashMap<K, V>,
}

impl<K: std::cmp::Eq + std::hash::Hash, V> HMap<K, V> {
    pub(crate) fn new(headers: HashMap<K, V>) -> Self {
        HMap { headers: headers }
    }
    pub fn get(&self, param: K) -> Option<&V> {
        self.headers.get(&param)
    }
}

impl<K: std::cmp::Eq + std::hash::Hash, V> QMap<K, V> {
    pub(crate) fn new(headers: HashMap<K, V>) -> Self {
        QMap { query: headers }
    }
    pub fn get(&self, param: K) -> Option<&V> {
        self.query.get(&param)
    }
}