Skip to content

Commit 28c1fa0

Browse files
committed
refactor: use Result for handler return type in variable getters and setters
1 parent 7f6e4e6 commit 28c1fa0

File tree

3 files changed

+157
-198
lines changed

3 files changed

+157
-198
lines changed

examples/httporigdst.rs

Lines changed: 21 additions & 24 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::{
@@ -28,30 +27,30 @@ impl NgxHttpOrigDstCtx {
2827
Ok(core::Status::NGX_OK.into())
2928
}
3029

31-
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) {
3231
if self.orig_dst_addr.len == 0 {
33-
(*v).set_not_found(1);
32+
v.set_not_found(1);
3433
return;
3534
}
3635

37-
(*v).set_valid(1);
38-
(*v).set_no_cacheable(0);
39-
(*v).set_not_found(0);
40-
(*v).set_len(self.orig_dst_addr.len as u32);
41-
(*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;
4241
}
4342

44-
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) {
4544
if self.orig_dst_port.len == 0 {
46-
(*v).set_not_found(1);
45+
v.set_not_found(1);
4746
return;
4847
}
4948

50-
(*v).set_valid(1);
51-
(*v).set_no_cacheable(0);
52-
(*v).set_not_found(0);
53-
(*v).set_len(self.orig_dst_port.len as u32);
54-
(*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;
5554
}
5655
}
5756

@@ -173,8 +172,8 @@ unsafe fn ngx_get_origdst(
173172

174173
http_variable_get!(
175174
ngx_http_orig_dst_addr_variable,
176-
|request: &mut http::Request, v: *mut ngx_variable_value_t, _: usize| {
177-
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());
178177
if let Some(obj) = ctx {
179178
ngx_log_debug_http!(request, "httporigdst: found context and binding variable",);
180179
obj.bind_addr(v);
@@ -210,8 +209,7 @@ http_variable_get!(
210209
);
211210
(*new_ctx).save(&ip, port, &request.pool())?;
212211
(*new_ctx).bind_addr(v);
213-
request
214-
.set_module_ctx(new_ctx as *mut c_void, &*addr_of!(ngx_http_orig_dst_module));
212+
request.set_module_ctx(new_ctx as _, Module::module());
215213
}
216214
}
217215
core::Status::NGX_OK.into()
@@ -220,8 +218,8 @@ http_variable_get!(
220218

221219
http_variable_get!(
222220
ngx_http_orig_dst_port_variable,
223-
|request: &mut http::Request, v: *mut ngx_variable_value_t, _: usize| {
224-
let ctx = request.get_module_ctx::<NgxHttpOrigDstCtx>(&*addr_of!(ngx_http_orig_dst_module));
221+
|request: &mut http::Request, v: &mut ngx_variable_value_t, _: usize| {
222+
let ctx = request.get_module_ctx::<NgxHttpOrigDstCtx>(Module::module());
225223
if let Some(obj) = ctx {
226224
ngx_log_debug_http!(request, "httporigdst: found context and binding variable",);
227225
obj.bind_port(v);
@@ -257,8 +255,7 @@ http_variable_get!(
257255
);
258256
(*new_ctx).save(&ip, port, &request.pool())?;
259257
(*new_ctx).bind_port(v);
260-
request
261-
.set_module_ctx(new_ctx as *mut c_void, &*addr_of!(ngx_http_orig_dst_module));
258+
request.set_module_ctx(new_ctx as _, Module::module());
262259
}
263260
}
264261
core::Status::NGX_OK.into()

0 commit comments

Comments
 (0)