use super::*;
impl Default for Context {
#[inline(always)]
fn default() -> Self {
Self {
request: Request::default(),
response: Response::default(),
route_params: RouteParams::default(),
original_target: None,
original_head: None,
attributes: ThreadSafeAttributeStore::default(),
}
}
}
impl PartialEq for Context {
#[inline(always)]
fn eq(&self, other: &Self) -> bool {
self.get_request() == other.get_request()
&& self.get_response() == other.get_response()
&& self.get_route_params() == other.get_route_params()
&& self.get_attributes().len() == other.get_attributes().len()
}
}
impl Eq for Context {}
impl Context {
#[inline(always)]
#[must_use]
pub fn build_response(&mut self) -> ResponseData {
let is_head: bool = self
.try_get_original_head()
.unwrap_or_else(|| self.get_request().get_method().is_head());
if is_head {
self.get_mut_response().build_head()
} else {
self.get_mut_response().build()
}
}
}
impl From<usize> for &'static Context {
#[inline(always)]
fn from(address: usize) -> &'static Context {
unsafe { &*(address as *const Context) }
}
}
impl<'a> From<usize> for &'a mut Context {
#[inline(always)]
fn from(address: usize) -> &'a mut Context {
unsafe { &mut *(address as *mut Context) }
}
}
impl From<&Context> for usize {
#[inline(always)]
fn from(ctx: &Context) -> Self {
ctx as *const Context as usize
}
}
impl From<&mut Context> for usize {
#[inline(always)]
fn from(ctx: &mut Context) -> Self {
ctx as *mut Context as usize
}
}
impl AsRef<Context> for Context {
#[inline(always)]
fn as_ref(&self) -> &Self {
let address: usize = self.into();
address.into()
}
}
impl AsMut<Context> for Context {
#[inline(always)]
fn as_mut(&mut self) -> &mut Self {
let address: usize = self.into();
address.into()
}
}
impl Lifetime for Context {
#[inline(always)]
unsafe fn leak(&self) -> &'static Self {
let address: usize = self.into();
address.into()
}
#[inline(always)]
unsafe fn leak_mut(&self) -> &'static mut Self {
let address: usize = self.into();
address.into()
}
}
impl Context {
pub(crate) fn reset_request_attributes(&mut self) -> &mut Self {
self.get_mut_attributes().clear();
self
}
#[inline(always)]
pub(crate) fn clear_route_params(&mut self) -> &mut Self {
let params: &mut RouteParams = self.get_mut_route_params();
if !params.is_empty() {
for pair in params.drain() {
return_route_param_entry(pair);
}
}
self
}
#[inline(always)]
pub fn try_get_route_param<T>(&self, name: T) -> Option<String>
where
T: AsRef<str>,
{
self.get_route_params().get(name.as_ref()).cloned()
}
#[inline(always)]
pub fn get_route_param<T>(&self, name: T) -> String
where
T: AsRef<str>,
{
self.try_get_route_param(name).unwrap()
}
pub fn get_request_target(&self) -> Option<String> {
self.get_request_target_ref().cloned()
}
pub(crate) fn get_request_target_ref(&self) -> Option<&String> {
self.original_target.as_ref()
}
#[inline(always)]
pub(crate) fn take_original_target(&mut self) -> Option<String> {
self.get_mut_original_target().take()
}
pub(crate) fn set_request_target<T>(&mut self, target: T) -> &mut Self
where
T: AsRef<str> + Into<String>,
{
let stored: &mut Option<String> = &mut self.original_target;
match stored {
Some(existing) => {
existing.clear();
existing.push_str(target.as_ref());
}
None => {
*stored = Some(target.into());
}
}
self
}
#[inline(always)]
fn is_plain_external_key(key: &str) -> bool {
key.len() <= EXTERNAL_ATTR_STACK_LIMIT
&& key
.bytes()
.all(|byte: u8| (0x20..0x7f).contains(&byte) && byte != b'"' && byte != b'\\')
}
#[inline(always)]
fn format_plain_external_key<'a>(key: &str, buf: &'a mut [u8; 128]) -> &'a str {
let prefix_len: usize = EXTERNAL_ATTR_PREFIX.len();
let key_bytes: &[u8] = key.as_bytes();
let suffix_len: usize = EXTERNAL_ATTR_SUFFIX.len();
buf[..prefix_len].copy_from_slice(EXTERNAL_ATTR_PREFIX);
buf[prefix_len..prefix_len + key_bytes.len()].copy_from_slice(key_bytes);
let suffix_start: usize = prefix_len + key_bytes.len();
buf[suffix_start..suffix_start + suffix_len].copy_from_slice(EXTERNAL_ATTR_SUFFIX);
let total: usize = suffix_start + suffix_len;
from_utf8(&buf[..total]).unwrap_or_default()
}
#[inline(always)]
fn internal_attribute_key(key: InternalAttribute) -> &'static str {
match key {
InternalAttribute::TaskPanicData => INTERNAL_TASK_PANIC_KEY,
InternalAttribute::RequestErrorData => INTERNAL_REQUEST_ERROR_KEY,
}
}
#[inline(always)]
pub fn try_get_attribute<V, K>(&self, key: K) -> Option<V>
where
V: AnySendSyncClone,
K: AsRef<str>,
{
let key: &str = key.as_ref();
if key == REQUEST_TARGET_ATTRIBUTE {
let target: Option<&String> = self.get_request_target_ref();
return match target {
Some(target) => {
let value: ArcAnySendSync = Arc::new(target.clone());
value.downcast_ref::<V>().cloned()
}
None => None,
};
}
if Self::is_plain_external_key(key) {
let mut stack_buf: [u8; 128] = [0; 128];
let formatted: &str = Self::format_plain_external_key(key, &mut stack_buf);
return self
.get_attributes()
.get(formatted)
.and_then(|arc: &ArcAnySendSync| arc.downcast_ref::<V>())
.cloned();
}
self.get_attributes()
.get(&Attribute::External(key.to_owned()).to_string())
.and_then(|arc: &ArcAnySendSync| arc.downcast_ref::<V>())
.cloned()
}
#[inline(always)]
pub fn get_attribute<V, K>(&self, key: K) -> V
where
V: AnySendSyncClone,
K: AsRef<str>,
{
self.try_get_attribute(key).unwrap()
}
#[inline(always)]
pub fn set_attribute<K, V>(&mut self, key: K, value: V) -> &mut Self
where
K: AsRef<str>,
V: AnySendSyncClone,
{
let key_str: &str = key.as_ref();
let map_key: String = if Self::is_plain_external_key(key_str) {
let mut stack_buf: [u8; 128] = [0; 128];
Self::format_plain_external_key(key_str, &mut stack_buf).to_owned()
} else {
Attribute::External(key_str.to_owned()).to_string()
};
self.get_mut_attributes().insert(map_key, Arc::new(value));
self
}
#[inline(always)]
pub fn remove_attribute<K>(&mut self, key: K) -> &mut Self
where
K: AsRef<str>,
{
let key_str: &str = key.as_ref();
if Self::is_plain_external_key(key_str) {
let mut stack_buf: [u8; 128] = [0; 128];
let formatted: &str = Self::format_plain_external_key(key_str, &mut stack_buf);
self.get_mut_attributes().remove(formatted);
return self;
}
self.get_mut_attributes()
.remove(&Attribute::External(key_str.to_owned()).to_string());
self
}
#[inline(always)]
pub fn clear_attribute(&mut self) -> &mut Self {
self.get_mut_attributes().clear();
self
}
#[inline(always)]
fn try_get_internal_attribute<V>(&self, key: InternalAttribute) -> Option<V>
where
V: AnySendSyncClone,
{
self.get_attributes()
.get(Self::internal_attribute_key(key))
.and_then(|arc: &ArcAnySendSync| arc.downcast_ref::<V>())
.cloned()
}
#[inline(always)]
fn get_internal_attribute<V>(&self, key: InternalAttribute) -> V
where
V: AnySendSyncClone,
{
self.try_get_internal_attribute(key).unwrap()
}
#[inline(always)]
fn set_internal_attribute<V>(&mut self, key: InternalAttribute, value: V) -> &mut Self
where
V: AnySendSyncClone,
{
self.get_mut_attributes().insert(
Self::internal_attribute_key(key).to_owned(),
Arc::new(value),
);
self
}
#[inline(always)]
pub fn set_task_panic(&mut self, panic_data: PanicData) -> &mut Self {
self.set_internal_attribute(InternalAttribute::TaskPanicData, panic_data)
}
#[inline(always)]
pub fn try_get_task_panic_data(&self) -> Option<PanicData> {
self.try_get_internal_attribute(InternalAttribute::TaskPanicData)
}
#[inline(always)]
pub fn get_task_panic_data(&self) -> PanicData {
self.get_internal_attribute(InternalAttribute::TaskPanicData)
}
#[inline(always)]
pub(crate) fn set_request_error_data(&mut self, request_error: RequestError) -> &mut Self {
self.set_internal_attribute(InternalAttribute::RequestErrorData, request_error)
}
#[inline(always)]
pub fn try_get_request_error_data(&self) -> Option<RequestError> {
self.try_get_internal_attribute(InternalAttribute::RequestErrorData)
}
#[inline(always)]
pub fn get_request_error_data(&self) -> RequestError {
self.get_internal_attribute(InternalAttribute::RequestErrorData)
}
}