-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello_ngx.rs
165 lines (144 loc) · 5.57 KB
/
hello_ngx.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use ngx::ffi::{
nginx_version, ngx_array_push, ngx_command_t, ngx_conf_t,
ngx_http_core_module, ngx_http_handler_pt, ngx_http_module_t,
ngx_http_phases_NGX_HTTP_ACCESS_PHASE, ngx_http_request_t,
ngx_int_t, ngx_module_t, ngx_str_t, ngx_uint_t,
NGX_CONF_TAKE1, NGX_HTTP_LOC_CONF, NGX_HTTP_MODULE,
NGX_RS_HTTP_LOC_CONF_OFFSET, NGX_RS_MODULE_SIGNATURE,
};
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};
struct Module;
impl http::HTTPModule for Module {
type MainConf = ();
type SrvConf = ();
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 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();
}
// set an Access phase handler
*h = Some(hello_ngx_access_handler);
core::Status::NGX_OK.into()
}
}
#[derive(Debug, Default)]
struct ModuleConfig {
enable: bool,
}
// Declaration of of ngx_command_t, this is the directives setup of the module
//
// name: The directive string, no spaces
// type_: set of flags that indicate where the directive is legal and how many arguments the directive takes (https://www.nginx.com/resources/wiki/extending/api/configuration/#c.ngx_command_t.type)
// set: element is a pointer to a function for setting up part of the module’s configuration; typically this function will translate the arguments passed to this directive and save an appropriate value in its configuration struct.
// conf: conf tells Nginx whether this value will get saved to the module’s main configuration, server configuration, or location configuration
// offset: then specifies which part of this configuration struct to write to.
#[no_mangle]
static mut ngx_http_hello_ngx_commands: [ngx_command_t; 2] = [
ngx_command_t {
name: ngx_string!("hello_ngx"),
type_: (NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1) as ngx_uint_t,
set: Some(ngx_http_hello_ngx_commands_set_enable),
conf: NGX_RS_HTTP_LOC_CONF_OFFSET,
offset: 0,
post: std::ptr::null_mut(),
},
ngx_null_command!(),
];
#[no_mangle]
static ngx_http_hello_ngx_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),
};
ngx_modules!(ngx_http_hello_ngx_module);
// All the various configuration used to setup the NGINX Module
#[no_mangle]
pub static mut ngx_http_hello_ngx_module: ngx_module_t = ngx_module_t {
ctx_index: ngx_uint_t::max_value(),
index: ngx_uint_t::max_value(),
name: std::ptr::null_mut(),
spare0: 0,
spare1: 0,
version: nginx_version as ngx_uint_t,
signature: NGX_RS_MODULE_SIGNATURE.as_ptr() as *const c_char,
ctx: &ngx_http_hello_ngx_module_ctx as *const _ as *mut _,
commands: unsafe { &ngx_http_hello_ngx_commands[0] as *const _ as *mut _ },
type_: NGX_HTTP_MODULE as ngx_uint_t,
init_master: None,
init_module: None,
init_process: None,
init_thread: None,
exit_thread: None,
exit_process: None,
exit_master: None,
spare_hook0: 0,
spare_hook1: 0,
spare_hook2: 0,
spare_hook3: 0,
spare_hook4: 0,
spare_hook5: 0,
spare_hook6: 0,
spare_hook7: 0,
};
impl http::Merge for ModuleConfig {
fn merge(&mut self, prev: &ModuleConfig) -> Result<(), MergeConfigError> {
if prev.enable {
self.enable = true;
};
Ok(())
}
}
//This is the request handler where we handle the request before it goes upstream
//https://github.com/nginxinc/ngx-rust/blob/master/src/http/request.rs#L11-L23
http_request_handler!(hello_ngx_access_handler, |request: &mut http::Request| {
let co = unsafe {
request.get_module_loc_conf::<ModuleConfig>(&ngx_http_hello_ngx_module)
};
let co = co.expect("module config is none");
ngx_log_debug_http!(request, "hello world module enabled: {}", co.enable);
match co.enable {
true => {
if request.user_agent().as_bytes().starts_with(b"hello-ngx") {
ngx_log_debug_http!(request, "Hello Rusty NGINX");
core::Status::NGX_DONE
} else {
core::Status::NGX_DECLINED
}
}
false => core::Status::NGX_DECLINED,
}
});
#[no_mangle]
extern "C" fn ngx_http_hello_ngx_commands_set_enable(
cf: *mut ngx_conf_t,
_cmd: *mut ngx_command_t,
conf: *mut c_void,
) -> *mut c_char {
unsafe {
let conf = &mut *(conf as *mut ModuleConfig);
let args = (*(*cf).args).elts as *mut ngx_str_t;
let val = (*args.add(1)).to_str();
// set default value optionally
conf.enable = false;
if val.len() == 2 && val.eq_ignore_ascii_case("on") {
conf.enable = true;
} else if val.len() == 3 && val.eq_ignore_ascii_case("off") {
conf.enable = false;
}
};
std::ptr::null_mut()
}