ngx/http/
conf.rs

1use ::core::ptr::NonNull;
2
3use crate::ffi::{
4    ngx_http_conf_ctx_t, ngx_http_core_srv_conf_t, ngx_http_request_t,
5    ngx_http_upstream_srv_conf_t, ngx_module_t,
6};
7use crate::http::HttpModule;
8
9/// Utility trait for types containing HTTP module configuration
10pub trait HttpModuleConfExt {
11    /// Get a non-null reference to the main configuration structure for HTTP module
12    ///
13    /// # Safety
14    /// Caller must ensure that type `T` matches the configuration type for the specified module.
15    #[inline]
16    unsafe fn http_main_conf_unchecked<T>(&self, _module: &ngx_module_t) -> Option<NonNull<T>> {
17        None
18    }
19
20    /// Get a non-null reference to the server configuration structure for HTTP module
21    ///
22    /// # Safety
23    /// Caller must ensure that type `T` matches the configuration type for the specified module.
24    #[inline]
25    unsafe fn http_server_conf_unchecked<T>(&self, _module: &ngx_module_t) -> Option<NonNull<T>> {
26        None
27    }
28
29    /// Get a non-null reference to the location configuration structure for HTTP module
30    ///
31    /// Applies to a single `location`, `if` or `limit_except` block
32    ///
33    /// # Safety
34    /// Caller must ensure that type `T` matches the configuration type for the specified module.
35    #[inline]
36    unsafe fn http_location_conf_unchecked<T>(&self, _module: &ngx_module_t) -> Option<NonNull<T>> {
37        None
38    }
39}
40
41impl HttpModuleConfExt for crate::ffi::ngx_cycle_t {
42    #[inline]
43    unsafe fn http_main_conf_unchecked<T>(&self, module: &ngx_module_t) -> Option<NonNull<T>> {
44        let http_conf = self
45            .conf_ctx
46            .add(nginx_sys::ngx_http_module.index)
47            .as_ref()?;
48        let conf_ctx = (*http_conf).cast::<ngx_http_conf_ctx_t>();
49        let conf_ctx = conf_ctx.as_ref()?;
50        NonNull::new((*conf_ctx.main_conf.add(module.ctx_index)).cast())
51    }
52}
53
54impl HttpModuleConfExt for crate::ffi::ngx_conf_t {
55    #[inline]
56    unsafe fn http_main_conf_unchecked<T>(&self, module: &ngx_module_t) -> Option<NonNull<T>> {
57        let conf_ctx = self.ctx.cast::<ngx_http_conf_ctx_t>();
58        let conf_ctx = conf_ctx.as_ref()?;
59        NonNull::new((*conf_ctx.main_conf.add(module.ctx_index)).cast())
60    }
61
62    #[inline]
63    unsafe fn http_server_conf_unchecked<T>(&self, module: &ngx_module_t) -> Option<NonNull<T>> {
64        let conf_ctx = self.ctx.cast::<ngx_http_conf_ctx_t>();
65        let conf_ctx = conf_ctx.as_ref()?;
66        NonNull::new((*conf_ctx.srv_conf.add(module.ctx_index)).cast())
67    }
68
69    #[inline]
70    unsafe fn http_location_conf_unchecked<T>(&self, module: &ngx_module_t) -> Option<NonNull<T>> {
71        let conf_ctx = self.ctx.cast::<ngx_http_conf_ctx_t>();
72        let conf_ctx = conf_ctx.as_ref()?;
73        NonNull::new((*conf_ctx.loc_conf.add(module.ctx_index)).cast())
74    }
75}
76
77impl HttpModuleConfExt for ngx_http_core_srv_conf_t {
78    #[inline]
79    unsafe fn http_main_conf_unchecked<T>(&self, module: &ngx_module_t) -> Option<NonNull<T>> {
80        let conf_ctx = self.ctx.as_ref()?;
81        NonNull::new((*conf_ctx.main_conf.add(module.ctx_index)).cast())
82    }
83
84    #[inline]
85    unsafe fn http_server_conf_unchecked<T>(&self, module: &ngx_module_t) -> Option<NonNull<T>> {
86        let conf_ctx = self.ctx.as_ref()?;
87        NonNull::new((*conf_ctx.srv_conf.add(module.ctx_index)).cast())
88    }
89
90    #[inline]
91    unsafe fn http_location_conf_unchecked<T>(&self, module: &ngx_module_t) -> Option<NonNull<T>> {
92        let conf_ctx = self.ctx.as_ref()?;
93        NonNull::new((*conf_ctx.loc_conf.add(module.ctx_index)).cast())
94    }
95}
96
97impl HttpModuleConfExt for ngx_http_request_t {
98    #[inline]
99    unsafe fn http_main_conf_unchecked<T>(&self, module: &ngx_module_t) -> Option<NonNull<T>> {
100        NonNull::new((*self.main_conf.add(module.ctx_index)).cast())
101    }
102
103    #[inline]
104    unsafe fn http_server_conf_unchecked<T>(&self, module: &ngx_module_t) -> Option<NonNull<T>> {
105        NonNull::new((*self.srv_conf.add(module.ctx_index)).cast())
106    }
107
108    #[inline]
109    unsafe fn http_location_conf_unchecked<T>(&self, module: &ngx_module_t) -> Option<NonNull<T>> {
110        NonNull::new((*self.loc_conf.add(module.ctx_index)).cast())
111    }
112}
113
114impl HttpModuleConfExt for ngx_http_upstream_srv_conf_t {
115    #[inline]
116    unsafe fn http_server_conf_unchecked<T>(&self, module: &ngx_module_t) -> Option<NonNull<T>> {
117        let conf = self.srv_conf;
118        if conf.is_null() {
119            return None;
120        }
121        NonNull::new((*conf.add(module.ctx_index)).cast())
122    }
123}
124
125/// Trait to define and access main module configuration
126///
127/// # Safety
128/// Caller must ensure that type `HttpModuleMainConf::MainConf` matches the configuration type
129/// for the specified module.
130pub unsafe trait HttpModuleMainConf: HttpModule {
131    /// Type for main module configuration
132    type MainConf;
133    /// Get reference to main module configuration
134    fn main_conf(o: &impl HttpModuleConfExt) -> Option<&'static Self::MainConf> {
135        unsafe { Some(o.http_main_conf_unchecked(Self::module())?.as_ref()) }
136    }
137    /// Get mutable reference to main module configuration
138    fn main_conf_mut(o: &impl HttpModuleConfExt) -> Option<&'static mut Self::MainConf> {
139        unsafe { Some(o.http_main_conf_unchecked(Self::module())?.as_mut()) }
140    }
141}
142
143/// Trait to define and access server-specific module configuration
144///
145/// # Safety
146/// Caller must ensure that type `HttpModuleServerConf::ServerConf` matches the configuration type
147/// for the specified module.
148pub unsafe trait HttpModuleServerConf: HttpModule {
149    /// Type for server-specific module configuration
150    type ServerConf;
151    /// Get reference to server-specific module configuration
152    fn server_conf(o: &impl HttpModuleConfExt) -> Option<&'static Self::ServerConf> {
153        unsafe { Some(o.http_server_conf_unchecked(Self::module())?.as_ref()) }
154    }
155    /// Get mutable reference to server-specific module configuration
156    fn server_conf_mut(o: &impl HttpModuleConfExt) -> Option<&'static mut Self::ServerConf> {
157        unsafe { Some(o.http_server_conf_unchecked(Self::module())?.as_mut()) }
158    }
159}
160
161/// Trait to define and access location-specific module configuration
162///
163/// Applies to a single `location`, `if` or `limit_except` block
164///
165/// # Safety
166/// Caller must ensure that type `HttpModuleLocationConf::LocationConf` matches the configuration
167/// type for the specified module.
168pub unsafe trait HttpModuleLocationConf: HttpModule {
169    /// Type for location-specific module configuration
170    type LocationConf;
171    /// Get reference to location-specific module configuration
172    fn location_conf(o: &impl HttpModuleConfExt) -> Option<&'static Self::LocationConf> {
173        unsafe { Some(o.http_location_conf_unchecked(Self::module())?.as_ref()) }
174    }
175    /// Get mutable reference to location-specific module configuration
176    fn location_conf_mut(o: &impl HttpModuleConfExt) -> Option<&'static mut Self::LocationConf> {
177        unsafe { Some(o.http_location_conf_unchecked(Self::module())?.as_mut()) }
178    }
179}
180
181mod core {
182    use crate::ffi::{
183        ngx_http_core_loc_conf_t, ngx_http_core_main_conf_t, ngx_http_core_module,
184        ngx_http_core_srv_conf_t,
185    };
186
187    /// Auxiliary structure to access `ngx_http_core_module` configuration.
188    pub struct NgxHttpCoreModule;
189
190    impl crate::http::HttpModule for NgxHttpCoreModule {
191        fn module() -> &'static crate::ffi::ngx_module_t {
192            unsafe { &*::core::ptr::addr_of!(ngx_http_core_module) }
193        }
194    }
195    unsafe impl crate::http::HttpModuleMainConf for NgxHttpCoreModule {
196        type MainConf = ngx_http_core_main_conf_t;
197    }
198    unsafe impl crate::http::HttpModuleServerConf for NgxHttpCoreModule {
199        type ServerConf = ngx_http_core_srv_conf_t;
200    }
201    unsafe impl crate::http::HttpModuleLocationConf for NgxHttpCoreModule {
202        type LocationConf = ngx_http_core_loc_conf_t;
203    }
204}
205
206pub use core::NgxHttpCoreModule;
207
208#[cfg(ngx_feature = "http_ssl")]
209mod ssl {
210    use crate::ffi::{ngx_http_ssl_module, ngx_http_ssl_srv_conf_t};
211
212    /// Auxiliary structure to access `ngx_http_ssl_module` configuration.
213    pub struct NgxHttpSslModule;
214
215    impl crate::http::HttpModule for NgxHttpSslModule {
216        fn module() -> &'static crate::ffi::ngx_module_t {
217            unsafe { &*::core::ptr::addr_of!(ngx_http_ssl_module) }
218        }
219    }
220    unsafe impl crate::http::HttpModuleServerConf for NgxHttpSslModule {
221        type ServerConf = ngx_http_ssl_srv_conf_t;
222    }
223}
224#[cfg(ngx_feature = "http_ssl")]
225pub use ssl::NgxHttpSslModule;
226
227mod upstream {
228    use crate::ffi::{
229        ngx_http_upstream_main_conf_t, ngx_http_upstream_module, ngx_http_upstream_srv_conf_t,
230    };
231
232    /// Auxiliary structure to access `ngx_http_upstream_module` configuration.
233    pub struct NgxHttpUpstreamModule;
234
235    impl crate::http::HttpModule for NgxHttpUpstreamModule {
236        fn module() -> &'static crate::ffi::ngx_module_t {
237            unsafe { &*::core::ptr::addr_of!(ngx_http_upstream_module) }
238        }
239    }
240    unsafe impl crate::http::HttpModuleMainConf for NgxHttpUpstreamModule {
241        type MainConf = ngx_http_upstream_main_conf_t;
242    }
243    unsafe impl crate::http::HttpModuleServerConf for NgxHttpUpstreamModule {
244        type ServerConf = ngx_http_upstream_srv_conf_t;
245    }
246}
247
248pub use upstream::NgxHttpUpstreamModule;
249
250#[cfg(all(nginx1_25_1, ngx_feature = "http_v2"))]
251mod http_v2 {
252    use crate::ffi::{ngx_http_v2_module, ngx_http_v2_srv_conf_t};
253
254    /// Auxiliary structure to access `ngx_http_v2_module` configuration.
255    pub struct NgxHttpV2Module;
256
257    impl crate::http::HttpModule for NgxHttpV2Module {
258        fn module() -> &'static crate::ffi::ngx_module_t {
259            unsafe { &*::core::ptr::addr_of!(ngx_http_v2_module) }
260        }
261    }
262    unsafe impl crate::http::HttpModuleServerConf for NgxHttpV2Module {
263        type ServerConf = ngx_http_v2_srv_conf_t;
264    }
265}
266// ngx_http_v2_module was not exposed by default until aefd862a
267#[cfg(all(nginx1_25_1, ngx_feature = "http_v2"))]
268pub use http_v2::NgxHttpV2Module;
269
270#[cfg(ngx_feature = "http_v3")]
271mod http_v3 {
272    use crate::ffi::{ngx_http_v3_module, ngx_http_v3_srv_conf_t};
273
274    /// Auxiliary structure to access `ngx_http_v3_module` configuration.
275    pub struct NgxHttpV3Module;
276
277    impl crate::http::HttpModule for NgxHttpV3Module {
278        fn module() -> &'static crate::ffi::ngx_module_t {
279            unsafe { &*::core::ptr::addr_of!(ngx_http_v3_module) }
280        }
281    }
282    unsafe impl crate::http::HttpModuleServerConf for NgxHttpV3Module {
283        type ServerConf = ngx_http_v3_srv_conf_t;
284    }
285}
286
287#[cfg(ngx_feature = "http_v3")]
288pub use http_v3::NgxHttpV3Module;