358
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
644
|
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;
|
|
132
|
362
|
133 if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
|
|
134 != NGX_OK)
|
|
135 {
|
358
|
136 switch (of.err) {
|
|
137
|
|
138 case 0:
|
|
139 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
140
|
|
141 case NGX_ENOENT:
|
|
142 case NGX_ENOTDIR:
|
|
143 case NGX_ENAMETOOLONG:
|
|
144
|
|
145 return NGX_DECLINED;
|
|
146
|
|
147 case NGX_EACCES:
|
|
148
|
|
149 level = NGX_LOG_ERR;
|
|
150 break;
|
|
151
|
|
152 default:
|
|
153
|
|
154 level = NGX_LOG_CRIT;
|
|
155 break;
|
|
156 }
|
|
157
|
|
158 ngx_log_error(level, log, of.err,
|
482
|
159 "%s \"%s\" failed", of.failed, path.data);
|
358
|
160
|
362
|
161 return NGX_DECLINED;
|
358
|
162 }
|
|
163
|
546
|
164 r->gzip_vary = 1;
|
|
165
|
|
166 if (rc != NGX_OK) {
|
|
167 return NGX_DECLINED;
|
|
168 }
|
|
169
|
358
|
170 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "http static fd: %d", of.fd);
|
|
171
|
|
172 if (of.is_dir) {
|
|
173 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http dir");
|
|
174 return NGX_DECLINED;
|
|
175 }
|
|
176
|
|
177 #if !(NGX_WIN32) /* the not regular files are probably Unix specific */
|
|
178
|
|
179 if (!of.is_file) {
|
568
|
180 ngx_log_error(NGX_LOG_CRIT, log, 0,
|
358
|
181 "\"%s\" is not a regular file", path.data);
|
|
182
|
|
183 return NGX_HTTP_NOT_FOUND;
|
|
184 }
|
|
185
|
|
186 #endif
|
|
187
|
438
|
188 r->root_tested = !r->error_page;
|
388
|
189
|
358
|
190 rc = ngx_http_discard_request_body(r);
|
|
191
|
|
192 if (rc != NGX_OK) {
|
|
193 return rc;
|
|
194 }
|
|
195
|
|
196 log->action = "sending response to client";
|
|
197
|
|
198 r->headers_out.status = NGX_HTTP_OK;
|
|
199 r->headers_out.content_length_n = of.size;
|
|
200 r->headers_out.last_modified_time = of.mtime;
|
|
201
|
|
202 if (ngx_http_set_content_type(r) != NGX_OK) {
|
|
203 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
204 }
|
|
205
|
|
206 h = ngx_list_push(&r->headers_out.headers);
|
|
207 if (h == NULL) {
|
|
208 return NGX_ERROR;
|
|
209 }
|
|
210
|
|
211 h->hash = 1;
|
570
|
212 ngx_str_set(&h->key, "Content-Encoding");
|
|
213 ngx_str_set(&h->value, "gzip");
|
|
214 r->headers_out.content_encoding = h;
|
358
|
215
|
494
|
216 r->ignore_content_encoding = 1;
|
358
|
217
|
|
218 /* we need to allocate all before the header would be sent */
|
|
219
|
|
220 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
|
|
221 if (b == NULL) {
|
|
222 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
223 }
|
|
224
|
|
225 b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
|
|
226 if (b->file == NULL) {
|
|
227 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
228 }
|
|
229
|
|
230 rc = ngx_http_send_header(r);
|
|
231
|
|
232 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
|
|
233 return rc;
|
|
234 }
|
|
235
|
|
236 b->file_pos = 0;
|
|
237 b->file_last = of.size;
|
|
238
|
|
239 b->in_file = b->file_last ? 1 : 0;
|
|
240 b->last_buf = 1;
|
|
241 b->last_in_chain = 1;
|
|
242
|
|
243 b->file->fd = of.fd;
|
|
244 b->file->name = path;
|
|
245 b->file->log = log;
|
406
|
246 b->file->directio = of.is_directio;
|
358
|
247
|
|
248 out.buf = b;
|
|
249 out.next = NULL;
|
|
250
|
|
251 return ngx_http_output_filter(r, &out);
|
|
252 }
|
|
253
|
|
254
|
|
255 static void *
|
|
256 ngx_http_gzip_static_create_conf(ngx_conf_t *cf)
|
|
257 {
|
|
258 ngx_http_gzip_static_conf_t *conf;
|
|
259
|
|
260 conf = ngx_palloc(cf->pool, sizeof(ngx_http_gzip_static_conf_t));
|
|
261 if (conf == NULL) {
|
496
|
262 return NULL;
|
358
|
263 }
|
|
264
|
|
265 conf->enable = NGX_CONF_UNSET;
|
|
266
|
|
267 return conf;
|
|
268 }
|
|
269
|
|
270
|
|
271 static char *
|
|
272 ngx_http_gzip_static_merge_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
273 {
|
|
274 ngx_http_gzip_static_conf_t *prev = parent;
|
|
275 ngx_http_gzip_static_conf_t *conf = child;
|
|
276
|
|
277 ngx_conf_merge_value(conf->enable, prev->enable, 0);
|
|
278
|
|
279 return NGX_CONF_OK;
|
|
280 }
|
|
281
|
|
282
|
|
283 static ngx_int_t
|
|
284 ngx_http_gzip_static_init(ngx_conf_t *cf)
|
|
285 {
|
|
286 ngx_http_handler_pt *h;
|
|
287 ngx_http_core_main_conf_t *cmcf;
|
|
288
|
|
289 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
|
290
|
|
291 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
|
|
292 if (h == NULL) {
|
|
293 return NGX_ERROR;
|
|
294 }
|
|
295
|
|
296 *h = ngx_http_gzip_static_handler;
|
|
297
|
|
298 return NGX_OK;
|
|
299 }
|