use std::collections::HashMap;
use actix_web::{web::Payload, HttpRequest};
use crate::types::method::Method;
use super::{form::Form, ua::UserAgent};
#[allow(dead_code)]
pub struct Request {
pub host: String,
pub port: u16,
pub method: Method,
pub user_agent: UserAgent,
pub headers: Vec<(String, String)>,
pub body: String,
pub query: Vec<(String, String)>,
pub path: String,
pub actix: HttpRequest,
pub stream: Payload,
pub params: HashMap<String, String>,
}
impl Request {
pub fn new(method: Method, host: String, port: u16, user_agent: UserAgent, headers: Vec<(String, String)>, body: String, query: Vec<(String, String)>, path: String, actix: HttpRequest, stream: Payload, params: HashMap<String, String>) -> Self {
Request {
host,
port,
method,
user_agent,
headers,
body,
query,
path,
actix,
stream,
params,
}
}
pub fn get_header(&self, key: &str) -> Option<&str> {
for (header_key, header_value) in &self.headers {
if header_key == key {
return Some(header_value);
}
}
None
}
pub fn get_query(&self, key: &str) -> Option<&str> {
for (query_key, query_value) in &self.query {
if query_key == key {
return Some(query_value);
}
}
None
}
pub fn get_segment(&self, index: usize) -> Option<&str> {
let segments: Vec<&str> = self.path.split("/").collect();
if segments.len() > index {
return Some(segments[index]);
}
None
}
pub fn get_param(&self, name: &str) -> Option<&str> {
return self.params.get(name).map(|v| v.as_str());
}
pub fn set_param(&mut self, index: &str, value: &str) {
self.params.entry(index.to_string()).or_insert(value.to_string());
}
pub fn get_actix(&self) -> &HttpRequest {
&self.actix
}
pub fn get_actix_mut(&mut self) -> &mut HttpRequest {
&mut self.actix
}
pub fn body_as_json(&self) -> serde_json::Value {
serde_json::from_str(&self.body).unwrap()
}
pub fn body_as_text(&self) -> String {
self.body.clone()
}
pub fn body_as_form(&self) -> Form {
Form::decode(&self.body, self.get_header("Content-Type").unwrap())
}
}