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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use json::{JsonValue, object};
use crate::WebConfig;

#[derive(Clone)]
pub struct Header {
    pub config: WebConfig,
}

impl Header {
    /// 头部信息处理
    pub fn def(&mut self, data: String) -> JsonValue {
        let body: Vec<&str> = data.split("\r\n\r\n").collect();
        let headers: Vec<&str> = body[0].split("\r\n").collect();
        let list: Vec<&str> = headers[0].split(" ").collect();

        let mut header = object! {};
        header["public"] = self.config.public.clone().into();
        header["domain"] = self.config.domain.clone().into();
        header["ssl"] = self.config.ssl.clone().into();
        header["temp_dir"] = self.config.temp_dir.clone().into();
        header["url"] = self.config.url.clone().into();

        header["data-length"] = (data.len()).into();
        header["header-length"] = (body[0].len() + 4).into();
        header["method"] = list[0].clone().into();
        header["uri"] = list[1].clone().into();
        header["version"] = list[2].clone().into();
        for item in headers.iter() {
            let row: Vec<&str> = item.split(": ").collect();
            if row.len() == 2 {
                header[row[0].to_string().to_lowercase().as_str()] = row[1].clone().into();
            }
        }
        header = Header::content_type(header.clone()).into();
        header["content-length"] = header["content-length"].to_string().parse::<i32>().unwrap_or(0).into();
        header
    }
    /// get参数处理
    pub fn get(data: String) -> JsonValue {
        let mut list = object! {};

        let query: Vec<&str> = data.split("?").collect();
        if query.len() == 1 {
            return list;
        }
        let query = query[1].clone();
        if query.is_empty() {
            return list;
        }
        let query: Vec<&str> = query.split("&").collect();
        for item in query.iter() {
            let row: Vec<&str> = item.split("=").collect();
            let key = row[0].clone();
            let value = row[1].clone();
            list[key] = value.into();
        }
        list
    }
    /// 请求类型
    pub fn content_type(mut headers: JsonValue) -> JsonValue {
        let content_type = headers["content-type"].to_string().to_lowercase();
        let content_type = content_type.as_str();
        let accept = headers["accept"].to_string().to_lowercase();
        let accept = accept.as_str();

        let content_type_list = object! {
            "text/html":"text/html",
            "text/xml":"text/xml",
            "application/json":"application/json",
            "multipart/form-data":"multipart/form-data",
            "application/x-www-form-urlencoded":"application/x-www-form-urlencoded",
            "text/plain":"text/plain"
        };
        for (key, value) in content_type_list.entries() {
            if content_type.contains(key) {
                match key {
                    "multipart/form-data" => {
                        let boundary: Vec<&str> = headers["content-type"].as_str().unwrap().split("boundary=").collect();
                        headers["boundary"] = boundary[1].clone().into();
                    }
                    _ => {}
                }
                headers["content-type"] = value.clone();
                return headers;
            }
        }
        if headers["content-type"].is_empty() {
            let accept_list = object! {
                "image/*":"image/*",
                "image/webp":"image/*",
                "image/gif":"image/gif"
            };
            for (key, value) in accept_list.entries() {
                if accept.contains(key) {
                    headers["content-type"] = value.clone();
                    return headers;
                }
            }
        }
        headers["content-type"] = "text/plain".into();
        return headers;
    }
}