Skip to content

Commit 5aa6a43

Browse files
committed
feat: trait for module request context
Introduce RequestContext trait for managing request-specific context data: - Provides create, remove, get, get_mut, and exists methods - Uses pool allocation with cleanup handlers for memory management - Add get_module_ctx_mut method to Request for mutable context access - Update ngx_log_debug_http macro to use request.log() method
1 parent 00bfa4d commit 5aa6a43

4 files changed

Lines changed: 51 additions & 1 deletion

File tree

src/http/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
mod conf;
22
mod module;
33
mod request;
4+
mod request_context;
45
mod status;
56
mod upstream;
67

78
pub use conf::*;
89
pub use module::*;
910
pub use request::*;
11+
pub use request_context::*;
1012
pub use status::*;

src/http/request.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,14 @@ impl Request {
249249
unsafe { ctx.as_ref() }
250250
}
251251

252+
/// Get mutable Module context
253+
pub fn get_module_ctx_mut<T>(&mut self, module: &ngx_module_t) -> Option<&mut T> {
254+
let ctx = self.get_module_ctx_ptr(module).cast::<T>();
255+
// SAFETY: ctx is either NULL or allocated with ngx_p(c)alloc and
256+
// explicitly initialized by the module
257+
unsafe { ctx.as_mut() }
258+
}
259+
252260
/// Sets the value as the module's context.
253261
///
254262
/// See <https://nginx.org/en/docs/dev/development_guide.html#http_request>

src/http/request_context.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use crate::http::{HttpModule, Request};
2+
use crate::ngx_log_debug_http;
3+
4+
/// A trait for managing request-specific context data.
5+
pub trait RequestContext<Module: HttpModule>: Sized {
6+
/// Creates a new context and associates it with the given request.
7+
/// No check is performed to see if a context already exists.
8+
fn create<F>(request: &mut Request, f: F) -> Option<&mut Self>
9+
where
10+
F: FnOnce() -> Self,
11+
{
12+
let ctx_ref = unsafe { request.pool().allocate_with_cleanup(f)?.as_mut() };
13+
request.set_module_ctx(ctx_ref as *mut _ as _, Module::module());
14+
Some(ctx_ref)
15+
}
16+
17+
/// Removes the context associated with the given request.
18+
fn remove(request: &mut Request) {
19+
if let Some(ctx_ptr) = request.get_module_ctx::<Self>(Module::module()) {
20+
unsafe { request.pool().remove(ctx_ptr as *const Self) };
21+
request.set_module_ctx(core::ptr::null_mut(), Module::module());
22+
ngx_log_debug_http!(request, "RequestContext removed from request");
23+
}
24+
}
25+
26+
/// Retrieves an immutable reference to the context associated with the given request.
27+
fn get(request: &Request) -> Option<&Self> {
28+
request.get_module_ctx::<Self>(Module::module())
29+
}
30+
31+
/// Retrieves a mutable reference to the context associated with the given request.
32+
fn get_mut(request: &mut Request) -> Option<&mut Self> {
33+
request.get_module_ctx_mut::<Self>(Module::module())
34+
}
35+
36+
/// Checks if a context is associated with the given request.
37+
fn exists(request: &Request) -> bool {
38+
request.get_module_ctx::<Self>(Module::module()).is_some()
39+
}
40+
}

src/log.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ macro_rules! ngx_log_debug {
153153
#[macro_export]
154154
macro_rules! ngx_log_debug_http {
155155
( $request:expr, $($arg:tt)+ ) => {
156-
let log = unsafe { (*$request.connection()).log };
156+
let log = $request.log();
157157
$crate::ngx_log_debug!(mask: $crate::log::DebugMask::Http, log, $($arg)+);
158158
}
159159
}

0 commit comments

Comments
 (0)