[patch] Slice filter: support for empty file.

胡聪 (hucc) hucong.c at foxmail.com
Mon Jun 19 18:25:45 UTC 2017


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.

diff -r d1816a2696de -r 79d38b2d27d4 src/http/modules/ngx_http_slice_filter_module.c
--- a/src/http/modules/ngx_http_slice_filter_module.c   Fri Jun 16 18:15:58 2017 +0300
+++ b/src/http/modules/ngx_http_slice_filter_module.c   Tue Jun 20 01:19:24 2017 +0800
@@ -22,6 +22,7 @@ typedef struct {
     ngx_str_t            etag;
     unsigned             last:1;
     unsigned             active:1;
+    unsigned             no_range:1;
     ngx_http_request_t  *sr;
 } ngx_http_slice_ctx_t;

@@ -114,7 +115,21 @@ ngx_http_slice_header_filter(ngx_http_re
     }

     if (r->headers_out.status != NGX_HTTP_PARTIAL_CONTENT) {
+
         if (r == r->main) {
+            if (ctx->no_range
+                && 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);
         }
@@ -440,9 +455,10 @@ ngx_http_slice_range_variable(ngx_http_r
 static off_t
 ngx_http_slice_get_start(ngx_http_request_t *r)
 {
-    off_t             start, cutoff, cutlim;
-    u_char           *p;
-    ngx_table_elt_t  *h;
+    off_t                       start, cutoff, cutlim;
+    u_char                     *p;
+    ngx_table_elt_t            *h;
+    ngx_http_slice_ctx_t       *ctx;

     if (r->headers_in.if_range) {
         return 0;
@@ -454,6 +470,8 @@ ngx_http_slice_get_start(ngx_http_reques
         || h->value.len < 7
         || ngx_strncasecmp(h->value.data, (u_char *) "bytes=", 6) != 0)
     {
+        ctx = ngx_http_get_module_ctx(r, ngx_http_slice_filter_module);
+        ctx->no_range = 1;
         return 0;
     }


More information about the nginx-devel mailing list