use json::JsonValue;
#[derive(Clone, Debug)]
pub struct Request {
pub config: JsonValue,
pub method: Method,
pub url: String,
pub paths: Vec<String>,
pub query: JsonValue,
pub header: JsonValue,
pub cookie: JsonValue,
pub custom: JsonValue,
pub body: JsonValue,
pub client_ip: String,
pub server_ip: String,
pub user_agent: String,
pub request_time: i64,
pub handle_time: f64,
}
impl Default for Request {
fn default() -> Self {
Self {
config: JsonValue::Null,
method: Method::None,
url: String::new(),
paths: vec![],
query: JsonValue::Null,
header: JsonValue::Null,
cookie: JsonValue::Null,
custom: JsonValue::Null,
body: JsonValue::Null,
client_ip: String::new(),
server_ip: String::new(),
user_agent: String::new(),
request_time: 0,
handle_time: 0.0,
}
}
}
#[derive(Clone, Debug)]
pub enum Method {
Post,
Get,
Head,
Put,
Delete,
Options,
Patch,
Trace,
None,
}
impl Method {
#[must_use]
pub fn from(name: &str) -> Self {
match name.to_lowercase().as_str() {
"post" => Self::Post,
"get" => Self::Get,
"head" => Self::Head,
"put" => Self::Put,
"delete" => Self::Delete,
"options" => Self::Options,
"patch" => Self::Patch,
"trace" => Self::Trace,
_ => Self::None,
}
}
pub fn str(&self) -> &'static str {
match self {
Method::Post => "POST",
Method::Get => "GET",
Method::Head => "HEAD",
Method::Put => "PUT",
Method::Delete => "DELETE",
Method::Options => "OPTIONS",
Method::Patch => "PATCH",
Method::Trace => "TRACE",
Method::None => "",
}
}
}
#[derive(Debug, Clone)]
pub enum ContentType {
FormData,
FormUrlencoded,
Json,
Xml,
Javascript,
Text,
Html,
Stream,
Other(String),
}
impl ContentType {
#[must_use]
pub fn from(name: &str) -> Self {
match name {
"multipart/form-data" => Self::FormData,
"application/x-www-form-urlencoded" => Self::FormUrlencoded,
"application/json" => Self::Json,
"application/xml" | "text/xml" => Self::Xml,
"application/javascript" => Self::Javascript,
"text/html" => Self::Html,
"text/plain" => Self::Text,
"application/octet-stream" => Self::Stream,
_ => Self::Other(name.to_string()),
}
}
#[must_use]
pub fn str(&self) -> &str {
match self {
Self::FormData => "multipart/form-data",
Self::FormUrlencoded => "application/x-www-form-urlencoded",
Self::Json => "application/json",
Self::Xml => "application/xml",
Self::Javascript => "application/javascript",
Self::Text => "text/plain",
Self::Html => "text/html",
Self::Stream => "application/octet-stream",
Self::Other(name) => name,
}
}
}