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