]> git.kaiwu.me - njs.git/commitdiff
Fixed js_body_filter when data is not in memory.
authorDmitry Volyntsev <xeioex@nginx.com>
Tue, 2 Dec 2025 01:03:25 +0000 (17:03 -0800)
committerDmitry Volyntsev <xeioexception@gmail.com>
Tue, 2 Dec 2025 22:06:59 +0000 (14:06 -0800)
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.

nginx/ngx_http_js_module.c
nginx/t/js_body_filter_file.t [new file with mode: 0644]

index 8f31b80d7b6f977dbc438255d6a8965d7e26c4b7..8b38dbfde95d7b0eef079e13a9ad548f043af9fe 100644 (file)
@@ -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 (file)
index 0000000..2188f7c
--- /dev/null
@@ -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', <<EOF);
+    var buffer = '';
+
+    function filter(r, data, flags) {
+        buffer += data;
+
+        if (flags.last) {
+            return r.sendBuffer(buffer, flags);
+        }
+    }
+
+    function clear_content_length(r) {
+        delete r.headersOut['Content-Length'];
+    }
+
+    export default {filter, clear_content_length};
+
+EOF
+
+$t->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');
+
+###############################################################################