Skip to content

Commit 90eab04

Browse files
committed
refactor: safer interface for module configuration access
* original version was prepared by Aleksei Bavshin
1 parent 5bc247f commit 90eab04

File tree

9 files changed

+441
-234
lines changed

9 files changed

+441
-234
lines changed

Cargo.lock

+4-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/async.rs

+23-17
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,29 @@ use std::time::Instant;
77

88
use ngx::core;
99
use ngx::ffi::{
10-
ngx_array_push, ngx_command_t, ngx_conf_t, ngx_connection_t, ngx_event_t, ngx_http_core_module,
11-
ngx_http_handler_pt, ngx_http_module_t, ngx_http_phases_NGX_HTTP_ACCESS_PHASE, ngx_int_t, ngx_module_t,
12-
ngx_post_event, ngx_posted_events, ngx_posted_next_events, ngx_str_t, ngx_uint_t, NGX_CONF_TAKE1,
13-
NGX_HTTP_LOC_CONF, NGX_HTTP_LOC_CONF_OFFSET, NGX_HTTP_MODULE,
10+
ngx_array_push, ngx_command_t, ngx_conf_t, ngx_connection_t, ngx_event_t, ngx_http_handler_pt, ngx_http_module_t,
11+
ngx_http_phases_NGX_HTTP_ACCESS_PHASE, ngx_int_t, ngx_module_t, ngx_post_event, ngx_posted_events,
12+
ngx_posted_next_events, ngx_str_t, ngx_uint_t, NGX_CONF_TAKE1, NGX_HTTP_LOC_CONF, NGX_HTTP_LOC_CONF_OFFSET,
13+
NGX_HTTP_MODULE,
1414
};
15-
use ngx::http::{self, HTTPModule, MergeConfigError};
15+
use ngx::http::{self, HttpModule, MergeConfigError};
16+
use ngx::http::{HttpModuleLocationConf, HttpModuleMainConf, NgxHttpCoreModule};
1617
use ngx::{http_request_handler, ngx_log_debug_http, ngx_string};
1718
use tokio::runtime::Runtime;
1819

1920
struct Module;
2021

21-
impl http::HTTPModule for Module {
22-
type MainConf = ();
23-
type SrvConf = ();
24-
type LocConf = ModuleConfig;
22+
impl http::HttpModule for Module {
23+
fn module() -> &'static ngx_module_t {
24+
unsafe { &*::core::ptr::addr_of!(ngx_http_async_module) }
25+
}
2526

2627
unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t {
27-
let cmcf = http::ngx_http_conf_get_module_main_conf(cf, &*addr_of!(ngx_http_core_module));
28+
// SAFETY: this function is called with non-NULL cf always
29+
let cf = &mut *cf;
30+
let cmcf = NgxHttpCoreModule::main_conf_mut(cf).expect("http core main conf");
2831

29-
let h = ngx_array_push(&mut (*cmcf).phases[ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize].handlers)
32+
let h = ngx_array_push(&mut cmcf.phases[ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize].handlers)
3033
as *mut ngx_http_handler_pt;
3134
if h.is_null() {
3235
return core::Status::NGX_ERROR.into();
@@ -42,6 +45,10 @@ struct ModuleConfig {
4245
enable: bool,
4346
}
4447

48+
impl HttpModuleLocationConf for Module {
49+
type LocationConf = ModuleConfig;
50+
}
51+
4552
static mut NGX_HTTP_ASYNC_COMMANDS: [ngx_command_t; 2] = [
4653
ngx_command_t {
4754
name: ngx_string!("async"),
@@ -57,10 +64,10 @@ static mut NGX_HTTP_ASYNC_COMMANDS: [ngx_command_t; 2] = [
5764
static NGX_HTTP_ASYNC_MODULE_CTX: ngx_http_module_t = ngx_http_module_t {
5865
preconfiguration: Some(Module::preconfiguration),
5966
postconfiguration: Some(Module::postconfiguration),
60-
create_main_conf: Some(Module::create_main_conf),
61-
init_main_conf: Some(Module::init_main_conf),
62-
create_srv_conf: Some(Module::create_srv_conf),
63-
merge_srv_conf: Some(Module::merge_srv_conf),
67+
create_main_conf: None,
68+
init_main_conf: None,
69+
create_srv_conf: None,
70+
merge_srv_conf: None,
6471
create_loc_conf: Some(Module::create_loc_conf),
6572
merge_loc_conf: Some(Module::merge_loc_conf),
6673
};
@@ -133,8 +140,7 @@ impl Drop for RequestCTX {
133140
}
134141

135142
http_request_handler!(async_access_handler, |request: &mut http::Request| {
136-
let co = unsafe { request.get_module_loc_conf::<ModuleConfig>(&*addr_of!(ngx_http_async_module)) };
137-
let co = co.expect("module config is none");
143+
let co = Module::location_conf(request).expect("module config is none");
138144

139145
ngx_log_debug_http!(request, "async module enabled: {}", co.enable);
140146

examples/awssig.rs

+18-14
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use std::ffi::{c_char, c_void};
2-
use std::ptr::addr_of;
32

43
use http::HeaderMap;
54
use ngx::core;
65
use ngx::ffi::{
7-
ngx_array_push, ngx_command_t, ngx_conf_t, ngx_http_core_module, ngx_http_handler_pt, ngx_http_module_t,
6+
ngx_array_push, ngx_command_t, ngx_conf_t, ngx_http_handler_pt, ngx_http_module_t,
87
ngx_http_phases_NGX_HTTP_PRECONTENT_PHASE, ngx_int_t, ngx_module_t, ngx_str_t, ngx_uint_t, NGX_CONF_TAKE1,
98
NGX_HTTP_LOC_CONF, NGX_HTTP_LOC_CONF_OFFSET, NGX_HTTP_MODULE, NGX_HTTP_SRV_CONF,
109
};
@@ -13,15 +12,17 @@ use ngx::{http_request_handler, ngx_log_debug_http, ngx_string};
1312

1413
struct Module;
1514

16-
impl HTTPModule for Module {
17-
type MainConf = ();
18-
type SrvConf = ();
19-
type LocConf = ModuleConfig;
15+
impl HttpModule for Module {
16+
fn module() -> &'static ngx_module_t {
17+
unsafe { &*::core::ptr::addr_of!(ngx_http_awssigv4_module) }
18+
}
2019

2120
unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t {
22-
let cmcf = ngx_http_conf_get_module_main_conf(cf, &*addr_of!(ngx_http_core_module));
21+
// SAFETY: this function is called with non-NULL cf always
22+
let cf = &mut *cf;
23+
let cmcf = NgxHttpCoreModule::main_conf_mut(cf).expect("http core main conf");
2324

24-
let h = ngx_array_push(&mut (*cmcf).phases[ngx_http_phases_NGX_HTTP_PRECONTENT_PHASE as usize].handlers)
25+
let h = ngx_array_push(&mut cmcf.phases[ngx_http_phases_NGX_HTTP_PRECONTENT_PHASE as usize].handlers)
2526
as *mut ngx_http_handler_pt;
2627
if h.is_null() {
2728
return core::Status::NGX_ERROR.into();
@@ -41,6 +42,10 @@ struct ModuleConfig {
4142
s3_endpoint: String,
4243
}
4344

45+
impl HttpModuleLocationConf for Module {
46+
type LocationConf = ModuleConfig;
47+
}
48+
4449
static mut NGX_HTTP_AWSSIGV4_COMMANDS: [ngx_command_t; 6] = [
4550
ngx_command_t {
4651
name: ngx_string!("awssigv4"),
@@ -88,10 +93,10 @@ static mut NGX_HTTP_AWSSIGV4_COMMANDS: [ngx_command_t; 6] = [
8893
static NGX_HTTP_AWSSIGV4_MODULE_CTX: ngx_http_module_t = ngx_http_module_t {
8994
preconfiguration: Some(Module::preconfiguration),
9095
postconfiguration: Some(Module::postconfiguration),
91-
create_main_conf: Some(Module::create_main_conf),
92-
init_main_conf: Some(Module::init_main_conf),
93-
create_srv_conf: Some(Module::create_srv_conf),
94-
merge_srv_conf: Some(Module::merge_srv_conf),
96+
create_main_conf: None,
97+
init_main_conf: None,
98+
create_srv_conf: None,
99+
merge_srv_conf: None,
95100
create_loc_conf: Some(Module::create_loc_conf),
96101
merge_loc_conf: Some(Module::merge_loc_conf),
97102
};
@@ -245,8 +250,7 @@ extern "C" fn ngx_http_awssigv4_commands_set_s3_endpoint(
245250

246251
http_request_handler!(awssigv4_header_handler, |request: &mut Request| {
247252
// get Module Config from request
248-
let conf = unsafe { request.get_module_loc_conf::<ModuleConfig>(&*addr_of!(ngx_http_awssigv4_module)) };
249-
let conf = conf.unwrap();
253+
let conf = Module::location_conf(request).expect("module conf");
250254
ngx_log_debug_http!(request, "AWS signature V4 module {}", {
251255
if conf.enable {
252256
"enabled"

examples/curl.rs

+20-15
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
use std::ffi::{c_char, c_void};
2-
use std::ptr::addr_of;
32

43
use ngx::core;
54
use ngx::ffi::{
6-
ngx_array_push, ngx_command_t, ngx_conf_t, ngx_http_core_module, ngx_http_handler_pt, ngx_http_module_t,
5+
ngx_array_push, ngx_command_t, ngx_conf_t, ngx_http_handler_pt, ngx_http_module_t,
76
ngx_http_phases_NGX_HTTP_ACCESS_PHASE, ngx_int_t, ngx_module_t, ngx_str_t, ngx_uint_t, NGX_CONF_TAKE1,
87
NGX_HTTP_LOC_CONF, NGX_HTTP_LOC_CONF_OFFSET, NGX_HTTP_MODULE,
98
};
10-
use ngx::http::{self, HTTPModule, MergeConfigError};
9+
use ngx::http::{self, HttpModule, MergeConfigError};
10+
use ngx::http::{HttpModuleLocationConf, HttpModuleMainConf, NgxHttpCoreModule};
1111
use ngx::{http_request_handler, ngx_log_debug_http, ngx_string};
1212

1313
struct Module;
1414

15-
impl http::HTTPModule for Module {
16-
type MainConf = ();
17-
type SrvConf = ();
18-
type LocConf = ModuleConfig;
15+
impl http::HttpModule for Module {
16+
fn module() -> &'static ngx_module_t {
17+
unsafe { &*::core::ptr::addr_of!(ngx_http_curl_module) }
18+
}
1919

2020
unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t {
21-
let cmcf = http::ngx_http_conf_get_module_main_conf(cf, &*addr_of!(ngx_http_core_module));
21+
// SAFETY: this function is called with non-NULL cf always
22+
let cf = &mut *cf;
23+
let cmcf = NgxHttpCoreModule::main_conf_mut(cf).expect("http core main conf");
2224

23-
let h = ngx_array_push(&mut (*cmcf).phases[ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize].handlers)
25+
let h = ngx_array_push(&mut cmcf.phases[ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize].handlers)
2426
as *mut ngx_http_handler_pt;
2527
if h.is_null() {
2628
return core::Status::NGX_ERROR.into();
@@ -36,6 +38,10 @@ struct ModuleConfig {
3638
enable: bool,
3739
}
3840

41+
impl HttpModuleLocationConf for Module {
42+
type LocationConf = ModuleConfig;
43+
}
44+
3945
static mut NGX_HTTP_CURL_COMMANDS: [ngx_command_t; 2] = [
4046
ngx_command_t {
4147
name: ngx_string!("curl"),
@@ -51,10 +57,10 @@ static mut NGX_HTTP_CURL_COMMANDS: [ngx_command_t; 2] = [
5157
static NGX_HTTP_CURL_MODULE_CTX: ngx_http_module_t = ngx_http_module_t {
5258
preconfiguration: Some(Module::preconfiguration),
5359
postconfiguration: Some(Module::postconfiguration),
54-
create_main_conf: Some(Module::create_main_conf),
55-
init_main_conf: Some(Module::init_main_conf),
56-
create_srv_conf: Some(Module::create_srv_conf),
57-
merge_srv_conf: Some(Module::merge_srv_conf),
60+
create_main_conf: None,
61+
init_main_conf: None,
62+
create_srv_conf: None,
63+
merge_srv_conf: None,
5864
create_loc_conf: Some(Module::create_loc_conf),
5965
merge_loc_conf: Some(Module::merge_loc_conf),
6066
};
@@ -84,8 +90,7 @@ impl http::Merge for ModuleConfig {
8490
}
8591

8692
http_request_handler!(curl_access_handler, |request: &mut http::Request| {
87-
let co = unsafe { request.get_module_loc_conf::<ModuleConfig>(&*addr_of!(ngx_http_curl_module)) };
88-
let co = co.expect("module config is none");
93+
let co = Module::location_conf(request).expect("module config is none");
8994

9095
ngx_log_debug_http!(request, "curl module enabled: {}", co.enable);
9196

examples/httporigdst.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use ngx::ffi::{
77
ngx_http_variable_t, ngx_inet_get_port, ngx_int_t, ngx_module_t, ngx_sock_ntop, ngx_str_t, ngx_variable_value_t,
88
sockaddr, sockaddr_storage, INET_ADDRSTRLEN, NGX_HTTP_MODULE,
99
};
10-
use ngx::http::{self, HTTPModule};
10+
use ngx::http::{self, HttpModule};
1111
use ngx::{http_variable_get, ngx_log_debug_http, ngx_string};
1212

1313
const IPV4_STRLEN: usize = INET_ADDRSTRLEN as usize;
@@ -70,12 +70,12 @@ impl NgxHttpOrigDstCtx {
7070
static NGX_HTTP_ORIG_DST_MODULE_CTX: ngx_http_module_t = ngx_http_module_t {
7171
preconfiguration: Some(Module::preconfiguration),
7272
postconfiguration: Some(Module::postconfiguration),
73-
create_main_conf: Some(Module::create_main_conf),
74-
init_main_conf: Some(Module::init_main_conf),
75-
create_srv_conf: Some(Module::create_srv_conf),
76-
merge_srv_conf: Some(Module::merge_srv_conf),
77-
create_loc_conf: Some(Module::create_loc_conf),
78-
merge_loc_conf: Some(Module::merge_loc_conf),
73+
create_main_conf: None,
74+
init_main_conf: None,
75+
create_srv_conf: None,
76+
merge_srv_conf: None,
77+
create_loc_conf: None,
78+
merge_loc_conf: None,
7979
};
8080

8181
// Generate the `ngx_modules` table with exported modules.
@@ -258,10 +258,10 @@ http_variable_get!(
258258

259259
struct Module;
260260

261-
impl HTTPModule for Module {
262-
type MainConf = ();
263-
type SrvConf = ();
264-
type LocConf = ();
261+
impl HttpModule for Module {
262+
fn module() -> &'static ngx_module_t {
263+
unsafe { &*::core::ptr::addr_of!(ngx_http_orig_dst_module) }
264+
}
265265

266266
// static ngx_int_t ngx_http_orig_dst_add_variables(ngx_conf_t *cf)
267267
unsafe extern "C" fn preconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t {

0 commit comments

Comments
 (0)