@@ -1156,6 +1156,8 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
1156
1156
ngx_http_modsecurity_ctx_t * ctx ;
1157
1157
ngx_int_t rc ;
1158
1158
apr_off_t content_length ;
1159
+ ngx_chain_t * cl , * out ;
1160
+ ngx_int_t last_buf = 0 ;
1159
1161
1160
1162
cf = ngx_http_get_module_loc_conf (r , ngx_http_modsecurity );
1161
1163
ctx = ngx_http_get_module_ctx (r , ngx_http_modsecurity );
@@ -1166,29 +1168,53 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
1166
1168
1167
1169
ngx_log_debug0 (NGX_LOG_DEBUG_HTTP , r -> connection -> log , 0 , "modSecurity: body filter" );
1168
1170
1169
- if (in == NULL ) {
1170
- /* waiting for more buffer */
1171
- r -> buffered |= NGX_HTTP_SSI_BUFFERED ;
1172
- return NGX_AGAIN ;
1171
+ for (cl = in ; cl ; cl = cl -> next ) {
1172
+ apr_bucket * e ;
1173
+ ngx_buf_t * buf = cl -> buf ;
1174
+ apr_bucket_brigade * bb = ctx -> brigade ;
1175
+ off_t size = ngx_buf_size (buf );
1176
+ if (size ) {
1177
+ char * data = apr_pmemdup (bb -> p , buf -> pos , size );
1178
+ if (data == NULL ) {
1179
+ return ngx_http_filter_finalize_request (r ,
1180
+ & ngx_http_modsecurity , NGX_HTTP_INTERNAL_SERVER_ERROR );
1181
+ }
1182
+ e = apr_bucket_pool_create (data , size , bb -> p , bb -> bucket_alloc );
1183
+ if (e == NULL ) {
1184
+ return ngx_http_filter_finalize_request (r ,
1185
+ & ngx_http_modsecurity , NGX_HTTP_INTERNAL_SERVER_ERROR );
1186
+ }
1187
+ APR_BRIGADE_INSERT_TAIL (bb , e );
1188
+ }
1189
+
1190
+ if (buf -> last_buf ) {
1191
+ last_buf = 1 ;
1192
+ buf -> last_buf = 0 ;
1193
+ e = apr_bucket_eos_create (bb -> bucket_alloc );
1194
+ if (e == NULL ) {
1195
+ return ngx_http_filter_finalize_request (r ,
1196
+ & ngx_http_modsecurity , NGX_HTTP_INTERNAL_SERVER_ERROR );
1197
+ }
1198
+ APR_BRIGADE_INSERT_TAIL (bb , e );
1199
+ break ;
1200
+ }
1201
+
1202
+ buf -> pos = buf -> last ;
1173
1203
}
1174
1204
1175
- rc = move_chain_to_brigade (in , ctx -> brigade , r -> pool , 0 );
1176
- if (rc != NGX_OK ) {
1177
- /* waiting for more buffer */
1178
- r -> buffered |= NGX_HTTP_SSI_BUFFERED ;
1179
- return rc ;
1205
+ if (!last_buf ) {
1206
+ return NGX_AGAIN ;
1180
1207
}
1181
1208
1182
1209
/* last buf has been saved */
1183
- r -> buffered &= ~NGX_HTTP_SSI_BUFFERED ;
1184
-
1185
1210
ctx -> complete = 1 ;
1186
1211
modsecSetResponseBrigade (ctx -> req , ctx -> brigade );
1187
1212
1188
1213
if (ngx_http_modsecurity_load_headers_in (r ) != NGX_OK
1189
1214
|| ngx_http_modsecurity_load_headers_out (r ) != NGX_OK ) {
1190
1215
1191
- return NGX_HTTP_INTERNAL_SERVER_ERROR ;
1216
+ return ngx_http_filter_finalize_request (r ,
1217
+ & ngx_http_modsecurity , NGX_HTTP_INTERNAL_SERVER_ERROR );
1192
1218
}
1193
1219
1194
1220
rc = ngx_http_modsecurity_status (r , modsecProcessResponse (ctx -> req ));
@@ -1199,15 +1225,17 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
1199
1225
1200
1226
apr_brigade_length (ctx -> brigade , 0 , & content_length );
1201
1227
1202
- rc = move_brigade_to_chain (ctx -> brigade , & in , r -> pool );
1228
+ rc = move_brigade_to_chain (ctx -> brigade , & out , r -> pool );
1203
1229
if (rc == NGX_ERROR ) {
1204
- return NGX_ERROR ;
1230
+ return ngx_http_filter_finalize_request (r ,
1231
+ & ngx_http_modsecurity , NGX_HTTP_INTERNAL_SERVER_ERROR );
1205
1232
}
1206
1233
1207
1234
if (ngx_http_modsecurity_save_headers_in (r ) != NGX_OK
1208
1235
|| ngx_http_modsecurity_save_headers_out (r ) != NGX_OK ) {
1209
1236
1210
- return ngx_http_filter_finalize_request (r , & ngx_http_modsecurity , NGX_HTTP_INTERNAL_SERVER_ERROR );
1237
+ return ngx_http_filter_finalize_request (r ,
1238
+ & ngx_http_modsecurity , NGX_HTTP_INTERNAL_SERVER_ERROR );
1211
1239
}
1212
1240
1213
1241
if (r -> headers_out .content_length_n != -1 ) {
@@ -1223,7 +1251,7 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
1223
1251
return ngx_http_filter_finalize_request (r , & ngx_http_modsecurity , rc );
1224
1252
}
1225
1253
1226
- return ngx_http_next_body_filter (r , in );
1254
+ return ngx_http_next_body_filter (r , out );
1227
1255
}
1228
1256
1229
1257
0 commit comments