Skip to content

HTTP: clearing out Content-length if js_body_filter is set. #897

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions nginx/ngx_http_js_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -1317,6 +1317,16 @@ 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 (r == r->main) {
ngx_http_clear_content_length(r);
ngx_http_clear_last_modified(r);
ngx_http_clear_etag(r);
}
}

if (jlcf->header_filter.len == 0) {
return ngx_http_next_header_filter(r);
}
Expand Down
35 changes: 25 additions & 10 deletions nginx/t/js_body_filter.t
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,23 @@ http {
js_content test.njs;
}

location /pass {
proxy_pass http://127.0.0.1:8081/len_source;
}

location /append {
js_body_filter test.append;
proxy_pass http://127.0.0.1:8081/source;
proxy_pass http://127.0.0.1:8081/chunked_source;
}

location /append_len {
js_body_filter test.append;
proxy_pass http://127.0.0.1:8081/len_source;
}

location /buffer_type {
js_body_filter test.buffer_type buffer_type=buffer;
proxy_pass http://127.0.0.1:8081/source;
proxy_pass http://127.0.0.1:8081/chunked_source;
}

location /buffer_type_nonutf8 {
Expand All @@ -62,28 +71,32 @@ http {

location /forward {
js_body_filter test.forward buffer_type=string;
proxy_pass http://127.0.0.1:8081/source;
proxy_pass http://127.0.0.1:8081/chunked_source;
}

location /filter {
proxy_buffering off;
js_body_filter test.filter;
proxy_pass http://127.0.0.1:8081/source;
proxy_pass http://127.0.0.1:8081/chunked_source;
}

location /prepend {
js_body_filter test.prepend;
proxy_pass http://127.0.0.1:8081/source;
proxy_pass http://127.0.0.1:8081/chunked_source;
}
}

server {
listen 127.0.0.1:8081;
server_name localhost;

location /source {
location /len_source {
return 200 '0123456789';
}

location /chunked_source {
postpone_output 1;
js_content test.source;
js_content test.chunked_source;
}

location /nonutf8_source {
Expand Down Expand Up @@ -127,7 +140,7 @@ $t->write_file('test.js', <<EOF);
}
}

function source(r) {
function chunked_source(r) {
var chunks = ['AAA', 'BB', 'C', 'DDDD'];
chunks.delay = 5;
chunks.r = r;
Expand Down Expand Up @@ -170,15 +183,17 @@ $t->write_file('test.js', <<EOF);
}

export default {njs: test_njs, append, buffer_type, filter, forward,
prepend, source, nonutf8_source};
prepend, chunked_source, nonutf8_source};

EOF

$t->try_run('no njs body filter')->plan(7);
$t->try_run('no njs body filter')->plan(9);

###############################################################################

like(http_get('/append'), qr/AAABBCDDDDXXX/, 'append');
like(http_get('/pass'), qr/Content-Length: 10/, 'pass');
unlike(http_get('/append_len'), qr/Content-Length/, 'append len');
like(http_get('/buffer_type'), qr/AAABBCDDDD/, 'buffer type');
like(http_get('/buffer_type_nonutf8'), qr/\xaa\xaa\xbb\xcc\xdd\xdd/,
'buffer type nonutf8');
Expand Down