Skip to content

Commit beef352

Browse files
committed
refactor: use Option for handler return type in variable getters and setters
1 parent 5331d63 commit beef352

File tree

4 files changed

+166
-208
lines changed

4 files changed

+166
-208
lines changed

examples/httporigdst.rs

Lines changed: 29 additions & 32 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::{
@@ -26,30 +25,30 @@ impl NgxHttpOrigDstCtx {
2625
self.orig_dst_port = unsafe { ngx_str_t::from_str(pool.as_ptr(), &port_str) };
2726
}
2827

29-
pub unsafe fn bind_addr(&self, v: *mut ngx_variable_value_t) {
28+
pub unsafe fn bind_addr(&self, v: &mut ngx_variable_value_t) {
3029
if self.orig_dst_addr.len == 0 {
31-
(*v).set_not_found(1);
30+
v.set_not_found(1);
3231
return;
3332
}
3433

35-
(*v).set_valid(1);
36-
(*v).set_no_cacheable(0);
37-
(*v).set_not_found(0);
38-
(*v).set_len(self.orig_dst_addr.len as u32);
39-
(*v).data = self.orig_dst_addr.data;
34+
v.set_valid(1);
35+
v.set_no_cacheable(0);
36+
v.set_not_found(0);
37+
v.set_len(self.orig_dst_addr.len as u32);
38+
v.data = self.orig_dst_addr.data;
4039
}
4140

42-
pub unsafe fn bind_port(&self, v: *mut ngx_variable_value_t) {
41+
pub unsafe fn bind_port(&self, v: &mut ngx_variable_value_t) {
4342
if self.orig_dst_port.len == 0 {
44-
(*v).set_not_found(1);
43+
v.set_not_found(1);
4544
return;
4645
}
4746

48-
(*v).set_valid(1);
49-
(*v).set_no_cacheable(0);
50-
(*v).set_not_found(0);
51-
(*v).set_len(self.orig_dst_port.len as u32);
52-
(*v).data = self.orig_dst_port.data;
47+
v.set_valid(1);
48+
v.set_no_cacheable(0);
49+
v.set_not_found(0);
50+
v.set_len(self.orig_dst_port.len as u32);
51+
v.data = self.orig_dst_port.data;
5352
}
5453
}
5554

@@ -171,12 +170,12 @@ unsafe fn ngx_get_origdst(
171170

172171
http_variable_get!(
173172
ngx_http_orig_dst_addr_variable,
174-
|request: &mut http::Request, v: *mut ngx_variable_value_t, _: usize| {
175-
let ctx = request.get_module_ctx::<NgxHttpOrigDstCtx>(&*addr_of!(ngx_http_orig_dst_module));
173+
|request: &mut http::Request, v: &mut ngx_variable_value_t, _: usize| {
174+
let ctx = request.get_module_ctx::<NgxHttpOrigDstCtx>(Module::module());
176175
if let Some(obj) = ctx {
177176
ngx_log_debug_http!(request, "httporigdst: found context and binding variable",);
178177
obj.bind_addr(v);
179-
return core::Status::NGX_OK;
178+
return Some(core::Status::NGX_OK.into());
180179
}
181180
// lazy initialization:
182181
// get original dest information
@@ -187,7 +186,7 @@ http_variable_get!(
187186
let r = ngx_get_origdst(request);
188187
match r {
189188
Err(e) => {
190-
return e;
189+
return core::ngx_make_opt(e.0);
191190
}
192191
Ok((ip, port)) => {
193192
// create context,
@@ -197,7 +196,7 @@ http_variable_get!(
197196
.alloc_with_cleanup::<NgxHttpOrigDstCtx>(Default::default());
198197

199198
if new_ctx.is_null() {
200-
return core::Status::NGX_ERROR;
199+
return None;
201200
}
202201

203202
ngx_log_debug_http!(
@@ -208,22 +207,21 @@ http_variable_get!(
208207
);
209208
(*new_ctx).save(&ip, port, &request.pool());
210209
(*new_ctx).bind_addr(v);
211-
request
212-
.set_module_ctx(new_ctx as *mut c_void, &*addr_of!(ngx_http_orig_dst_module));
210+
request.set_module_ctx(new_ctx as _, Module::module());
213211
}
214212
}
215-
core::Status::NGX_OK
213+
Some(core::Status::NGX_OK.into())
216214
}
217215
);
218216

219217
http_variable_get!(
220218
ngx_http_orig_dst_port_variable,
221-
|request: &mut http::Request, v: *mut ngx_variable_value_t, _: usize| {
222-
let ctx = request.get_module_ctx::<NgxHttpOrigDstCtx>(&*addr_of!(ngx_http_orig_dst_module));
219+
|request: &mut http::Request, v: &mut ngx_variable_value_t, _: usize| {
220+
let ctx = request.get_module_ctx::<NgxHttpOrigDstCtx>(Module::module());
223221
if let Some(obj) = ctx {
224222
ngx_log_debug_http!(request, "httporigdst: found context and binding variable",);
225223
obj.bind_port(v);
226-
return core::Status::NGX_OK;
224+
return Some(core::Status::NGX_OK.into());
227225
}
228226
// lazy initialization:
229227
// get original dest information
@@ -234,7 +232,7 @@ http_variable_get!(
234232
let r = ngx_get_origdst(request);
235233
match r {
236234
Err(e) => {
237-
return e;
235+
return core::ngx_make_opt(e.0);
238236
}
239237
Ok((ip, port)) => {
240238
// create context,
@@ -244,7 +242,7 @@ http_variable_get!(
244242
.alloc_with_cleanup::<NgxHttpOrigDstCtx>(Default::default());
245243

246244
if new_ctx.is_null() {
247-
return core::Status::NGX_ERROR;
245+
return None;
248246
}
249247

250248
ngx_log_debug_http!(
@@ -255,11 +253,10 @@ http_variable_get!(
255253
);
256254
(*new_ctx).save(&ip, port, &request.pool());
257255
(*new_ctx).bind_port(v);
258-
request
259-
.set_module_ctx(new_ctx as *mut c_void, &*addr_of!(ngx_http_orig_dst_module));
256+
request.set_module_ctx(new_ctx as _, Module::module());
260257
}
261258
}
262-
core::Status::NGX_OK
259+
Some(core::Status::NGX_OK.into())
263260
}
264261
);
265262

0 commit comments

Comments
 (0)