Skip to content

chore: reformat with max-width = 100 #151

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 2 commits into from
Apr 21, 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
15 changes: 15 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# This file specifies the revisions hidden from the blame view in GitHub UI.
#
# You can achieve the same effect locally with "--ignore-revs-file":
#
# git blame --ignore-revs-file .git-blame-ignore-revs
#
# or "blame.ignoreRevsFile" configuration option
#
# git config --local blame.ignoreRevsFile .git-blame-ignore-revs
#
# See also:
# https://docs.github.com./en/repositories/working-with-files/using-files/viewing-and-understanding-files

# Tree-wide formatting change: max-width 120 -> 100
d866b64a3c83105c20b0c005392f8e8fcaaf2066
38 changes: 24 additions & 14 deletions examples/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ 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_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::{HttpModuleLocationConf, HttpModuleMainConf, NgxHttpCoreModule};
Expand All @@ -29,8 +29,9 @@ impl http::HttpModule for Module {
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)
as *mut ngx_http_handler_pt;
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 Down Expand Up @@ -104,9 +105,9 @@ unsafe extern "C" fn check_async_work_done(event: *mut ngx_event_t) {
// Triggering async_access_handler again
ngx_post_event((*c).write, addr_of_mut!(ngx_posted_events));
} else {
// 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.
// 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.
ngx_post_event(event, addr_of_mut!(ngx_posted_next_events));
}
}
Expand Down Expand Up @@ -148,7 +149,9 @@ http_request_handler!(async_access_handler, |request: &mut http::Request| {
return core::Status::NGX_DECLINED;
}

if let Some(ctx) = unsafe { request.get_module_ctx::<RequestCTX>(&*addr_of!(ngx_http_async_module)) } {
if let Some(ctx) =
unsafe { request.get_module_ctx::<RequestCTX>(&*addr_of!(ngx_http_async_module)) }
{
if !ctx.done.load(Ordering::Relaxed) {
return core::Status::NGX_AGAIN;
}
Expand Down Expand Up @@ -180,12 +183,16 @@ http_request_handler!(async_access_handler, |request: &mut http::Request| {
// not really thread safe, we should apply all these operation in nginx thread
// but this is just an example. proper way would be storing these headers in the request ctx
// and apply them when we get back to the nginx thread.
req.add_header_out("X-Async-Time", start.elapsed().as_millis().to_string().as_str());
req.add_header_out(
"X-Async-Time",
start.elapsed().as_millis().to_string().as_str(),
);

done_flag.store(true, Ordering::Release);
// there is a small issue here. If traffic is low we may get stuck behind a 300ms timer
// in the nginx event loop. To workaround it we can notify the event loop using pthread_kill( nginx_thread, SIGIO )
// to wake up the event loop. (or patch nginx and use the same trick as the thread pool)
// in the nginx event loop. To workaround it we can notify the event loop using
// pthread_kill( nginx_thread, SIGIO ) to wake up the event loop. (or patch nginx
// and use the same trick as the thread pool)
}));

core::Status::NGX_AGAIN
Expand Down Expand Up @@ -216,7 +223,10 @@ extern "C" fn ngx_http_async_commands_set_enable(

fn ngx_http_async_runtime() -> &'static Runtime {
// Should not be called from the master process
assert_ne!(unsafe { ngx::ffi::ngx_process }, ngx::ffi::NGX_PROCESS_MASTER as _);
assert_ne!(
unsafe { ngx::ffi::ngx_process },
ngx::ffi::NGX_PROCESS_MASTER as _
);

static RUNTIME: OnceLock<Runtime> = OnceLock::new();
RUNTIME.get_or_init(|| {
Expand Down
10 changes: 6 additions & 4 deletions examples/awssig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use http::HeaderMap;
use ngx::core;
use ngx::ffi::{
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,
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,
};
use ngx::http::*;
use ngx::{http_request_handler, ngx_log_debug_http, ngx_string};
Expand All @@ -22,8 +23,9 @@ impl HttpModule for Module {
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)
as *mut ngx_http_handler_pt;
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 Down
9 changes: 5 additions & 4 deletions examples/curl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::ffi::{c_char, c_void};
use ngx::core;
use ngx::ffi::{
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,
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::{HttpModuleLocationConf, HttpModuleMainConf, NgxHttpCoreModule};
Expand All @@ -22,8 +22,9 @@ impl http::HttpModule for Module {
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)
as *mut ngx_http_handler_pt;
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 Down
49 changes: 38 additions & 11 deletions examples/httporigdst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::ptr::addr_of;
use ngx::core;
use ngx::ffi::{
in_port_t, ngx_conf_t, ngx_connection_local_sockaddr, ngx_http_add_variable, ngx_http_module_t,
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,
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_variable_get, ngx_log_debug_http, ngx_string};
Expand Down Expand Up @@ -33,7 +33,13 @@ impl NgxHttpOrigDstCtx {
if port_data.is_null() {
return core::Status::NGX_ERROR;
}
unsafe { libc::memcpy(port_data, port_str.as_bytes().as_ptr() as *const c_void, port_str.len()) };
unsafe {
libc::memcpy(
port_data,
port_str.as_bytes().as_ptr() as *const c_void,
port_str.len(),
)
};
self.orig_dst_port.len = port_str.len();
self.orig_dst_port.data = port_data as *mut u8;

Expand Down Expand Up @@ -118,7 +124,9 @@ static mut NGX_HTTP_ORIG_DST_VARS: [ngx_http_variable_t; 2] = [
},
];

unsafe fn ngx_get_origdst(request: &mut http::Request) -> Result<(String, in_port_t), core::Status> {
unsafe fn ngx_get_origdst(
request: &mut http::Request,
) -> Result<(String, in_port_t), core::Status> {
let c = request.connection();

if (*c).type_ != libc::SOCK_STREAM {
Expand Down Expand Up @@ -168,7 +176,10 @@ unsafe fn ngx_get_origdst(request: &mut http::Request) -> Result<(String, in_por
)
};
if e == 0 {
ngx_log_debug_http!(request, "httporigdst: ngx_sock_ntop failed to convert sockaddr");
ngx_log_debug_http!(
request,
"httporigdst: ngx_sock_ntop failed to convert sockaddr"
);
return Err(core::Status::NGX_ERROR);
}
ip.truncate(e);
Expand Down Expand Up @@ -201,16 +212,24 @@ http_variable_get!(
Ok((ip, port)) => {
// create context,
// set context
let new_ctx = request.pool().allocate::<NgxHttpOrigDstCtx>(Default::default());
let new_ctx = request
.pool()
.allocate::<NgxHttpOrigDstCtx>(Default::default());

if new_ctx.is_null() {
return core::Status::NGX_ERROR;
}

ngx_log_debug_http!(request, "httporigdst: saving ip - {:?}, port - {}", ip, port,);
ngx_log_debug_http!(
request,
"httporigdst: saving ip - {:?}, port - {}",
ip,
port,
);
(*new_ctx).save(&ip, port, &mut request.pool());
(*new_ctx).bind_addr(v);
request.set_module_ctx(new_ctx as *mut c_void, &*addr_of!(ngx_http_orig_dst_module));
request
.set_module_ctx(new_ctx as *mut c_void, &*addr_of!(ngx_http_orig_dst_module));
}
}
core::Status::NGX_OK
Expand Down Expand Up @@ -240,16 +259,24 @@ http_variable_get!(
Ok((ip, port)) => {
// create context,
// set context
let new_ctx = request.pool().allocate::<NgxHttpOrigDstCtx>(Default::default());
let new_ctx = request
.pool()
.allocate::<NgxHttpOrigDstCtx>(Default::default());

if new_ctx.is_null() {
return core::Status::NGX_ERROR;
}

ngx_log_debug_http!(request, "httporigdst: saving ip - {:?}, port - {}", ip, port,);
ngx_log_debug_http!(
request,
"httporigdst: saving ip - {:?}, port - {}",
ip,
port,
);
(*new_ctx).save(&ip, port, &mut request.pool());
(*new_ctx).bind_port(v);
request.set_module_ctx(new_ctx as *mut c_void, &*addr_of!(ngx_http_orig_dst_module));
request
.set_module_ctx(new_ctx as *mut c_void, &*addr_of!(ngx_http_orig_dst_module));
}
}
core::Status::NGX_OK
Expand Down
48 changes: 36 additions & 12 deletions examples/upstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@ use std::slice;

use ngx::core::{Pool, Status};
use ngx::ffi::{
ngx_atoi, ngx_command_t, ngx_conf_t, ngx_connection_t, ngx_event_free_peer_pt, ngx_event_get_peer_pt,
ngx_http_module_t, ngx_http_upstream_init_peer_pt, ngx_http_upstream_init_pt, ngx_http_upstream_init_round_robin,
ngx_http_upstream_srv_conf_t, ngx_http_upstream_t, ngx_int_t, ngx_module_t, ngx_peer_connection_t, ngx_str_t,
ngx_uint_t, NGX_CONF_NOARGS, NGX_CONF_TAKE1, NGX_CONF_UNSET, NGX_ERROR, NGX_HTTP_MODULE, NGX_HTTP_SRV_CONF_OFFSET,
NGX_HTTP_UPS_CONF, NGX_LOG_EMERG,
ngx_atoi, ngx_command_t, ngx_conf_t, ngx_connection_t, ngx_event_free_peer_pt,
ngx_event_get_peer_pt, ngx_http_module_t, ngx_http_upstream_init_peer_pt,
ngx_http_upstream_init_pt, ngx_http_upstream_init_round_robin, ngx_http_upstream_srv_conf_t,
ngx_http_upstream_t, ngx_int_t, ngx_module_t, ngx_peer_connection_t, ngx_str_t, ngx_uint_t,
NGX_CONF_NOARGS, NGX_CONF_TAKE1, NGX_CONF_UNSET, NGX_ERROR, NGX_HTTP_MODULE,
NGX_HTTP_SRV_CONF_OFFSET, NGX_HTTP_UPS_CONF, NGX_LOG_EMERG,
};
use ngx::http::{HttpModule, Merge, MergeConfigError, Request};
use ngx::http::{HttpModuleServerConf, NgxHttpUpstreamModule};
use ngx::{http_upstream_init_peer_pt, ngx_conf_log_error, ngx_log_debug_http, ngx_log_debug_mask, ngx_string};
use ngx::{
http_upstream_init_peer_pt, ngx_conf_log_error, ngx_log_debug_http, ngx_log_debug_mask,
ngx_string,
};

#[derive(Clone, Copy, Debug)]
#[repr(C)]
Expand Down Expand Up @@ -161,7 +165,10 @@ http_upstream_init_peer_pt!(
// ngx_http_usptream_get_custom_peer
// For demonstration purposes, use the original get callback, but log this callback proxies through
// to the original.
unsafe extern "C" fn ngx_http_upstream_get_custom_peer(pc: *mut ngx_peer_connection_t, data: *mut c_void) -> ngx_int_t {
unsafe extern "C" fn ngx_http_upstream_get_custom_peer(
pc: *mut ngx_peer_connection_t,
data: *mut c_void,
) -> ngx_int_t {
let hcpd: *mut UpstreamPeerData = unsafe { mem::transmute(data) };

ngx_log_debug_mask!(
Expand Down Expand Up @@ -209,7 +216,11 @@ unsafe extern "C" fn ngx_http_upstream_init_custom(
cf: *mut ngx_conf_t,
us: *mut ngx_http_upstream_srv_conf_t,
) -> ngx_int_t {
ngx_log_debug_mask!(DebugMask::Http, (*cf).log, "CUSTOM UPSTREAM peer init_upstream");
ngx_log_debug_mask!(
DebugMask::Http,
(*cf).log,
"CUSTOM UPSTREAM peer init_upstream"
);

// SAFETY: this function is called with non-NULL uf always
let us = unsafe { &mut *us };
Expand All @@ -226,14 +237,22 @@ unsafe extern "C" fn ngx_http_upstream_init_custom(

let init_upstream_ptr = hccf.original_init_upstream.unwrap();
if init_upstream_ptr(cf, us) != Status::NGX_OK.into() {
ngx_conf_log_error!(NGX_LOG_EMERG, cf, "CUSTOM UPSTREAM failed calling init_upstream");
ngx_conf_log_error!(
NGX_LOG_EMERG,
cf,
"CUSTOM UPSTREAM failed calling init_upstream"
);
return isize::from(Status::NGX_ERROR);
}

hccf.original_init_peer = us.peer.init;
us.peer.init = Some(http_upstream_init_custom_peer);

ngx_log_debug_mask!(DebugMask::Http, (*cf).log, "CUSTOM UPSTREAM end peer init_upstream");
ngx_log_debug_mask!(
DebugMask::Http,
(*cf).log,
"CUSTOM UPSTREAM end peer init_upstream"
);
isize::from(Status::NGX_OK)
}

Expand All @@ -252,7 +271,8 @@ unsafe extern "C" fn ngx_http_upstream_commands_set_custom(
let ccf = &mut (*(conf as *mut SrvConfig));

if (*cf.args).nelts == 2 {
let value: &[ngx_str_t] = slice::from_raw_parts((*cf.args).elts as *const ngx_str_t, (*cf.args).nelts);
let value: &[ngx_str_t] =
slice::from_raw_parts((*cf.args).elts as *const ngx_str_t, (*cf.args).nelts);
let n = ngx_atoi(value[1].data, value[1].len);
if n == (NGX_ERROR as isize) || n == 0 {
ngx_conf_log_error!(
Expand Down Expand Up @@ -306,7 +326,11 @@ impl HttpModule for Module {

(*conf).max = NGX_CONF_UNSET as u32;

ngx_log_debug_mask!(DebugMask::Http, (*cf).log, "CUSTOM UPSTREAM end create_srv_conf");
ngx_log_debug_mask!(
DebugMask::Http,
(*cf).log,
"CUSTOM UPSTREAM end create_srv_conf"
);
conf as *mut c_void
}
}
Expand Down
Loading
Loading