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] pub fn from(name: &str) -> Self {
64        match name.to_lowercase().as_str() {
65            "post" => Self::Post,
66            "get" => Self::Get,
67            "head" => Self::Head,
68            "put" => Self::Put,
69            "delete" => Self::Delete,
70            "options" => Self::Options,
71            "patch" => Self::Patch,
72            "trace" => Self::Trace,
73            _ => Self::None,
74        }
75    }
76    pub fn str(&mut self) -> &'static str {
77        match self {
78            Method::Post => "POST",
79            Method::Get => "GET",
80            Method::Head => "HEAD",
81            Method::Put => "PUT",
82            Method::Delete => "DELETE",
83            Method::Options => "OPTIONS",
84            Method::Patch => "PATCH",
85            Method::Trace => "TRACE",
86            Method::None => "",
87        }
88    }
89}
90#[derive(Debug, Clone)]
91pub enum ContentType {
92    FormData,
93    FormUrlencoded,
94    Json,
95    Xml,
96    Javascript,
97    Text,
98    Html,
99    Other(String),
100}
101impl ContentType {
102    #[must_use] pub fn from(name: &str) -> Self {
103        match name {
104            "multipart/form-data" => Self::FormData,
105            "application/x-www-form-urlencoded" => Self::FormUrlencoded,
106            "application/json" => Self::Json,
107            "application/xml" | "text/xml" => Self::Xml,
108            "application/javascript" => Self::Javascript,
109            "text/html" => Self::Html,
110            "text/plain" => Self::Text,
111            _ => Self::Other(name.to_string()),
112        }
113    }
114    pub fn str(&mut self) -> String {
115        match self {
116            Self::FormData => "multipart/form-data",
117            Self::FormUrlencoded => "application/x-www-form-urlencoded",
118            Self::Json => "application/json",
119            Self::Xml => "application/xml",
120            Self::Javascript => "application/javascript",
121            Self::Text => "text/plain",
122            Self::Html => "text/html",
123            Self::Other(name) => name,
124        }.to_string()
125    }
126}