Skip to content

Safer interface for module configuration access. #142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions examples/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,29 @@ use std::time::Instant;

use ngx::core;
use ngx::ffi::{
ngx_array_push, ngx_command_t, ngx_conf_t, ngx_connection_t, ngx_event_t, ngx_http_core_module,
ngx_http_handler_pt, ngx_http_module_t, ngx_http_phases_NGX_HTTP_ACCESS_PHASE, ngx_int_t, ngx_module_t,
ngx_post_event, ngx_posted_events, ngx_posted_next_events, ngx_str_t, ngx_uint_t, NGX_CONF_TAKE1,
NGX_HTTP_LOC_CONF, NGX_HTTP_LOC_CONF_OFFSET, NGX_HTTP_MODULE,
ngx_array_push, ngx_command_t, ngx_conf_t, ngx_connection_t, ngx_event_t, ngx_http_handler_pt, ngx_http_module_t,
ngx_http_phases_NGX_HTTP_ACCESS_PHASE, ngx_int_t, ngx_module_t, ngx_post_event, ngx_posted_events,
ngx_posted_next_events, ngx_str_t, ngx_uint_t, NGX_CONF_TAKE1, NGX_HTTP_LOC_CONF, NGX_HTTP_LOC_CONF_OFFSET,
NGX_HTTP_MODULE,
};
use ngx::http::{self, HTTPModule, MergeConfigError};
use ngx::http::{self, HttpModule, MergeConfigError};
use ngx::http::{HttpModuleLocationConf, HttpModuleMainConf, NgxHttpCoreModule};
use ngx::{http_request_handler, ngx_log_debug_http, ngx_string};
use tokio::runtime::Runtime;

struct Module;

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

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

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

unsafe impl HttpModuleLocationConf for Module {
type LocationConf = ModuleConfig;
}

static mut NGX_HTTP_ASYNC_COMMANDS: [ngx_command_t; 2] = [
ngx_command_t {
name: ngx_string!("async"),
Expand All @@ -57,10 +64,10 @@ static mut NGX_HTTP_ASYNC_COMMANDS: [ngx_command_t; 2] = [
static NGX_HTTP_ASYNC_MODULE_CTX: ngx_http_module_t = ngx_http_module_t {
preconfiguration: Some(Module::preconfiguration),
postconfiguration: Some(Module::postconfiguration),
create_main_conf: Some(Module::create_main_conf),
init_main_conf: Some(Module::init_main_conf),
create_srv_conf: Some(Module::create_srv_conf),
merge_srv_conf: Some(Module::merge_srv_conf),
create_main_conf: None,
init_main_conf: None,
create_srv_conf: None,
merge_srv_conf: None,
create_loc_conf: Some(Module::create_loc_conf),
merge_loc_conf: Some(Module::merge_loc_conf),
};
Expand Down Expand Up @@ -133,8 +140,7 @@ impl Drop for RequestCTX {
}

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

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

Expand Down
32 changes: 18 additions & 14 deletions examples/awssig.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::ffi::{c_char, c_void};
use std::ptr::addr_of;

use http::HeaderMap;
use ngx::core;
use ngx::ffi::{
ngx_array_push, ngx_command_t, ngx_conf_t, ngx_http_core_module, ngx_http_handler_pt, ngx_http_module_t,
ngx_array_push, ngx_command_t, ngx_conf_t, ngx_http_handler_pt, ngx_http_module_t,
ngx_http_phases_NGX_HTTP_PRECONTENT_PHASE, ngx_int_t, ngx_module_t, ngx_str_t, ngx_uint_t, NGX_CONF_TAKE1,
NGX_HTTP_LOC_CONF, NGX_HTTP_LOC_CONF_OFFSET, NGX_HTTP_MODULE, NGX_HTTP_SRV_CONF,
};
Expand All @@ -13,15 +12,17 @@ use ngx::{http_request_handler, ngx_log_debug_http, ngx_string};

struct Module;

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

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

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

unsafe impl HttpModuleLocationConf for Module {
type LocationConf = ModuleConfig;
}

static mut NGX_HTTP_AWSSIGV4_COMMANDS: [ngx_command_t; 6] = [
ngx_command_t {
name: ngx_string!("awssigv4"),
Expand Down Expand Up @@ -88,10 +93,10 @@ static mut NGX_HTTP_AWSSIGV4_COMMANDS: [ngx_command_t; 6] = [
static NGX_HTTP_AWSSIGV4_MODULE_CTX: ngx_http_module_t = ngx_http_module_t {
preconfiguration: Some(Module::preconfiguration),
postconfiguration: Some(Module::postconfiguration),
create_main_conf: Some(Module::create_main_conf),
init_main_conf: Some(Module::init_main_conf),
create_srv_conf: Some(Module::create_srv_conf),
merge_srv_conf: Some(Module::merge_srv_conf),
create_main_conf: None,
init_main_conf: None,
create_srv_conf: None,
merge_srv_conf: None,
create_loc_conf: Some(Module::create_loc_conf),
merge_loc_conf: Some(Module::merge_loc_conf),
};
Expand Down Expand Up @@ -245,8 +250,7 @@ extern "C" fn ngx_http_awssigv4_commands_set_s3_endpoint(

http_request_handler!(awssigv4_header_handler, |request: &mut Request| {
// get Module Config from request
let conf = unsafe { request.get_module_loc_conf::<ModuleConfig>(&*addr_of!(ngx_http_awssigv4_module)) };
let conf = conf.unwrap();
let conf = Module::location_conf(request).expect("module conf");
ngx_log_debug_http!(request, "AWS signature V4 module {}", {
if conf.enable {
"enabled"
Expand Down
35 changes: 20 additions & 15 deletions examples/curl.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
use std::ffi::{c_char, c_void};
use std::ptr::addr_of;

use ngx::core;
use ngx::ffi::{
ngx_array_push, ngx_command_t, ngx_conf_t, ngx_http_core_module, ngx_http_handler_pt, ngx_http_module_t,
ngx_array_push, ngx_command_t, ngx_conf_t, ngx_http_handler_pt, ngx_http_module_t,
ngx_http_phases_NGX_HTTP_ACCESS_PHASE, ngx_int_t, ngx_module_t, ngx_str_t, ngx_uint_t, NGX_CONF_TAKE1,
NGX_HTTP_LOC_CONF, NGX_HTTP_LOC_CONF_OFFSET, NGX_HTTP_MODULE,
};
use ngx::http::{self, HTTPModule, MergeConfigError};
use ngx::http::{self, HttpModule, MergeConfigError};
use ngx::http::{HttpModuleLocationConf, HttpModuleMainConf, NgxHttpCoreModule};
use ngx::{http_request_handler, ngx_log_debug_http, ngx_string};

struct Module;

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

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

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

unsafe impl HttpModuleLocationConf for Module {
type LocationConf = ModuleConfig;
}

static mut NGX_HTTP_CURL_COMMANDS: [ngx_command_t; 2] = [
ngx_command_t {
name: ngx_string!("curl"),
Expand All @@ -51,10 +57,10 @@ static mut NGX_HTTP_CURL_COMMANDS: [ngx_command_t; 2] = [
static NGX_HTTP_CURL_MODULE_CTX: ngx_http_module_t = ngx_http_module_t {
preconfiguration: Some(Module::preconfiguration),
postconfiguration: Some(Module::postconfiguration),
create_main_conf: Some(Module::create_main_conf),
init_main_conf: Some(Module::init_main_conf),
create_srv_conf: Some(Module::create_srv_conf),
merge_srv_conf: Some(Module::merge_srv_conf),
create_main_conf: None,
init_main_conf: None,
create_srv_conf: None,
merge_srv_conf: None,
create_loc_conf: Some(Module::create_loc_conf),
merge_loc_conf: Some(Module::merge_loc_conf),
};
Expand Down Expand Up @@ -84,8 +90,7 @@ impl http::Merge for ModuleConfig {
}

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

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

Expand Down
22 changes: 11 additions & 11 deletions examples/httporigdst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ngx::ffi::{
ngx_http_variable_t, ngx_inet_get_port, ngx_int_t, ngx_module_t, ngx_sock_ntop, ngx_str_t, ngx_variable_value_t,
sockaddr, sockaddr_storage, INET_ADDRSTRLEN, NGX_HTTP_MODULE,
};
use ngx::http::{self, HTTPModule};
use ngx::http::{self, HttpModule};
use ngx::{http_variable_get, ngx_log_debug_http, ngx_string};

const IPV4_STRLEN: usize = INET_ADDRSTRLEN as usize;
Expand Down Expand Up @@ -70,12 +70,12 @@ impl NgxHttpOrigDstCtx {
static NGX_HTTP_ORIG_DST_MODULE_CTX: ngx_http_module_t = ngx_http_module_t {
preconfiguration: Some(Module::preconfiguration),
postconfiguration: Some(Module::postconfiguration),
create_main_conf: Some(Module::create_main_conf),
init_main_conf: Some(Module::init_main_conf),
create_srv_conf: Some(Module::create_srv_conf),
merge_srv_conf: Some(Module::merge_srv_conf),
create_loc_conf: Some(Module::create_loc_conf),
merge_loc_conf: Some(Module::merge_loc_conf),
create_main_conf: None,
init_main_conf: None,
create_srv_conf: None,
merge_srv_conf: None,
create_loc_conf: None,
merge_loc_conf: None,
};

// Generate the `ngx_modules` table with exported modules.
Expand Down Expand Up @@ -258,10 +258,10 @@ http_variable_get!(

struct Module;

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

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