Skip to content

Commit e47f42d

Browse files
committed
feat: add getters for srv_conf and main_conf
Use as_ref() for pointer-to-reference casts.
1 parent 3dd7ea5 commit e47f42d

File tree

1 file changed

+43
-16
lines changed

1 file changed

+43
-16
lines changed

src/http/request.rs

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -145,19 +145,49 @@ 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 allocated with ngx_p(c)alloc and
156+
// explicitly initialized by the module
157+
unsafe {
158+
let scf = *self.0.main_conf.add(module.ctx_index);
159+
scf.cast::<T>().as_ref()
160+
}
161+
}
162+
163+
/// Server-specific configuration for a module.
164+
///
165+
/// Applies to a single `server` block.
166+
///
167+
/// # Safety
168+
/// Caller must ensure that type `T` matches the configuration type for the specified module.
169+
pub fn get_module_srv_conf<T>(&self, module: &ngx_module_t) -> Option<&'static T> {
170+
// SAFETY: server conf is either NULL or allocated with ngx_p(c)alloc and
171+
// explicitly initialized by the module
172+
unsafe {
173+
let scf = *self.0.srv_conf.add(module.ctx_index);
174+
scf.cast::<T>().as_ref()
175+
}
151176
}
152177

153-
/// Module location configuration.
178+
/// Location-specific configuration for a module.
179+
///
180+
/// Applies to a signle `location`, `if` or `limit_except` block.
181+
///
182+
/// # Safety
183+
/// Caller must ensure that type `T` matches the configuration type for the specified module.
154184
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;
185+
// SAFETY: location conf is either NULL or allocated with ngx_p(c)alloc and
186+
// explicitly initialized by the module
187+
unsafe {
188+
let lcf = *self.0.loc_conf.add(module.ctx_index);
189+
lcf.cast::<T>().as_ref()
158190
}
159-
let lc = unsafe { &*lc_prt };
160-
Some(lc)
161191
}
162192

163193
/// Get Module context pointer
@@ -167,13 +197,10 @@ impl Request {
167197

168198
/// Get Module context
169199
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)
200+
let ctx = self.get_module_ctx_ptr(module).cast::<T>();
201+
// SAFETY: ctx is either NULL or allocated with ngx_p(c)alloc and
202+
// explicitly initialized by the module
203+
unsafe { ctx.as_ref() }
177204
}
178205

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

0 commit comments

Comments
 (0)