Slice filter: support multiple ranges.

胡聪 (hucc) hucong.c at foxmail.com
Thu Oct 26 18:28:17 UTC 2017


# HG changeset patch
# User hucongcong <hucong.c at foxmail.com>
# Date 1509041837 -28800
#      Fri Oct 27 02:17:17 2017 +0800
# Node ID 2366fd2d2aa2820ab846fb51bba0e399fbb7ecc0
# Parent  f7b01b22c785c8ee7976efed0c6c7cb71c6a511d
Slice filter: support multiple ranges.

diff -r f7b01b22c785 -r 2366fd2d2aa2 src/http/modules/ngx_http_slice_filter_module.c
--- a/src/http/modules/ngx_http_slice_filter_module.c	Fri Oct 27 02:11:49 2017 +0800
+++ b/src/http/modules/ngx_http_slice_filter_module.c	Fri Oct 27 02:17:17 2017 +0800
@@ -22,6 +22,8 @@ typedef struct {
     ngx_str_t            etag;
     unsigned             last:1;
     unsigned             active:1;
+    unsigned             multipart:1;
+    ngx_uint_t           index;
     ngx_http_request_t  *sr;
 } ngx_http_slice_ctx_t;
 
@@ -103,7 +105,9 @@ ngx_http_slice_header_filter(ngx_http_re
 {
     off_t                            end;
     ngx_int_t                        rc;
+    ngx_uint_t                       i;
     ngx_table_elt_t                 *h;
+    ngx_http_range_t                *range;
     ngx_http_slice_ctx_t            *ctx;
     ngx_http_slice_loc_conf_t       *slcf;
     ngx_http_slice_content_range_t   cr;
@@ -182,27 +186,48 @@ ngx_http_slice_header_filter(ngx_http_re
 
     r->allow_ranges = 1;
     r->subrequest_ranges = 1;
-    r->single_range = 1;
 
     rc = ngx_http_next_header_filter(r);
 
-    if (r != r->main) {
-        return rc;
+    if (r == r->main) {
+        r->preserve_body = 1;
+
+        if (r->headers_out.status == NGX_HTTP_PARTIAL_CONTENT) {
+            ctx->multipart = (r->headers_out.ranges->nelts != 1);
+            range = r->headers_out.ranges->elts;
+
+            if (ctx->start + (off_t) slcf->size <= range[0].start) {
+                ctx->start = slcf->size * (range[0].start / slcf->size);
+            }
+
+            ctx->end = range[r->headers_out.ranges->nelts - 1].end;
+
+        } else {
+            ctx->end = cr.complete_length;
+        }
     }
 
-    r->preserve_body = 1;
+    if (ctx->multipart) {
+        end = ctx->start + (off_t) slcf->size - 1;
+
+        range = r->headers_out.ranges->elts;
+        for (i = ctx->index; i < r->headers_out.ranges->nelts - 1; i++) {
+
+            if (end < range[i].end) {
+                break;
+            }
 
-    if (r->headers_out.status == NGX_HTTP_PARTIAL_CONTENT) {
-        if (ctx->start + (off_t) slcf->size <= r->headers_out.content_offset) {
-            ctx->start = slcf->size
-                         * (r->headers_out.content_offset / slcf->size);
+            if (end < range[i + 1].start && ctx->start >= range[i].end) {
+                i++;
+                ctx->index = i;
+                ctx->start = slcf->size * (range[i].start / slcf->size);
+
+                ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                               "range multipart so fast forward to %O-%O @%O",
+                               range[i].start, range[i].end, ctx->start);
+                break;
+            }
         }
-
-        ctx->end = r->headers_out.content_offset
-                   + r->headers_out.content_length_n;
-
-    } else {
-        ctx->end = cr.complete_length;
     }
 
     return rc;


More information about the nginx-devel mailing list