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: i64,
28    /// 耗时
29    pub handle_time: f64,
30}
31impl Default for Request {
32    fn default() -> Self {
33        Self {
34            config: JsonValue::Null,
35            method: Method::None,
36            url: String::new(),
37            paths: vec![],
38            query: JsonValue::Null,
39            header: JsonValue::Null,
40            cookie: JsonValue::Null,
41            custom: JsonValue::Null,
42            body: JsonValue::Null,
43            client_ip: String::new(),
44            server_ip: String::new(),
45            request_time: 0,
46            handle_time: 0.0,
47        }
48    }
49}
50#[derive(Clone, Debug)]
51pub enum Method {
52    Post,
53    Get,
54    Head,
55    Put,
56    Delete,
57    Options,
58    Patch,
59    Trace,
60    None,
61}
62impl Method {
63    #[must_use]
64    pub fn from(name: &str) -> Self {
65        match name.to_lowercase().as_str() {
66            "post" => Self::Post,
67            "get" => Self::Get,
68            "head" => Self::Head,
69            "put" => Self::Put,
70            "delete" => Self::Delete,
71            "options" => Self::Options,
72            "patch" => Self::Patch,
73            "trace" => Self::Trace,
74            _ => Self::None,
75        }
76    }
77    pub fn str(&mut self) -> &'static str {
78        match self {
79            Method::Post => "POST",
80            Method::Get => "GET",
81            Method::Head => "HEAD",
82            Method::Put => "PUT",
83            Method::Delete => "DELETE",
84            Method::Options => "OPTIONS",
85            Method::Patch => "PATCH",
86            Method::Trace => "TRACE",
87            Method::None => "",
88        }
89    }
90}
91#[derive(Debug, Clone)]
92pub enum ContentType {
93    FormData,
94    FormUrlencoded,
95    Json,
96    Xml,
97    Javascript,
98    Text,
99    Html,
100    Other(String),
101}
102impl ContentType {
103    #[must_use]
104    pub fn from(name: &str) -> Self {
105        match name {
106            "multipart/form-data" => Self::FormData,
107            "application/x-www-form-urlencoded" => Self::FormUrlencoded,
108            "application/json" => Self::Json,
109            "application/xml" | "text/xml" => Self::Xml,
110            "application/javascript" => Self::Javascript,
111            "text/html" => Self::Html,
112            "text/plain" => Self::Text,
113            _ => Self::Other(name.to_string()),
114        }
115    }
116    pub fn str(&mut self) -> String {
117        match self {
118            Self::FormData => "multipart/form-data",
119            Self::FormUrlencoded => "application/x-www-form-urlencoded",
120            Self::Json => "application/json",
121            Self::Xml => "application/xml",
122            Self::Javascript => "application/javascript",
123            Self::Text => "text/plain",
124            Self::Html => "text/html",
125            Self::Other(name) => name,
126        }.to_string()
127    }
128}