Skip to content

Commit c93c6bf

Browse files
committed
chore: reformat with max-width = 100
1 parent b09e48e commit c93c6bf

18 files changed

+306
-99
lines changed

examples/async.rs

+18-9
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ 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_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,
10+
ngx_array_push, ngx_command_t, ngx_conf_t, ngx_connection_t, ngx_event_t, ngx_http_handler_pt,
11+
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,
13+
NGX_CONF_TAKE1, NGX_HTTP_LOC_CONF, NGX_HTTP_LOC_CONF_OFFSET, NGX_HTTP_MODULE,
1414
};
1515
use ngx::http::{self, HttpModule, MergeConfigError};
1616
use ngx::http::{HttpModuleLocationConf, HttpModuleMainConf, NgxHttpCoreModule};
@@ -29,8 +29,9 @@ impl http::HttpModule for Module {
2929
let cf = &mut *cf;
3030
let cmcf = NgxHttpCoreModule::main_conf_mut(cf).expect("http core main conf");
3131

32-
let h = ngx_array_push(&mut cmcf.phases[ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize].handlers)
33-
as *mut ngx_http_handler_pt;
32+
let h = ngx_array_push(
33+
&mut cmcf.phases[ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize].handlers,
34+
) as *mut ngx_http_handler_pt;
3435
if h.is_null() {
3536
return core::Status::NGX_ERROR.into();
3637
}
@@ -148,7 +149,9 @@ http_request_handler!(async_access_handler, |request: &mut http::Request| {
148149
return core::Status::NGX_DECLINED;
149150
}
150151

151-
if let Some(ctx) = unsafe { request.get_module_ctx::<RequestCTX>(&*addr_of!(ngx_http_async_module)) } {
152+
if let Some(ctx) =
153+
unsafe { request.get_module_ctx::<RequestCTX>(&*addr_of!(ngx_http_async_module)) }
154+
{
152155
if !ctx.done.load(Ordering::Relaxed) {
153156
return core::Status::NGX_AGAIN;
154157
}
@@ -180,7 +183,10 @@ http_request_handler!(async_access_handler, |request: &mut http::Request| {
180183
// not really thread safe, we should apply all these operation in nginx thread
181184
// but this is just an example. proper way would be storing these headers in the request ctx
182185
// and apply them when we get back to the nginx thread.
183-
req.add_header_out("X-Async-Time", start.elapsed().as_millis().to_string().as_str());
186+
req.add_header_out(
187+
"X-Async-Time",
188+
start.elapsed().as_millis().to_string().as_str(),
189+
);
184190

185191
done_flag.store(true, Ordering::Release);
186192
// there is a small issue here. If traffic is low we may get stuck behind a 300ms timer
@@ -216,7 +222,10 @@ extern "C" fn ngx_http_async_commands_set_enable(
216222

217223
fn ngx_http_async_runtime() -> &'static Runtime {
218224
// Should not be called from the master process
219-
assert_ne!(unsafe { ngx::ffi::ngx_process }, ngx::ffi::NGX_PROCESS_MASTER as _);
225+
assert_ne!(
226+
unsafe { ngx::ffi::ngx_process },
227+
ngx::ffi::NGX_PROCESS_MASTER as _
228+
);
220229

221230
static RUNTIME: OnceLock<Runtime> = OnceLock::new();
222231
RUNTIME.get_or_init(|| {

examples/awssig.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ use http::HeaderMap;
44
use ngx::core;
55
use ngx::ffi::{
66
ngx_array_push, ngx_command_t, ngx_conf_t, ngx_http_handler_pt, ngx_http_module_t,
7-
ngx_http_phases_NGX_HTTP_PRECONTENT_PHASE, ngx_int_t, ngx_module_t, ngx_str_t, ngx_uint_t, NGX_CONF_TAKE1,
8-
NGX_HTTP_LOC_CONF, NGX_HTTP_LOC_CONF_OFFSET, NGX_HTTP_MODULE, NGX_HTTP_SRV_CONF,
7+
ngx_http_phases_NGX_HTTP_PRECONTENT_PHASE, ngx_int_t, ngx_module_t, ngx_str_t, ngx_uint_t,
8+
NGX_CONF_TAKE1, NGX_HTTP_LOC_CONF, NGX_HTTP_LOC_CONF_OFFSET, NGX_HTTP_MODULE,
9+
NGX_HTTP_SRV_CONF,
910
};
1011
use ngx::http::*;
1112
use ngx::{http_request_handler, ngx_log_debug_http, ngx_string};
@@ -22,8 +23,9 @@ impl HttpModule for Module {
2223
let cf = &mut *cf;
2324
let cmcf = NgxHttpCoreModule::main_conf_mut(cf).expect("http core main conf");
2425

25-
let h = ngx_array_push(&mut cmcf.phases[ngx_http_phases_NGX_HTTP_PRECONTENT_PHASE as usize].handlers)
26-
as *mut ngx_http_handler_pt;
26+
let h = ngx_array_push(
27+
&mut cmcf.phases[ngx_http_phases_NGX_HTTP_PRECONTENT_PHASE as usize].handlers,
28+
) as *mut ngx_http_handler_pt;
2729
if h.is_null() {
2830
return core::Status::NGX_ERROR.into();
2931
}

examples/curl.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::ffi::{c_char, c_void};
33
use ngx::core;
44
use ngx::ffi::{
55
ngx_array_push, ngx_command_t, ngx_conf_t, ngx_http_handler_pt, ngx_http_module_t,
6-
ngx_http_phases_NGX_HTTP_ACCESS_PHASE, ngx_int_t, ngx_module_t, ngx_str_t, ngx_uint_t, NGX_CONF_TAKE1,
7-
NGX_HTTP_LOC_CONF, NGX_HTTP_LOC_CONF_OFFSET, NGX_HTTP_MODULE,
6+
ngx_http_phases_NGX_HTTP_ACCESS_PHASE, ngx_int_t, ngx_module_t, ngx_str_t, ngx_uint_t,
7+
NGX_CONF_TAKE1, NGX_HTTP_LOC_CONF, NGX_HTTP_LOC_CONF_OFFSET, NGX_HTTP_MODULE,
88
};
99
use ngx::http::{self, HttpModule, MergeConfigError};
1010
use ngx::http::{HttpModuleLocationConf, HttpModuleMainConf, NgxHttpCoreModule};
@@ -22,8 +22,9 @@ impl http::HttpModule for Module {
2222
let cf = &mut *cf;
2323
let cmcf = NgxHttpCoreModule::main_conf_mut(cf).expect("http core main conf");
2424

25-
let h = ngx_array_push(&mut cmcf.phases[ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize].handlers)
26-
as *mut ngx_http_handler_pt;
25+
let h = ngx_array_push(
26+
&mut cmcf.phases[ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize].handlers,
27+
) as *mut ngx_http_handler_pt;
2728
if h.is_null() {
2829
return core::Status::NGX_ERROR.into();
2930
}

examples/httporigdst.rs

+38-11
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use std::ptr::addr_of;
44
use ngx::core;
55
use ngx::ffi::{
66
in_port_t, ngx_conf_t, ngx_connection_local_sockaddr, ngx_http_add_variable, ngx_http_module_t,
7-
ngx_http_variable_t, ngx_inet_get_port, ngx_int_t, ngx_module_t, ngx_sock_ntop, ngx_str_t, ngx_variable_value_t,
8-
sockaddr, sockaddr_storage, INET_ADDRSTRLEN, NGX_HTTP_MODULE,
7+
ngx_http_variable_t, ngx_inet_get_port, ngx_int_t, ngx_module_t, ngx_sock_ntop, ngx_str_t,
8+
ngx_variable_value_t, sockaddr, sockaddr_storage, INET_ADDRSTRLEN, NGX_HTTP_MODULE,
99
};
1010
use ngx::http::{self, HttpModule};
1111
use ngx::{http_variable_get, ngx_log_debug_http, ngx_string};
@@ -33,7 +33,13 @@ impl NgxHttpOrigDstCtx {
3333
if port_data.is_null() {
3434
return core::Status::NGX_ERROR;
3535
}
36-
unsafe { libc::memcpy(port_data, port_str.as_bytes().as_ptr() as *const c_void, port_str.len()) };
36+
unsafe {
37+
libc::memcpy(
38+
port_data,
39+
port_str.as_bytes().as_ptr() as *const c_void,
40+
port_str.len(),
41+
)
42+
};
3743
self.orig_dst_port.len = port_str.len();
3844
self.orig_dst_port.data = port_data as *mut u8;
3945

@@ -118,7 +124,9 @@ static mut NGX_HTTP_ORIG_DST_VARS: [ngx_http_variable_t; 2] = [
118124
},
119125
];
120126

121-
unsafe fn ngx_get_origdst(request: &mut http::Request) -> Result<(String, in_port_t), core::Status> {
127+
unsafe fn ngx_get_origdst(
128+
request: &mut http::Request,
129+
) -> Result<(String, in_port_t), core::Status> {
122130
let c = request.connection();
123131

124132
if (*c).type_ != libc::SOCK_STREAM {
@@ -168,7 +176,10 @@ unsafe fn ngx_get_origdst(request: &mut http::Request) -> Result<(String, in_por
168176
)
169177
};
170178
if e == 0 {
171-
ngx_log_debug_http!(request, "httporigdst: ngx_sock_ntop failed to convert sockaddr");
179+
ngx_log_debug_http!(
180+
request,
181+
"httporigdst: ngx_sock_ntop failed to convert sockaddr"
182+
);
172183
return Err(core::Status::NGX_ERROR);
173184
}
174185
ip.truncate(e);
@@ -201,16 +212,24 @@ http_variable_get!(
201212
Ok((ip, port)) => {
202213
// create context,
203214
// set context
204-
let new_ctx = request.pool().allocate::<NgxHttpOrigDstCtx>(Default::default());
215+
let new_ctx = request
216+
.pool()
217+
.allocate::<NgxHttpOrigDstCtx>(Default::default());
205218

206219
if new_ctx.is_null() {
207220
return core::Status::NGX_ERROR;
208221
}
209222

210-
ngx_log_debug_http!(request, "httporigdst: saving ip - {:?}, port - {}", ip, port,);
223+
ngx_log_debug_http!(
224+
request,
225+
"httporigdst: saving ip - {:?}, port - {}",
226+
ip,
227+
port,
228+
);
211229
(*new_ctx).save(&ip, port, &mut request.pool());
212230
(*new_ctx).bind_addr(v);
213-
request.set_module_ctx(new_ctx as *mut c_void, &*addr_of!(ngx_http_orig_dst_module));
231+
request
232+
.set_module_ctx(new_ctx as *mut c_void, &*addr_of!(ngx_http_orig_dst_module));
214233
}
215234
}
216235
core::Status::NGX_OK
@@ -240,16 +259,24 @@ http_variable_get!(
240259
Ok((ip, port)) => {
241260
// create context,
242261
// set context
243-
let new_ctx = request.pool().allocate::<NgxHttpOrigDstCtx>(Default::default());
262+
let new_ctx = request
263+
.pool()
264+
.allocate::<NgxHttpOrigDstCtx>(Default::default());
244265

245266
if new_ctx.is_null() {
246267
return core::Status::NGX_ERROR;
247268
}
248269

249-
ngx_log_debug_http!(request, "httporigdst: saving ip - {:?}, port - {}", ip, port,);
270+
ngx_log_debug_http!(
271+
request,
272+
"httporigdst: saving ip - {:?}, port - {}",
273+
ip,
274+
port,
275+
);
250276
(*new_ctx).save(&ip, port, &mut request.pool());
251277
(*new_ctx).bind_port(v);
252-
request.set_module_ctx(new_ctx as *mut c_void, &*addr_of!(ngx_http_orig_dst_module));
278+
request
279+
.set_module_ctx(new_ctx as *mut c_void, &*addr_of!(ngx_http_orig_dst_module));
253280
}
254281
}
255282
core::Status::NGX_OK

examples/upstream.rs

+36-12
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@ use std::slice;
1212

1313
use ngx::core::{Pool, Status};
1414
use ngx::ffi::{
15-
ngx_atoi, ngx_command_t, ngx_conf_t, ngx_connection_t, ngx_event_free_peer_pt, ngx_event_get_peer_pt,
16-
ngx_http_module_t, ngx_http_upstream_init_peer_pt, ngx_http_upstream_init_pt, ngx_http_upstream_init_round_robin,
17-
ngx_http_upstream_srv_conf_t, ngx_http_upstream_t, ngx_int_t, ngx_module_t, ngx_peer_connection_t, ngx_str_t,
18-
ngx_uint_t, NGX_CONF_NOARGS, NGX_CONF_TAKE1, NGX_CONF_UNSET, NGX_ERROR, NGX_HTTP_MODULE, NGX_HTTP_SRV_CONF_OFFSET,
19-
NGX_HTTP_UPS_CONF, NGX_LOG_EMERG,
15+
ngx_atoi, ngx_command_t, ngx_conf_t, ngx_connection_t, ngx_event_free_peer_pt,
16+
ngx_event_get_peer_pt, ngx_http_module_t, ngx_http_upstream_init_peer_pt,
17+
ngx_http_upstream_init_pt, ngx_http_upstream_init_round_robin, ngx_http_upstream_srv_conf_t,
18+
ngx_http_upstream_t, ngx_int_t, ngx_module_t, ngx_peer_connection_t, ngx_str_t, ngx_uint_t,
19+
NGX_CONF_NOARGS, NGX_CONF_TAKE1, NGX_CONF_UNSET, NGX_ERROR, NGX_HTTP_MODULE,
20+
NGX_HTTP_SRV_CONF_OFFSET, NGX_HTTP_UPS_CONF, NGX_LOG_EMERG,
2021
};
2122
use ngx::http::{HttpModule, Merge, MergeConfigError, Request};
2223
use ngx::http::{HttpModuleServerConf, NgxHttpUpstreamModule};
23-
use ngx::{http_upstream_init_peer_pt, ngx_conf_log_error, ngx_log_debug_http, ngx_log_debug_mask, ngx_string};
24+
use ngx::{
25+
http_upstream_init_peer_pt, ngx_conf_log_error, ngx_log_debug_http, ngx_log_debug_mask,
26+
ngx_string,
27+
};
2428

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

167174
ngx_log_debug_mask!(
@@ -209,7 +216,11 @@ unsafe extern "C" fn ngx_http_upstream_init_custom(
209216
cf: *mut ngx_conf_t,
210217
us: *mut ngx_http_upstream_srv_conf_t,
211218
) -> ngx_int_t {
212-
ngx_log_debug_mask!(DebugMask::Http, (*cf).log, "CUSTOM UPSTREAM peer init_upstream");
219+
ngx_log_debug_mask!(
220+
DebugMask::Http,
221+
(*cf).log,
222+
"CUSTOM UPSTREAM peer init_upstream"
223+
);
213224

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

227238
let init_upstream_ptr = hccf.original_init_upstream.unwrap();
228239
if init_upstream_ptr(cf, us) != Status::NGX_OK.into() {
229-
ngx_conf_log_error!(NGX_LOG_EMERG, cf, "CUSTOM UPSTREAM failed calling init_upstream");
240+
ngx_conf_log_error!(
241+
NGX_LOG_EMERG,
242+
cf,
243+
"CUSTOM UPSTREAM failed calling init_upstream"
244+
);
230245
return isize::from(Status::NGX_ERROR);
231246
}
232247

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

236-
ngx_log_debug_mask!(DebugMask::Http, (*cf).log, "CUSTOM UPSTREAM end peer init_upstream");
251+
ngx_log_debug_mask!(
252+
DebugMask::Http,
253+
(*cf).log,
254+
"CUSTOM UPSTREAM end peer init_upstream"
255+
);
237256
isize::from(Status::NGX_OK)
238257
}
239258

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

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

307327
(*conf).max = NGX_CONF_UNSET as u32;
308328

309-
ngx_log_debug_mask!(DebugMask::Http, (*cf).log, "CUSTOM UPSTREAM end create_srv_conf");
329+
ngx_log_debug_mask!(
330+
DebugMask::Http,
331+
(*cf).log,
332+
"CUSTOM UPSTREAM end create_srv_conf"
333+
);
310334
conf as *mut c_void
311335
}
312336
}

0 commit comments

Comments
 (0)