upstream nginx-0.7.31
[nginx.git] / nginx / src / http / modules / ngx_http_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 static ngx_int_t ngx_http_static_handler(ngx_http_request_t *r);
13 static ngx_int_t ngx_http_static_init(ngx_conf_t *cf);
14
15
16 ngx_http_module_t  ngx_http_static_module_ctx = {
17     NULL,                                  /* preconfiguration */
18     ngx_http_static_init,                  /* postconfiguration */
19
20     NULL,                                  /* create main configuration */
21     NULL,                                  /* init main configuration */
22
23     NULL,                                  /* create server configuration */
24     NULL,                                  /* merge server configuration */
25
26     NULL,                                  /* create location configuration */
27     NULL                                   /* merge location configuration */
28 };
29
30
31 ngx_module_t  ngx_http_static_module = {
32     NGX_MODULE_V1,
33     &ngx_http_static_module_ctx,           /* module context */
34     NULL,                                  /* module directives */
35     NGX_HTTP_MODULE,                       /* module type */
36     NULL,                                  /* init master */
37     NULL,                                  /* init module */
38     NULL,                                  /* init process */
39     NULL,                                  /* init thread */
40     NULL,                                  /* exit thread */
41     NULL,                                  /* exit process */
42     NULL,                                  /* exit master */
43     NGX_MODULE_V1_PADDING
44 };
45
46
47 static ngx_int_t
48 ngx_http_static_handler(ngx_http_request_t *r)
49 {
50     u_char                    *last, *location;
51     size_t                     root, len;
52     ngx_str_t                  path;
53     ngx_int_t                  rc;
54     ngx_uint_t                 level;
55     ngx_log_t                 *log;
56     ngx_buf_t                 *b;
57     ngx_chain_t                out;
58     ngx_open_file_info_t       of;
59     ngx_http_core_loc_conf_t  *clcf;
60
61     if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD|NGX_HTTP_POST))) {
62         return NGX_HTTP_NOT_ALLOWED;
63     }
64
65     if (r->uri.data[r->uri.len - 1] == '/') {
66         return NGX_DECLINED;
67     }
68
69     /* TODO: Win32 */
70     if (r->zero_in_uri) {
71         return NGX_DECLINED;
72     }
73
74     log = r->connection->log;
75
76     /*
77      * ngx_http_map_uri_to_path() allocates memory for terminating '\0'
78      * so we do not need to reserve memory for '/' for possible redirect
79      */
80
81     last = ngx_http_map_uri_to_path(r, &path, &root, 0);
82     if (last == NULL) {
83         return NGX_HTTP_INTERNAL_SERVER_ERROR;
84     }
85
86     path.len = last - path.data;
87
88     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
89                    "http filename: \"%s\"", path.data);
90
91     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
92
93     ngx_memzero(&of, sizeof(ngx_open_file_info_t));
94
95     of.directio = clcf->directio;
96     of.valid = clcf->open_file_cache_valid;
97     of.min_uses = clcf->open_file_cache_min_uses;
98     of.errors = clcf->open_file_cache_errors;
99     of.events = clcf->open_file_cache_events;
100
101     if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
102         != NGX_OK)
103     {
104         switch (of.err) {
105
106         case 0:
107             return NGX_HTTP_INTERNAL_SERVER_ERROR;
108
109         case NGX_ENOENT:
110         case NGX_ENOTDIR:
111         case NGX_ENAMETOOLONG:
112
113             level = NGX_LOG_ERR;
114             rc = NGX_HTTP_NOT_FOUND;
115             break;
116
117         case NGX_EACCES:
118
119             level = NGX_LOG_ERR;
120             rc = NGX_HTTP_FORBIDDEN;
121             break;
122
123         default:
124
125             level = NGX_LOG_CRIT;
126             rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
127             break;
128         }
129
130         if (rc != NGX_HTTP_NOT_FOUND || clcf->log_not_found) {
131             ngx_log_error(level, log, of.err,
132                           ngx_open_file_n " \"%s\" failed", path.data);
133         }
134
135         return rc;
136     }
137
138     r->root_tested = !r->error_page;
139
140     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "http static fd: %d", of.fd);
141
142     if (of.is_dir) {
143
144         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http dir");
145
146         r->headers_out.location = ngx_palloc(r->pool, sizeof(ngx_table_elt_t));
147         if (r->headers_out.location == NULL) {
148             return NGX_HTTP_INTERNAL_SERVER_ERROR;
149         }
150
151         len = r->uri.len + 1;
152
153         if (!clcf->alias && clcf->root_lengths == NULL && r->args.len == 0) {
154             location = path.data + clcf->root.len;
155
156             *last = '/';
157
158         } else {
159             if (r->args.len) {
160                 len += r->args.len + 1;
161             }
162
163             location = ngx_pnalloc(r->pool, len);
164             if (location == NULL) {
165                 return NGX_HTTP_INTERNAL_SERVER_ERROR;
166             }
167
168             last = ngx_copy(location, r->uri.data, r->uri.len);
169
170             *last = '/';
171
172             if (r->args.len) {
173                 *++last = '?';
174                 ngx_memcpy(++last, r->args.data, r->args.len);
175             }
176         }
177
178         /*
179          * we do not need to set the r->headers_out.location->hash and
180          * r->headers_out.location->key fields
181          */
182
183         r->headers_out.location->value.len = len;
184         r->headers_out.location->value.data = location;
185
186         return NGX_HTTP_MOVED_PERMANENTLY;
187     }
188
189 #if !(NGX_WIN32) /* the not regular files are probably Unix specific */
190
191     if (!of.is_file) {
192         ngx_log_error(NGX_LOG_CRIT, log, ngx_errno,
193                       "\"%s\" is not a regular file", path.data);
194
195         return NGX_HTTP_NOT_FOUND;
196     }
197
198 #endif
199
200     if (r->method & NGX_HTTP_POST) {
201         return NGX_HTTP_NOT_ALLOWED;
202     }
203
204     rc = ngx_http_discard_request_body(r);
205
206     if (rc != NGX_OK) {
207         return rc;
208     }
209
210     log->action = "sending response to client";
211
212     r->headers_out.status = NGX_HTTP_OK;
213     r->headers_out.content_length_n = of.size;
214     r->headers_out.last_modified_time = of.mtime;
215
216     if (ngx_http_set_content_type(r) != NGX_OK) {
217         return NGX_HTTP_INTERNAL_SERVER_ERROR;
218     }
219
220     if (r != r->main && of.size == 0) {
221         return ngx_http_send_header(r);
222     }
223
224     r->allow_ranges = 1;
225
226     /* we need to allocate all before the header would be sent */
227
228     b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
229     if (b == NULL) {
230         return NGX_HTTP_INTERNAL_SERVER_ERROR;
231     }
232
233     b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
234     if (b->file == NULL) {
235         return NGX_HTTP_INTERNAL_SERVER_ERROR;
236     }
237
238     rc = ngx_http_send_header(r);
239
240     if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
241         return rc;
242     }
243
244     b->file_pos = 0;
245     b->file_last = of.size;
246
247     b->in_file = b->file_last ? 1: 0;
248     b->last_buf = (r == r->main) ? 1: 0;
249     b->last_in_chain = 1;
250
251     b->file->fd = of.fd;
252     b->file->name = path;
253     b->file->log = log;
254     b->file->directio = of.is_directio;
255
256     out.buf = b;
257     out.next = NULL;
258
259     return ngx_http_output_filter(r, &out);
260 }
261
262
263 static ngx_int_t
264 ngx_http_static_init(ngx_conf_t *cf)
265 {
266     ngx_http_handler_pt        *h;
267     ngx_http_core_main_conf_t  *cmcf;
268
269     cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
270
271     h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
272     if (h == NULL) {
273         return NGX_ERROR;
274     }
275
276     *h = ngx_http_static_handler;
277
278     return NGX_OK;
279 }