0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
660
|
4 * Copyright (C) Nginx, Inc.
|
0
|
5 */
|
|
6
|
|
7
|
|
8 #include <ngx_config.h>
|
|
9 #include <ngx_core.h>
|
|
10 #include <ngx_http.h>
|
|
11
|
|
12
|
608
|
13 static ngx_int_t ngx_http_test_precondition(ngx_http_request_t *r);
|
|
14 static ngx_int_t ngx_http_test_not_modified(ngx_http_request_t *r);
|
230
|
15 static ngx_int_t ngx_http_not_modified_filter_init(ngx_conf_t *cf);
|
0
|
16
|
|
17
|
|
18 static ngx_http_module_t ngx_http_not_modified_filter_module_ctx = {
|
58
|
19 NULL, /* preconfiguration */
|
230
|
20 ngx_http_not_modified_filter_init, /* postconfiguration */
|
0
|
21
|
|
22 NULL, /* create main configuration */
|
|
23 NULL, /* init main configuration */
|
|
24
|
|
25 NULL, /* create server configuration */
|
|
26 NULL, /* merge server configuration */
|
|
27
|
|
28 NULL, /* create location configuration */
|
|
29 NULL /* merge location configuration */
|
|
30 };
|
|
31
|
|
32
|
|
33 ngx_module_t ngx_http_not_modified_filter_module = {
|
58
|
34 NGX_MODULE_V1,
|
0
|
35 &ngx_http_not_modified_filter_module_ctx, /* module context */
|
|
36 NULL, /* module directives */
|
|
37 NGX_HTTP_MODULE, /* module type */
|
90
|
38 NULL, /* init master */
|
230
|
39 NULL, /* init module */
|
90
|
40 NULL, /* init process */
|
|
41 NULL, /* init thread */
|
|
42 NULL, /* exit thread */
|
|
43 NULL, /* exit process */
|
|
44 NULL, /* exit master */
|
|
45 NGX_MODULE_V1_PADDING
|
0
|
46 };
|
|
47
|
|
48
|
|
49 static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
|
|
50
|
|
51
|
480
|
52 static ngx_int_t
|
|
53 ngx_http_not_modified_header_filter(ngx_http_request_t *r)
|
0
|
54 {
|
|
55 if (r->headers_out.status != NGX_HTTP_OK
|
146
|
56 || r != r->main
|
0
|
57 || r->headers_out.last_modified_time == -1)
|
|
58 {
|
|
59 return ngx_http_next_header_filter(r);
|
|
60 }
|
612
|
61
|
608
|
62 if (r->headers_in.if_unmodified_since) {
|
|
63 return ngx_http_test_precondition(r);
|
|
64 }
|
612
|
65
|
608
|
66 if (r->headers_in.if_modified_since) {
|
|
67 return ngx_http_test_not_modified(r);
|
|
68 }
|
|
69
|
|
70 return ngx_http_next_header_filter(r);
|
|
71 }
|
|
72
|
|
73
|
|
74 static ngx_int_t
|
|
75 ngx_http_test_precondition(ngx_http_request_t *r)
|
|
76 {
|
|
77 time_t iums;
|
|
78
|
|
79 iums = ngx_http_parse_time(r->headers_in.if_unmodified_since->value.data,
|
|
80 r->headers_in.if_unmodified_since->value.len);
|
|
81
|
|
82 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
83 "http iums:%d lm:%d", iums, r->headers_out.last_modified_time);
|
|
84
|
|
85 if (iums >= r->headers_out.last_modified_time) {
|
|
86 return ngx_http_next_header_filter(r);
|
|
87 }
|
|
88
|
|
89 return ngx_http_filter_finalize_request(r, NULL,
|
|
90 NGX_HTTP_PRECONDITION_FAILED);
|
|
91 }
|
|
92
|
|
93
|
|
94 static ngx_int_t
|
|
95 ngx_http_test_not_modified(ngx_http_request_t *r)
|
|
96 {
|
|
97 time_t ims;
|
|
98 ngx_http_core_loc_conf_t *clcf;
|
0
|
99
|
444
|
100 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
101
|
|
102 if (clcf->if_modified_since == NGX_HTTP_IMS_OFF) {
|
|
103 return ngx_http_next_header_filter(r);
|
|
104 }
|
|
105
|
0
|
106 ims = ngx_http_parse_time(r->headers_in.if_modified_since->value.data,
|
|
107 r->headers_in.if_modified_since->value.len);
|
126
|
108
|
0
|
109 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
110 "http ims:%d lm:%d", ims, r->headers_out.last_modified_time);
|
|
111
|
424
|
112 if (ims != r->headers_out.last_modified_time) {
|
|
113
|
444
|
114 if (clcf->if_modified_since == NGX_HTTP_IMS_EXACT
|
424
|
115 || ims < r->headers_out.last_modified_time)
|
|
116 {
|
|
117 return ngx_http_next_header_filter(r);
|
|
118 }
|
0
|
119 }
|
|
120
|
424
|
121 r->headers_out.status = NGX_HTTP_NOT_MODIFIED;
|
464
|
122 r->headers_out.status_line.len = 0;
|
424
|
123 r->headers_out.content_type.len = 0;
|
|
124 ngx_http_clear_content_length(r);
|
|
125 ngx_http_clear_accept_ranges(r);
|
|
126
|
544
|
127 if (r->headers_out.content_encoding) {
|
|
128 r->headers_out.content_encoding->hash = 0;
|
|
129 r->headers_out.content_encoding = NULL;
|
|
130 }
|
|
131
|
0
|
132 return ngx_http_next_header_filter(r);
|
|
133 }
|
|
134
|
|
135
|
480
|
136 static ngx_int_t
|
|
137 ngx_http_not_modified_filter_init(ngx_conf_t *cf)
|
0
|
138 {
|
|
139 ngx_http_next_header_filter = ngx_http_top_header_filter;
|
|
140 ngx_http_top_header_filter = ngx_http_not_modified_header_filter;
|
|
141
|
|
142 return NGX_OK;
|
|
143 }
|