358
|
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))) {
|
362
|
85 return NGX_DECLINED;
|
358
|
86 }
|
|
87
|
|
88 if (r->uri.data[r->uri.len - 1] == '/') {
|
|
89 return NGX_DECLINED;
|
|
90 }
|
|
91
|
|
92 if (r->zero_in_uri) {
|
|
93 return NGX_DECLINED;
|
|
94 }
|
|
95
|
|
96 gzcf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_static_module);
|
|
97
|
528
|
98 if (!gzcf->enable) {
|
|
99 return NGX_DECLINED;
|
|
100 }
|
|
101
|
546
|
102 rc = ngx_http_gzip_ok(r);
|
|
103
|
528
|
104 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
105
|
546
|
106 if (!clcf->gzip_vary && rc != NGX_OK) {
|
358
|
107 return NGX_DECLINED;
|
|
108 }
|
|
109
|
|
110 log = r->connection->log;
|
|
111
|
|
112 p = ngx_http_map_uri_to_path(r, &path, &root, sizeof(".gz") - 1);
|
|
113 if (p == NULL) {
|
|
114 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
115 }
|
|
116
|
|
117 *p++ = '.';
|
|
118 *p++ = 'g';
|
|
119 *p++ = 'z';
|
|
120 *p = '\0';
|
|
121
|
|
122 path.len = p - path.data;
|
|
123
|
|
124 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
|
|
125 "http filename: \"%s\"", path.data);
|
|
126
|
384
|
127 ngx_memzero(&of, sizeof(ngx_open_file_info_t));
|
|
128
|
532
|
129 of.read_ahead = clcf->read_ahead;
|
390
|
130 of.directio = clcf->directio;
|
358
|
131 of.valid = clcf->open_file_cache_valid;
|
|
132 of.min_uses = clcf->open_file_cache_min_uses;
|
|
133 of.errors = clcf->open_file_cache_errors;
|
|
134 of.events = clcf->open_file_cache_events;
|
|
135
|
362
|
136 if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
|
|
137 != NGX_OK)
|
|
138 {
|
358
|
139 switch (of.err) {
|
|
140
|
|
141 case 0:
|
|
142 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
143
|
|
144 case NGX_ENOENT:
|
|
145 case NGX_ENOTDIR:
|
|
146 case NGX_ENAMETOOLONG:
|
|
147
|
|
148 return NGX_DECLINED;
|
|
149
|
|
150 case NGX_EACCES:
|
|
151
|
|
152 level = NGX_LOG_ERR;
|
|
153 break;
|
|
154
|
|
155 default:
|
|
156
|
|
157 level = NGX_LOG_CRIT;
|
|
158 break;
|
|
159 }
|
|
160
|
|
161 ngx_log_error(level, log, of.err,
|
482
|
162 "%s \"%s\" failed", of.failed, path.data);
|
358
|
163
|
362
|
164 return NGX_DECLINED;
|
358
|
165 }
|
|
166
|
546
|
167 r->gzip_vary = 1;
|
|
168
|
|
169 if (rc != NGX_OK) {
|
|
170 return NGX_DECLINED;
|
|
171 }
|
|
172
|
358
|
173 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "http static fd: %d", of.fd);
|
|
174
|
|
175 if (of.is_dir) {
|
|
176 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http dir");
|
|
177 return NGX_DECLINED;
|
|
178 }
|
|
179
|
|
180 #if !(NGX_WIN32) /* the not regular files are probably Unix specific */
|
|
181
|
|
182 if (!of.is_file) {
|
|
183 ngx_log_error(NGX_LOG_CRIT, log, ngx_errno,
|
|
184 "\"%s\" is not a regular file", path.data);
|
|
185
|
|
186 return NGX_HTTP_NOT_FOUND;
|
|
187 }
|
|
188
|
|
189 #endif
|
|
190
|
438
|
191 r->root_tested = !r->error_page;
|
388
|
192
|
358
|
193 rc = ngx_http_discard_request_body(r);
|
|
194
|
|
195 if (rc != NGX_OK) {
|
|
196 return rc;
|
|
197 }
|
|
198
|
|
199 log->action = "sending response to client";
|
|
200
|
|
201 r->headers_out.status = NGX_HTTP_OK;
|
|
202 r->headers_out.content_length_n = of.size;
|
|
203 r->headers_out.last_modified_time = of.mtime;
|
|
204
|
|
205 if (ngx_http_set_content_type(r) != NGX_OK) {
|
|
206 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
207 }
|
|
208
|
|
209 h = ngx_list_push(&r->headers_out.headers);
|
|
210 if (h == NULL) {
|
|
211 return NGX_ERROR;
|
|
212 }
|
|
213
|
|
214 h->hash = 1;
|
|
215 h->key.len = sizeof("Content-Encoding") - 1;
|
|
216 h->key.data = (u_char *) "Content-Encoding";
|
|
217 h->value.len = sizeof("gzip") - 1;
|
|
218 h->value.data = (u_char *) "gzip";
|
|
219
|
|
220 r->headers_out.content_encoding = h;
|
494
|
221 r->ignore_content_encoding = 1;
|
358
|
222
|
|
223 /* we need to allocate all before the header would be sent */
|
|
224
|
|
225 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
|
|
226 if (b == NULL) {
|
|
227 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
228 }
|
|
229
|
|
230 b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
|
|
231 if (b->file == NULL) {
|
|
232 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
233 }
|
|
234
|
|
235 rc = ngx_http_send_header(r);
|
|
236
|
|
237 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
|
|
238 return rc;
|
|
239 }
|
|
240
|
|
241 b->file_pos = 0;
|
|
242 b->file_last = of.size;
|
|
243
|
|
244 b->in_file = b->file_last ? 1 : 0;
|
|
245 b->last_buf = 1;
|
|
246 b->last_in_chain = 1;
|
|
247
|
|
248 b->file->fd = of.fd;
|
|
249 b->file->name = path;
|
|
250 b->file->log = log;
|
406
|
251 b->file->directio = of.is_directio;
|
358
|
252
|
|
253 out.buf = b;
|
|
254 out.next = NULL;
|
|
255
|
|
256 return ngx_http_output_filter(r, &out);
|
|
257 }
|
|
258
|
|
259
|
|
260 static void *
|
|
261 ngx_http_gzip_static_create_conf(ngx_conf_t *cf)
|
|
262 {
|
|
263 ngx_http_gzip_static_conf_t *conf;
|
|
264
|
|
265 conf = ngx_palloc(cf->pool, sizeof(ngx_http_gzip_static_conf_t));
|
|
266 if (conf == NULL) {
|
496
|
267 return NULL;
|
358
|
268 }
|
|
269
|
|
270 conf->enable = NGX_CONF_UNSET;
|
|
271
|
|
272 return conf;
|
|
273 }
|
|
274
|
|
275
|
|
276 static char *
|
|
277 ngx_http_gzip_static_merge_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
278 {
|
|
279 ngx_http_gzip_static_conf_t *prev = parent;
|
|
280 ngx_http_gzip_static_conf_t *conf = child;
|
|
281
|
|
282 ngx_conf_merge_value(conf->enable, prev->enable, 0);
|
|
283
|
|
284 return NGX_CONF_OK;
|
|
285 }
|
|
286
|
|
287
|
|
288 static ngx_int_t
|
|
289 ngx_http_gzip_static_init(ngx_conf_t *cf)
|
|
290 {
|
|
291 ngx_http_handler_pt *h;
|
|
292 ngx_http_core_main_conf_t *cmcf;
|
|
293
|
|
294 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
|
295
|
|
296 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
|
|
297 if (h == NULL) {
|
|
298 return NGX_ERROR;
|
|
299 }
|
|
300
|
|
301 *h = ngx_http_gzip_static_handler;
|
|
302
|
|
303 return NGX_OK;
|
|
304 }
|