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