Skip to content

Commit 9a02667

Browse files
committed
refactor: use Result for handler return type in variable getters and setters
1 parent 2ad203c commit 9a02667

File tree

6 files changed

+186
-242
lines changed

6 files changed

+186
-242
lines changed

examples/async.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::ffi::{c_char, c_void};
2-
use std::ptr::{addr_of, addr_of_mut};
2+
use std::ptr::addr_of_mut;
33
use std::sync::atomic::{AtomicBool, AtomicPtr, Ordering};
44
use std::sync::{Arc, OnceLock};
55
use std::time::Instant;
@@ -148,22 +148,20 @@ http_request_handler!(async_access_handler, |request: &mut http::Request| {
148148
return core::Status::NGX_DECLINED.into();
149149
}
150150

151-
if let Some(ctx) =
152-
unsafe { request.get_module_ctx::<RequestCTX>(&*addr_of!(ngx_http_async_module)) }
153-
{
151+
if let Some(ctx) = request.get_module_ctx::<RequestCTX>(Module::module()) {
154152
if !ctx.done.load(Ordering::Relaxed) {
155153
return core::Status::NGX_AGAIN.into();
156154
}
157155

158156
return core::Status::NGX_OK.into();
159157
}
160158

161-
let ctx = request
159+
let mut ctx = request
162160
.pool()
163161
.allocate_with_cleanup(RequestCTX::default())?;
164-
request.set_module_ctx(ctx.cast(), unsafe { &*addr_of!(ngx_http_async_module) });
162+
request.set_module_ctx(ctx, Module::module());
165163

166-
let ctx = unsafe { &mut *ctx };
164+
let ctx = unsafe { ctx.as_mut() };
167165
ctx.event.handler = Some(check_async_work_done);
168166
ctx.event.data = request.connection().cast();
169167
ctx.event.log = unsafe { (*request.connection()).log };

examples/httporigdst.rs

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use std::ffi::{c_int, c_void};
2-
use std::ptr::addr_of;
1+
use std::ffi::c_int;
32

43
use ngx::core;
54
use ngx::ffi::{
@@ -20,50 +19,38 @@ struct NgxHttpOrigDstCtx {
2019

2120
impl NgxHttpOrigDstCtx {
2221
pub fn save(&mut self, addr: &str, port: in_port_t, pool: &core::Pool) -> core::NgxResult {
23-
let addr_data = pool.allocate_unaligned(addr.len())?;
24-
unsafe { libc::memcpy(addr_data, addr.as_ptr() as *const c_void, addr.len()) };
25-
self.orig_dst_addr.len = addr.len();
26-
self.orig_dst_addr.data = addr_data as *mut u8;
22+
self.orig_dst_addr = unsafe { ngx_str_t::from_str(pool.as_ptr(), addr) };
2723

2824
let port_str = port.to_string();
29-
let port_data = pool.allocate_unaligned(port_str.len())?;
30-
unsafe {
31-
libc::memcpy(
32-
port_data,
33-
port_str.as_bytes().as_ptr() as *const c_void,
34-
port_str.len(),
35-
)
36-
};
37-
self.orig_dst_port.len = port_str.len();
38-
self.orig_dst_port.data = port_data as *mut u8;
25+
self.orig_dst_port = unsafe { ngx_str_t::from_str(pool.as_ptr(), &port_str) };
3926

4027
Ok(core::Status::NGX_OK.into())
4128
}
4229

43-
pub unsafe fn bind_addr(&self, v: *mut ngx_variable_value_t) {
30+
pub unsafe fn bind_addr(&self, v: &mut ngx_variable_value_t) {
4431
if self.orig_dst_addr.len == 0 {
45-
(*v).set_not_found(1);
32+
v.set_not_found(1);
4633
return;
4734
}
4835

49-
(*v).set_valid(1);
50-
(*v).set_no_cacheable(0);
51-
(*v).set_not_found(0);
52-
(*v).set_len(self.orig_dst_addr.len as u32);
53-
(*v).data = self.orig_dst_addr.data;
36+
v.set_valid(1);
37+
v.set_no_cacheable(0);
38+
v.set_not_found(0);
39+
v.set_len(self.orig_dst_addr.len as u32);
40+
v.data = self.orig_dst_addr.data;
5441
}
5542

56-
pub unsafe fn bind_port(&self, v: *mut ngx_variable_value_t) {
43+
pub unsafe fn bind_port(&self, v: &mut ngx_variable_value_t) {
5744
if self.orig_dst_port.len == 0 {
58-
(*v).set_not_found(1);
45+
v.set_not_found(1);
5946
return;
6047
}
6148

62-
(*v).set_valid(1);
63-
(*v).set_no_cacheable(0);
64-
(*v).set_not_found(0);
65-
(*v).set_len(self.orig_dst_port.len as u32);
66-
(*v).data = self.orig_dst_port.data;
49+
v.set_valid(1);
50+
v.set_no_cacheable(0);
51+
v.set_not_found(0);
52+
v.set_len(self.orig_dst_port.len as u32);
53+
v.data = self.orig_dst_port.data;
6754
}
6855
}
6956

@@ -185,8 +172,8 @@ unsafe fn ngx_get_origdst(
185172

186173
http_variable_get!(
187174
ngx_http_orig_dst_addr_variable,
188-
|request: &mut http::Request, v: *mut ngx_variable_value_t, _: usize| {
189-
let ctx = request.get_module_ctx::<NgxHttpOrigDstCtx>(&*addr_of!(ngx_http_orig_dst_module));
175+
|request: &mut http::Request, v: &mut ngx_variable_value_t, _: usize| {
176+
let ctx = request.get_module_ctx::<NgxHttpOrigDstCtx>(Module::module());
190177
if let Some(obj) = ctx {
191178
ngx_log_debug_http!(request, "httporigdst: found context and binding variable",);
192179
obj.bind_addr(v);
@@ -206,7 +193,7 @@ http_variable_get!(
206193
Ok((ip, port)) => {
207194
// create context,
208195
// set context
209-
let new_ctx = request
196+
let mut new_ctx = request
210197
.pool()
211198
.allocate_with_cleanup::<NgxHttpOrigDstCtx>(Default::default())?;
212199

@@ -216,10 +203,9 @@ http_variable_get!(
216203
ip,
217204
port,
218205
);
219-
(*new_ctx).save(&ip, port, &request.pool())?;
220-
(*new_ctx).bind_addr(v);
221-
request
222-
.set_module_ctx(new_ctx as *mut c_void, &*addr_of!(ngx_http_orig_dst_module));
206+
new_ctx.as_mut().save(&ip, port, &request.pool())?;
207+
new_ctx.as_mut().bind_addr(v);
208+
request.set_module_ctx(new_ctx, Module::module());
223209
}
224210
}
225211
core::Status::NGX_OK.into()
@@ -228,8 +214,8 @@ http_variable_get!(
228214

229215
http_variable_get!(
230216
ngx_http_orig_dst_port_variable,
231-
|request: &mut http::Request, v: *mut ngx_variable_value_t, _: usize| {
232-
let ctx = request.get_module_ctx::<NgxHttpOrigDstCtx>(&*addr_of!(ngx_http_orig_dst_module));
217+
|request: &mut http::Request, v: &mut ngx_variable_value_t, _: usize| {
218+
let ctx = request.get_module_ctx::<NgxHttpOrigDstCtx>(Module::module());
233219
if let Some(obj) = ctx {
234220
ngx_log_debug_http!(request, "httporigdst: found context and binding variable",);
235221
obj.bind_port(v);
@@ -249,7 +235,7 @@ http_variable_get!(
249235
Ok((ip, port)) => {
250236
// create context,
251237
// set context
252-
let new_ctx = request
238+
let mut new_ctx = request
253239
.pool()
254240
.allocate_with_cleanup::<NgxHttpOrigDstCtx>(Default::default())?;
255241

@@ -259,10 +245,9 @@ http_variable_get!(
259245
ip,
260246
port,
261247
);
262-
(*new_ctx).save(&ip, port, &request.pool())?;
263-
(*new_ctx).bind_port(v);
264-
request
265-
.set_module_ctx(new_ctx as *mut c_void, &*addr_of!(ngx_http_orig_dst_module));
248+
new_ctx.as_mut().save(&ip, port, &request.pool())?;
249+
new_ctx.as_mut().bind_port(v);
250+
request.set_module_ctx(new_ctx, Module::module());
266251
}
267252
}
268253
core::Status::NGX_OK.into()

0 commit comments

Comments
 (0)