[patch] Slice filter: support for empty file.

Maxim Dounin mdounin at mdounin.ru
Tue Jun 20 15:01:17 UTC 2017


Hello!

On Tue, Jun 20, 2017 at 10:39:44AM +0800, 胡聪 (hucc) wrote:

> Hi,
> 
> >416 will be returned when the request has no Range HEADER and the target file is
> >empty.  Apparently, it does not conform to the HTTP protocol. Empty file seems
> >inevitable in the CDN service where Nginx is heavily used.
> >
> ># HG changeset patch
> ># User hucongcong <hucong.c at foxmail.com>
> ># Date 1497892764 -28800
> >#      Tue Jun 20 01:19:24 2017 +0800
> ># Node ID 79d38b2d27d4eb92395cf1ff43bbfe23498bc69a
> ># Parent  d1816a2696de8c2faa1cd913a151e5f62a8620f3
> >Slice filter: support for empty file.
> 
> The following modifications may be more appropriate.
> 
> # HG changeset patch
> # User hucongcong <hucong.c at foxmail.com>
> # Date 1497926137 -28800
> #      Tue Jun 20 10:35:37 2017 +0800
> # Node ID e42abb7e28c4f0dd3d66cb81aa2623e7fae8b4da
> # Parent  a39bc74873faf9e5bea616561b43f6ecc55229f9
> Slice filter: support for empty file.
> 
> diff -r a39bc74873fa -r e42abb7e28c4 src/http/modules/ngx_http_slice_filter_module.c
> --- a/src/http/modules/ngx_http_slice_filter_module.c   Mon Jun 19 14:25:42 2017 +0300
> +++ b/src/http/modules/ngx_http_slice_filter_module.c   Tue Jun 20 10:35:37 2017 +0800
> @@ -114,7 +114,21 @@ ngx_http_slice_header_filter(ngx_http_re
>      }
> 
>      if (r->headers_out.status != NGX_HTTP_PARTIAL_CONTENT) {
> +
>          if (r == r->main) {
> +            if (r->headers_in.range == NULL
> +                && r->headers_out.status == NGX_HTTP_RANGE_NOT_SATISFIABLE)
> +            {
> +                r->header_only = 1;
> +                ngx_str_null(&r->headers_out.content_type);
> +
> +                r->headers_out.status = NGX_HTTP_OK;
> +                r->headers_out.status_line.len = 0;
> +                r->headers_out.content_length_n = 0;
> +                r->headers_out.content_range->hash = 0;
> +                r->headers_out.content_range = NULL;
> +            }
> +
>              ngx_http_set_ctx(r, NULL, ngx_http_slice_filter_module);
>              return ngx_http_next_header_filter(r);
>          }

Returning the 416 response with some headers deleted and status 
code changed to 200 doesn't look like a safe solution.

On the other hand, changing nginx's range filter to allow range 
requests to 0-byte files should make this unneeded.  I've reopened 
ticket #1031 which is about range requests to 0-byte files, see 
https://trac.nginx.org/nginx/ticket/1031.

-- 
Maxim Dounin
http://nginx.org/


More information about the nginx-devel mailing list