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}
27#[derive(Clone, Debug)]
28pub enum Method {
29    Post,
30    Get,
31    Head,
32    Put,
33    Delete,
34    Options,
35    Patch,
36    Trace,
37    None,
38}
39impl Method {
40    pub fn from(name: &str) -> Self {
41        match name.to_lowercase().as_str() {
42            "post" => Self::Post,
43            "get" => Self::Get,
44            "head" => Self::Head,
45            "put" => Self::Put,
46            "delete" => Self::Delete,
47            "options" => Self::Options,
48            "patch" => Self::Patch,
49            "trace" => Self::Trace,
50            _ => Self::None,
51        }
52    }
53    pub fn str(&mut self) -> &'static str {
54        match self {
55            Method::Post => "POST",
56            Method::Get => "GET",
57            Method::Head => "HEAD",
58            Method::Put => "PUT",
59            Method::Delete => "DELETE",
60            Method::Options => "OPTIONS",
61            Method::Patch => "PATCH",
62            Method::Trace => "TRACE",
63            Method::None => "",
64        }
65    }
66}
67#[derive(Debug, Clone)]
68pub enum ContentType {
69    FormData,
70    FormUrlencoded,
71    Json,
72    Xml,
73    Javascript,
74    Text,
75    Html,
76    Other(String),
77}
78impl ContentType {
79    pub fn from(name: &str) -> Self {
80        match name {
81            "multipart/form-data" => Self::FormData,
82            "application/x-www-form-urlencoded" => Self::FormUrlencoded,
83            "application/json" => Self::Json,
84            "application/xml" | "text/xml" => Self::Xml,
85            "application/javascript" => Self::Javascript,
86            "text/html" => Self::Html,
87            "text/plain" => Self::Text,
88            _ => Self::Other(name.to_string()),
89        }
90    }
91    pub fn str(&mut self) -> String {
92        match self {
93            Self::FormData => "multipart/form-data",
94            Self::FormUrlencoded => "application/x-www-form-urlencoded",
95            Self::Json => "application/json",
96            Self::Xml => "application/xml",
97            Self::Javascript => "application/javascript",
98            Self::Text => "text/plain",
99            Self::Html => "text/html",
100            Self::Other(name) => name,
101        }.to_string()
102    }
103}