spacegate_kernel/
extension.rs

1mod reflect;
2pub use reflect::*;
3mod gateway_name;
4pub use gateway_name::*;
5mod matched;
6pub use matched::*;
7mod peer_addr;
8pub use peer_addr::*;
9mod backend_host;
10pub use backend_host::*;
11mod enter_time;
12pub use enter_time::*;
13mod request_id;
14pub use defer::*;
15mod defer;
16pub use original_ip_addr::*;
17mod original_ip_addr;
18pub use is_east_west_traffic::*;
19
20use crate::{extractor::OptionalExtract, injector::Inject};
21mod is_east_west_traffic;
22pub mod user_group;
23/// Just extract and attach the extension to the request
24#[derive(Debug, Clone)]
25pub struct Extension<E>(pub E);
26
27impl<E> Extension<E> {
28    pub const fn new(e: E) -> Self {
29        Self(e)
30    }
31
32    pub fn into_inner(self) -> E {
33        self.0
34    }
35    pub const fn inner(&self) -> &E {
36        &self.0
37    }
38}
39
40impl<E: Clone + Send + Sync + 'static> Inject for Extension<E> {
41    fn inject(&self, req: &mut hyper::Request<crate::SgBody>) -> crate::BoxResult<()> {
42        req.extensions_mut().insert(self.0.clone());
43        Ok(())
44    }
45}
46
47impl<E: Clone + Send + Sync + 'static> OptionalExtract for Extension<E> {
48    fn extract(req: &hyper::Request<crate::SgBody>) -> Option<Self> {
49        req.extensions().get::<E>().map(|e| Self(e.clone()))
50    }
51}
52
53/// FromBackend is a marker type to indicate that the response is from backend.
54#[derive(Debug, Clone, Copy)]
55pub struct FromBackend {
56    _priv: (),
57}
58
59impl FromBackend {
60    /// # Safety
61    ///
62    /// **Ensure** the response is from the **real backend**, do not cheat on users of this type.
63    pub const unsafe fn new() -> Self {
64        Self { _priv: () }
65    }
66}