br_addon/
request.rs

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