Skip to content

Commit 40a4dff

Browse files
committed
feat: add getters for srv_conf and main_conf
Use as_ref() for pointer-to-reference casts.
1 parent 0e82e95 commit 40a4dff

File tree

1 file changed

+39
-16
lines changed

1 file changed

+39
-16
lines changed

src/http/request.rs

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -145,19 +145,46 @@ impl Request {
145145
unsafe { (*self.connection()).log }
146146
}
147147

148-
/// Module location configuration.
149-
fn get_module_loc_conf_ptr(&self, module: &ngx_module_t) -> *mut c_void {
150-
unsafe { *self.0.loc_conf.add(module.ctx_index) }
148+
/// Global configuration for a module.
149+
///
150+
/// Applies to the entire `http` block.
151+
///
152+
/// # Safety
153+
/// Caller must ensure that type `T` matches the configuration type for the specified module.
154+
pub fn get_module_main_conf<T>(&self, module: &ngx_module_t) -> Option<&'static T> {
155+
// SAFETY: main conf is either NULL or explicitly initialized by the module
156+
unsafe {
157+
let scf = *self.0.main_conf.add(module.ctx_index);
158+
scf.cast::<T>().as_ref()
159+
}
160+
}
161+
162+
/// Server-specific configuration for a module.
163+
///
164+
/// Applies to a single `server` block.
165+
///
166+
/// # Safety
167+
/// Caller must ensure that type `T` matches the configuration type for the specified module.
168+
pub fn get_module_srv_conf<T>(&self, module: &ngx_module_t) -> Option<&'static T> {
169+
// SAFETY: server conf is either NULL or explicitly initialized by the module
170+
unsafe {
171+
let scf = *self.0.srv_conf.add(module.ctx_index);
172+
scf.cast::<T>().as_ref()
173+
}
151174
}
152175

153-
/// Module location configuration.
176+
/// Location-specific configuration for a module.
177+
///
178+
/// Applies to a signle `location`, `if` or `limit_except` block.
179+
///
180+
/// # Safety
181+
/// Caller must ensure that type `T` matches the configuration type for the specified module.
154182
pub fn get_module_loc_conf<T>(&self, module: &ngx_module_t) -> Option<&'static T> {
155-
let lc_prt = self.get_module_loc_conf_ptr(module) as *mut T;
156-
if lc_prt.is_null() {
157-
return None;
183+
// SAFETY: location conf is either NULL or explicitly initialized by the module
184+
unsafe {
185+
let lcf = *self.0.loc_conf.add(module.ctx_index);
186+
lcf.cast::<T>().as_ref()
158187
}
159-
let lc = unsafe { &*lc_prt };
160-
Some(lc)
161188
}
162189

163190
/// Get Module context pointer
@@ -167,13 +194,9 @@ impl Request {
167194

168195
/// Get Module context
169196
pub fn get_module_ctx<T>(&self, module: &ngx_module_t) -> Option<&T> {
170-
let cf = self.get_module_ctx_ptr(module) as *mut T;
171-
172-
if cf.is_null() {
173-
return None;
174-
}
175-
let co = unsafe { &*cf };
176-
Some(co)
197+
let ctx = self.get_module_ctx_ptr(module) as *const T;
198+
// SAFETY: ctx is either NULL or explicitly initialized by the module
199+
unsafe { ctx.as_ref() }
177200
}
178201

179202
/// Sets the value as the module's context.

0 commit comments

Comments
 (0)