Skip to main content

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