pub mod body;
pub mod header;
pub mod localmap;
pub mod param;
use {
self::{localmap::LocalMap, param::Params},
cookie::{Cookie, CookieJar},
http::{header::HeaderMap, Request},
std::{marker::PhantomData, rc::Rc},
};
#[derive(Debug)]
pub struct Input<'task> {
pub request: &'task Request<()>,
pub params: &'task Option<Params<'task>>,
pub cookies: &'task mut Cookies<'task>,
pub locals: &'task mut LocalMap,
pub response_headers: &'task mut Option<HeaderMap>,
pub(crate) _marker: PhantomData<Rc<()>>,
}
#[derive(Debug)]
pub struct Cookies<'task> {
jar: &'task mut Option<CookieJar>,
request: &'task Request<()>,
_marker: PhantomData<Rc<()>>,
}
impl<'task> Cookies<'task> {
pub(crate) fn new(jar: &'task mut Option<CookieJar>, request: &'task Request<()>) -> Self {
Self {
jar,
request,
_marker: PhantomData,
}
}
pub fn jar(&mut self) -> crate::error::Result<&mut CookieJar> {
if let Some(ref mut jar) = self.jar {
return Ok(jar);
}
let jar = self.jar.get_or_insert_with(CookieJar::new);
for raw in self.request.headers().get_all(http::header::COOKIE) {
let raw_s = raw.to_str().map_err(crate::error::bad_request)?;
for s in raw_s.split(';').map(|s| s.trim()) {
let cookie = Cookie::parse_encoded(s)
.map_err(crate::error::bad_request)?
.into_owned();
jar.add_original(cookie);
}
}
Ok(jar)
}
}
#[cfg(feature = "secure")]
mod secure {
use crate::error::Result;
use cookie::{Key, PrivateJar, SignedJar};
impl<'a> super::Cookies<'a> {
#[inline]
pub fn signed_jar(&mut self, key: &Key) -> Result<SignedJar<'_>> {
Ok(self.jar()?.signed(key))
}
#[inline]
pub fn private_jar(&mut self, key: &Key) -> Result<PrivateJar<'_>> {
Ok(self.jar()?.private(key))
}
}
}