Skip to content

Commit e47e4e6

Browse files
committed
chore: expose minimal sufficient set of symbols via ::prelude::*
1 parent a575249 commit e47e4e6

File tree

10 files changed

+148
-49
lines changed

10 files changed

+148
-49
lines changed

examples/async.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
use std::ffi::{c_char, c_void};
21
use std::ptr::{addr_of, addr_of_mut};
32
use std::sync::atomic::AtomicBool;
43
use std::sync::Arc;
54
use std::time::Instant;
65

76
use ngx::core;
8-
use ngx::ffi::{
9-
nginx_version, ngx_array_push, ngx_command_t, ngx_conf_t, ngx_cycle, ngx_event_t, ngx_http_core_module,
10-
ngx_http_core_run_phases, ngx_http_handler_pt, ngx_http_module_t, ngx_http_phases_NGX_HTTP_ACCESS_PHASE,
11-
ngx_http_request_t, ngx_int_t, ngx_module_t, ngx_posted_events, ngx_queue_s, ngx_str_t, ngx_uint_t, NGX_CONF_TAKE1,
12-
NGX_HTTP_LOC_CONF, NGX_HTTP_MODULE, NGX_RS_HTTP_LOC_CONF_OFFSET, NGX_RS_MODULE_SIGNATURE,
13-
};
7+
use ngx::core::prelude::*;
8+
use ngx::ffi::{ngx_array_push, ngx_http_core_run_phases, ngx_queue_s, NGX_RS_MODULE_SIGNATURE};
9+
use ngx::http::prelude::*;
1410
use ngx::http::{self, HTTPModule, MergeConfigError};
1511
use ngx::{http_request_handler, ngx_log_debug_http, ngx_null_command, ngx_string};
1612
use tokio::runtime::Runtime;
@@ -25,8 +21,7 @@ impl http::HTTPModule for Module {
2521
unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t {
2622
let cmcf = http::ngx_http_conf_get_module_main_conf(cf, &*addr_of!(ngx_http_core_module));
2723

28-
let h = ngx_array_push(&mut (*cmcf).phases[ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize].handlers)
29-
as *mut ngx_http_handler_pt;
24+
let h = ngx_array_push(&mut (*cmcf).phases[NGX_HTTP_ACCESS_PHASE].handlers) as *mut ngx_http_handler_pt;
3025
if h.is_null() {
3126
return core::Status::NGX_ERROR.into();
3227
}
@@ -59,7 +54,7 @@ static mut NGX_HTTP_ASYNC_COMMANDS: [ngx_command_t; 2] = [
5954
name: ngx_string!("async"),
6055
type_: (NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1) as ngx_uint_t,
6156
set: Some(ngx_http_async_commands_set_enable),
62-
conf: NGX_RS_HTTP_LOC_CONF_OFFSET,
57+
conf: NGX_HTTP_LOC_CONF_OFFSET,
6358
offset: 0,
6459
post: std::ptr::null_mut(),
6560
},

examples/awssig.rs

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

43
use http::HeaderMap;
54
use ngx::core;
6-
use ngx::ffi::{
7-
nginx_version, ngx_array_push, ngx_command_t, ngx_conf_t, ngx_http_core_module, ngx_http_handler_pt,
8-
ngx_http_module_t, ngx_http_phases_NGX_HTTP_PRECONTENT_PHASE, ngx_int_t, ngx_module_t, ngx_str_t, ngx_uint_t,
9-
NGX_CONF_TAKE1, NGX_HTTP_LOC_CONF, NGX_HTTP_MODULE, NGX_HTTP_SRV_CONF, NGX_RS_HTTP_LOC_CONF_OFFSET,
10-
NGX_RS_MODULE_SIGNATURE,
11-
};
5+
use ngx::core::prelude::*;
6+
use ngx::ffi::{ngx_array_push, NGX_RS_MODULE_SIGNATURE};
7+
use ngx::http::prelude::*;
128
use ngx::http::*;
139
use ngx::{http_request_handler, ngx_log_debug_http, ngx_null_command, ngx_string};
1410

@@ -22,8 +18,7 @@ impl HTTPModule for Module {
2218
unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t {
2319
let cmcf = ngx_http_conf_get_module_main_conf(cf, &*addr_of!(ngx_http_core_module));
2420

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;
21+
let h = ngx_array_push(&mut (*cmcf).phases[NGX_HTTP_PRECONTENT_PHASE].handlers) as *mut ngx_http_handler_pt;
2722
if h.is_null() {
2823
return core::Status::NGX_ERROR.into();
2924
}
@@ -47,39 +42,39 @@ static mut NGX_HTTP_AWSSIGV4_COMMANDS: [ngx_command_t; 6] = [
4742
name: ngx_string!("awssigv4"),
4843
type_: (NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE1) as ngx_uint_t,
4944
set: Some(ngx_http_awssigv4_commands_set_enable),
50-
conf: NGX_RS_HTTP_LOC_CONF_OFFSET,
45+
conf: NGX_HTTP_LOC_CONF_OFFSET,
5146
offset: 0,
5247
post: std::ptr::null_mut(),
5348
},
5449
ngx_command_t {
5550
name: ngx_string!("awssigv4_access_key"),
5651
type_: (NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE1) as ngx_uint_t,
5752
set: Some(ngx_http_awssigv4_commands_set_access_key),
58-
conf: NGX_RS_HTTP_LOC_CONF_OFFSET,
53+
conf: NGX_HTTP_LOC_CONF_OFFSET,
5954
offset: 0,
6055
post: std::ptr::null_mut(),
6156
},
6257
ngx_command_t {
6358
name: ngx_string!("awssigv4_secret_key"),
6459
type_: (NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE1) as ngx_uint_t,
6560
set: Some(ngx_http_awssigv4_commands_set_secret_key),
66-
conf: NGX_RS_HTTP_LOC_CONF_OFFSET,
61+
conf: NGX_HTTP_LOC_CONF_OFFSET,
6762
offset: 0,
6863
post: std::ptr::null_mut(),
6964
},
7065
ngx_command_t {
7166
name: ngx_string!("awssigv4_s3_bucket"),
7267
type_: (NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE1) as ngx_uint_t,
7368
set: Some(ngx_http_awssigv4_commands_set_s3_bucket),
74-
conf: NGX_RS_HTTP_LOC_CONF_OFFSET,
69+
conf: NGX_HTTP_LOC_CONF_OFFSET,
7570
offset: 0,
7671
post: std::ptr::null_mut(),
7772
},
7873
ngx_command_t {
7974
name: ngx_string!("awssigv4_s3_endpoint"),
8075
type_: (NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE1) as ngx_uint_t,
8176
set: Some(ngx_http_awssigv4_commands_set_s3_endpoint),
82-
conf: NGX_RS_HTTP_LOC_CONF_OFFSET,
77+
conf: NGX_HTTP_LOC_CONF_OFFSET,
8378
offset: 0,
8479
post: std::ptr::null_mut(),
8580
},

examples/curl.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
use std::ffi::{c_char, c_void};
21
use std::ptr::addr_of;
32

43
use ngx::core;
5-
use ngx::ffi::{
6-
nginx_version, ngx_array_push, ngx_command_t, ngx_conf_t, ngx_http_core_module, ngx_http_handler_pt,
7-
ngx_http_module_t, ngx_http_phases_NGX_HTTP_ACCESS_PHASE, ngx_int_t, ngx_module_t, ngx_str_t, ngx_uint_t,
8-
NGX_CONF_TAKE1, NGX_HTTP_LOC_CONF, NGX_HTTP_MODULE, NGX_RS_HTTP_LOC_CONF_OFFSET, NGX_RS_MODULE_SIGNATURE,
9-
};
4+
use ngx::core::prelude::*;
5+
use ngx::ffi::{ngx_array_push, NGX_RS_MODULE_SIGNATURE};
6+
use ngx::http::prelude::*;
107
use ngx::http::{self, HTTPModule, MergeConfigError};
118
use ngx::{http_request_handler, ngx_log_debug_http, ngx_null_command, ngx_string};
129

@@ -20,8 +17,7 @@ impl http::HTTPModule for Module {
2017
unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t {
2118
let cmcf = http::ngx_http_conf_get_module_main_conf(cf, &*addr_of!(ngx_http_core_module));
2219

23-
let h = ngx_array_push(&mut (*cmcf).phases[ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize].handlers)
24-
as *mut ngx_http_handler_pt;
20+
let h = ngx_array_push(&mut (*cmcf).phases[NGX_HTTP_ACCESS_PHASE].handlers) as *mut ngx_http_handler_pt;
2521
if h.is_null() {
2622
return core::Status::NGX_ERROR.into();
2723
}
@@ -41,7 +37,7 @@ static mut NGX_HTTP_CURL_COMMANDS: [ngx_command_t; 2] = [
4137
name: ngx_string!("curl"),
4238
type_: (NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1) as ngx_uint_t,
4339
set: Some(ngx_http_curl_commands_set_enable),
44-
conf: NGX_RS_HTTP_LOC_CONF_OFFSET,
40+
conf: NGX_HTTP_LOC_CONF_OFFSET,
4541
offset: 0,
4642
post: std::ptr::null_mut(),
4743
},

examples/httporigdst.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
use std::ffi::{c_char, c_int, c_void};
1+
use std::ffi::c_int;
22
use std::ptr::addr_of;
33

44
use ngx::core;
5+
use ngx::core::prelude::*;
56
use ngx::ffi::{
6-
in_port_t, nginx_version, 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_uint_t,
8-
ngx_variable_value_t, sockaddr, sockaddr_storage, INET_ADDRSTRLEN, NGX_HTTP_MODULE, NGX_RS_MODULE_SIGNATURE,
7+
in_port_t, ngx_connection_local_sockaddr, ngx_inet_get_port, ngx_sock_ntop, sockaddr, sockaddr_storage,
8+
INET_ADDRSTRLEN, NGX_RS_MODULE_SIGNATURE,
99
};
10+
use ngx::http::prelude::*;
1011
use ngx::http::{self, HTTPModule};
1112
use ngx::{http_variable_get, ngx_http_null_variable, ngx_log_debug_http, ngx_null_string, ngx_string};
1213

examples/upstream.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,18 @@
66
* The NGINX authors are grateful to @gabihodoroaga for their contributions
77
* to the community at large.
88
*/
9-
use std::ffi::{c_char, c_void};
109
use std::mem;
1110
use std::ptr::addr_of;
1211
use std::slice;
1312

13+
use ngx::core::prelude::*;
1414
use ngx::core::{Pool, Status};
1515
use ngx::ffi::{
16-
nginx_version, ngx_atoi, ngx_command_t, ngx_conf_t, ngx_connection_t, ngx_event_free_peer_pt,
17-
ngx_event_get_peer_pt, ngx_http_module_t, ngx_http_upstream_init_peer_pt, ngx_http_upstream_init_pt,
16+
ngx_atoi, ngx_event_free_peer_pt, ngx_event_get_peer_pt, ngx_http_upstream_init_peer_pt, ngx_http_upstream_init_pt,
1817
ngx_http_upstream_init_round_robin, ngx_http_upstream_module, ngx_http_upstream_srv_conf_t, ngx_http_upstream_t,
19-
ngx_int_t, ngx_module_t, ngx_peer_connection_t, ngx_str_t, ngx_uint_t, NGX_CONF_NOARGS, NGX_CONF_TAKE1,
20-
NGX_CONF_UNSET, NGX_ERROR, NGX_HTTP_MODULE, NGX_HTTP_UPS_CONF, NGX_LOG_EMERG, NGX_RS_HTTP_SRV_CONF_OFFSET,
21-
NGX_RS_MODULE_SIGNATURE,
18+
ngx_peer_connection_t, NGX_ERROR, NGX_RS_MODULE_SIGNATURE,
2219
};
20+
use ngx::http::prelude::*;
2321
use ngx::http::{
2422
ngx_http_conf_get_module_srv_conf, ngx_http_conf_upstream_srv_conf_immutable,
2523
ngx_http_conf_upstream_srv_conf_mutable, HTTPModule, Merge, MergeConfigError, Request,
@@ -94,7 +92,7 @@ static mut NGX_HTTP_UPSTREAM_CUSTOM_COMMANDS: [ngx_command_t; 2] = [
9492
name: ngx_string!("custom"),
9593
type_: (NGX_HTTP_UPS_CONF | NGX_CONF_NOARGS | NGX_CONF_TAKE1) as ngx_uint_t,
9694
set: Some(ngx_http_upstream_commands_set_custom),
97-
conf: NGX_RS_HTTP_SRV_CONF_OFFSET,
95+
conf: NGX_HTTP_SRV_CONF_OFFSET,
9896
offset: 0,
9997
post: std::ptr::null_mut(),
10098
},

nginx-sys/build/wrapper.h

-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33
#include <ngx_config.h>
44
#include <ngx_core.h>
55

6-
// Define as constants since bindgen can't parse these values
7-
const size_t NGX_RS_HTTP_MAIN_CONF_OFFSET = NGX_HTTP_MAIN_CONF_OFFSET;
8-
const size_t NGX_RS_HTTP_SRV_CONF_OFFSET = NGX_HTTP_SRV_CONF_OFFSET;
9-
const size_t NGX_RS_HTTP_LOC_CONF_OFFSET = NGX_HTTP_LOC_CONF_OFFSET;
10-
116
const char *NGX_RS_MODULE_SIGNATURE = NGX_MODULE_SIGNATURE;
127

138
// `--prefix=` results in not emitting the declaration

src/core/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
mod buffer;
22
mod pool;
3+
/// The prelude module.
4+
///
5+
/// This module provides common constants and types that are used in NGINX http modules.
6+
pub mod prelude;
37
mod status;
48
mod string;
59

src/core/prelude.rs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
pub use std::ffi::{c_char, c_void};
2+
3+
pub use crate::ffi::ngx_command_t;
4+
pub use crate::ffi::ngx_conf_t;
5+
pub use crate::ffi::ngx_connection_t;
6+
pub use crate::ffi::ngx_event_t;
7+
pub use crate::ffi::ngx_int_t;
8+
pub use crate::ffi::ngx_module_t;
9+
pub use crate::ffi::ngx_str_t;
10+
pub use crate::ffi::ngx_uint_t;
11+
pub use crate::ffi::ngx_variable_value_t;
12+
13+
pub use crate::ffi::NGX_LOG_ALERT;
14+
pub use crate::ffi::NGX_LOG_CRIT;
15+
pub use crate::ffi::NGX_LOG_DEBUG;
16+
pub use crate::ffi::NGX_LOG_EMERG;
17+
pub use crate::ffi::NGX_LOG_ERR;
18+
pub use crate::ffi::NGX_LOG_INFO;
19+
pub use crate::ffi::NGX_LOG_NOTICE;
20+
pub use crate::ffi::NGX_LOG_WARN;
21+
22+
pub use crate::ffi::NGX_ANY_CONF;
23+
pub use crate::ffi::NGX_CONF_1MORE;
24+
pub use crate::ffi::NGX_CONF_2MORE;
25+
pub use crate::ffi::NGX_CONF_ANY;
26+
pub use crate::ffi::NGX_CONF_ARGS_NUMBER;
27+
pub use crate::ffi::NGX_CONF_BLOCK;
28+
pub use crate::ffi::NGX_CONF_FLAG;
29+
pub use crate::ffi::NGX_CONF_NOARGS;
30+
pub use crate::ffi::NGX_CONF_TAKE1;
31+
pub use crate::ffi::NGX_CONF_TAKE2;
32+
pub use crate::ffi::NGX_CONF_TAKE3;
33+
pub use crate::ffi::NGX_CONF_TAKE4;
34+
pub use crate::ffi::NGX_CONF_TAKE5;
35+
pub use crate::ffi::NGX_CONF_TAKE6;
36+
pub use crate::ffi::NGX_CONF_TAKE7;
37+
pub use crate::ffi::NGX_CONF_UNSET;
38+
pub use crate::ffi::NGX_DIRECT_CONF;
39+
pub use crate::ffi::NGX_MAIN_CONF;
40+
41+
pub use crate::ffi::ngx_cycle;
42+
pub use crate::ffi::ngx_posted_events;

src/http/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
mod conf;
22
mod module;
3+
/// The prelude module.
4+
///
5+
/// This module provides common constants and types that are used in NGINX http modules.
6+
pub mod prelude;
37
mod request;
48
mod status;
59
mod upstream;

src/http/prelude.rs

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
use core::mem::offset_of;
2+
3+
use crate::ffi::*;
4+
5+
pub use crate::ffi::ngx_http_handler_pt;
6+
pub use crate::ffi::ngx_http_module_t;
7+
pub use crate::ffi::ngx_http_request_t;
8+
pub use crate::ffi::ngx_http_variable_t;
9+
pub use crate::ffi::ngx_http_variable_value_t;
10+
11+
/// The offset of the `main_conf` field in the `ngx_http_conf_ctx_t` struct.
12+
///
13+
/// This is used to access the main configuration context for an HTTP module.
14+
pub const NGX_HTTP_MAIN_CONF_OFFSET: usize = offset_of!(ngx_http_conf_ctx_t, main_conf);
15+
/// The offset of the `srv_conf` field in the `ngx_http_conf_ctx_t` struct.
16+
///
17+
/// This is used to access the server configuration context for an HTTP module.
18+
pub const NGX_HTTP_SRV_CONF_OFFSET: usize = offset_of!(ngx_http_conf_ctx_t, srv_conf);
19+
/// The offset of the `loc_conf` field in the `ngx_http_conf_ctx_t` struct.
20+
///
21+
/// This is used to access the location configuration context for an HTTP module.
22+
pub const NGX_HTTP_LOC_CONF_OFFSET: usize = offset_of!(ngx_http_conf_ctx_t, loc_conf);
23+
24+
pub use crate::ffi::NGX_HTTP_MODULE;
25+
26+
pub use crate::ffi::NGX_HTTP_LIF_CONF;
27+
pub use crate::ffi::NGX_HTTP_LMT_CONF;
28+
pub use crate::ffi::NGX_HTTP_LOC_CONF;
29+
pub use crate::ffi::NGX_HTTP_MAIN_CONF;
30+
pub use crate::ffi::NGX_HTTP_SIF_CONF;
31+
pub use crate::ffi::NGX_HTTP_SRV_CONF;
32+
pub use crate::ffi::NGX_HTTP_UPS_CONF;
33+
34+
/// First phase.
35+
///
36+
/// The ngx_http_realip_module registers its handler at this phase to enable
37+
/// substitution of client addresses before any other module is invoked.
38+
pub const NGX_HTTP_POST_READ_PHASE: usize = ngx_http_phases_NGX_HTTP_POST_READ_PHASE as usize;
39+
/// Phase where rewrite directives defined in a server block (but outside a location block) are processed.
40+
///
41+
/// The ngx_http_rewrite_module installs its handler at this phase.
42+
pub const NGX_HTTP_SERVER_REWRITE_PHASE: usize = ngx_http_phases_NGX_HTTP_SERVER_REWRITE_PHASE as usize;
43+
/// Same as NGX_HTTP_SERVER_REWRITE_PHASE, but for rewrite rules defined in the location, chosen in
44+
/// the previous phase.
45+
pub const NGX_HTTP_REWRITE_PHASE: usize = ngx_http_phases_NGX_HTTP_REWRITE_PHASE as usize;
46+
/// A common phase for different types of handlers, not associated with access control.
47+
///
48+
/// The standard nginx modules ngx_http_limit_conn_module and ngx_http_limit_req_module register
49+
/// their handlers at this phase.
50+
pub const NGX_HTTP_PREACCESS_PHASE: usize = ngx_http_phases_NGX_HTTP_PREACCESS_PHASE as usize;
51+
/// Phase where it is verified that the client is authorized to make the request.
52+
pub const NGX_HTTP_ACCESS_PHASE: usize = ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize;
53+
/// Phase for handlers to be called prior to generating content.
54+
///
55+
/// Standard modules such as ngx_http_try_files_module and ngx_http_mirror_module register their
56+
/// handlers at this phase.
57+
pub const NGX_HTTP_PRECONTENT_PHASE: usize = ngx_http_phases_NGX_HTTP_PRECONTENT_PHASE as usize;
58+
/// Phase where the response is normally generated.
59+
///
60+
/// Multiple nginx standard modules register their handlers at this phase.
61+
pub const NGX_HTTP_CONTENT_PHASE: usize = ngx_http_phases_NGX_HTTP_CONTENT_PHASE as usize;
62+
/// Phase where request logging is performed.
63+
///
64+
/// Currently, only the ngx_http_log_module registers its handler at this stage for access logging.
65+
pub const NGX_HTTP_LOG_PHASE: usize = ngx_http_phases_NGX_HTTP_LOG_PHASE as usize;
66+
67+
pub use crate::ffi::ngx_http_core_module;
68+
69+
pub use crate::ffi::ngx_http_add_variable;

0 commit comments

Comments
 (0)