Skip to main content

volo_http/context/
server.rs

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