Skip to content

Commit 6b230aa

Browse files
committed
HTTP: fixed buffer_type inheritance in if blocks.
Previously, when js_body_filter was used inside an if block that evaluated to true, the data parameter received Buffer type instead of the expected String type. This happened because buffer_type field in ngx_http_js_loc_conf_t was not properly initialized, causing the configuration merge to fail when nginx created a new location context for if blocks. This fixes #999 issue on Github.
1 parent 81e4a3e commit 6b230aa

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

nginx/ngx_http_js_module.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8260,6 +8260,8 @@ ngx_http_js_create_loc_conf(ngx_conf_t *cf)
82608260
conf->ssl_verify = NGX_CONF_UNSET;
82618261
conf->ssl_verify_depth = NGX_CONF_UNSET;
82628262
#endif
8263+
conf->buffer_type = NGX_CONF_UNSET_UINT;
8264+
82638265
return conf;
82648266
}
82658267

nginx/t/js_body_filter_if.t

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,24 @@ http {
5858
5959
proxy_pass http://127.0.0.1:8081/source;
6060
}
61+
62+
location /type_check_true {
63+
if ($arg_check) {
64+
set $dummy 1;
65+
}
66+
67+
js_body_filter test.type_check;
68+
proxy_pass http://127.0.0.1:8081/backend;
69+
}
70+
71+
location /type_check_false {
72+
if ($arg_nonexistent = dummy) {
73+
set $dummy 1;
74+
}
75+
76+
js_body_filter test.type_check;
77+
proxy_pass http://127.0.0.1:8081/backend;
78+
}
6179
}
6280
6381
server {
@@ -68,6 +86,10 @@ http {
6886
postpone_output 1;
6987
js_content test.source;
7088
}
89+
90+
location /backend {
91+
return 200 'payload';
92+
}
7193
}
7294
}
7395
@@ -113,15 +135,27 @@ $t->write_file('test.js', <<EOF);
113135
r.done();
114136
}
115137
116-
export default {njs: test_njs, append, prepend, source};
138+
function type_check(r, data, flags) {
139+
if (flags.last) {
140+
return r.sendBuffer(data, flags);
141+
}
142+
143+
r.sendBuffer(data.constructor.name + ": " + data, flags);
144+
}
145+
146+
export default {njs: test_njs, append, prepend, source, type_check};
117147
118148
EOF
119149

120-
$t->try_run('no njs body filter')->plan(2);
150+
$t->try_run('no njs body filter')->plan(4);
121151

122152
###############################################################################
123153

124154
like(http_get('/filter?name=append'), qr/AAABBCDDDDXXX/, 'append');
125155
like(http_get('/filter?name=prepend'), qr/XXXAAABBCDDDD/, 'prepend');
156+
like(http_get('/type_check_true?check=1'), qr/String: payload/,
157+
'type check with if block true');
158+
like(http_get('/type_check_false'), qr/String: payload/,
159+
'type check with if block false');
126160

127161
###############################################################################

0 commit comments

Comments
 (0)