Skip to content

Commit 4ae3f5b

Browse files
author
Matthew Yacobucci
committed
feat: Supporting changes for upstream example module.
- functions to get mut and const module context from the ngx_http_upstream_srv_conf_t - Request from trait - Request upstream getter - Request peer init macro - Debug logger with mask (NGX_LOG_DEBUG_EVENT, NGX_LOG_DEBUG_HTTP, etc) argument
1 parent 4aa2e35 commit 4ae3f5b

File tree

7 files changed

+94
-2
lines changed

7 files changed

+94
-2
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ members = [
66

77
[package]
88
name = "ngx"
9-
version = "0.3.0-beta"
9+
version = "0.4.0-beta"
1010
edition = "2021"
1111
autoexamples = false
1212
categories = ["api-bindings", "network-programming"]

src/http/conf.rs

+30
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,33 @@ pub unsafe fn ngx_http_conf_get_module_loc_conf(
3131
let http_conf_ctx = (*cf).ctx as *mut ngx_http_conf_ctx_t;
3232
*(*http_conf_ctx).loc_conf.add(module.ctx_index) as *mut ngx_http_core_loc_conf_t
3333
}
34+
35+
/// # Safety
36+
///
37+
/// The caller has provided a value `ngx_http_upstream_srv_conf_t. If the `us` argument is null, a
38+
/// None Option is returned; however, if the `us` internal fields are invalid or the module index
39+
/// is out of bounds failures may still occur.
40+
pub unsafe fn ngx_http_conf_upstream_srv_conf_immutable<T>(
41+
us: *const ngx_http_upstream_srv_conf_t,
42+
module: &ngx_module_t,
43+
) -> Option<*const T> {
44+
if us.is_null() {
45+
return None;
46+
}
47+
Some(*(*us).srv_conf.add(module.ctx_index) as *const T)
48+
}
49+
50+
/// # Safety
51+
///
52+
/// The caller has provided a value `ngx_http_upstream_srv_conf_t. If the `us` argument is null, a
53+
/// None Option is returned; however, if the `us` internal fields are invalid or the module index
54+
/// is out of bounds failures may still occur.
55+
pub unsafe fn ngx_http_conf_upstream_srv_conf_mutable<T>(
56+
us: *const ngx_http_upstream_srv_conf_t,
57+
module: &ngx_module_t,
58+
) -> Option<*mut T> {
59+
if us.is_null() {
60+
return None;
61+
}
62+
Some(*(*us).srv_conf.add(module.ctx_index) as *mut T)
63+
}

src/http/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ mod conf;
22
mod module;
33
mod request;
44
mod status;
5+
mod upstream;
56

67
pub use conf::*;
78
pub use module::*;
89
pub use request::*;
910
pub use status::*;
11+
pub use upstream::*;

src/http/request.rs

+22
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,17 @@ macro_rules! http_variable_get {
7979
#[repr(transparent)]
8080
pub struct Request(ngx_http_request_t);
8181

82+
impl<'a> From<&'a Request> for *const ngx_http_request_t {
83+
fn from(request: &'a Request) -> Self {
84+
&request.0 as *const _
85+
}
86+
}
87+
impl<'a> From<&'a mut Request> for *mut ngx_http_request_t {
88+
fn from(request: &'a mut Request) -> Self {
89+
&request.0 as *const _ as *mut _
90+
}
91+
}
92+
8293
impl Request {
8394
/// Create a [`Request`] from an [`ngx_http_request_t`].
8495
///
@@ -104,6 +115,17 @@ impl Request {
104115
unsafe { Pool::from_ngx_pool(self.0.pool) }
105116
}
106117

118+
/// Returns the result as an `Option` if it exists, otherwise `None`.
119+
///
120+
/// The option wraps a pointer to a [`ngx_http_upstream_t`] upstream server object.
121+
///
122+
/// [`ngx_http_upstream_t`]: is best described in
123+
/// https://nginx.org/en/docs/dev/development_guide.html#http_request
124+
/// https://nginx.org/en/docs/dev/development_guide.html#http_load_balancing
125+
pub fn upstream(&self) -> Option<*mut ngx_http_upstream_t> {
126+
Some(self.0.upstream)
127+
}
128+
107129
/// Pointer to a [`ngx_connection_t`] client connection object.
108130
///
109131
/// [`ngx_connection_t`]: https://nginx.org/en/docs/dev/development_guide.html#connection

src/http/upstream.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// Define a static upstream peer initializer
2+
///
3+
/// Initializes the upstream 'get', 'free', and 'session' callbacks and gives the module writer an
4+
/// opportunity to set custom data.
5+
/// Load Balancing: <https://nginx.org/en/docs/dev/development_guide.html#http_load_balancing>
6+
#[macro_export]
7+
macro_rules! http_upstream_peer_init {
8+
( $name: ident, $handler: expr ) => {
9+
#[no_mangle]
10+
extern "C" fn $name(r: *mut ngx_http_request_t, us: *mut ngx_http_upstream_srv_conf_t) -> ngx_int_t {
11+
let status: Status = $handler(unsafe { &mut Request::from_ngx_http_request(r) }, us);
12+
status.0
13+
}
14+
};
15+
}

src/log.rs

+23
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,26 @@ macro_rules! ngx_log_debug_http {
2727
$crate::ngx_log_debug!(log, $($arg)*);
2828
}
2929
}
30+
31+
/// Log with appropriate debug mask.
32+
///
33+
/// When the request logger is available `ngx_log_debug_http` can be used for `NGX_LOG_DEBUG_HTTP` masks.
34+
/// This macro is useful when other masks are necessary or when the request logger is not
35+
/// conveniently accessible.
36+
///
37+
/// See https://nginx.org/en/docs/dev/development_guide.html#logging for details and available
38+
/// masks.
39+
#[macro_export]
40+
macro_rules! ngx_log_debug_mask {
41+
( $mask:expr, $log:expr, $($arg:tt)* ) => {
42+
let log_level = unsafe { (*$log).log_level };
43+
if log_level & $mask as usize != 0 {
44+
let level = $mask as $crate::ffi::ngx_uint_t;
45+
let fmt = ::std::ffi::CString::new("%s").unwrap();
46+
let c_message = ::std::ffi::CString::new(format!($($arg)*)).unwrap();
47+
unsafe {
48+
$crate::ffi::ngx_log_error_core(level, $log, 0, fmt.as_ptr(), c_message.as_ptr());
49+
}
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)