Skip to content

Commit

Permalink
Code review notes and changes after rebasingx
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Yacobucci committed Sep 1, 2023
1 parent 55807ea commit 8081f84
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 24 deletions.
30 changes: 19 additions & 11 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This crate provides a couple of example using [ngx](https://crates.io/crates/ngx
- [awssig.rs](./awssig.rs) - An example of NGINX dynamic module that can sign GET request using AWS Signature v4.
- [curl](./curl.rs) - An example of the Access Phase NGINX dynamic module that blocks HTTP requests if `user-agent` header starts with `curl`.
- [httporigdst](./httporigdst.rs) - A dynamic module recovers the original IP address and port number of the destination packet.
- [upstream](./upstream.rs) - A dynamic module demonstrating the setup code to write an upstream filter or load balancer.

To build all these examples simply run:

Expand Down Expand Up @@ -168,20 +169,27 @@ This module was converted from https://github.com/gabihodoroaga/nginx-upstream-m
load_module "modules/upstream.so"
http {
upstream backend {
server localhost:8081;
upstream backend {
server localhost:15501;
custom 32;
}
server {
listen 15500;
server_name _;
custom 32;
}
location / {
proxy_pass http://backend;
}
}
server {
listen 8080;
server_name _;
server {
listen 15501;
location / {
proxy_pass http://backend;
location / {
return 418;
}
}
}
}
```

Expand Down Expand Up @@ -215,4 +223,4 @@ http {
nginx -t && nginx -s reload
```

7. Test with `curl`. Traffic should pass to your listener on port 8081 (this could be another NGINX server for example). With debug logging enabled you should notice the "custom" log messages (see the source code for log examples).
7. Test with `curl`. Traffic should pass to your listener on port 8081 (this could be another NGINX server for example). With debug logging enabled you should notice the upstream log messages (see the source code for log examples, prefixed with "CUSTOM UPSTREAM").
24 changes: 24 additions & 0 deletions examples/upstream.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# example configuration block to test upstream.rs
http {
upstream backend {
server localhost:15501;
custom 32;
}

server {
listen 15500;
server_name _;

location / {
proxy_pass http://backend;
}
}

server {
listen 15501;

location / {
return 418;
}
}
}
27 changes: 14 additions & 13 deletions examples/upstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ use ngx::{
ngx_http_upstream_init_peer_pt, ngx_http_upstream_init_pt, ngx_http_upstream_init_round_robin,
ngx_http_upstream_module, 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_UPS_CONF, NGX_LOG_DEBUG_HTTP, NGX_LOG_EMERG, NGX_RS_HTTP_SRV_CONF_OFFSET,
NGX_RS_MODULE_SIGNATURE,
NGX_HTTP_MODULE, NGX_HTTP_UPS_CONF, NGX_LOG_EMERG, NGX_RS_HTTP_SRV_CONF_OFFSET, NGX_RS_MODULE_SIGNATURE,
},
http::{
ngx_http_conf_get_module_srv_conf, ngx_http_conf_upstream_srv_conf_immutable,
ngx_http_conf_upstream_srv_conf_mutable, HTTPModule, Merge, MergeConfigError, Request,
},
http_upstream_peer_init, ngx_log_debug_http, ngx_log_debug_mask, ngx_modules, ngx_null_command, ngx_string,
http_upstream_init_peer_pt,
log::DebugMask,
ngx_log_debug_http, ngx_log_debug_mask, ngx_modules, ngx_null_command, ngx_string,
};
use std::{
mem,
Expand Down Expand Up @@ -140,7 +141,7 @@ pub static mut ngx_http_upstream_custom_module: ngx_module_t = ngx_module_t {
// http_upstream_init_custom_peer
// The module's custom peer.init callback. On HTTP request the peer upstream get and free callbacks
// are saved into peer data and replaced with this module's custom callbacks.
http_upstream_peer_init!(
http_upstream_init_peer_pt!(
http_upstream_init_custom_peer,
|request: &mut Request, us: *mut ngx_http_upstream_srv_conf_t| {
ngx_log_debug_http!(request, "CUSTOM UPSTREAM request peer init");
Expand Down Expand Up @@ -194,7 +195,7 @@ unsafe extern "C" fn ngx_http_upstream_get_custom_peer(pc: *mut ngx_peer_connect
let hcpd: *mut UpstreamPeerData = unsafe { mem::transmute(data) };

ngx_log_debug_mask!(
NGX_LOG_DEBUG_HTTP,
DebugMask::Http,
(*pc).log,
"CUSTOM UPSTREAM get peer, try: {}, conn: {:p}",
(*pc).tries,
Expand All @@ -208,7 +209,7 @@ unsafe extern "C" fn ngx_http_upstream_get_custom_peer(pc: *mut ngx_peer_connect
return rc;
}

ngx_log_debug_mask!(NGX_LOG_DEBUG_HTTP, (*pc).log, "CUSTOM UPSTREAM end get peer");
ngx_log_debug_mask!(DebugMask::Http, (*pc).log, "CUSTOM UPSTREAM end get peer");
Status::NGX_OK.into()
}

Expand All @@ -221,15 +222,15 @@ unsafe extern "C" fn ngx_http_upstream_free_custom_peer(
data: *mut c_void,
state: ngx_uint_t,
) {
ngx_log_debug_mask!(NGX_LOG_DEBUG_HTTP, (*pc).log, "CUSTOM UPSTREAM free peer");
ngx_log_debug_mask!(DebugMask::Http, (*pc).log, "CUSTOM UPSTREAM free peer");

let hcpd: *mut UpstreamPeerData = unsafe { mem::transmute(data) };

let original_free_peer = (*hcpd).original_free_peer.unwrap();

original_free_peer(pc, (*hcpd).data, state);

ngx_log_debug_mask!(NGX_LOG_DEBUG_HTTP, (*pc).log, "CUSTOM UPSTREAM end free peer");
ngx_log_debug_mask!(DebugMask::Http, (*pc).log, "CUSTOM UPSTREAM end free peer");
}

// ngx_http_upstream_init_custom
Expand All @@ -240,7 +241,7 @@ 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!(NGX_LOG_DEBUG_HTTP, (*cf).log, "CUSTOM UPSTREAM peer init_upstream");
ngx_log_debug_mask!(DebugMask::Http, (*cf).log, "CUSTOM UPSTREAM peer init_upstream");

let maybe_conf: Option<*mut SrvConfig> =
ngx_http_conf_upstream_srv_conf_mutable(us, &ngx_http_upstream_custom_module);
Expand Down Expand Up @@ -273,7 +274,7 @@ unsafe extern "C" fn ngx_http_upstream_init_custom(
(*hccf).original_init_peer = (*us).peer.init;
(*us).peer.init = Some(http_upstream_init_custom_peer);

ngx_log_debug_mask!(NGX_LOG_DEBUG_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 @@ -286,7 +287,7 @@ unsafe extern "C" fn ngx_http_upstream_commands_set_custom(
cmd: *mut ngx_command_t,
conf: *mut c_void,
) -> *mut c_char {
ngx_log_debug_mask!(NGX_LOG_DEBUG_HTTP, (*cf).log, "CUSTOM UPSTREAM module init");
ngx_log_debug_mask!(DebugMask::Http, (*cf).log, "CUSTOM UPSTREAM module init");

let mut ccf = &mut (*(conf as *mut SrvConfig));

Expand Down Expand Up @@ -318,7 +319,7 @@ unsafe extern "C" fn ngx_http_upstream_commands_set_custom(

(*uscf).peer.init_upstream = Some(ngx_http_upstream_init_custom);

ngx_log_debug_mask!(NGX_LOG_DEBUG_HTTP, (*cf).log, "CUSTOM UPSTREAM end module init");
ngx_log_debug_mask!(DebugMask::Http, (*cf).log, "CUSTOM UPSTREAM end module init");
// NGX_CONF_OK
std::ptr::null_mut()
}
Expand Down Expand Up @@ -350,7 +351,7 @@ impl HTTPModule for Module {

(*conf).max = NGX_CONF_UNSET as u32;

ngx_log_debug_mask!(NGX_LOG_DEBUG_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
}
}

0 comments on commit 8081f84

Please sign in to comment.