From: Dmitry Volyntsev Date: Tue, 2 Dec 2025 01:03:25 +0000 (-0800) Subject: Fixed js_body_filter when data is not in memory. X-Git-Tag: 0.9.5~10 X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=81e4a3eaa4b9d8a88a46646da9a76e8309f956e7;p=njs.git Fixed js_body_filter when data is not in memory. Previously, when upstream data was delivered from nginx cache js_body_filter was not able to process it correctly. In particular, it was treated as a chain of empty buffers. The fix is to set r->filter_need_in_memory flag, which ensures that ngx_http_core_module reads the data into memory before js_body_filter sees it. This fixes #992 issue on Github. --- diff --git a/nginx/ngx_http_js_module.c b/nginx/ngx_http_js_module.c index 8f31b80d..8b38dbfd 100644 --- a/nginx/ngx_http_js_module.c +++ b/nginx/ngx_http_js_module.c @@ -1370,6 +1370,10 @@ ngx_http_js_header_filter(ngx_http_request_t *r) jlcf = ngx_http_get_module_loc_conf(r, ngx_http_js_module); + if (jlcf->body_filter.len != 0) { + r->filter_need_in_memory = 1; + } + if (jlcf->header_filter.len == 0) { return ngx_http_next_header_filter(r); } diff --git a/nginx/t/js_body_filter_file.t b/nginx/t/js_body_filter_file.t new file mode 100644 index 00000000..2188f7cb --- /dev/null +++ b/nginx/t/js_body_filter_file.t @@ -0,0 +1,99 @@ +#!/usr/bin/perl + +# (C) Dmitry Volyntsev +# (C) Nginx, Inc. + +# Tests for http njs module, body filter, when data is develivered from a file. + +############################################################################### + +use warnings; +use strict; + +use Test::More; + +BEGIN { use FindBin; chdir($FindBin::Bin); } + +use lib 'lib'; +use Test::Nginx; + +############################################################################### + +select STDERR; $| = 1; +select STDOUT; $| = 1; + +my $t = Test::Nginx->new()->has(qw/http proxy/) + ->write_file_expand('nginx.conf', <<'EOF'); + +%%TEST_GLOBALS%% + +daemon off; + +events { +} + +http { + %%TEST_GLOBALS_HTTP%% + + js_import test.js; + + proxy_cache_path /tmp/one levels=1 keys_zone=one:1m; + + server { + listen 127.0.0.1:8080; + server_name localhost; + + proxy_cache one; + proxy_cache_valid any 1s; + + location /origin/ { + return 200 ORIGIN; + } + + location /normal/ { + proxy_pass http://127.0.0.1:8080/origin/; + + js_header_filter test.clear_content_length; + js_body_filter test.filter; + } + + location /sendfile/ { + sendfile on; + proxy_pass http://127.0.0.1:8080/origin/; + + js_header_filter test.clear_content_length; + js_body_filter test.filter; + } + } +} + +EOF + +$t->write_file('test.js', <try_run('no njs body filter')->plan(2); + +############################################################################### + +like(http_get('/normal/'), qr/ORIGIN$/, 'normal proxy with njs body filter'); +like(http_get('/sendfile/'), qr/ORIGIN$/, + 'sendfile proxy with njs body filter'); + +###############################################################################