Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples with subrequest #136

Open
azoyan opened this issue Feb 27, 2025 · 1 comment
Open

Examples with subrequest #136

azoyan opened this issue Feb 27, 2025 · 1 comment

Comments

@azoyan
Copy link

azoyan commented Feb 27, 2025

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
Copy link
Author

azoyan commented Mar 3, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant