Skip to content

Body discarding with js_body_filter #992

@lonerr

Description

@lonerr

It seems that combination of proxy_cache and sendfile results in an empty first argument of js_body_ filter.

Below is a minimal configuration that reproduces the issue:

load_module modules/ngx_http_js_module.so;

events {
    worker_connections 1024;
}

http {
    js_import lib.js;

    proxy_cache_path /tmp/one levels=1 keys_zone=one:1m;

    server {
        listen 80;

        proxy_cache one;
        proxy_cache_valid any 1s;

        location /origin/ {
            return 200 ok\n;
        }
        location / {
            proxy_pass http://127.0.0.1/origin/;

            js_header_filter lib.headerFilter;
            js_body_filter   lib.bodyFilter buffer_type=buffer;
        }
        location /sf/ {
            proxy_pass http://127.0.0.1/origin/;
            sendfile on;

            js_header_filter lib.headerFilter;
            js_body_filter   lib.bodyFilter  buffer_type=buffer;
        }
    }
}

Where lib.js looks like:

var buffer = '';

function bodyFilter(r, data, flags) {
    buffer += data;

    if (flags.last)
        return r.sendBuffer(buffer, flags);
}

function headerFilter(r) {
    delete r.headersOut['Content-Length'];
}

export default { bodyFilter, headerFilter }

The results (second unexpected):

% curl -s http://127.0.0.1/ | wc -l
       1
% curl -s http://127.0.0.1/sf/ | wc -l
       0

Test was performed on Ubuntu 22.04 with packages from mainline repo at nginx.org

I hope these snippets are enough to catch the bug but don't hesitate to ask me about any additional info you need.
Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions