tiny_web/sys/
request.rs

1use std::{collections::HashMap, str::FromStr};
2
3use serde_json::Value;
4
5/// Describes received files
6///
7/// # Values
8///
9/// * `size: usize` - File size.
10/// * `name: String` - File name.
11/// * `tmp: std::path::PathBuf` - Absolute path to file location.
12#[derive(Debug)]
13pub struct WebFile {
14    /// File size
15    pub size: usize,
16    /// File name
17    pub name: String,
18    /// Absolute path to file location
19    pub tmp: std::path::PathBuf,
20}
21
22/// Raw HTTP data if the HTTP POST data was not recognized
23#[derive(Debug)]
24pub enum RawData {
25    None,
26    Json(Value),
27    String(String),
28    Raw(Vec<u8>),
29}
30
31/// Input http protocol datas
32///
33/// # Values
34///
35/// * `get: HashMap<String, String>` - GET data.
36/// * `post: HashMap<String, String>` - POST data.
37/// * `file: HashMap<String, Vec<WebFile>>` - FILE data.
38/// * `cookie: HashMap<String, String>` - Cookies.
39/// * `params: HashMap<String, String>` - Params from web servers.
40#[derive(Debug)]
41pub struct Input {
42    /// GET data
43    pub get: HashMap<String, String>,
44    /// POST data
45    pub post: HashMap<String, String>,
46    /// FILE data
47    pub file: HashMap<String, Vec<WebFile>>,
48    /// Cookies
49    pub cookie: HashMap<String, String>,
50    /// Params from web servers
51    pub params: HashMap<String, String>,
52    /// Raw data
53    pub raw: RawData,
54}
55
56/// Http version
57#[derive(Debug, Clone, PartialEq)]
58pub enum HttpVersion {
59    None,
60    HTTP1_0,
61    HTTP1_1,
62    HTTP2,
63}
64
65/// Request parameters
66///
67///  # Values
68///
69/// * `ajax: bool` - Ajax query (only software detect).
70/// * `host: String` - Request host. Example: subdomain.domain.zone.
71/// * `scheme: String` - Request scheme. Example: http / https.
72/// * `agent: String` - HTTP_USER_AGENT.
73/// * `referer: String` - HTTP_REFERER.
74/// * `ip: String` - Client IP.
75/// * `method: String` - REQUEST_METHOD.
76/// * `path: String` - DOCUMENT_ROOT.
77/// * `url: String` - Request url. Example: /product/view/item/145
78/// * `input: Input` - Input http protocol datas.
79#[derive(Debug)]
80pub struct Request {
81    /// Ajax query (only software detect)
82    pub ajax: bool,
83    /// Request host. Example: subdomain.domain.zone
84    pub host: String,
85    /// Request scheme. Example: http / https
86    pub scheme: String,
87    /// HTTP_USER_AGENT
88    pub agent: String,
89    /// HTTP_REFERER
90    pub referer: String,
91    /// Client IP
92    pub ip: String,
93    /// REQUEST_METHOD
94    pub method: HttpMethod,
95    /// DOCUMENT_ROOT
96    pub path: String,
97    /// Request url. Example: /product/view/item/145
98    pub url: String,
99    /// Input http protocol datas
100    pub input: Input,
101    /// Site name. Example: https://example.com
102    pub site: String,
103    /// Http version
104    pub version: HttpVersion,
105}
106
107/// HTTP Methods
108#[derive(Debug, Clone)]
109pub enum HttpMethod {
110    Get,
111    Head,
112    Post,
113    Put,
114    Delete,
115    Connect,
116    Options,
117    Trace,
118    Patch,
119    Other(String),
120}
121impl FromStr for HttpMethod {
122    type Err = ();
123
124    fn from_str(method: &str) -> Result<Self, Self::Err> {
125        Ok(match method {
126            "GET" => HttpMethod::Get,
127            "HEAD" => HttpMethod::Head,
128            "POST" => HttpMethod::Post,
129            "PUT" => HttpMethod::Put,
130            "DELETE" => HttpMethod::Delete,
131            "CONNECT" => HttpMethod::Connect,
132            "OPTIONS" => HttpMethod::Options,
133            "TRACE" => HttpMethod::Trace,
134            "PATCH" => HttpMethod::Patch,
135            _ => HttpMethod::Other(method.to_owned()),
136        })
137    }
138}