Re: [PATCH] Slice filter: proxy_cache_background_update support (ticket #1348)

2024-06-12 Thread J Carter
On Tue, 11 Jun 2024 02:46:13 +0100
J Carter  wrote:

> Style cleanup of previous patch. 
> 
> Also removed 'status already
> returned' guard in slice range variable handler, as it is no longer
> needed given other changes in patch.
>

Additional fixes.

# HG changeset patch
# User J Carter 
# Date 1718225771 -3600
#  Wed Jun 12 21:56:11 2024 +0100
# Node ID 8edd891af4d6474ea139490e3662241212926244
# Parent  02e9411009b987f408214ab4a8b6b6093f843bcd
Slice filter: proxy_cache_background_update support (ticket #1348).

Previously, subrequests of a slice subrequest would have an empty
$slice_range variable value. This prevented
proxy_cache_background_update and friends from successfully
fetching and populating correctly.

This occurred for two reasons:
- Firstly, a single context was reused for all slice subrequests,
where each $slice_range value was overwritten by subsequent slice
subrequests.
- Secondly, subrequests not initiated by slice filter were unable to
access $slice_range in a parent subrequest.

Each slice subrequests now retains $slice_range and subrequests of
slice subrequests now utilize the parent slice subrequest's
$slice_range if available.

diff --git a/src/http/modules/ngx_http_slice_filter_module.c 
b/src/http/modules/ngx_http_slice_filter_module.c
--- a/src/http/modules/ngx_http_slice_filter_module.c
+++ b/src/http/modules/ngx_http_slice_filter_module.c
@@ -18,11 +18,16 @@ typedef struct {
 typedef struct {
 off_tstart;
 off_tend;
-ngx_str_trange;
 ngx_str_tetag;
 unsigned last:1;
 unsigned active:1;
 ngx_http_request_t  *sr;
+} ngx_http_slice_shctx_t;
+
+
+typedef struct {
+ngx_str_t range;
+ngx_http_slice_shctx_t *sh;
 } ngx_http_slice_ctx_t;
 
 
@@ -105,6 +110,7 @@ ngx_http_slice_header_filter(ngx_http_re
 ngx_int_trc;
 ngx_table_elt_t *h;
 ngx_http_slice_ctx_t*ctx;
+ngx_http_slice_shctx_t  *sh;
 ngx_http_slice_loc_conf_t   *slcf;
 ngx_http_slice_content_range_t   cr;
 
@@ -113,6 +119,8 @@ ngx_http_slice_header_filter(ngx_http_re
 return ngx_http_next_header_filter(r);
 }
 
+sh = ctx->sh;
+
 if (r->headers_out.status != NGX_HTTP_PARTIAL_CONTENT) {
 if (r == r->main) {
 ngx_http_set_ctx(r, NULL, ngx_http_slice_filter_module);
@@ -127,10 +135,10 @@ ngx_http_slice_header_filter(ngx_http_re
 
 h = r->headers_out.etag;
 
-if (ctx->etag.len) {
+if (sh->etag.len) {
 if (h == NULL
-|| h->value.len != ctx->etag.len
-|| ngx_strncmp(h->value.data, ctx->etag.data, ctx->etag.len)
+|| h->value.len != sh->etag.len
+|| ngx_strncmp(h->value.data, sh->etag.data, sh->etag.len)
!= 0)
 {
 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
@@ -140,7 +148,7 @@ ngx_http_slice_header_filter(ngx_http_re
 }
 
 if (h) {
-ctx->etag = h->value;
+sh->etag = h->value;
 }
 
 if (ngx_http_slice_parse_content_range(r, ) != NGX_OK) {
@@ -163,15 +171,15 @@ ngx_http_slice_header_filter(ngx_http_re
 
 end = ngx_min(cr.start + (off_t) slcf->size, cr.complete_length);
 
-if (cr.start != ctx->start || cr.end != end) {
+if (cr.start != sh->start || cr.end != end) {
 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
   "unexpected range in slice response: %O-%O",
   cr.start, cr.end);
 return NGX_ERROR;
 }
 
-ctx->start = end;
-ctx->active = 1;
+sh->start = end;
+sh->active = 1;
 
 r->headers_out.status = NGX_HTTP_OK;
 r->headers_out.status_line.len = 0;
@@ -198,16 +206,16 @@ ngx_http_slice_header_filter(ngx_http_re
 r->preserve_body = 1;
 
 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 (sh->start + (off_t) slcf->size <= r->headers_out.content_offset) {
+sh->start = slcf->size
+* (r->headers_out.content_offset / slcf->size);
 }
 
-ctx->end = r->headers_out.content_offset
-   + r->headers_out.content_length_n;
+sh->end = r->headers_out.content_offset
+  + r->headers_out.content_length_n;
 
 } else {
-ctx->end = cr.complete_length;
+sh->end = cr.complete_length;
 }
 
 return rc;
@@ -217,9 +225,11 @@ ngx_http_slice_header_filter(ngx_http_re
 static ngx_int_t
 ngx_http_slice_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
 {
+u_char *p;
 ngx_int_t   rc;
 ngx_chain_t*cl;
-ngx_http_slice_ctx_t   *ctx;
+ngx_http_slice_ctx_t   *ctx, *sr_ctx;
+

Re: [PATCH] Slice filter: proxy_cache_background_update support (ticket #1348)

2024-06-10 Thread J Carter
Style cleanup of previous patch. 

Also removed 'status already
returned' guard in slice range variable handler, as it is no longer
needed given other changes in patch.

# HG changeset patch
# User J Carter 
# Date 1718069043 -3600
#  Tue Jun 11 02:24:03 2024 +0100
# Node ID bc3e20f3f1f6f86da2ad44bb5b4742bced210b97
# Parent  02e9411009b987f408214ab4a8b6b6093f843bcd
Slice filter: proxy_cache_background_update support (ticket #1348).

Previously, subrequests of a slice subrequest would have an empty
$slice_range variable value. This prevented
proxy_cache_background_update and friends from successfully
fetching and populating correctly.

This occurred for two reasons:
- Firstly, a single context was reused for all slice subrequests,
where each $slice_range value was overwritten by subsequent slice
subrequests.
- Secondly, subrequests not initiated by slice filter were unable to
access $slice_range in a parent subrequest.

Each slice subrequests now retains $slice_range and subrequests of
slice subrequests now utilize the parent slice subrequest's
$slice_range if available.

diff --git a/src/http/modules/ngx_http_slice_filter_module.c 
b/src/http/modules/ngx_http_slice_filter_module.c
--- a/src/http/modules/ngx_http_slice_filter_module.c
+++ b/src/http/modules/ngx_http_slice_filter_module.c
@@ -18,11 +18,16 @@ typedef struct {
 typedef struct {
 off_tstart;
 off_tend;
-ngx_str_trange;
 ngx_str_tetag;
 unsigned last:1;
 unsigned active:1;
 ngx_http_request_t  *sr;
+} ngx_http_slice_shctx_t;
+
+
+typedef struct {
+ngx_str_t range;
+ngx_http_slice_shctx_t *sh;
 } ngx_http_slice_ctx_t;
 
 
@@ -105,6 +110,7 @@ ngx_http_slice_header_filter(ngx_http_re
 ngx_int_trc;
 ngx_table_elt_t *h;
 ngx_http_slice_ctx_t*ctx;
+ngx_http_slice_shctx_t  *sh;
 ngx_http_slice_loc_conf_t   *slcf;
 ngx_http_slice_content_range_t   cr;
 
@@ -113,6 +119,8 @@ ngx_http_slice_header_filter(ngx_http_re
 return ngx_http_next_header_filter(r);
 }
 
+sh = ctx->sh;
+
 if (r->headers_out.status != NGX_HTTP_PARTIAL_CONTENT) {
 if (r == r->main) {
 ngx_http_set_ctx(r, NULL, ngx_http_slice_filter_module);
@@ -127,10 +135,10 @@ ngx_http_slice_header_filter(ngx_http_re
 
 h = r->headers_out.etag;
 
-if (ctx->etag.len) {
+if (sh->etag.len) {
 if (h == NULL
-|| h->value.len != ctx->etag.len
-|| ngx_strncmp(h->value.data, ctx->etag.data, ctx->etag.len)
+|| h->value.len != sh->etag.len
+|| ngx_strncmp(h->value.data, sh->etag.data, sh->etag.len)
!= 0)
 {
 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
@@ -140,7 +148,7 @@ ngx_http_slice_header_filter(ngx_http_re
 }
 
 if (h) {
-ctx->etag = h->value;
+sh->etag = h->value;
 }
 
 if (ngx_http_slice_parse_content_range(r, ) != NGX_OK) {
@@ -163,15 +171,15 @@ ngx_http_slice_header_filter(ngx_http_re
 
 end = ngx_min(cr.start + (off_t) slcf->size, cr.complete_length);
 
-if (cr.start != ctx->start || cr.end != end) {
+if (cr.start != sh->start || cr.end != end) {
 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
   "unexpected range in slice response: %O-%O",
   cr.start, cr.end);
 return NGX_ERROR;
 }
 
-ctx->start = end;
-ctx->active = 1;
+sh->start = end;
+sh->active = 1;
 
 r->headers_out.status = NGX_HTTP_OK;
 r->headers_out.status_line.len = 0;
@@ -198,16 +206,16 @@ ngx_http_slice_header_filter(ngx_http_re
 r->preserve_body = 1;
 
 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 (sh->start + (off_t) slcf->size <= r->headers_out.content_offset) {
+sh->start = slcf->size
+* (r->headers_out.content_offset / slcf->size);
 }
 
-ctx->end = r->headers_out.content_offset
-   + r->headers_out.content_length_n;
+sh->end = r->headers_out.content_offset
+  + r->headers_out.content_length_n;
 
 } else {
-ctx->end = cr.complete_length;
+sh->end = cr.complete_length;
 }
 
 return rc;
@@ -217,9 +225,11 @@ ngx_http_slice_header_filter(ngx_http_re
 static ngx_int_t
 ngx_http_slice_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
 {
+u_char *p;
 ngx_int_t   rc;
 ngx_chain_t*cl;
-ngx_http_slice_ctx_t   *ctx;
+ngx_http_slice_ctx_t   *ctx, *sr_ctx;
+ngx_http_slice_shctx_t *sh;
 ngx_http_slice_loc_conf_t  *slcf;
 
 ctx = 

[PATCH] Slice filter: proxy_cache_background_update support (ticket #1348)

2024-06-08 Thread J Carter
# HG changeset patch
# User J Carter 
# Date 1717886685 -3600
#  Sat Jun 08 23:44:45 2024 +0100
# Node ID 1b8a60f7640be4a900ac77d8022b7d8cc6944186
# Parent  02e9411009b987f408214ab4a8b6b6093f843bcd
Slice filter: proxy_cache_background_update support (ticket #1348).

Previously, subrequests of a slice subrequest would have an empty
$slice_range variable value. This prevented
proxy_cache_background_update and friends from successfully
fetching and populating correctly.

This occurred for two reasons:
- Firstly, a single context was reused for all slice subrequests,
where each $slice_range value was overwritten by subsequent slice
subrequests.
- Secondly, subrequests not initiated by slice filter were unable to
access $slice_range in a parent subrequest.

Each slice subrequests now retains $slice_range and subrequests of
slice subrequests now utilize the parent slice subrequest's
$slice_range if available.

diff --git a/src/http/modules/ngx_http_slice_filter_module.c 
b/src/http/modules/ngx_http_slice_filter_module.c
--- a/src/http/modules/ngx_http_slice_filter_module.c
+++ b/src/http/modules/ngx_http_slice_filter_module.c
@@ -18,11 +18,16 @@ typedef struct {
 typedef struct {
 off_tstart;
 off_tend;
-ngx_str_trange;
 ngx_str_tetag;
 unsigned last:1;
 unsigned active:1;
 ngx_http_request_t  *sr;
+} ngx_http_slice_shctx_t;
+
+
+typedef struct {
+ngx_str_t range;
+ngx_http_slice_shctx_t *sh;
 } ngx_http_slice_ctx_t;
 
 
@@ -105,6 +110,7 @@ ngx_http_slice_header_filter(ngx_http_re
 ngx_int_trc;
 ngx_table_elt_t *h;
 ngx_http_slice_ctx_t*ctx;
+ngx_http_slice_shctx_t  *sh;
 ngx_http_slice_loc_conf_t   *slcf;
 ngx_http_slice_content_range_t   cr;
 
@@ -113,6 +119,8 @@ ngx_http_slice_header_filter(ngx_http_re
 return ngx_http_next_header_filter(r);
 }
 
+sh = ctx->sh;
+
 if (r->headers_out.status != NGX_HTTP_PARTIAL_CONTENT) {
 if (r == r->main) {
 ngx_http_set_ctx(r, NULL, ngx_http_slice_filter_module);
@@ -127,10 +135,10 @@ ngx_http_slice_header_filter(ngx_http_re
 
 h = r->headers_out.etag;
 
-if (ctx->etag.len) {
+if (sh->etag.len) {
 if (h == NULL
-|| h->value.len != ctx->etag.len
-|| ngx_strncmp(h->value.data, ctx->etag.data, ctx->etag.len)
+|| h->value.len != sh->etag.len
+|| ngx_strncmp(h->value.data, sh->etag.data, sh->etag.len)
!= 0)
 {
 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
@@ -140,7 +148,7 @@ ngx_http_slice_header_filter(ngx_http_re
 }
 
 if (h) {
-ctx->etag = h->value;
+sh->etag = h->value;
 }
 
 if (ngx_http_slice_parse_content_range(r, ) != NGX_OK) {
@@ -163,15 +171,15 @@ ngx_http_slice_header_filter(ngx_http_re
 
 end = ngx_min(cr.start + (off_t) slcf->size, cr.complete_length);
 
-if (cr.start != ctx->start || cr.end != end) {
+if (cr.start != sh->start || cr.end != end) {
 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
   "unexpected range in slice response: %O-%O",
   cr.start, cr.end);
 return NGX_ERROR;
 }
 
-ctx->start = end;
-ctx->active = 1;
+sh->start = end;
+sh->active = 1;
 
 r->headers_out.status = NGX_HTTP_OK;
 r->headers_out.status_line.len = 0;
@@ -198,16 +206,16 @@ ngx_http_slice_header_filter(ngx_http_re
 r->preserve_body = 1;
 
 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
+if (sh->start + (off_t) slcf->size <= r->headers_out.content_offset) {
+sh->start = slcf->size
  * (r->headers_out.content_offset / slcf->size);
 }
 
-ctx->end = r->headers_out.content_offset
+sh->end = r->headers_out.content_offset
+ r->headers_out.content_length_n;
 
 } else {
-ctx->end = cr.complete_length;
+sh->end = cr.complete_length;
 }
 
 return rc;
@@ -217,9 +225,11 @@ ngx_http_slice_header_filter(ngx_http_re
 static ngx_int_t
 ngx_http_slice_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
 {
+u_char *p;
 ngx_int_t   rc;
 ngx_chain_t*cl;
-ngx_http_slice_ctx_t   *ctx;
+ngx_http_slice_ctx_t   *ctx, *sr_ctx;
+ngx_http_slice_shctx_t *sh;
 ngx_http_slice_loc_conf_t  *slcf;
 
 ctx = ngx_http_get_module_ctx(r, ngx_http_slice_filter_module);
@@ -228,32 +238,34 @@ ngx_http_slice_body_filter(ngx_http_requ
 return ngx_http_next_body_filter(r, in);
 }
 
+sh = ctx->sh;
+
 for (cl = in; cl; cl = cl->next) {
 if (cl->buf->last_buf) {