http_scrap/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
pub struct Response<'res> {
    pub response: &'res [u8],
}

pub enum Type {
    PATH,
    METHOD,
    COOKIE,
    CONTENT,
}

impl<'res> Response<'res> {
    pub fn response(&mut self, types: Type) -> String {
        let response = String::from_utf8_lossy(&self.response);

        match types {
            Type::METHOD => {
                let method = response.split(" ").nth(0);
                match method {
                    Some(method) => method.to_string(),
                    None => "method not found".to_string(),
                }
            }
            Type::PATH => {
                let path = response.split(" ").nth(1);
                match path {
                    Some(path) => path.to_string(),
                    None => "path not found".to_string(),
                }
            }
            Type::CONTENT => {
                let content = response.find("\r\n\r\n");
                match content {
                    Some(content) => {
                        let body = response[content..].trim().trim_end_matches("\0");
                        body.to_string()
                    }
                    None => "content not found".to_string(),
                }
            }
            Type::COOKIE => {
                let body = response.find("Cookie: ");
                match body {
                    Some(cookie) => {
                        let body = &response[cookie..]
                            .trim()
                            .trim_start_matches("Cookie: ")
                            .trim_end_matches("\r\n\r\n");
                        body.to_string()
                    }
                    None => "Cookie not found".to_string(),
                }
            }
        }
    }
}