Skip to main content

framework_web/
request.rs

1use bytes::Bytes;
2use framework_core::MultiStringValue;
3use std::fmt::{Display, Formatter};
4
5#[derive(Debug, PartialEq, Eq, Hash, Clone)]
6pub enum WebMethod {
7    Get,
8    Post,
9    Put,
10    Patch,
11    Delete,
12    Options,
13    Other(String),
14}
15
16impl WebMethod {
17    pub fn from_name(value: &str) -> Self {
18        let value = value.to_ascii_uppercase();
19        match value.as_str() {
20            "GET" => Self::Get,
21            "POST" => Self::Post,
22            "PUT" => Self::Put,
23            "PATCH" => Self::Patch,
24            "DELETE" => Self::Delete,
25            "OPTIONS" => Self::Options,
26            _ => Self::Other(value),
27        }
28    }
29}
30
31impl Display for WebMethod {
32    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
33        match self {
34            Self::Get => formatter.write_str("GET"),
35            Self::Post => formatter.write_str("POST"),
36            Self::Put => formatter.write_str("PUT"),
37            Self::Patch => formatter.write_str("PATCH"),
38            Self::Delete => formatter.write_str("DELETE"),
39            Self::Options => formatter.write_str("OPTIONS"),
40            Self::Other(value) => formatter.write_str(value),
41        }
42    }
43}
44
45#[derive(Debug)]
46pub struct WebRequest {
47    pub method: WebMethod,
48    pub scheme: String,
49    pub authority: String,
50    pub path: String,
51    pub headers: MultiStringValue,
52    pub query: MultiStringValue,
53    pub body: Bytes,
54    pub client_ip: Option<String>,
55    /// 链路标识:优先取 `x-trace-id` 请求头,缺失时由服务器生成。
56    pub trace_id: String,
57    /// 服务器收到请求的时刻(毫秒时间戳)。
58    pub receive_time: i64,
59}