[patch] Range filter: move ranges array to ngx_http_headers_out_t.

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


# HG changeset patch
# User hucongcong <hucong.c at foxmail.com>
# Date 1509041509 -28800
#      Fri Oct 27 02:11:49 2017 +0800
# Node ID f7b01b22c785c8ee7976efed0c6c7cb71c6a511d
# Parent  798418b3f9c21811debf6cbeaea3eb554c886905
Range filter: move ranges array to ngx_http_headers_out_t.

diff -r 798418b3f9c2 -r f7b01b22c785 src/http/modules/ngx_http_range_filter_module.c
--- a/src/http/modules/ngx_http_range_filter_module.c	Fri Oct 27 01:21:46 2017 +0800
+++ b/src/http/modules/ngx_http_range_filter_module.c	Fri Oct 27 02:11:49 2017 +0800
@@ -49,17 +49,10 @@
 
 
 typedef struct {
-    off_t        start;
-    off_t        end;
-    ngx_str_t    content_range;
-} ngx_http_range_t;
-
+    off_t       offset;
+    ngx_uint_t  index;  /* start with 1 */
 
-typedef struct {
-    off_t        offset;
-    ngx_str_t    boundary_header;
-    ngx_uint_t   index;            /* start with 1 */
-    ngx_array_t  ranges;
+    ngx_str_t   boundary_header;
 } ngx_http_range_filter_ctx_t;
 
 
@@ -240,7 +233,7 @@ parse:
         r->headers_out.status = NGX_HTTP_PARTIAL_CONTENT;
         r->headers_out.status_line.len = 0;
 
-        if (ctx->ranges.nelts == 1) {
+        if (r->headers_out.ranges->nelts == 1) {
             return ngx_http_range_singlepart_header(r, ctx);
         }
 
@@ -286,15 +279,15 @@ ngx_http_range_parse(ngx_http_request_t 
         mctx = ngx_http_get_module_ctx(r->main,
                                        ngx_http_range_body_filter_module);
         if (mctx) {
+            r->headers_out.ranges = r->main->headers_out.ranges;
             ctx->boundary_header = mctx->boundary_header;
-            ctx->ranges = mctx->ranges;
             return NGX_OK;
         }
     }
 
-    if (ngx_array_init(&ctx->ranges, r->pool, 1, sizeof(ngx_http_range_t))
-        != NGX_OK)
-    {
+    r->headers_out.ranges = ngx_array_create(r->pool, 1,
+                                             sizeof(ngx_http_range_t));
+    if (r->headers_out.ranges == NULL) {
         return NGX_ERROR;
     }
 
@@ -391,7 +384,7 @@ ngx_http_range_parse(ngx_http_request_t 
 
             } else {
 
-                if (ctx->ranges.nelts) {
+                if (r->headers_out.ranges->nelts) {
 
                     if (start < range->start) {
                         ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
@@ -414,11 +407,11 @@ ngx_http_range_parse(ngx_http_request_t 
                     if (start < range->end + NGX_HTTP_RANGE_MULTIPART_GAP) {
                         size -= range->end - range->start;
                         start = range->start;
-                        ctx->ranges.nelts--;
+                        r->headers_out.ranges->nelts--;
                     }
                 }
 
-                range = ngx_array_push(&ctx->ranges);
+                range = ngx_array_push(r->headers_out.ranges);
                 if (range == NULL) {
                     return NGX_ERROR;
                 }
@@ -447,31 +440,31 @@ ngx_http_range_parse(ngx_http_request_t 
         end = content_length;
 
         i = 0;
-        range = ctx->ranges.elts;
+        range = r->headers_out.ranges->elts;
 
-        while (ctx->ranges.nelts) {
+        while (r->headers_out.ranges->nelts) {
 
-            i = ctx->ranges.nelts - 1;
+            i = r->headers_out.ranges->nelts - 1;
             if (start >= range[i].start) {
                 break;
             }
 
             size -= range[i].end - range[i].start;
-            ctx->ranges.nelts--;
+            r->headers_out.ranges->nelts--;
         }
 
-        if (ctx->ranges.nelts
+        if (r->headers_out.ranges->nelts
             && start < range[i].end + NGX_HTTP_RANGE_MULTIPART_GAP)
         {
             size -= range[i].end - range[i].start;
             start = range[i].start;
-            ctx->ranges.nelts--;
+            r->headers_out.ranges->nelts--;
             i--;
         }
 
-        if (ctx->ranges.nelts == 0 || end > range[i].end) {
+        if (r->headers_out.ranges->nelts == 0 || end > range[i].end) {
 
-            range = ngx_array_push(&ctx->ranges);
+            range = ngx_array_push(r->headers_out.ranges);
             if (range == NULL) {
                 return NGX_ERROR;
             }
@@ -487,7 +480,7 @@ ngx_http_range_parse(ngx_http_request_t 
         }
     }
 
-    if (ctx->ranges.nelts == 0) {
+    if (r->headers_out.ranges->nelts == 0) {
         return NGX_HTTP_RANGE_NOT_SATISFIABLE;
     }
 
@@ -530,7 +523,7 @@ ngx_http_range_singlepart_header(ngx_htt
 
     /* "Content-Range: bytes SSSS-EEEE/TTTT" header */
 
-    range = ctx->ranges.elts;
+    range = r->headers_out.ranges->elts;
 
     content_range->value.len = ngx_sprintf(content_range->value.data,
                                            "bytes %O-%O/%O",
@@ -646,8 +639,8 @@ ngx_http_range_multipart_header(ngx_http
 
     len = sizeof(CRLF "--") - 1 + NGX_ATOMIC_T_LEN + sizeof("--" CRLF) - 1;
 
-    range = ctx->ranges.elts;
-    for (i = 0; i < ctx->ranges.nelts; i++) {
+    range = r->headers_out.ranges->elts;
+    for (i = 0; i < r->headers_out.ranges->nelts; i++) {
 
         /* the size of the range: "SSSS-EEEE/TTTT" CRLF CRLF */
 
@@ -731,7 +724,7 @@ ngx_http_range_body_filter(ngx_http_requ
         return ngx_http_next_body_filter(r, in);
     }
 
-    if (ctx->ranges.nelts == 1) {
+    if (r->headers_out.ranges->nelts == 1) {
         return ngx_http_range_singlepart_body(r, ctx, in);
     }
 
@@ -754,7 +747,7 @@ ngx_http_range_singlepart_body(ngx_http_
 
     out = NULL;
     ll = &out;
-    range = ctx->ranges.elts;
+    range = r->headers_out.ranges->elts;
 
     for (cl = in; cl; cl = cl->next) {
 
@@ -840,10 +833,10 @@ ngx_http_range_multipart_body(ngx_http_r
     ngx_chain_t       *out, *cl, *ncl, **ll;
     ngx_http_range_t  *range, *tail;
 
-    range = ctx->ranges.elts;
+    range = r->headers_out.ranges->elts;
 
     if (!ctx->index) {
-        for (i = 0; i < ctx->ranges.nelts; i++) {
+        for (i = 0; i < r->headers_out.ranges->nelts; i++) {
             if (ctx->offset < range[i].end) {
                 ctx->index = i + 1;
                 break;
@@ -851,7 +844,7 @@ ngx_http_range_multipart_body(ngx_http_r
         }
     }
 
-    tail = range + ctx->ranges.nelts - 1;
+    tail = range + r->headers_out.ranges->nelts - 1;
     range += ctx->index - 1;
 
     out = NULL;
@@ -1026,7 +1019,7 @@ ngx_http_range_link_boundary_header(ngx_
         return NGX_ERROR;
     }
 
-    range = ctx->ranges.elts;
+    range = r->headers_out.ranges->elts;
     b->temporary = 1;
     b->pos = range[ctx->index - 1].content_range.data;
     b->last = range[ctx->index - 1].content_range.data
diff -r 798418b3f9c2 -r f7b01b22c785 src/http/ngx_http_request.h
--- a/src/http/ngx_http_request.h	Fri Oct 27 01:21:46 2017 +0800
+++ b/src/http/ngx_http_request.h	Fri Oct 27 02:11:49 2017 +0800
@@ -251,6 +251,13 @@ typedef struct {
 
 
 typedef struct {
+    off_t                             start;
+    off_t                             end;
+    ngx_str_t                         content_range;
+} ngx_http_range_t;
+
+
+typedef struct {
     ngx_list_t                        headers;
     ngx_list_t                        trailers;
 
@@ -278,6 +285,7 @@ typedef struct {
     u_char                           *content_type_lowcase;
     ngx_uint_t                        content_type_hash;
 
+    ngx_array_t                      *ranges;        /* ngx_http_range_t */
     ngx_array_t                       cache_control;
 
     off_t                             content_length_n;


More information about the nginx-devel mailing list