df_web/
header.rs

1use json::{JsonValue, object};
2use crate::WebConfig;
3
4#[derive(Clone)]
5pub struct Header {
6    pub config: WebConfig,
7}
8
9impl Header {
10    /// 头部信息处理
11    pub fn def(&mut self, data: String) -> JsonValue {
12        let body: Vec<&str> = data.split("\r\n\r\n").collect();
13        let headers: Vec<&str> = body[0].split("\r\n").collect();
14        let list: Vec<&str> = headers[0].split(" ").collect();
15
16        let mut header = object! {};
17        header["public"] = self.config.public.clone().into();
18        header["domain"] = self.config.domain.clone().into();
19        header["ssl"] = self.config.ssl.clone().into();
20        header["temp_dir"] = self.config.temp_dir.clone().into();
21        header["url"] = self.config.url.clone().into();
22
23        header["data-length"] = (data.len()).into();
24        header["header-length"] = (body[0].len() + 4).into();
25        header["method"] = list[0].clone().into();
26        header["uri"] = list[1].clone().into();
27        header["version"] = list[2].clone().into();
28        for item in headers.iter() {
29            let row: Vec<&str> = item.split(": ").collect();
30            if row.len() == 2 {
31                header[row[0].to_string().to_lowercase().as_str()] = row[1].clone().into();
32            }
33        }
34        header = Header::content_type(header.clone()).into();
35        header["content-length"] = header["content-length"].to_string().parse::<i32>().unwrap_or(0).into();
36        header
37    }
38    /// get参数处理
39    pub fn get(data: String) -> JsonValue {
40        let mut list = object! {};
41
42        let query: Vec<&str> = data.split("?").collect();
43        if query.len() == 1 {
44            return list;
45        }
46        let query = query[1].clone();
47        if query.is_empty() {
48            return list;
49        }
50        let query: Vec<&str> = query.split("&").collect();
51        for item in query.iter() {
52            let row: Vec<&str> = item.split("=").collect();
53            let key = row[0].clone();
54            let value = row[1].clone();
55            list[key] = value.into();
56        }
57        list
58    }
59    /// 请求类型
60    pub fn content_type(mut headers: JsonValue) -> JsonValue {
61        let content_type = headers["content-type"].to_string().to_lowercase();
62        let content_type = content_type.as_str();
63        let accept = headers["accept"].to_string().to_lowercase();
64        let accept = accept.as_str();
65
66        let content_type_list = object! {
67            "text/html":"text/html",
68            "text/xml":"text/xml",
69            "application/json":"application/json",
70            "multipart/form-data":"multipart/form-data",
71            "application/x-www-form-urlencoded":"application/x-www-form-urlencoded",
72            "text/plain":"text/plain"
73        };
74        for (key, value) in content_type_list.entries() {
75            if content_type.contains(key) {
76                match key {
77                    "multipart/form-data" => {
78                        let boundary: Vec<&str> = headers["content-type"].as_str().unwrap().split("boundary=").collect();
79                        headers["boundary"] = boundary[1].clone().into();
80                    }
81                    _ => {}
82                }
83                headers["content-type"] = value.clone();
84                return headers;
85            }
86        }
87        if headers["content-type"].is_empty() {
88            let accept_list = object! {
89                "image/*":"image/*",
90                "image/webp":"image/*",
91                "image/gif":"image/gif"
92            };
93            for (key, value) in accept_list.entries() {
94                if accept.contains(key) {
95                    headers["content-type"] = value.clone();
96                    return headers;
97                }
98            }
99        }
100        headers["content-type"] = "text/plain".into();
101        return headers;
102    }
103}