10000 Core: added "not_modified_check" directive. · nginx/nginx@7dbb2a1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7dbb2a1

Browse files
committed
Core: added "not_modified_check" directive.
This directive establishes four distinct modes for managing 304 responses, which determine how the If-None-Match and If-Modified-Since headers are evaluated. When the directive parameter is set to off, the response is always treated as modified, regardless of the headers. When set to any, a 304 response is returned if either the If-Modified-Since or If-None-Match condition is satisfied. When set to strict, a 304 response is returned only if both the If-Modified-Since and If-None-Match conditions are met. This is the default setting and reflects the traditional behavior of NGINX. When set to prefer_if_none_match, it will follow RFC 9110, Section 13.1.3. In this mode, if the If-None-Match header is present, the If-Modified-Since header is ignored.
1 parent adda704 commit 7dbb2a1

File tree

3 files changed

+75
-8
lines changed

3 files changed

+75
-8
lines changed

src/http/modules/ngx_http_not_modified_filter_module.c

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
5454
static ngx_int_t
5555
ngx_http_not_modified_header_filter(ngx_http_request_t *r)
5656
{
57+
ngx_http_core_loc_conf_t *clcf;
58+
5759
if (r->headers_out.status != NGX_HTTP_OK
5860
|| r != r->main
5961
|| r->disable_not_modified)
@@ -77,19 +79,58 @@ ngx_http_not_modified_header_filter(ngx_http_request_t *r)
7779

7880
if ( 10000 r->headers_in.if_modified_since || r->headers_in.if_none_match) {
7981

80-
if (r->headers_in.if_modified_since
81-
&& ngx_http_test_if_modified(r))
82-
{
82+
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
83+
84+
if (clcf->not_modified_check == NGX_HTTP_NMC_STRICT) {
85+
if (r->headers_in.if_modified_since
86+
&& ngx_http_test_if_modified(r))
87+
{
88+
return ngx_http_next_header_filter(r);
89+
}
90+
91+
if (r->headers_in.if_none_match
92+
&& !ngx_http_test_if_match(r, r->headers_in.if_none_match, 1))
93+
{
94+
return ngx_http_next_header_filter(r);
95+
}
96+
97+
} else if (clcf->not_modified_check == NGX_HTTP_NMC_PREFER_INM) {
98+
if (r->headers_in.if_none_match) {
99+
if (ngx_http_test_if_match(r, r->headers_in.if_none_match, 1))
100+
{
101+
goto not_modified;
102+
}
103+
104+
return ngx_http_next_header_filter(r);
105+
}
106+
107+
if (r->headers_in.if_modified_since
108+
&& ngx_http_test_if_modified(r))
109+
{
110+
return ngx_http_next_header_filter(r);
111+
}
112+
113+
} else if (clcf->not_modified_check == NGX_HTTP_NMC_ANY) {
114+
115+
if (r->headers_in.if_modified_since
116+
&& !ngx_http_test_if_modified(r))
117+
{
118+
goto not_modified;
119+
}
120+
121+
if (r->headers_in.if_none_match
122+
&& ngx_http_test_if_match(r, r->headers_in.if_none_match, 1))
123+
{
124+
goto not_modified;
125+
}
126+
83127
return ngx_http_next_header_filter(r);
84-
}
85128

86-
if (r->headers_in.if_none_match
87-
&& !ngx_http_test_if_match(r, r->headers_in.if_none_match, 1))
88-
{
129+
} else { /* NGX_HTTP_NMC_OFF */
89130
return ngx_http_next_header_filter(r);
90131
}
91132

92-
/* not modified */
133+
not_modified:
93134

94135
r->headers_out.status = NGX_HTTP_NOT_MODIFIED;
95136
r->headers_out.status_line.len = 0;

src/http/ngx_http_core_module.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@ static ngx_conf_enum_t ngx_http_core_server_tokens[] = {
129129
};
130130

131131

132+
static ngx_conf_enum_t ngx_http_core_not_modified_check[] = {
133+
{ ngx_string("off"), NGX_HTTP_NMC_OFF },
134+
{ ngx_string("any"), NGX_HTTP_NMC_ANY },
135+
{ ngx_string("strict"), NGX_HTTP_NMC_STRICT },
136+
{ ngx_string("prefer_if_none_match"), NGX_HTTP_NMC_PREFER_INM },
137+
{ ngx_null_string, 0 }
138+
};
139+
140+
132141
static ngx_conf_enum_t ngx_http_core_if_modified_since[] = {
133142
{ ngx_string("off"), NGX_HTTP_IMS_OFF },
134143
{ ngx_string("exact"), NGX_HTTP_IMS_EXACT },
@@ -642,6 +651,13 @@ static ngx_command_t ngx_http_core_commands[] = {
642651
offsetof(ngx_http_core_loc_conf_t, server_tokens),
643652
&ngx_http_core_server_tokens },
644653

654+
{ ngx_string("not_modified_check"),
655+
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
656+
ngx_conf_set_enum_slot,
657+
NGX_HTTP_LOC_CONF_OFFSET,
658+
offsetof(ngx_http_core_loc_conf_t, not_modified_check),
659+
&ngx_http_core_not_modified_check },
660+
645661
{ ngx_string("if_modified_since"),
646662
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
647663
ngx_conf_set_enum_slot,
@@ -3592,6 +3608,7 @@ ngx_http_core_create_loc_conf(ngx_conf_t *cf)
35923608
clcf->client_body_timeout = NGX_CONF_UNSET_MSEC;
35933609
clcf->satisfy = NGX_CONF_UNSET_UINT;
35943610
clcf->auth_delay = NGX_CONF_UNSET_MSEC;
3611+
clcf->not_modified_check = NGX_CONF_UNSET_UINT;
35953612
clcf->if_modified_since = NGX_CONF_UNSET_UINT;
35963613
clcf->max_ranges = NGX_CONF_UNSET_UINT;
35973614
clcf->client_body_in_file_only = NGX_CONF_UNSET_UINT;
@@ -3812,6 +3829,8 @@ ngx_http_core_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
38123829
ngx_conf_merge_uint_value(conf->satisfy, prev->satisfy,
38133830
NGX_HTTP_SATISFY_ALL);
38143831
ngx_conf_merge_msec_value(conf->auth_delay, prev->auth_delay, 0);
3832+
ngx_conf_merge_uint_value(conf->not_modified_check,
3833+
prev->not_modified_check, NGX_HTTP_NMC_STRICT);
38153834
ngx_conf_merge_uint_value(conf->if_modified_since, prev->if_modified_since,
38163835
NGX_HTTP_IMS_EXACT);
38173836
ngx_conf_merge_uint_value(conf->max_ranges, prev->max_ranges,

src/http/ngx_http_core_module.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ typedef struct ngx_thread_pool_s ngx_thread_pool_t;
6060
#define NGX_HTTP_SERVER_TOKENS_BUILD 2
6161

6262

63+
#define NGX_HTTP_NMC_OFF 0
64+
#define NGX_HTTP_NMC_ANY 1
65+
#define NGX_HTTP_NMC_STRICT 2
66+
#define NGX_HTTP_NMC_PREFER_INM 3
67+
68+
6369
typedef struct ngx_http_location_tree_node_s ngx_http_location_tree_node_t;
6470
typedef struct ngx_http_core_loc_conf_s ngx_http_core_loc_conf_t;
6571

@@ -384,6 +390,7 @@ struct ngx_http_core_loc_conf_s {
384390
ngx_uint_t keepalive_disable; /* keepalive_disable */
385391
ngx_uint_t satisfy; /* satisfy */
386392
ngx_uint_t lingering_close; /* lingering_close */
393+
ngx_uint_t not_modified_check; /* not_modified_check */
387394
ngx_uint_t if_modified_since; /* if_modified_since */
388395
ngx_uint_t max_ranges; /* max_ranges */
389396
ngx_uint_t client_body_in_file_only; /* client_body_in_file_only */

0 commit comments

Comments
 (0)
0