comparison src/http/modules/ngx_http_headers_filter_module.c @ 50:72eb30262aac NGINX_0_1_25

nginx 0.1.25 *) Bugfix: nginx did run on Linux parisc. *) Feature: nginx now does not start under FreeBSD if the sysctl kern.ipc.somaxconn value is too big. *) Bugfix: if a request was internally redirected by the ngx_http_index_module module to the ngx_http_proxy_module or ngx_http_fastcgi_module modules, then the index file was not closed after request completion. *) Feature: the "proxy_pass" can be used in location with regular expression. *) Feature: the ngx_http_rewrite_filter_module module supports the condition like "if ($HTTP_USER_AGENT ~ MSIE)". *) Bugfix: nginx started too slow if the large number of addresses and text values were used in the "geo" directive. *) Change: a variable name must be declared as "$name" in the "geo" directive. The previous variant without "$" is still supported, but will be removed soon. *) Feature: the "%{VARIABLE}v" logging parameter. *) Feature: the "set $name value" directive. *) Bugfix: gcc 4.0 compatibility. *) Feature: the --with-openssl-opt=OPTIONS autoconfiguration directive.
author Igor Sysoev <http://sysoev.ru>
date Sat, 19 Mar 2005 00:00:00 +0300
parents
children b55cbf18157e
comparison
equal deleted inserted replaced
49:93dabbc9efb9 50:72eb30262aac
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
12 typedef struct {
13 time_t expires;
14 } ngx_http_headers_conf_t;
15
16
17 #define NGX_HTTP_EXPIRES_UNSET -2147483647
18 #define NGX_HTTP_EXPIRES_OFF -2147483646
19 #define NGX_HTTP_EXPIRES_EPOCH -2147483645
20
21
22 static ngx_int_t ngx_http_headers_filter_init(ngx_cycle_t *cycle);
23 static void *ngx_http_headers_create_conf(ngx_conf_t *cf);
24 static char *ngx_http_headers_merge_conf(ngx_conf_t *cf,
25 void *parent, void *child);
26 static char *ngx_http_headers_expires(ngx_conf_t *cf, ngx_command_t *cmd,
27 void *conf);
28
29
30 static ngx_command_t ngx_http_headers_filter_commands[] = {
31
32 { ngx_string("expires"),
33 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
34 ngx_http_headers_expires,
35 NGX_HTTP_LOC_CONF_OFFSET,
36 0,
37 NULL},
38
39 ngx_null_command
40 };
41
42
43 static ngx_http_module_t ngx_http_headers_filter_module_ctx = {
44 NULL, /* pre conf */
45
46 NULL, /* create main configuration */
47 NULL, /* init main configuration */
48
49 NULL, /* create server configuration */
50 NULL, /* merge server configuration */
51
52 ngx_http_headers_create_conf, /* create location configuration */
53 ngx_http_headers_merge_conf /* merge location configuration */
54 };
55
56
57 ngx_module_t ngx_http_headers_filter_module = {
58 NGX_MODULE,
59 &ngx_http_headers_filter_module_ctx, /* module context */
60 ngx_http_headers_filter_commands, /* module directives */
61 NGX_HTTP_MODULE, /* module type */
62 ngx_http_headers_filter_init, /* init module */
63 NULL /* init process */
64 };
65
66
67 static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
68
69
70 static ngx_int_t
71 ngx_http_headers_filter(ngx_http_request_t *r)
72 {
73 size_t len;
74 ngx_table_elt_t *expires, *cc;
75 ngx_http_headers_conf_t *conf;
76
77 if (r->headers_out.status != NGX_HTTP_OK) {
78 return ngx_http_next_header_filter(r);
79 }
80
81 conf = ngx_http_get_module_loc_conf(r, ngx_http_headers_filter_module);
82
83 if (conf->expires != NGX_HTTP_EXPIRES_OFF) {
84
85 expires = ngx_list_push(&r->headers_out.headers);
86 if (expires == NULL) {
87 return NGX_ERROR;
88 }
89
90 r->headers_out.expires = expires;
91
92 cc = ngx_list_push(&r->headers_out.headers);
93 if (cc == NULL) {
94 return NGX_ERROR;
95 }
96
97 r->headers_out.cache_control = cc;
98
99 len = sizeof("Mon, 28 Sep 1970 06:00:00 GMT");
100
101 expires->key.len = sizeof("Expires") - 1;
102 expires->key.data = (u_char *) "Expires";
103 expires->value.len = len - 1;
104
105 cc->key.len = sizeof("Cache-Control") - 1;
106 cc->key.data = (u_char *) "Cache-Control";
107
108 if (conf->expires == NGX_HTTP_EXPIRES_EPOCH) {
109 expires->value.data = (u_char *) "Thu, 01 Jan 1970 00:00:01 GMT";
110
111 cc->value.len = sizeof("no-cache") - 1;
112 cc->value.data = (u_char *) "no-cache";
113
114 } else {
115 expires->value.data = ngx_palloc(r->pool, len);
116 if (expires->value.data == NULL) {
117 return NGX_ERROR;
118 }
119
120 if (conf->expires == 0) {
121 ngx_memcpy(expires->value.data, ngx_cached_http_time.data,
122 ngx_cached_http_time.len + 1);
123
124 cc->value.len = sizeof("max-age=0") - 1;
125 cc->value.data = (u_char *) "max-age=0";
126
127 } else {
128 ngx_http_time(expires->value.data, ngx_time() + conf->expires);
129
130 if (conf->expires < 0) {
131 cc->value.len = sizeof("no-cache") - 1;
132 cc->value.data = (u_char *) "no-cache";
133
134 } else {
135 cc->value.data = ngx_palloc(r->pool, sizeof("max-age=")
136 + NGX_TIME_T_LEN + 1);
137 if (cc->value.data == NULL) {
138 return NGX_ERROR;
139 }
140
141 cc->value.len = ngx_sprintf(cc->value.data, "max-age=%T",
142 conf->expires)
143 - cc->value.data;
144
145 }
146 }
147 }
148 }
149
150 return ngx_http_next_header_filter(r);
151 }
152
153
154 static ngx_int_t
155 ngx_http_headers_filter_init(ngx_cycle_t *cycle)
156 {
157 ngx_http_next_header_filter = ngx_http_top_header_filter;
158 ngx_http_top_header_filter = ngx_http_headers_filter;
159
160 return NGX_OK;
161 }
162
163
164 static void *
165 ngx_http_headers_create_conf(ngx_conf_t *cf)
166 {
167 ngx_http_headers_conf_t *conf;
168
169 conf = ngx_palloc(cf->pool, sizeof(ngx_http_headers_conf_t));
170 if (conf == NULL) {
171 return NGX_CONF_ERROR;
172 }
173
174 conf->expires = NGX_HTTP_EXPIRES_UNSET;
175
176 return conf;
177 }
178
179
180 static char *
181 ngx_http_headers_merge_conf(ngx_conf_t *cf, void *parent, void *child)
182 {
183 ngx_http_headers_conf_t *prev = parent;
184 ngx_http_headers_conf_t *conf = child;
185
186 if (conf->expires == NGX_HTTP_EXPIRES_UNSET) {
187 conf->expires = (prev->expires == NGX_HTTP_EXPIRES_UNSET) ?
188 NGX_HTTP_EXPIRES_OFF : prev->expires;
189 }
190
191 return NGX_CONF_OK;
192 }
193
194
195 static char *
196 ngx_http_headers_expires(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
197 {
198 ngx_http_headers_conf_t *hcf = conf;
199
200 ngx_uint_t minus;
201 ngx_str_t *value;
202
203 if (hcf->expires != NGX_HTTP_EXPIRES_UNSET) {
204 return "is duplicate";
205 }
206
207 value = cf->args->elts;
208
209 if (ngx_strcmp(value[1].data, "epoch") == 0) {
210 hcf->expires = NGX_HTTP_EXPIRES_EPOCH;
211 return NGX_CONF_OK;
212 }
213
214 if (ngx_strcmp(value[1].data, "off") == 0) {
215 hcf->expires = NGX_HTTP_EXPIRES_OFF;
216 return NGX_CONF_OK;
217 }
218
219 if (value[1].data[0] == '+') {
220 value[1].data++;
221 value[1].len--;
222 minus = 0;
223
224 } else if (value[1].data[0] == '-') {
225 value[1].data++;
226 value[1].len--;
227 minus = 1;
228
229 } else {
230 minus = 0;
231 }
232
233 hcf->expires = ngx_parse_time(&value[1], 1);
234
235 if (hcf->expires == NGX_ERROR) {
236 return "invalid value";
237 }
238
239 if (hcf->expires == NGX_PARSE_LARGE_TIME) {
240 return "value must be less than 68 years";
241 }
242
243 if (minus) {
244 hcf->expires = - hcf->expires;
245 }
246
247 return NGX_CONF_OK;
248 }