diff src/http/modules/ngx_http_range_filter_module.c @ 391:1d9bef53cd8e

Range filter: late_ranges functionality. Add one more filtering point after postpone filter. This allows to serve range capable replies with subrequests. It's not as efficient as range filtering for static data (i.e. doesn't save us from reading data from disk if some filter needs them in memory), but it may save some network bandwidth for us and for our users.
author Maxim Dounin <mdounin@mdounin.ru>
date Mon, 21 Jul 2008 05:33:01 +0400
parents a5f67d82aea3
children 77df96611112
line wrap: on
line diff
--- a/src/http/modules/ngx_http_range_filter_module.c
+++ b/src/http/modules/ngx_http_range_filter_module.c
@@ -60,6 +60,7 @@ typedef struct {
 
 static ngx_int_t ngx_http_range_header_filter_init(ngx_conf_t *cf);
 static ngx_int_t ngx_http_range_body_filter_init(ngx_conf_t *cf);
+static ngx_int_t ngx_http_range_late_filter_init(ngx_conf_t *cf);
 
 
 static ngx_http_module_t  ngx_http_range_header_filter_module_ctx = {
@@ -124,8 +125,40 @@ ngx_module_t  ngx_http_range_body_filter
 };
 
 
+static ngx_http_module_t  ngx_http_range_late_filter_module_ctx = {
+    NULL,                                  /* preconfiguration */
+    ngx_http_range_late_filter_init,       /* postconfiguration */
+
+    NULL,                                  /* create main configuration */
+    NULL,                                  /* init main configuration */
+
+    NULL,                                  /* create server configuration */
+    NULL,                                  /* merge server configuration */
+
+    NULL,                                  /* create location configuration */
+    NULL,                                  /* merge location configuration */
+};
+
+
+ngx_module_t  ngx_http_range_late_filter_module = {
+    NGX_MODULE_V1,
+    &ngx_http_range_late_filter_module_ctx, /* module context */
+    NULL,                                  /* module directives */
+    NGX_HTTP_MODULE,                       /* module type */
+    NULL,                                  /* init master */
+    NULL,                                  /* init module */
+    NULL,                                  /* init process */
+    NULL,                                  /* init thread */
+    NULL,                                  /* exit thread */
+    NULL,                                  /* exit process */
+    NULL,                                  /* exit master */
+    NGX_MODULE_V1_PADDING
+};
+
+
 static ngx_http_output_header_filter_pt  ngx_http_next_header_filter;
-static ngx_http_output_body_filter_pt    ngx_http_next_body_filter;
+static ngx_http_output_body_filter_pt    ngx_http_next_body_early_filter;
+static ngx_http_output_body_filter_pt    ngx_http_next_body_late_filter;
 
 
 static ngx_int_t
@@ -484,7 +517,8 @@ next_filter:
 
 
 static ngx_int_t
-ngx_http_range_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
+ngx_http_range_body_generic_filter(ngx_http_request_t *r, ngx_chain_t *in,
+    ngx_http_output_body_filter_pt ngx_http_next_body_filter)
 {
     off_t                         start, last;
     ngx_buf_t                    *b, *buf;
@@ -741,6 +775,30 @@ overlapped:
 
 
 static ngx_int_t
+ngx_http_range_body_early_filter(ngx_http_request_t *r, ngx_chain_t *in)
+{
+    if (!r->allow_ranges || r->late_ranges) {
+        return ngx_http_next_body_early_filter(r, in);
+    }
+
+    return ngx_http_range_body_generic_filter(r, in,
+                                              ngx_http_next_body_early_filter);
+}
+
+
+static ngx_int_t
+ngx_http_range_body_late_filter(ngx_http_request_t *r, ngx_chain_t *in)
+{
+    if (!r->allow_ranges || !r->late_ranges) {
+        return ngx_http_next_body_late_filter(r, in);
+    }
+
+    return ngx_http_range_body_generic_filter(r, in,
+                                              ngx_http_next_body_late_filter);
+}
+
+
+static ngx_int_t
 ngx_http_range_header_filter_init(ngx_conf_t *cf)
 {
     ngx_http_next_header_filter = ngx_http_top_header_filter;
@@ -753,8 +811,18 @@ ngx_http_range_header_filter_init(ngx_co
 static ngx_int_t
 ngx_http_range_body_filter_init(ngx_conf_t *cf)
 {
-    ngx_http_next_body_filter = ngx_http_top_body_filter;
-    ngx_http_top_body_filter = ngx_http_range_body_filter;
+    ngx_http_next_body_early_filter = ngx_http_top_body_filter;
+    ngx_http_top_body_filter = ngx_http_range_body_early_filter;
 
     return NGX_OK;
 }
+
+
+static ngx_int_t
+ngx_http_range_late_filter_init(ngx_conf_t *cf)
+{
+    ngx_http_next_body_late_filter = ngx_http_top_body_filter;
+    ngx_http_top_body_filter = ngx_http_range_body_late_filter;
+
+    return NGX_OK;
+}