11#include <ngx_http_redirectionio_module.h>
22
3- static ngx_chain_t * ngx_http_redirectionio_body_filter_replace (ngx_http_redirectionio_ctx_t * ctx , ngx_pool_t * pool , ngx_chain_t * cl );
3+ static ngx_chain_t * ngx_http_redirectionio_body_filter_replace (ngx_http_redirectionio_ctx_t * ctx , ngx_http_request_t * r , ngx_chain_t * cl );
44
55static ngx_int_t ngx_http_redirectionio_create_filter_body (ngx_http_request_t * r );
66
@@ -247,10 +247,14 @@ ngx_int_t ngx_http_redirectionio_body_filter(ngx_http_request_t *r, ngx_chain_t
247247 tsize = 0 ;
248248
249249 for (current_chain = input_chain ; current_chain ; current_chain = current_chain -> next ) {
250- tmp_chain = ngx_http_redirectionio_body_filter_replace (ctx , r -> pool , current_chain );
250+ tmp_chain = ngx_http_redirectionio_body_filter_replace (ctx , r , current_chain );
251251
252252 // Buffered case
253253 if (tmp_chain == NULL ) {
254+ // Update sent chain
255+ previous_chain = current_chain ;
256+ tsize += ngx_buf_size (current_chain -> buf );
257+
254258 continue ;
255259 }
256260
@@ -296,7 +300,7 @@ ngx_int_t ngx_http_redirectionio_body_filter(ngx_http_request_t *r, ngx_chain_t
296300 return rc ;
297301}
298302
299- static ngx_chain_t * ngx_http_redirectionio_body_filter_replace (ngx_http_redirectionio_ctx_t * ctx , ngx_pool_t * pool , ngx_chain_t * cl ) {
303+ static ngx_chain_t * ngx_http_redirectionio_body_filter_replace (ngx_http_redirectionio_ctx_t * ctx , ngx_http_request_t * r , ngx_chain_t * cl ) {
300304 ngx_chain_t * out = NULL , * el = NULL ;
301305 struct REDIRECTIONIO_Buffer buf_in , buf_out ;
302306 char * memory_buf ;
@@ -309,24 +313,24 @@ static ngx_chain_t* ngx_http_redirectionio_body_filter_replace(ngx_http_redirect
309313 buf_out = redirectionio_action_body_filter_filter (ctx -> body_filter , buf_in );
310314
311315 // Same output as input
312- if (buf_out .data == buf_in .data ) {
316+ if (buf_out .len > 0 && buf_out . data == buf_in .data ) {
313317 return cl ;
314318 }
315319
316320 if (buf_out .len > 0 ) {
317321 mbsize = buf_out .len ;
318- out = ngx_palloc (pool , sizeof (ngx_chain_t ));
322+ out = ngx_palloc (r -> pool , sizeof (ngx_chain_t ));
319323
320324 if (out == NULL ) {
321325 return cl ;
322326 }
323327
324- memory_buf = ngx_palloc (pool , mbsize );
328+ memory_buf = ngx_palloc (r -> pool , mbsize );
325329 ngx_memcpy (memory_buf , buf_out .data , mbsize );
326330 redirectionio_api_buffer_drop (buf_out );
327331
328332 out -> next = NULL ;
329- out -> buf = ngx_create_temp_buf (pool , mbsize );
333+ out -> buf = ngx_create_temp_buf (r -> pool , mbsize );
330334
331335 if (out -> buf == NULL ) {
332336 return cl ;
@@ -354,7 +358,7 @@ static ngx_chain_t* ngx_http_redirectionio_body_filter_replace(ngx_http_redirect
354358 }
355359
356360 mbsize = buf_out .len ;
357- el = ngx_palloc (pool , sizeof (ngx_chain_t ));
361+ el = ngx_palloc (r -> pool , sizeof (ngx_chain_t ));
358362
359363 if (el == NULL ) {
360364 if (out != NULL ) {
@@ -366,14 +370,14 @@ static ngx_chain_t* ngx_http_redirectionio_body_filter_replace(ngx_http_redirect
366370 return cl ;
367371 }
368372
369- memory_buf = ngx_palloc (pool , mbsize );
373+ memory_buf = ngx_palloc (r -> pool , mbsize );
370374 ngx_memcpy (memory_buf , buf_out .data , mbsize );
371375 redirectionio_api_buffer_drop (buf_out );
372376
373377 el -> next = NULL ;
374- el -> buf = ngx_create_temp_buf (pool , mbsize );
378+ el -> buf = ngx_create_temp_buf (r -> pool , mbsize );
375379
376- if (out -> buf == NULL ) {
380+ if (el -> buf == NULL ) {
377381 if (out != NULL ) {
378382 out -> buf -> last_buf = 1 ;
379383
@@ -384,7 +388,7 @@ static ngx_chain_t* ngx_http_redirectionio_body_filter_replace(ngx_http_redirect
384388 }
385389
386390 el -> buf -> pos = (u_char * )memory_buf ;
387- el -> buf -> last = out -> buf -> pos + mbsize ;
391+ el -> buf -> last = el -> buf -> pos + mbsize ;
388392 el -> buf -> last_buf = 1 ;
389393 el -> buf -> last_in_chain = 1 ;
390394 el -> buf -> tag = (ngx_buf_tag_t ) & ngx_http_redirectionio_module ;
0 commit comments