[PATCH 4 of 4] Handle empty response body

Maxim Dounin mdounin at mdounin.ru
Wed Mar 28 02:02:38 UTC 2012


Hello!

On Wed, Mar 21, 2012 at 09:28:34PM +0000, Laurence Rowe wrote:

> # HG changeset patch
> # User Laurence Rowe <laurence at lrowe.co.uk>
> # Date 1332363819 0
> # Node ID 1a21700079c90be39eaf5f6c5d7aef65be43ff95
> # Parent  65fd4892a78371e863d43e31d4430cdb7333a35d
> Handle empty response body
> 
> Responses, and especially proxied responses, may have empty bodies. These
> should pass through the filter without causing an internal server error or
> crashing nginx.
> 
> Example config:
> 
>     location = /testme {
>         default_type text/xml;
>         return 204;
>         xslt_stylesheet example.xsl;
>     }

This should return 500 as of now, due to malformed xml on input, 
and this is correct behaviour.  Do you see any other problems?

> diff --git a/src/http/modules/ngx_http_xslt_filter_module.c b/src/http/modules/ngx_http_xslt_filter_module.c
> --- a/src/http/modules/ngx_http_xslt_filter_module.c
> +++ b/src/http/modules/ngx_http_xslt_filter_module.c
> @@ -284,10 +284,17 @@
>  
>          if (cl->buf->last_buf || cl->buf->last_in_chain) {
>  
> +            if (ctx->ctxt == NULL) {

This might not happen before this patch due to 
ngx_http_xslt_add_chunk() call just before, which will either 
create ctx->ctxt or fail.

> +                /* empty body */
> +                return ngx_http_next_header_filter(r);

And both the code and comment is incorrect here: 

1. Calling header filter is not enough to provide empty body.

2. I think this should really produce 500, at least in case of 
normal xml parsing.

> +            }
> +
>              ctx->doc = ctx->ctxt->myDoc;
>  
>  #if (NGX_HTTP_XSLT_REUSE_DTD)
> -            ctx->doc->extSubset = NULL;
> +            if (ctx->doc) {
> +                ctx->doc->extSubset = NULL;
> +            }
>  #endif

This wasn't possible either, as ctxt->myDoc was always available 
on successfull parsing.  If it's not available now it some cases 
due to relaxed error handling - this have to be addressed either 
before or with the change which introduce the problem.

>  
>              wellFormed = ctx->ctxt->wellFormed;
> @@ -377,6 +384,10 @@
>  
>      if (ctx->ctxt == NULL) {
>  
> +        if (b->last == b->pos) {
> +            return NGX_OK;
> +        }
> +
>          if (ctx->html_parser) {
>              if (r->headers_out.charset.len) {
>                  enc = xmlParseCharEncoding(

Maxim Dounin



More information about the nginx-devel mailing list