comparison src/http/modules/ngx_http_gzip_static_module.c @ 358:9121a0a91f47 NGINX_0_6_23

nginx 0.6.23 *) Change: the "off" parameter in the "ssl_session_cache" directive; now this is default parameter. *) Change: the "open_file_cache_retest" directive was renamed to the "open_file_cache_valid". *) Feature: the "open_file_cache_min_uses" directive. *) Feature: the ngx_http_gzip_static_module. *) Feature: the "gzip_disable" directive. *) Feature: the "memcached_pass" directive may be used inside the "if" block. *) Bugfix: a segmentation fault occurred in worker process, if the "memcached_pass" and "if" directives were used in the same location. *) Bugfix: if a "satisfy_any on" directive was used and not all access and auth modules directives were set, then other given access and auth directives were not tested; *) Bugfix: regex parameters in a "valid_referers" directive were not inherited from previous level. *) Bugfix: a "post_action" directive did run if a request was completed with 499 status code. *) Bugfix: optimization of 16K buffer usage in a SSL connection. Thanks to Ben Maurer. *) Bugfix: the STARTTLS in SMTP mode did not work. Thanks to Oleg Motienko. *) Bugfix: in HTTPS mode requests might fail with the "bad write retry" error; bug appeared in 0.5.13.
author Igor Sysoev <http://sysoev.ru>
date Thu, 27 Dec 2007 00:00:00 +0300
parents
children 54fad6c4b555
comparison
equal deleted inserted replaced
357:16d557a75356 358:9121a0a91f47
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 ngx_flag_t enable;
14 } ngx_http_gzip_static_conf_t;
15
16
17 static ngx_int_t ngx_http_gzip_static_handler(ngx_http_request_t *r);
18 static void *ngx_http_gzip_static_create_conf(ngx_conf_t *cf);
19 static char *ngx_http_gzip_static_merge_conf(ngx_conf_t *cf, void *parent,
20 void *child);
21 static ngx_int_t ngx_http_gzip_static_init(ngx_conf_t *cf);
22
23
24 static ngx_command_t ngx_http_gzip_static_commands[] = {
25
26 { ngx_string("gzip_static"),
27 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
28 ngx_conf_set_flag_slot,
29 NGX_HTTP_LOC_CONF_OFFSET,
30 offsetof(ngx_http_gzip_static_conf_t, enable),
31 NULL },
32
33 ngx_null_command
34 };
35
36
37 ngx_http_module_t ngx_http_gzip_static_module_ctx = {
38 NULL, /* preconfiguration */
39 ngx_http_gzip_static_init, /* postconfiguration */
40
41 NULL, /* create main configuration */
42 NULL, /* init main configuration */
43
44 NULL, /* create server configuration */
45 NULL, /* merge server configuration */
46
47 ngx_http_gzip_static_create_conf, /* create location configuration */
48 ngx_http_gzip_static_merge_conf /* merge location configuration */
49 };
50
51
52 ngx_module_t ngx_http_gzip_static_module = {
53 NGX_MODULE_V1,
54 &ngx_http_gzip_static_module_ctx, /* module context */
55 ngx_http_gzip_static_commands, /* module directives */
56 NGX_HTTP_MODULE, /* module type */
57 NULL, /* init master */
58 NULL, /* init module */
59 NULL, /* init process */
60 NULL, /* init thread */
61 NULL, /* exit thread */
62 NULL, /* exit process */
63 NULL, /* exit master */
64 NGX_MODULE_V1_PADDING
65 };
66
67
68 static ngx_int_t
69 ngx_http_gzip_static_handler(ngx_http_request_t *r)
70 {
71 u_char *p;
72 size_t root;
73 ngx_str_t path;
74 ngx_int_t rc;
75 ngx_uint_t level;
76 ngx_log_t *log;
77 ngx_buf_t *b;
78 ngx_chain_t out;
79 ngx_table_elt_t *h;
80 ngx_open_file_info_t of;
81 ngx_http_core_loc_conf_t *clcf;
82 ngx_http_gzip_static_conf_t *gzcf;
83
84 if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
85 return NGX_HTTP_NOT_ALLOWED;
86 }
87
88 if (r->uri.data[r->uri.len - 1] == '/') {
89 return NGX_DECLINED;
90 }
91
92 /* TODO: Win32 */
93 if (r->zero_in_uri) {
94 return NGX_DECLINED;
95 }
96
97 gzcf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_static_module);
98
99 if (!gzcf->enable || ngx_http_gzip_ok(r) != NGX_OK) {
100 return NGX_DECLINED;
101 }
102
103 log = r->connection->log;
104
105 p = ngx_http_map_uri_to_path(r, &path, &root, sizeof(".gz") - 1);
106 if (p == NULL) {
107 return NGX_HTTP_INTERNAL_SERVER_ERROR;
108 }
109
110 *p++ = '.';
111 *p++ = 'g';
112 *p++ = 'z';
113 *p = '\0';
114
115 path.len = p - path.data;
116
117 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
118 "http filename: \"%s\"", path.data);
119
120 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
121
122 of.test_dir = 0;
123 of.valid = clcf->open_file_cache_valid;
124 of.min_uses = clcf->open_file_cache_min_uses;
125 of.errors = clcf->open_file_cache_errors;
126 of.events = clcf->open_file_cache_events;
127
128 rc = ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool);
129
130 if (rc == NGX_ERROR) {
131
132 switch (of.err) {
133
134 case 0:
135 return NGX_HTTP_INTERNAL_SERVER_ERROR;
136
137 case NGX_ENOENT:
138 case NGX_ENOTDIR:
139 case NGX_ENAMETOOLONG:
140
141 return NGX_DECLINED;
142
143 case NGX_EACCES:
144
145 level = NGX_LOG_ERR;
146 rc = NGX_DECLINED;
147 break;
148
149 default:
150
151 level = NGX_LOG_CRIT;
152 rc = NGX_DECLINED;
153 break;
154 }
155
156 ngx_log_error(level, log, of.err,
157 ngx_open_file_n " \"%s\" failed", path.data);
158
159 return rc;
160 }
161
162 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "http static fd: %d", of.fd);
163
164 if (of.is_dir) {
165 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http dir");
166 return NGX_DECLINED;
167 }
168
169 #if !(NGX_WIN32) /* the not regular files are probably Unix specific */
170
171 if (!of.is_file) {
172 ngx_log_error(NGX_LOG_CRIT, log, ngx_errno,
173 "\"%s\" is not a regular file", path.data);
174
175 return NGX_HTTP_NOT_FOUND;
176 }
177
178 #endif
179
180 rc = ngx_http_discard_request_body(r);
181
182 if (rc != NGX_OK) {
183 return rc;
184 }
185
186 log->action = "sending response to client";
187
188 r->headers_out.status = NGX_HTTP_OK;
189 r->headers_out.content_length_n = of.size;
190 r->headers_out.last_modified_time = of.mtime;
191
192 if (ngx_http_set_content_type(r) != NGX_OK) {
193 return NGX_HTTP_INTERNAL_SERVER_ERROR;
194 }
195
196 h = ngx_list_push(&r->headers_out.headers);
197 if (h == NULL) {
198 return NGX_ERROR;
199 }
200
201 h->hash = 1;
202 h->key.len = sizeof("Content-Encoding") - 1;
203 h->key.data = (u_char *) "Content-Encoding";
204 h->value.len = sizeof("gzip") - 1;
205 h->value.data = (u_char *) "gzip";
206
207 r->headers_out.content_encoding = h;
208
209 if (clcf->gzip_vary) {
210 h = ngx_list_push(&r->headers_out.headers);
211 if (h == NULL) {
212 return NGX_ERROR;
213 }
214
215 h->hash = 1;
216 h->key.len = sizeof("Vary") - 1;
217 h->key.data = (u_char *) "Vary";
218 h->value.len = sizeof("Accept-Encoding") - 1;
219 h->value.data = (u_char *) "Accept-Encoding";
220 }
221
222 /* we need to allocate all before the header would be sent */
223
224 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
225 if (b == NULL) {
226 return NGX_HTTP_INTERNAL_SERVER_ERROR;
227 }
228
229 b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
230 if (b->file == NULL) {
231 return NGX_HTTP_INTERNAL_SERVER_ERROR;
232 }
233
234 rc = ngx_http_send_header(r);
235
236 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
237 return rc;
238 }
239
240 b->file_pos = 0;
241 b->file_last = of.size;
242
243 b->in_file = b->file_last ? 1 : 0;
244 b->last_buf = 1;
245 b->last_in_chain = 1;
246
247 b->file->fd = of.fd;
248 b->file->name = path;
249 b->file->log = log;
250
251 out.buf = b;
252 out.next = NULL;
253
254 return ngx_http_output_filter(r, &out);
255 }
256
257
258 static void *
259 ngx_http_gzip_static_create_conf(ngx_conf_t *cf)
260 {
261 ngx_http_gzip_static_conf_t *conf;
262
263 conf = ngx_palloc(cf->pool, sizeof(ngx_http_gzip_static_conf_t));
264 if (conf == NULL) {
265 return NGX_CONF_ERROR;
266 }
267
268 conf->enable = NGX_CONF_UNSET;
269
270 return conf;
271 }
272
273
274 static char *
275 ngx_http_gzip_static_merge_conf(ngx_conf_t *cf, void *parent, void *child)
276 {
277 ngx_http_gzip_static_conf_t *prev = parent;
278 ngx_http_gzip_static_conf_t *conf = child;
279
280 ngx_conf_merge_value(conf->enable, prev->enable, 0);
281
282 return NGX_CONF_OK;
283 }
284
285
286 static ngx_int_t
287 ngx_http_gzip_static_init(ngx_conf_t *cf)
288 {
289 ngx_http_handler_pt *h;
290 ngx_http_core_main_conf_t *cmcf;
291
292 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
293
294 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
295 if (h == NULL) {
296 return NGX_ERROR;
297 }
298
299 *h = ngx_http_gzip_static_handler;
300
301 return NGX_OK;
302 }