upstream nginx-0.7.31
[nginx.git] / nginx / src / http / modules / ngx_http_gzip_static_module.c
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     /* TODO: Win32 */
93     if (r->zero_in_uri) {
94         return NGX_DECLINED;
95     }
96
97     gzcf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_static_module);
98
99     if (!gzcf->enable || ngx_http_gzip_ok(r) != NGX_OK) {
100         return NGX_DECLINED;
101     }
102
103     log = r->connection->log;
104
105     p = ngx_http_map_uri_to_path(r, &path, &root, sizeof(".gz") - 1);
106     if (p == NULL) {
107         return NGX_HTTP_INTERNAL_SERVER_ERROR;
108     }
109
110     *p++ = '.';
111     *p++ = 'g';
112     *p++ = 'z';
113     *p = '\0';
114
115     path.len = p - path.data;
116
117     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
118                    "http filename: \"%s\"", path.data);
119
120     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
121
122     ngx_memzero(&of, sizeof(ngx_open_file_info_t));
123
124     of.directio = clcf->directio;
125     of.valid = clcf->open_file_cache_valid;
126     of.min_uses = clcf->open_file_cache_min_uses;
127     of.errors = clcf->open_file_cache_errors;
128     of.events = clcf->open_file_cache_events;
129
130     if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
131         != NGX_OK)
132     {
133         switch (of.err) {
134
135         case 0:
136             return NGX_HTTP_INTERNAL_SERVER_ERROR;
137
138         case NGX_ENOENT:
139         case NGX_ENOTDIR:
140         case NGX_ENAMETOOLONG:
141
142             return NGX_DECLINED;
143
144         case NGX_EACCES:
145
146             level = NGX_LOG_ERR;
147             break;
148
149         default:
150
151             level = NGX_LOG_CRIT;
152             break;
153         }
154
155         ngx_log_error(level, log, of.err,
156                       ngx_open_file_n " \"%s\" failed", path.data);
157
158         return NGX_DECLINED;
159     }
160
161     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "http static fd: %d", of.fd);
162
163     if (of.is_dir) {
164         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http dir");
165         return NGX_DECLINED;
166     }
167
168 #if !(NGX_WIN32) /* the not regular files are probably Unix specific */
169
170     if (!of.is_file) {
171         ngx_log_error(NGX_LOG_CRIT, log, ngx_errno,
172                       "\"%s\" is not a regular file", path.data);
173
174         return NGX_HTTP_NOT_FOUND;
175     }
176
177 #endif
178
179     r->root_tested = !r->error_page;
180
181     rc = ngx_http_discard_request_body(r);
182
183     if (rc != NGX_OK) {
184         return rc;
185     }
186
187     log->action = "sending response to client";
188
189     r->headers_out.status = NGX_HTTP_OK;
190     r->headers_out.content_length_n = of.size;
191     r->headers_out.last_modified_time = of.mtime;
192
193     if (ngx_http_set_content_type(r) != NGX_OK) {
194         return NGX_HTTP_INTERNAL_SERVER_ERROR;
195     }
196
197     h = ngx_list_push(&r->headers_out.headers);
198     if (h == NULL) {
199         return NGX_ERROR;
200     }
201
202     h->hash = 1;
203     h->key.len = sizeof("Content-Encoding") - 1;
204     h->key.data = (u_char *) "Content-Encoding";
205     h->value.len = sizeof("gzip") - 1;
206     h->value.data = (u_char *) "gzip";
207
208     r->headers_out.content_encoding = h;
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 }