comparison src/http/modules/ngx_http_static_module.c @ 1454:f497ed7682a7

open_file_cache in HTTP
author Igor Sysoev <igor@sysoev.ru>
date Sat, 01 Sep 2007 12:12:48 +0000
parents aabbf66b61ea
children 223e92651ca5
comparison
equal deleted inserted replaced
1453:f2feed5bffe1 1454:f497ed7682a7
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
130 104 rc = ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool);
131 if (fd == NGX_INVALID_FILE) { 105
132 err = ngx_errno; 106 if (rc == NGX_ERROR) {
133 107
134 if (err == NGX_ENOENT 108 switch (of.err) {
135 || err == NGX_ENOTDIR 109
136 || err == NGX_ENAMETOOLONG) 110 case 0:
137 { 111 return NGX_HTTP_INTERNAL_SERVER_ERROR;
112
113 case NGX_ENOENT:
114 case NGX_ENOTDIR:
115 case NGX_ENAMETOOLONG:
116
138 level = NGX_LOG_ERR; 117 level = NGX_LOG_ERR;
139 rc = NGX_HTTP_NOT_FOUND; 118 rc = NGX_HTTP_NOT_FOUND;
140 119 break;
141 } else if (err == NGX_EACCES) { 120
121 case NGX_EACCES:
122
142 level = NGX_LOG_ERR; 123 level = NGX_LOG_ERR;
143 rc = NGX_HTTP_FORBIDDEN; 124 rc = NGX_HTTP_FORBIDDEN;
144 125 break;
145 } else { 126
127 default:
128
146 level = NGX_LOG_CRIT; 129 level = NGX_LOG_CRIT;
147 rc = NGX_HTTP_INTERNAL_SERVER_ERROR; 130 rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
148 } 131 break;
149 132 }
150 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
151 133
152 if (rc != NGX_HTTP_NOT_FOUND || clcf->log_not_found) { 134 if (rc != NGX_HTTP_NOT_FOUND || clcf->log_not_found) {
153 ngx_log_error(level, log, err, 135 ngx_log_error(level, log, of.err,
154 ngx_open_file_n " \"%s\" failed", path.data); 136 ngx_open_file_n " \"%s\" failed", path.data);
155 } 137 }
156 138
157 return rc; 139 return rc;
158 } 140 }
159 141
142 fd = of.fd;
143
160 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "http static fd: %d", fd); 144 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "http static fd: %d", fd);
161 145
162 if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) { 146 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 147
176 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http dir"); 148 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 149
183 r->headers_out.location = ngx_palloc(r->pool, sizeof(ngx_table_elt_t)); 150 r->headers_out.location = ngx_palloc(r->pool, sizeof(ngx_table_elt_t));
184 if (r->headers_out.location == NULL) { 151 if (r->headers_out.location == NULL) {
185 return NGX_HTTP_INTERNAL_SERVER_ERROR; 152 return NGX_HTTP_INTERNAL_SERVER_ERROR;
186 } 153 }
187
188 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
189 154
190 if (!clcf->alias && clcf->root_lengths == NULL) { 155 if (!clcf->alias && clcf->root_lengths == NULL) {
191 location = path.data + clcf->root.len; 156 location = path.data + clcf->root.len;
192 157
193 } else { 158 } else {
212 return NGX_HTTP_MOVED_PERMANENTLY; 177 return NGX_HTTP_MOVED_PERMANENTLY;
213 } 178 }
214 179
215 #if !(NGX_WIN32) /* the not regular files are probably Unix specific */ 180 #if !(NGX_WIN32) /* the not regular files are probably Unix specific */
216 181
217 if (!ngx_is_file(&fi)) { 182 if (!of.is_file) {
218 ngx_log_error(NGX_LOG_CRIT, log, ngx_errno, 183 ngx_log_error(NGX_LOG_CRIT, log, ngx_errno,
219 "\"%s\" is not a regular file", path.data); 184 "\"%s\" is not a regular file", path.data);
220 185
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; 186 return NGX_HTTP_NOT_FOUND;
227 } 187 }
228 188
229 #endif 189 #endif
230 190
231 log->action = "sending response to client"; 191 log->action = "sending response to client";
232 192
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; 193 r->headers_out.status = NGX_HTTP_OK;
241 r->headers_out.content_length_n = ngx_file_size(&fi); 194 r->headers_out.content_length_n = of.size;
242 r->headers_out.last_modified_time = ngx_file_mtime(&fi); 195 r->headers_out.last_modified_time = of.mtime;
243 196
244 if (ngx_http_set_content_type(r) != NGX_OK) { 197 if (ngx_http_set_content_type(r) != NGX_OK) {
245 return NGX_HTTP_INTERNAL_SERVER_ERROR; 198 return NGX_HTTP_INTERNAL_SERVER_ERROR;
246 } 199 }
247 200
248 if (r != r->main && ngx_file_size(&fi) == 0) { 201 if (r != r->main && of.size == 0) {
249 return ngx_http_send_header(r); 202 return ngx_http_send_header(r);
250 } 203 }
251 204
252 r->allow_ranges = 1; 205 r->allow_ranges = 1;
253 206
268 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { 221 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
269 return rc; 222 return rc;
270 } 223 }
271 224
272 b->file_pos = 0; 225 b->file_pos = 0;
273 b->file_last = ngx_file_size(&fi); 226 b->file_last = of.size;
274 227
275 b->in_file = b->file_last ? 1: 0; 228 b->in_file = b->file_last ? 1: 0;
276 b->last_buf = (r == r->main) ? 1: 0; 229 b->last_buf = (r == r->main) ? 1: 0;
277 b->last_in_chain = 1; 230 b->last_in_chain = 1;
278 231
282 235
283 out.buf = b; 236 out.buf = b;
284 out.next = NULL; 237 out.next = NULL;
285 238
286 return ngx_http_output_filter(r, &out); 239 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 } 240 }
318 241
319 242
320 static ngx_int_t 243 static ngx_int_t
321 ngx_http_static_init(ngx_conf_t *cf) 244 ngx_http_static_init(ngx_conf_t *cf)