br_addon/
request.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
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
use json::JsonValue;
use std::collections::BTreeMap;

#[derive(Clone, Debug)]
pub struct Request {
    /// 配置参数
    pub config: JsonValue,
    /// 当前请求类型
    pub method: Method,
    /// 地址参数
    pub query: BTreeMap<String, String>,
    /// header信息
    pub header: BTreeMap<String, String>,
    /// Cookie信息
    pub cookie: BTreeMap<String, String>,
    /// 自定义数据
    pub custom: BTreeMap<String, JsonValue>,
    /// 请求体
    pub body: JsonValue,
    /// 客户端IP
    pub client_ip: String,
}
#[derive(Clone, Debug)]
pub enum Method {
    Post,
    Get,
    Head,
    Put,
    Delete,
    Options,
    Patch,
    Trace,
    None,
}
impl Method {
    pub fn from(name: &str) -> Self {
        match name.to_lowercase().as_str() {
            "post" => Self::Post,
            "get" => Self::Get,
            "head" => Self::Head,
            "put" => Self::Put,
            "delete" => Self::Delete,
            "options" => Self::Options,
            "patch" => Self::Patch,
            "trace" => Self::Trace,
            _ => Self::None,
        }
    }
    pub fn str(&mut self) -> &'static str {
        match self {
            Method::Post => "POST",
            Method::Get => "GET",
            Method::Head => "HEAD",
            Method::Put => "PUT",
            Method::Delete => "DELETE",
            Method::Options => "OPTIONS",
            Method::Patch => "PATCH",
            Method::Trace => "TRACE",
            Method::None => "",
        }
    }
}
#[derive(Debug, Clone)]
pub enum ContentType {
    FormData,
    FormUrlencoded,
    Json,
    Xml,
    Javascript,
    Text,
    Html,
    Other(String),
}
impl ContentType {
    pub fn from(name: &str) -> Self {
        match name {
            "multipart/form-data" => Self::FormData,
            "application/x-www-form-urlencoded" => Self::FormUrlencoded,
            "application/json" => Self::Json,
            "application/xml" | "text/xml" => Self::Xml,
            "application/javascript" => Self::Javascript,
            "text/html" => Self::Html,
            "text/plain" => Self::Text,
            _ => Self::Other(name.to_string()),
        }
    }
    pub fn str(&mut self) -> String {
        match self {
            Self::FormData => "multipart/form-data",
            Self::FormUrlencoded => "application/x-www-form-urlencoded",
            Self::Json => "application/json",
            Self::Xml => "application/xml",
            Self::Javascript => "application/javascript",
            Self::Text => "text/plain",
            Self::Html => "text/html",
            Self::Other(name) => name,
        }
        .to_string()
    }
}