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_DECLINED;
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
98 if (!gzcf->enable || ngx_http_gzip_ok(r) != NGX_OK) {
99 return NGX_DECLINED;
100 }
101
102 log = r->connection->log;
103
104 p = ngx_http_map_uri_to_path(r, &path, &root, sizeof(".gz") - 1);
105 if (p == NULL) {
106 return NGX_HTTP_INTERNAL_SERVER_ERROR;
107 }
108
109 *p++ = '.';
110 *p++ = 'g';
111 *p++ = 'z';
112 *p = '\0';
113
114 path.len = p - path.data;
115
116 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
117 "http filename: \"%s\"", path.data);
118
119 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
120
121 ngx_memzero(&of, sizeof(ngx_open_file_info_t));
122
123 of.directio = clcf->directio;
124 of.valid = clcf->open_file_cache_valid;
125 of.min_uses = clcf->open_file_cache_min_uses;
126 of.errors = clcf->open_file_cache_errors;
127 of.events = clcf->open_file_cache_events;
128
129 if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
130 != NGX_OK)
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 break;
147
148 default:
149
150 level = NGX_LOG_CRIT;
151 break;
152 }
153
154 ngx_log_error(level, log, of.err,
155 "%s \"%s\" failed", of.failed, path.data);
156
157 return NGX_DECLINED;
158 }
159
160 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "http static fd: %d", of.fd);
161
162 if (of.is_dir) {
163 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http dir");
164 return NGX_DECLINED;
165 }
166
167 #if !(NGX_WIN32) /* the not regular files are probably Unix specific */
168
169 if (!of.is_file) {
170 ngx_log_error(NGX_LOG_CRIT, log, ngx_errno,
171 "\"%s\" is not a regular file", path.data);
172
173 return NGX_HTTP_NOT_FOUND;
174 }
175
176 #endif
177
178 r->root_tested = !r->error_page;
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 r->ignore_content_encoding = 1;
209
210 /* we need to allocate all before the header would be sent */
211
212 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
213 if (b == NULL) {
214 return NGX_HTTP_INTERNAL_SERVER_ERROR;
215 }
216
217 b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
218 if (b->file == NULL) {
219 return NGX_HTTP_INTERNAL_SERVER_ERROR;
220 }
221
222 rc = ngx_http_send_header(r);
223
224 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
225 return rc;
226 }
227
228 b->file_pos = 0;
229 b->file_last = of.size;
230
231 b->in_file = b->file_last ? 1 : 0;
232 b->last_buf = 1;
233 b->last_in_chain = 1;
234
235 b->file->fd = of.fd;
236 b->file->name = path;
237 b->file->log = log;
238 b->file->directio = of.is_directio;
239
240 out.buf = b;
241 out.next = NULL;
242
243 return ngx_http_output_filter(r, &out);
244 }
245
246
247 static void *
248 ngx_http_gzip_static_create_conf(ngx_conf_t *cf)
249 {
250 ngx_http_gzip_static_conf_t *conf;
251
252 conf = ngx_palloc(cf->pool, sizeof(ngx_http_gzip_static_conf_t));
253 if (conf == NULL) {
254 return NGX_CONF_ERROR;
255 }
256
257 conf->enable = NGX_CONF_UNSET;
258
259 return conf;
260 }
261
262
263 static char *
264 ngx_http_gzip_static_merge_conf(ngx_conf_t *cf, void *parent, void *child)
265 {
266 ngx_http_gzip_static_conf_t *prev = parent;
267 ngx_http_gzip_static_conf_t *conf = child;
268
269 ngx_conf_merge_value(conf->enable, prev->enable, 0);
270
271 return NGX_CONF_OK;
272 }
273
274
275 static ngx_int_t
276 ngx_http_gzip_static_init(ngx_conf_t *cf)
277 {
278 ngx_http_handler_pt *h;
279 ngx_http_core_main_conf_t *cmcf;
280
281 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
282
283 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
284 if (h == NULL) {
285 return NGX_ERROR;
286 }
287
288 *h = ngx_http_gzip_static_handler;
289
290 return NGX_OK;
291 }
292
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.