volo_http/context/
server.rs

1//! Context and its utilities of server
2
3use volo::{
4    context::{Context, Reusable, Role, RpcCx, RpcInfo},
5    net::Address,
6    newtype_impl_context,
7};
8
9use crate::{
10    server::param::PathParamsVec,
11    utils::macros::{impl_deref_and_deref_mut, impl_getter},
12};
13
14/// RPC context of http server
15#[derive(Debug)]
16pub struct ServerContext(pub(crate) RpcCx<ServerCxInner, Config>);
17
18impl ServerContext {
19    /// Create a new [`ServerContext`] with the address of client
20    pub fn new(peer: Address) -> Self {
21        let mut cx = RpcCx::new(
22            RpcInfo::<Config>::with_role(Role::Server),
23            ServerCxInner::default(),
24        );
25        cx.rpc_info_mut().caller_mut().set_address(peer);
26        Self(cx)
27    }
28}
29
30impl_deref_and_deref_mut!(ServerContext, RpcCx<ServerCxInner, Config>, 0);
31
32newtype_impl_context!(ServerContext, Config, 0);
33
34/// Inner details of [`ServerContext`]
35#[derive(Clone, Debug, Default)]
36pub struct ServerCxInner {
37    /// Path params from [`Uri`]
38    ///
39    /// See [`Router::route`] and [`PathParamsVec`], [`PathParamsMap`] or [`PathParams`] for more
40    /// details.
41    ///
42    /// [`Uri`]: http::uri::Uri
43    /// [`Router::route`]: crate::server::route::Router::route
44    /// [`PathParamsMap`]: crate::server::param::PathParamsMap
45    /// [`PathParams`]: crate::server::param::PathParams
46    pub params: PathParamsVec,
47}
48
49impl ServerCxInner {
50    impl_getter!(params, PathParamsVec);
51}
52
53/// Configuration of the request
54///
55/// It is empty currently
56#[derive(Clone, Debug, Default)]
57pub struct Config {
58    #[cfg(feature = "__tls")]
59    tls: bool,
60}
61
62impl Config {
63    /// Return if the request is using TLS.
64    #[cfg(feature = "__tls")]
65    pub fn is_tls(&self) -> bool {
66        self.tls
67    }
68
69    #[cfg(feature = "__tls")]
70    pub(crate) fn set_tls(&mut self, tls: bool) {
71        self.tls = tls;
72    }
73}
74
75impl Reusable for Config {
76    fn clear(&mut self) {
77        *self = Default::default();
78    }
79}