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