Skip to content

Commit 87f3cf9

Browse files
committed
feature: added new config directive srcache_store_ranges for storing 206 Partial Content responses generated by the standard ngx_http_range_filter_module.
1 parent b156fd4 commit 87f3cf9

File tree

4 files changed

+447
-10
lines changed

4 files changed

+447
-10
lines changed

src/ngx_http_srcache_filter_module.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,13 @@ static ngx_command_t ngx_http_srcache_commands[] = {
171171
offsetof(ngx_http_srcache_loc_conf_t, pass_headers),
172172
NULL },
173173

174+
{ ngx_string("srcache_store_ranges"),
175+
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
176+
ngx_conf_set_flag_slot,
177+
NGX_HTTP_LOC_CONF_OFFSET,
178+
offsetof(ngx_http_srcache_loc_conf_t, store_ranges),
179+
NULL },
180+
174181
{ ngx_string("srcache_ignore_content_encoding"),
175182
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
176183
ngx_conf_set_flag_slot,
@@ -271,6 +278,7 @@ ngx_http_srcache_create_loc_conf(ngx_conf_t *cf)
271278
conf->store_private = NGX_CONF_UNSET;
272279
conf->store_no_store = NGX_CONF_UNSET;
273280
conf->store_no_cache = NGX_CONF_UNSET;
281+
conf->store_ranges = NGX_CONF_UNSET;
274282

275283
conf->max_expire = NGX_CONF_UNSET;
276284
conf->default_expire = NGX_CONF_UNSET;
@@ -326,6 +334,7 @@ ngx_http_srcache_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
326334
ngx_conf_merge_value(conf->store_private, prev->store_private, 0);
327335
ngx_conf_merge_value(conf->store_no_store, prev->store_no_store, 0);
328336
ngx_conf_merge_value(conf->store_no_cache, prev->store_no_cache, 0);
337+
ngx_conf_merge_value(conf->store_ranges, prev->store_ranges, 0);
329338

330339
ngx_conf_merge_value(conf->max_expire, prev->max_expire, 0);
331340
ngx_conf_merge_value(conf->default_expire, prev->default_expire, 60);

src/ngx_http_srcache_filter_module.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ typedef struct {
6969
ngx_flag_t store_private;
7070
ngx_flag_t store_no_store;
7171
ngx_flag_t store_no_cache;
72+
ngx_flag_t store_ranges;
7273

7374
ngx_flag_t ignore_content_encoding;
7475

src/ngx_http_srcache_store.c

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "ngx_http_srcache_store.h"
1414
#include "ngx_http_srcache_fetch.h"
1515
#include "ngx_http_srcache_util.h"
16+
#include <assert.h>
1617

1718

1819
static ngx_int_t ngx_http_srcache_store_post_subrequest(ngx_http_request_t *r,
@@ -259,7 +260,12 @@ ngx_http_srcache_header_filter(ngx_http_request_t *r)
259260
r->header_only = 1;
260261
}
261262

262-
return NGX_OK;
263+
/* store the response header to ctx->body_to_cache */
264+
if (ngx_http_srcache_store_response_header(r, ctx) == NGX_ERROR) {
265+
return NGX_ERROR;
266+
}
267+
268+
return ngx_http_srcache_next_header_filter(r);
263269
}
264270

265271

@@ -392,15 +398,27 @@ ngx_http_srcache_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
392398
if (ctx->store_response) {
393399
dd("storing the response: %p", in);
394400

395-
if (ctx->response_length == 0) {
396-
/* store the response header to ctx->body_to_cache */
397-
if (ngx_http_srcache_store_response_header(r, ctx) == NGX_ERROR) {
398-
return NGX_ERROR;
399-
}
401+
slcf = ngx_http_get_module_loc_conf(r, ngx_http_srcache_filter_module);
402+
403+
if (r->headers_out.status == NGX_HTTP_PARTIAL_CONTENT
404+
&& ctx->http_status == NGX_HTTP_OK)
405+
{
406+
u_char *p;
400407

401-
if (ngx_http_srcache_next_header_filter(r) == NGX_ERROR) {
402-
return NGX_ERROR;
408+
if (!slcf->store_ranges) {
409+
ctx->store_response = 0;
410+
goto done;
403411
}
412+
413+
dd("fix 206 status code");
414+
415+
/* handle 206 Partial Content generated by the range filter */
416+
cl = ctx->body_to_cache;
417+
assert(cl && cl->buf && cl->buf->last - cl->buf->pos > 12);
418+
p = cl->buf->pos + sizeof("HTTP/1.x 20") - 1;
419+
*p = '6';
420+
421+
ctx->http_status = NGX_HTTP_PARTIAL_CONTENT;
404422
}
405423

406424
for (cl = in; cl; cl = cl->next) {
@@ -411,8 +429,6 @@ ngx_http_srcache_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
411429
}
412430
}
413431

414-
slcf = ngx_http_get_module_loc_conf(r, ngx_http_srcache_filter_module);
415-
416432
if (slcf->store_max_size != 0
417433
&& ctx->response_length > slcf->store_max_size)
418434
{

0 commit comments

Comments
 (0)