br_addon/
request.rs

1use json::JsonValue;
2
3#[derive(Clone, Debug)]
4pub struct Request {
5    /// 配置参数
6    pub config: JsonValue,
7    /// 当前请求类型
8    pub method: Method,
9    /// 请求地址
10    pub url: String,
11    pub paths: Vec<String>,
12    /// 地址参数
13    pub query: JsonValue,
14    /// header信息
15    pub header: JsonValue,
16    /// Cookie信息
17    pub cookie: JsonValue,
18    /// 自定义数据
19    pub custom: JsonValue,
20    /// 请求体
21    pub body: JsonValue,
22    /// 客户端IP
23    pub client_ip: String,
24    /// 服务器IP
25    pub server_ip: String,
26    /// 请求时间
27    pub request_time: f64,
28}
29impl Default for Request {
30    fn default() -> Self {
31        Self {
32            config: JsonValue::Null,
33            method: Method::None,
34            url: "".to_string(),
35            paths: vec![],
36            query: JsonValue::Null,
37            header: JsonValue::Null,
38            cookie: JsonValue::Null,
39            custom: JsonValue::Null,
40            body: JsonValue::Null,
41            client_ip: "".to_string(),
42            server_ip: "".to_string(),
43            request_time: 0.0,
44        }
45    }
46}
47#[derive(Clone, Debug)]
48pub enum Method {
49    Post,
50    Get,
51    Head,
52    Put,
53    Delete,
54    Options,
55    Patch,
56    Trace,
57    None,
58}
59impl Method {
60    pub fn from(name: &str) -> Self {
61        match name.to_lowercase().as_str() {
62            "post" => Self::Post,
63            "get" => Self::Get,
64            "head" => Self::Head,
65            "put" => Self::Put,
66            "delete" => Self::Delete,
67            "options" => Self::Options,
68            "patch" => Self::Patch,
69            "trace" => Self::Trace,
70            _ => Self::None,
71        }
72    }
73    pub fn str(&mut self) -> &'static str {
74        match self {
75            Method::Post => "POST",
76            Method::Get => "GET",
77            Method::Head => "HEAD",
78            Method::Put => "PUT",
79            Method::Delete => "DELETE",
80            Method::Options => "OPTIONS",
81            Method::Patch => "PATCH",
82            Method::Trace => "TRACE",
83            Method::None => "",
84        }
85    }
86}
87#[derive(Debug, Clone)]
88pub enum ContentType {
89    FormData,
90    FormUrlencoded,
91    Json,
92    Xml,
93    Javascript,
94    Text,
95    Html,
96    Other(String),
97}
98impl ContentType {
99    pub fn from(name: &str) -> Self {
100        match name {
101            "multipart/form-data" => Self::FormData,
102            "application/x-www-form-urlencoded" => Self::FormUrlencoded,
103            "application/json" => Self::Json,
104            "application/xml" | "text/xml" => Self::Xml,
105            "application/javascript" => Self::Javascript,
106            "text/html" => Self::Html,
107            "text/plain" => Self::Text,
108            _ => Self::Other(name.to_string()),
109        }
110    }
111    pub fn str(&mut self) -> String {
112        match self {
113            Self::FormData => "multipart/form-data",
114            Self::FormUrlencoded => "application/x-www-form-urlencoded",
115            Self::Json => "application/json",
116            Self::Xml => "application/xml",
117            Self::Javascript => "application/javascript",
118            Self::Text => "text/plain",
119            Self::Html => "text/html",
120            Self::Other(name) => name,
121        }.to_string()
122    }
123}