Skip to content
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

fix: remove shared ref to static mut #73

Merged
merged 3 commits into from
Apr 15, 2024
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
9 changes: 5 additions & 4 deletions examples/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use ngx::http::MergeConfigError;
use ngx::{core, core::Status, http, http::HTTPModule};
use ngx::{http_request_handler, ngx_log_debug_http, ngx_modules, ngx_null_command, ngx_string};
use std::os::raw::{c_char, c_void};
use std::ptr::{addr_of, addr_of_mut};
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use std::time::Instant;
Expand All @@ -21,7 +22,7 @@ impl http::HTTPModule for Module {
type LocConf = ModuleConfig;

unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t {
let cmcf = http::ngx_http_conf_get_module_main_conf(cf, &ngx_http_core_module);
let cmcf = http::ngx_http_conf_get_module_main_conf(cf, &*addr_of!(ngx_http_core_module));

let h = ngx_array_push(&mut (*cmcf).phases[ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize].handlers)
as *mut ngx_http_handler_pt;
Expand Down Expand Up @@ -131,7 +132,7 @@ unsafe extern "C" fn check_async_work_done(event: *mut ngx_event_t) {
// this doesn't have have good performance but works as a simple thread-safe example and doesn't causes
// segfault. The best method that provides both thread-safety and performance requires
// an nginx patch.
post_event(event, &ngx_posted_events as *const _ as _);
post_event(event, addr_of_mut!(ngx_posted_events));
}
}

Expand Down Expand Up @@ -162,7 +163,7 @@ unsafe fn post_event(event: *mut ngx_event_t, queue: *mut ngx_queue_s) {
}

http_request_handler!(async_access_handler, |request: &mut http::Request| {
let co = unsafe { request.get_module_loc_conf::<ModuleConfig>(&ngx_http_async_module) };
let co = unsafe { request.get_module_loc_conf::<ModuleConfig>(&*addr_of!(ngx_http_async_module)) };
let co = co.expect("module config is none");
if !co.enable {
return core::Status::NGX_DECLINED;
Expand Down Expand Up @@ -203,7 +204,7 @@ http_request_handler!(async_access_handler, |request: &mut http::Request| {
event.data = Arc::into_raw(event_data.clone()) as _;
event.log = (*ngx_cycle).log;

post_event(event, &ngx_posted_events as *const _ as _);
post_event(event, addr_of_mut!(ngx_posted_events));
}

ngx_log_debug_http!(request, "async module enabled: {}", co.enable);
Expand Down
5 changes: 3 additions & 2 deletions examples/awssig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use ngx::ffi::{
use ngx::{core, core::Status, http::*};
use ngx::{http_request_handler, ngx_log_debug_http, ngx_modules, ngx_null_command, ngx_string};
use std::os::raw::{c_char, c_void};
use std::ptr::addr_of;

struct Module;

Expand All @@ -17,7 +18,7 @@ impl HTTPModule for Module {
type LocConf = ModuleConfig;

unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t {
let cmcf = ngx_http_conf_get_module_main_conf(cf, &ngx_http_core_module);
let cmcf = ngx_http_conf_get_module_main_conf(cf, &*addr_of!(ngx_http_core_module));

let h = ngx_array_push(&mut (*cmcf).phases[ngx_http_phases_NGX_HTTP_PRECONTENT_PHASE as usize].handlers)
as *mut ngx_http_handler_pt;
Expand Down Expand Up @@ -269,7 +270,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>(&ngx_http_awssigv4_module) };
let conf = unsafe { request.get_module_loc_conf::<ModuleConfig>(&*addr_of!(ngx_http_awssigv4_module)) };
let conf = conf.unwrap();
ngx_log_debug_http!(request, "AWS signature V4 module {}", {
if conf.enable {
Expand Down
5 changes: 3 additions & 2 deletions examples/curl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use ngx::http::MergeConfigError;
use ngx::{core, core::Status, http, http::HTTPModule};
use ngx::{http_request_handler, ngx_log_debug_http, ngx_modules, ngx_null_command, ngx_string};
use std::os::raw::{c_char, c_void};
use std::ptr::addr_of;

struct Module;

Expand All @@ -17,7 +18,7 @@ impl http::HTTPModule for Module {
type LocConf = ModuleConfig;

unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t {
let cmcf = http::ngx_http_conf_get_module_main_conf(cf, &ngx_http_core_module);
let cmcf = http::ngx_http_conf_get_module_main_conf(cf, &*addr_of!(ngx_http_core_module));

let h = ngx_array_push(&mut (*cmcf).phases[ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize].handlers)
as *mut ngx_http_handler_pt;
Expand Down Expand Up @@ -104,7 +105,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>(&ngx_http_curl_module) };
let co = unsafe { request.get_module_loc_conf::<ModuleConfig>(&*addr_of!(ngx_http_curl_module)) };
let co = co.expect("module config is none");

ngx_log_debug_http!(request, "curl module enabled: {}", co.enable);
Expand Down
8 changes: 5 additions & 3 deletions examples/upstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use ngx::{
use std::{
mem,
os::raw::{c_char, c_void},
ptr::addr_of,
slice,
};

Expand Down Expand Up @@ -152,7 +153,7 @@ http_upstream_init_peer_pt!(
}

let maybe_conf: Option<*const SrvConfig> =
unsafe { ngx_http_conf_upstream_srv_conf_immutable(us, &ngx_http_upstream_custom_module) };
unsafe { ngx_http_conf_upstream_srv_conf_immutable(us, &*addr_of!(ngx_http_upstream_custom_module)) };
if maybe_conf.is_none() {
return Status::NGX_ERROR;
}
Expand Down Expand Up @@ -244,7 +245,7 @@ unsafe extern "C" fn ngx_http_upstream_init_custom(
ngx_log_debug_mask!(DebugMask::Http, (*cf).log, "CUSTOM UPSTREAM peer init_upstream");

let maybe_conf: Option<*mut SrvConfig> =
ngx_http_conf_upstream_srv_conf_mutable(us, &ngx_http_upstream_custom_module);
ngx_http_conf_upstream_srv_conf_mutable(us, &*addr_of!(ngx_http_upstream_custom_module));
if maybe_conf.is_none() {
ngx_conf_log_error(
NGX_LOG_EMERG as usize,
Expand Down Expand Up @@ -309,7 +310,8 @@ unsafe extern "C" fn ngx_http_upstream_commands_set_custom(
}

let uscf: *mut ngx_http_upstream_srv_conf_t =
ngx_http_conf_get_module_srv_conf(cf, &ngx_http_upstream_module) as *mut ngx_http_upstream_srv_conf_t;
ngx_http_conf_get_module_srv_conf(cf, &*addr_of!(ngx_http_upstream_module))
as *mut ngx_http_upstream_srv_conf_t;

ccf.original_init_upstream = if (*uscf).peer.init_upstream.is_some() {
(*uscf).peer.init_upstream
Expand Down
Loading