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