358
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
660
|
4 * Copyright (C) Nginx, Inc.
|
358
|
5 */
|
|
6
|
|
7
|
|
8 #include <ngx_config.h>
|
|
9 #include <ngx_core.h>
|
|
10 #include <ngx_http.h>
|
|
11
|
|
12
|
|
13 typedef struct {
|
|
14 ngx_flag_t enable;
|
|
15 } ngx_http_gzip_static_conf_t;
|
|
16
|
|
17
|
|
18 static ngx_int_t ngx_http_gzip_static_handler(ngx_http_request_t *r);
|
|
19 static void *ngx_http_gzip_static_create_conf(ngx_conf_t *cf);
|
|
20 static char *ngx_http_gzip_static_merge_conf(ngx_conf_t *cf, void *parent,
|
|
21 void *child);
|
|
22 static ngx_int_t ngx_http_gzip_static_init(ngx_conf_t *cf);
|
|
23
|
|
24
|
|
25 static ngx_command_t ngx_http_gzip_static_commands[] = {
|
|
26
|
|
27 { ngx_string("gzip_static"),
|
|
28 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
29 ngx_conf_set_flag_slot,
|
|
30 NGX_HTTP_LOC_CONF_OFFSET,
|
|
31 offsetof(ngx_http_gzip_static_conf_t, enable),
|
|
32 NULL },
|
|
33
|
|
34 ngx_null_command
|
|
35 };
|
|
36
|
|
37
|
|
38 ngx_http_module_t ngx_http_gzip_static_module_ctx = {
|
|
39 NULL, /* preconfiguration */
|
|
40 ngx_http_gzip_static_init, /* postconfiguration */
|
|
41
|
|
42 NULL, /* create main configuration */
|
|
43 NULL, /* init main configuration */
|
|
44
|
|
45 NULL, /* create server configuration */
|
|
46 NULL, /* merge server configuration */
|
|
47
|
|
48 ngx_http_gzip_static_create_conf, /* create location configuration */
|
|
49 ngx_http_gzip_static_merge_conf /* merge location configuration */
|
|
50 };
|
|
51
|
|
52
|
|
53 ngx_module_t ngx_http_gzip_static_module = {
|
|
54 NGX_MODULE_V1,
|
|
55 &ngx_http_gzip_static_module_ctx, /* module context */
|
|
56 ngx_http_gzip_static_commands, /* module directives */
|
|
57 NGX_HTTP_MODULE, /* module type */
|
|
58 NULL, /* init master */
|
|
59 NULL, /* init module */
|
|
60 NULL, /* init process */
|
|
61 NULL, /* init thread */
|
|
62 NULL, /* exit thread */
|
|
63 NULL, /* exit process */
|
|
64 NULL, /* exit master */
|
|
65 NGX_MODULE_V1_PADDING
|
|
66 };
|
|
67
|
|
68
|
|
69 static ngx_int_t
|
|
70 ngx_http_gzip_static_handler(ngx_http_request_t *r)
|
|
71 {
|
|
72 u_char *p;
|
|
73 size_t root;
|
|
74 ngx_str_t path;
|
|
75 ngx_int_t rc;
|
|
76 ngx_uint_t level;
|
|
77 ngx_log_t *log;
|
|
78 ngx_buf_t *b;
|
|
79 ngx_chain_t out;
|
|
80 ngx_table_elt_t *h;
|
|
81 ngx_open_file_info_t of;
|
|
82 ngx_http_core_loc_conf_t *clcf;
|
|
83 ngx_http_gzip_static_conf_t *gzcf;
|
|
84
|
|
85 if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
|
362
|
86 return NGX_DECLINED;
|
358
|
87 }
|
|
88
|
|
89 if (r->uri.data[r->uri.len - 1] == '/') {
|
|
90 return NGX_DECLINED;
|
|
91 }
|
|
92
|
|
93 gzcf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_static_module);
|
|
94
|
528
|
95 if (!gzcf->enable) {
|
|
96 return NGX_DECLINED;
|
|
97 }
|
|
98
|
546
|
99 rc = ngx_http_gzip_ok(r);
|
|
100
|
528
|
101 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
102
|
546
|
103 if (!clcf->gzip_vary && rc != NGX_OK) {
|
358
|
104 return NGX_DECLINED;
|
|
105 }
|
|
106
|
|
107 log = r->connection->log;
|
|
108
|
|
109 p = ngx_http_map_uri_to_path(r, &path, &root, sizeof(".gz") - 1);
|
|
110 if (p == NULL) {
|
|
111 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
112 }
|
|
113
|
|
114 *p++ = '.';
|
|
115 *p++ = 'g';
|
|
116 *p++ = 'z';
|
|
117 *p = '\0';
|
|
118
|
|
119 path.len = p - path.data;
|
|
120
|
|
121 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
|
|
122 "http filename: \"%s\"", path.data);
|
|
123
|
384
|
124 ngx_memzero(&of, sizeof(ngx_open_file_info_t));
|
|
125
|
532
|
126 of.read_ahead = clcf->read_ahead;
|
390
|
127 of.directio = clcf->directio;
|
358
|
128 of.valid = clcf->open_file_cache_valid;
|
|
129 of.min_uses = clcf->open_file_cache_min_uses;
|
|
130 of.errors = clcf->open_file_cache_errors;
|
|
131 of.events = clcf->open_file_cache_events;
|
662
|
132 #if (NGX_HAVE_OPENAT)
|
|
133 of.disable_symlinks = clcf->disable_symlinks;
|
|
134 #endif
|
358
|
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:
|
662
|
151 #if (NGX_HAVE_OPENAT)
|
|
152 case NGX_EMLINK:
|
|
153 case NGX_ELOOP:
|
|
154 #endif
|
358
|
155
|
|
156 level = NGX_LOG_ERR;
|
|
157 break;
|
|
158
|
|
159 default:
|
|
160
|
|
161 level = NGX_LOG_CRIT;
|
|
162 break;
|
|
163 }
|
|
164
|
|
165 ngx_log_error(level, log, of.err,
|
482
|
166 "%s \"%s\" failed", of.failed, path.data);
|
358
|
167
|
362
|
168 return NGX_DECLINED;
|
358
|
169 }
|
|
170
|
546
|
171 r->gzip_vary = 1;
|
|
172
|
|
173 if (rc != NGX_OK) {
|
|
174 return NGX_DECLINED;
|
|
175 }
|
|
176
|
358
|
177 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "http static fd: %d", of.fd);
|
|
178
|
|
179 if (of.is_dir) {
|
|
180 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http dir");
|
|
181 return NGX_DECLINED;
|
|
182 }
|
|
183
|
|
184 #if !(NGX_WIN32) /* the not regular files are probably Unix specific */
|
|
185
|
|
186 if (!of.is_file) {
|
568
|
187 ngx_log_error(NGX_LOG_CRIT, log, 0,
|
358
|
188 "\"%s\" is not a regular file", path.data);
|
|
189
|
|
190 return NGX_HTTP_NOT_FOUND;
|
|
191 }
|
|
192
|
|
193 #endif
|
|
194
|
438
|
195 r->root_tested = !r->error_page;
|
388
|
196
|
358
|
197 rc = ngx_http_discard_request_body(r);
|
|
198
|
|
199 if (rc != NGX_OK) {
|
|
200 return rc;
|
|
201 }
|
|
202
|
|
203 log->action = "sending response to client";
|
|
204
|
|
205 r->headers_out.status = NGX_HTTP_OK;
|
|
206 r->headers_out.content_length_n = of.size;
|
|
207 r->headers_out.last_modified_time = of.mtime;
|
|
208
|
|
209 if (ngx_http_set_content_type(r) != NGX_OK) {
|
|
210 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
211 }
|
|
212
|
|
213 h = ngx_list_push(&r->headers_out.headers);
|
|
214 if (h == NULL) {
|
|
215 return NGX_ERROR;
|
|
216 }
|
|
217
|
|
218 h->hash = 1;
|
570
|
219 ngx_str_set(&h->key, "Content-Encoding");
|
|
220 ngx_str_set(&h->value, "gzip");
|
|
221 r->headers_out.content_encoding = h;
|
358
|
222
|
494
|
223 r->ignore_content_encoding = 1;
|
358
|
224
|
|
225 /* we need to allocate all before the header would be sent */
|
|
226
|
|
227 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
|
|
228 if (b == NULL) {
|
|
229 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
230 }
|
|
231
|
|
232 b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
|
|
233 if (b->file == NULL) {
|
|
234 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
235 }
|
|
236
|
|
237 rc = ngx_http_send_header(r);
|
|
238
|
|
239 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
|
|
240 return rc;
|
|
241 }
|
|
242
|
|
243 b->file_pos = 0;
|
|
244 b->file_last = of.size;
|
|
245
|
|
246 b->in_file = b->file_last ? 1 : 0;
|
|
247 b->last_buf = 1;
|
|
248 b->last_in_chain = 1;
|
|
249
|
|
250 b->file->fd = of.fd;
|
|
251 b->file->name = path;
|
|
252 b->file->log = log;
|
406
|
253 b->file->directio = of.is_directio;
|
358
|
254
|
|
255 out.buf = b;
|
|
256 out.next = NULL;
|
|
257
|
|
258 return ngx_http_output_filter(r, &out);
|
|
259 }
|
|
260
|
|
261
|
|
262 static void *
|
|
263 ngx_http_gzip_static_create_conf(ngx_conf_t *cf)
|
|
264 {
|
|
265 ngx_http_gzip_static_conf_t *conf;
|
|
266
|
|
267 conf = ngx_palloc(cf->pool, sizeof(ngx_http_gzip_static_conf_t));
|
|
268 if (conf == NULL) {
|
496
|
269 return NULL;
|
358
|
270 }
|
|
271
|
|
272 conf->enable = NGX_CONF_UNSET;
|
|
273
|
|
274 return conf;
|
|
275 }
|
|
276
|
|
277
|
|
278 static char *
|
|
279 ngx_http_gzip_static_merge_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
280 {
|
|
281 ngx_http_gzip_static_conf_t *prev = parent;
|
|
282 ngx_http_gzip_static_conf_t *conf = child;
|
|
283
|
|
284 ngx_conf_merge_value(conf->enable, prev->enable, 0);
|
|
285
|
|
286 return NGX_CONF_OK;
|
|
287 }
|
|
288
|
|
289
|
|
290 static ngx_int_t
|
|
291 ngx_http_gzip_static_init(ngx_conf_t *cf)
|
|
292 {
|
|
293 ngx_http_handler_pt *h;
|
|
294 ngx_http_core_main_conf_t *cmcf;
|
|
295
|
|
296 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
|
297
|
|
298 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
|
|
299 if (h == NULL) {
|
|
300 return NGX_ERROR;
|
|
301 }
|
|
302
|
|
303 *h = ngx_http_gzip_static_handler;
|
|
304
|
|
305 return NGX_OK;
|
|
306 }
|