Examples with subrequest
Opened this issue · 1 comments
azoyan commented
Is your feature request related to a problem? Please describe.
Show please example of module where we use subrequest with original body to custom own server, handle response and depend on response (403 or 200) return error or continue execution.
I don't know why i have Segfault on my module:
http_request_handler!(wafng_read_post_handler, |request: &mut http::Request| {
let module = unsafe { addr_of!(NGX_HTTP_WAFNG_MODULE).as_ref().unwrap() };
let config = request
.get_module_loc_conf::<ModuleConfig>(module)
.expect("module config is none");
if matches!(config.mode, Mode::Off) {
return Status::NGX_DECLINED;
}
let status = request.subrequest("/score", module, subrequest_callback);
}
unsafe extern "C" fn subrequest_callback(
r: *mut ngx_http_request_t,
v: *mut c_void,
i: ngx_int_t,
) -> ngx_int_t {
let req = unsafe { Request::from_ngx_http_request(r) };
for (header, value) in req.headers_in_iterator() {
eprint!("Header = {header}, value = {value}");
}
eprintln!("\nRECEIVED SUBREQUEST");
0
}
azoyan commented
Another words, is it possible to code behavior like this?
const THRESHOLD = 40;
const IS_PROXY_MODE = false;
function score(r) {
r.subrequest("/score" + r.uri, (response) => {
const wafReplyJson = JSON.parse(response.responseText);
if (wafReplyJson.description.length > 0) {
const weight = wafReplyJson.score.weighted;
if (IS_PROXY_MODE) {
r.headersOut['X-Waf-Score'] = weight;
}
else {
if (weight < THRESHOLD) {
return r.return(403, "Access Denied\n")
}
}
}
r.internalRedirect("/backend");
});
}
export default { score };
This is example of usage njs module.