Mercurial > hg > nginx-ranges
comparison src/http/modules/ngx_http_static_module.c @ 332:3a91bfeffaba NGINX_0_6_10
nginx 0.6.10
*) Feature: the "open_file_cache", "open_file_cache_retest", and
"open_file_cache_errors" directives.
*) Bugfix: socket leak; bug appeared in 0.6.7.
*) Bugfix: a charset set by the "charset" directive was not appended to
the "Content-Type" header set by $r->send_http_header().
*) Bugfix: a segmentation fault might occur in worker process if
/dev/poll method was used.
author | Igor Sysoev <http://sysoev.ru> |
---|---|
date | Mon, 03 Sep 2007 00:00:00 +0400 |
parents | 9fc4ab6673f9 |
children | b743d290eb3b |
comparison
equal
deleted
inserted
replaced
331:b69d5e83bf82 | 332:3a91bfeffaba |
---|---|
7 #include <ngx_config.h> | 7 #include <ngx_config.h> |
8 #include <ngx_core.h> | 8 #include <ngx_core.h> |
9 #include <ngx_http.h> | 9 #include <ngx_http.h> |
10 | 10 |
11 | 11 |
12 typedef struct { | |
13 ngx_http_cache_hash_t *redirect_cache; | |
14 } ngx_http_static_loc_conf_t; | |
15 | |
16 | |
17 static ngx_int_t ngx_http_static_handler(ngx_http_request_t *r); | 12 static ngx_int_t ngx_http_static_handler(ngx_http_request_t *r); |
18 static void *ngx_http_static_create_loc_conf(ngx_conf_t *cf); | |
19 static char *ngx_http_static_merge_loc_conf(ngx_conf_t *cf, | |
20 void *parent, void *child); | |
21 static ngx_int_t ngx_http_static_init(ngx_conf_t *cf); | 13 static ngx_int_t ngx_http_static_init(ngx_conf_t *cf); |
22 | |
23 | |
24 static ngx_command_t ngx_http_static_commands[] = { | |
25 | |
26 #if (NGX_HTTP_CACHE) | |
27 | |
28 { ngx_string("redirect_cache"), | |
29 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE3, | |
30 ngx_http_set_cache_slot, | |
31 NGX_HTTP_LOC_CONF_OFFSET, | |
32 offsetof(ngx_http_static_loc_conf_t, redirect_cache), | |
33 NULL }, | |
34 | |
35 #endif | |
36 | |
37 ngx_null_command | |
38 }; | |
39 | 14 |
40 | 15 |
41 ngx_http_module_t ngx_http_static_module_ctx = { | 16 ngx_http_module_t ngx_http_static_module_ctx = { |
42 NULL, /* preconfiguration */ | 17 NULL, /* preconfiguration */ |
43 ngx_http_static_init, /* postconfiguration */ | 18 ngx_http_static_init, /* postconfiguration */ |
46 NULL, /* init main configuration */ | 21 NULL, /* init main configuration */ |
47 | 22 |
48 NULL, /* create server configuration */ | 23 NULL, /* create server configuration */ |
49 NULL, /* merge server configuration */ | 24 NULL, /* merge server configuration */ |
50 | 25 |
51 ngx_http_static_create_loc_conf, /* create location configuration */ | 26 NULL, /* create location configuration */ |
52 ngx_http_static_merge_loc_conf /* merge location configuration */ | 27 NULL /* merge location configuration */ |
53 }; | 28 }; |
54 | 29 |
55 | 30 |
56 ngx_module_t ngx_http_static_module = { | 31 ngx_module_t ngx_http_static_module = { |
57 NGX_MODULE_V1, | 32 NGX_MODULE_V1, |
58 &ngx_http_static_module_ctx, /* module context */ | 33 &ngx_http_static_module_ctx, /* module context */ |
59 ngx_http_static_commands, /* module directives */ | 34 NULL, /* module directives */ |
60 NGX_HTTP_MODULE, /* module type */ | 35 NGX_HTTP_MODULE, /* module type */ |
61 NULL, /* init master */ | 36 NULL, /* init master */ |
62 NULL, /* init module */ | 37 NULL, /* init module */ |
63 NULL, /* init process */ | 38 NULL, /* init process */ |
64 NULL, /* init thread */ | 39 NULL, /* init thread */ |
73 ngx_http_static_handler(ngx_http_request_t *r) | 48 ngx_http_static_handler(ngx_http_request_t *r) |
74 { | 49 { |
75 u_char *last, *location; | 50 u_char *last, *location; |
76 size_t root; | 51 size_t root; |
77 ngx_fd_t fd; | 52 ngx_fd_t fd; |
53 ngx_str_t path; | |
78 ngx_int_t rc; | 54 ngx_int_t rc; |
79 ngx_uint_t level; | 55 ngx_uint_t level; |
80 ngx_str_t path; | |
81 ngx_err_t err; | |
82 ngx_log_t *log; | 56 ngx_log_t *log; |
83 ngx_buf_t *b; | 57 ngx_buf_t *b; |
84 ngx_chain_t out; | 58 ngx_chain_t out; |
85 ngx_file_info_t fi; | 59 ngx_open_file_info_t of; |
86 ngx_pool_cleanup_t *cln; | |
87 ngx_pool_cleanup_file_t *clnf; | |
88 ngx_http_core_loc_conf_t *clcf; | 60 ngx_http_core_loc_conf_t *clcf; |
89 | 61 |
90 if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) { | 62 if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) { |
91 return NGX_HTTP_NOT_ALLOWED; | 63 return NGX_HTTP_NOT_ALLOWED; |
92 } | 64 } |
116 last = ngx_http_map_uri_to_path(r, &path, &root, 0); | 88 last = ngx_http_map_uri_to_path(r, &path, &root, 0); |
117 if (last == NULL) { | 89 if (last == NULL) { |
118 return NGX_HTTP_INTERNAL_SERVER_ERROR; | 90 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
119 } | 91 } |
120 | 92 |
93 path.len = last - path.data; | |
94 | |
121 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, | 95 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, |
122 "http filename: \"%s\"", path.data); | 96 "http filename: \"%s\"", path.data); |
123 | 97 |
124 cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_pool_cleanup_file_t)); | 98 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); |
125 if (cln == NULL) { | 99 |
126 return NGX_HTTP_INTERNAL_SERVER_ERROR; | 100 of.test_dir = 0; |
127 } | 101 of.retest = clcf->open_file_cache_retest; |
128 | 102 of.errors = clcf->open_file_cache_errors; |
129 fd = ngx_open_file(path.data, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0); | 103 of.events = clcf->open_file_cache_events; |
130 | 104 |
131 if (fd == NGX_INVALID_FILE) { | 105 rc = ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool); |
132 err = ngx_errno; | 106 |
133 | 107 if (rc == NGX_ERROR) { |
134 if (err == NGX_ENOENT | 108 |
135 || err == NGX_ENOTDIR | 109 switch (of.err) { |
136 || err == NGX_ENAMETOOLONG) | 110 |
137 { | 111 case 0: |
112 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
113 | |
114 case NGX_ENOENT: | |
115 case NGX_ENOTDIR: | |
116 case NGX_ENAMETOOLONG: | |
117 | |
138 level = NGX_LOG_ERR; | 118 level = NGX_LOG_ERR; |
139 rc = NGX_HTTP_NOT_FOUND; | 119 rc = NGX_HTTP_NOT_FOUND; |
140 | 120 break; |
141 } else if (err == NGX_EACCES) { | 121 |
122 case NGX_EACCES: | |
123 | |
142 level = NGX_LOG_ERR; | 124 level = NGX_LOG_ERR; |
143 rc = NGX_HTTP_FORBIDDEN; | 125 rc = NGX_HTTP_FORBIDDEN; |
144 | 126 break; |
145 } else { | 127 |
128 default: | |
129 | |
146 level = NGX_LOG_CRIT; | 130 level = NGX_LOG_CRIT; |
147 rc = NGX_HTTP_INTERNAL_SERVER_ERROR; | 131 rc = NGX_HTTP_INTERNAL_SERVER_ERROR; |
148 } | 132 break; |
149 | 133 } |
150 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); | |
151 | 134 |
152 if (rc != NGX_HTTP_NOT_FOUND || clcf->log_not_found) { | 135 if (rc != NGX_HTTP_NOT_FOUND || clcf->log_not_found) { |
153 ngx_log_error(level, log, err, | 136 ngx_log_error(level, log, of.err, |
154 ngx_open_file_n " \"%s\" failed", path.data); | 137 ngx_open_file_n " \"%s\" failed", path.data); |
155 } | 138 } |
156 | 139 |
157 return rc; | 140 return rc; |
158 } | 141 } |
159 | 142 |
143 fd = of.fd; | |
144 | |
160 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "http static fd: %d", fd); | 145 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "http static fd: %d", fd); |
161 | 146 |
162 if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) { | 147 if (of.is_dir) { |
163 ngx_log_error(NGX_LOG_CRIT, log, ngx_errno, | |
164 ngx_fd_info_n " \"%s\" failed", path.data); | |
165 | |
166 if (ngx_close_file(fd) == NGX_FILE_ERROR) { | |
167 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, | |
168 ngx_close_file_n " \"%s\" failed", path.data); | |
169 } | |
170 | |
171 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
172 } | |
173 | |
174 if (ngx_is_dir(&fi)) { | |
175 | 148 |
176 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http dir"); | 149 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http dir"); |
177 | |
178 if (ngx_close_file(fd) == NGX_FILE_ERROR) { | |
179 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, | |
180 ngx_close_file_n " \"%s\" failed", path.data); | |
181 } | |
182 | 150 |
183 r->headers_out.location = ngx_palloc(r->pool, sizeof(ngx_table_elt_t)); | 151 r->headers_out.location = ngx_palloc(r->pool, sizeof(ngx_table_elt_t)); |
184 if (r->headers_out.location == NULL) { | 152 if (r->headers_out.location == NULL) { |
185 return NGX_HTTP_INTERNAL_SERVER_ERROR; | 153 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
186 } | 154 } |
187 | |
188 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); | |
189 | 155 |
190 if (!clcf->alias && clcf->root_lengths == NULL) { | 156 if (!clcf->alias && clcf->root_lengths == NULL) { |
191 location = path.data + clcf->root.len; | 157 location = path.data + clcf->root.len; |
192 | 158 |
193 } else { | 159 } else { |
212 return NGX_HTTP_MOVED_PERMANENTLY; | 178 return NGX_HTTP_MOVED_PERMANENTLY; |
213 } | 179 } |
214 | 180 |
215 #if !(NGX_WIN32) /* the not regular files are probably Unix specific */ | 181 #if !(NGX_WIN32) /* the not regular files are probably Unix specific */ |
216 | 182 |
217 if (!ngx_is_file(&fi)) { | 183 if (!of.is_file) { |
218 ngx_log_error(NGX_LOG_CRIT, log, ngx_errno, | 184 ngx_log_error(NGX_LOG_CRIT, log, ngx_errno, |
219 "\"%s\" is not a regular file", path.data); | 185 "\"%s\" is not a regular file", path.data); |
220 | 186 |
221 if (ngx_close_file(fd) == NGX_FILE_ERROR) { | |
222 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, | |
223 ngx_close_file_n " \"%s\" failed", path.data); | |
224 } | |
225 | |
226 return NGX_HTTP_NOT_FOUND; | 187 return NGX_HTTP_NOT_FOUND; |
227 } | 188 } |
228 | 189 |
229 #endif | 190 #endif |
230 | 191 |
231 log->action = "sending response to client"; | 192 log->action = "sending response to client"; |
232 | 193 |
233 cln->handler = ngx_pool_cleanup_file; | |
234 clnf = cln->data; | |
235 | |
236 clnf->fd = fd; | |
237 clnf->name = path.data; | |
238 clnf->log = r->pool->log; | |
239 | |
240 r->headers_out.status = NGX_HTTP_OK; | 194 r->headers_out.status = NGX_HTTP_OK; |
241 r->headers_out.content_length_n = ngx_file_size(&fi); | 195 r->headers_out.content_length_n = of.size; |
242 r->headers_out.last_modified_time = ngx_file_mtime(&fi); | 196 r->headers_out.last_modified_time = of.mtime; |
243 | 197 |
244 if (ngx_http_set_content_type(r) != NGX_OK) { | 198 if (ngx_http_set_content_type(r) != NGX_OK) { |
245 return NGX_HTTP_INTERNAL_SERVER_ERROR; | 199 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
246 } | 200 } |
247 | 201 |
248 if (r != r->main && ngx_file_size(&fi) == 0) { | 202 if (r != r->main && of.size == 0) { |
249 return ngx_http_send_header(r); | 203 return ngx_http_send_header(r); |
250 } | 204 } |
251 | 205 |
252 r->allow_ranges = 1; | 206 r->allow_ranges = 1; |
253 | 207 |
268 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { | 222 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { |
269 return rc; | 223 return rc; |
270 } | 224 } |
271 | 225 |
272 b->file_pos = 0; | 226 b->file_pos = 0; |
273 b->file_last = ngx_file_size(&fi); | 227 b->file_last = of.size; |
274 | 228 |
275 b->in_file = b->file_last ? 1: 0; | 229 b->in_file = b->file_last ? 1: 0; |
276 b->last_buf = (r == r->main) ? 1: 0; | 230 b->last_buf = (r == r->main) ? 1: 0; |
277 b->last_in_chain = 1; | 231 b->last_in_chain = 1; |
278 | 232 |
282 | 236 |
283 out.buf = b; | 237 out.buf = b; |
284 out.next = NULL; | 238 out.next = NULL; |
285 | 239 |
286 return ngx_http_output_filter(r, &out); | 240 return ngx_http_output_filter(r, &out); |
287 } | |
288 | |
289 | |
290 static void * | |
291 ngx_http_static_create_loc_conf(ngx_conf_t *cf) | |
292 { | |
293 ngx_http_static_loc_conf_t *conf; | |
294 | |
295 conf = ngx_palloc(cf->pool, sizeof(ngx_http_static_loc_conf_t)); | |
296 if (conf == NULL) { | |
297 return NGX_CONF_ERROR; | |
298 } | |
299 | |
300 conf->redirect_cache = NULL; | |
301 | |
302 return conf; | |
303 } | |
304 | |
305 | |
306 static char * | |
307 ngx_http_static_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | |
308 { | |
309 ngx_http_static_loc_conf_t *prev = parent; | |
310 ngx_http_static_loc_conf_t *conf = child; | |
311 | |
312 if (conf->redirect_cache == NULL) { | |
313 conf->redirect_cache = prev->redirect_cache; | |
314 } | |
315 | |
316 return NGX_CONF_OK; | |
317 } | 241 } |
318 | 242 |
319 | 243 |
320 static ngx_int_t | 244 static ngx_int_t |
321 ngx_http_static_init(ngx_conf_t *cf) | 245 ngx_http_static_init(ngx_conf_t *cf) |