comparison src/http/modules/ngx_http_index_module.c @ 50:72eb30262aac NGINX_0_1_25

nginx 0.1.25 *) Bugfix: nginx did run on Linux parisc. *) Feature: nginx now does not start under FreeBSD if the sysctl kern.ipc.somaxconn value is too big. *) Bugfix: if a request was internally redirected by the ngx_http_index_module module to the ngx_http_proxy_module or ngx_http_fastcgi_module modules, then the index file was not closed after request completion. *) Feature: the "proxy_pass" can be used in location with regular expression. *) Feature: the ngx_http_rewrite_filter_module module supports the condition like "if ($HTTP_USER_AGENT ~ MSIE)". *) Bugfix: nginx started too slow if the large number of addresses and text values were used in the "geo" directive. *) Change: a variable name must be declared as "$name" in the "geo" directive. The previous variant without "$" is still supported, but will be removed soon. *) Feature: the "%{VARIABLE}v" logging parameter. *) Feature: the "set $name value" directive. *) Bugfix: gcc 4.0 compatibility. *) Feature: the --with-openssl-opt=OPTIONS autoconfiguration directive.
author Igor Sysoev <http://sysoev.ru>
date Sat, 19 Mar 2005 00:00:00 +0300
parents
children b55cbf18157e
comparison
equal deleted inserted replaced
49:93dabbc9efb9 50:72eb30262aac
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_array_t indices;
14 size_t max_index_len;
15 ngx_http_cache_hash_t *index_cache;
16 } ngx_http_index_loc_conf_t;
17
18
19 typedef struct {
20 ngx_uint_t index;
21 u_char *last;
22 ngx_str_t path;
23 ngx_str_t redirect;
24 ngx_http_cache_entry_t *cache;
25 ngx_uint_t tested; /* unsigned tested:1 */
26 } ngx_http_index_ctx_t;
27
28
29 #define NGX_HTTP_DEFAULT_INDEX "index.html"
30
31
32 static ngx_int_t ngx_http_index_test_dir(ngx_http_request_t *r,
33 ngx_http_index_ctx_t *ctx);
34 static ngx_int_t ngx_http_index_error(ngx_http_request_t *r,
35 ngx_http_index_ctx_t *ctx, ngx_err_t err);
36
37 static ngx_int_t ngx_http_index_init(ngx_cycle_t *cycle);
38 static void *ngx_http_index_create_loc_conf(ngx_conf_t *cf);
39 static char *ngx_http_index_merge_loc_conf(ngx_conf_t *cf,
40 void *parent, void *child);
41 static char *ngx_http_index_set_index(ngx_conf_t *cf, ngx_command_t *cmd,
42 void *conf);
43
44
45 static ngx_command_t ngx_http_index_commands[] = {
46
47 { ngx_string("index"),
48 NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
49 ngx_http_index_set_index,
50 NGX_HTTP_LOC_CONF_OFFSET,
51 0,
52 NULL },
53
54 #if (NGX_HTTP_CACHE)
55
56 { ngx_string("index_cache"),
57 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE3,
58 ngx_http_set_cache_slot,
59 NGX_HTTP_LOC_CONF_OFFSET,
60 offsetof(ngx_http_index_loc_conf_t, index_cache),
61 NULL },
62
63 #endif
64
65 ngx_null_command
66 };
67
68
69 ngx_http_module_t ngx_http_index_module_ctx = {
70 NULL, /* pre conf */
71
72 NULL, /* create main configuration */
73 NULL, /* init main configuration */
74
75 NULL, /* create server configuration */
76 NULL, /* merge server configuration */
77
78 ngx_http_index_create_loc_conf, /* create location configration */
79 ngx_http_index_merge_loc_conf /* merge location configration */
80 };
81
82
83 ngx_module_t ngx_http_index_module = {
84 NGX_MODULE,
85 &ngx_http_index_module_ctx, /* module context */
86 ngx_http_index_commands, /* module directives */
87 NGX_HTTP_MODULE, /* module type */
88 ngx_http_index_init, /* init module */
89 NULL /* init process */
90 };
91
92
93 /*
94 * Try to open the first index file before the test of the directory existence
95 * because the valid requests should be many more than invalid ones.
96 * If open() would fail, then stat() should be more quickly because some data
97 * is already cached in the kernel.
98 * Besides, Win32 has ERROR_PATH_NOT_FOUND (NGX_ENOTDIR).
99 * Unix has ENOTDIR error, although it less helpfull - it points only
100 * that path contains the usual file in place of the directory.
101 */
102
103 static ngx_int_t ngx_http_index_handler(ngx_http_request_t *r)
104 {
105 u_char *name;
106 ngx_fd_t fd;
107 ngx_int_t rc;
108 ngx_str_t *index;
109 ngx_err_t err;
110 ngx_log_t *log;
111 ngx_http_index_ctx_t *ctx;
112 ngx_http_core_loc_conf_t *clcf;
113 ngx_http_index_loc_conf_t *ilcf;
114 #if (NGX_HTTP_CACHE0)
115 /* crc must be in ctx !! */
116 uint32_t crc;
117 #endif
118
119 if (r->uri.data[r->uri.len - 1] != '/') {
120 return NGX_DECLINED;
121 }
122
123 /* TODO: Win32 */
124 if (r->zero_in_uri) {
125 return NGX_DECLINED;
126 }
127
128 log = r->connection->log;
129
130 /*
131 * we use context because the handler supports an async file opening
132 * and thus can be called several times
133 */
134
135 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
136 ilcf = ngx_http_get_module_loc_conf(r, ngx_http_index_module);
137
138 ctx = ngx_http_get_module_ctx(r, ngx_http_index_module);
139 if (ctx == NULL) {
140
141 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_index_ctx_t));
142 if (ctx == NULL) {
143 return NGX_HTTP_INTERNAL_SERVER_ERROR;
144 }
145
146 ngx_http_set_ctx(r, ctx, ngx_http_index_module);
147
148 #if (NGX_HTTP_CACHE)
149
150 if (ilcf->index_cache) {
151 ctx->cache = ngx_http_cache_get(ilcf->index_cache, NULL,
152 &r->uri, &crc);
153
154 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
155 "http index cache get: %p", ctx->cache);
156
157 if (ctx->cache && !ctx->cache->expired) {
158
159 ctx->cache->accessed = ngx_cached_time;
160
161 ctx->redirect.len = ctx->cache->data.value.len;
162 ctx->redirect.data = ngx_palloc(r->pool, ctx->redirect.len + 1);
163 if (ctx->redirect.data == NULL) {
164 ngx_http_cache_unlock(ilcf->index_cache, ctx->cache, log);
165 return NGX_HTTP_INTERNAL_SERVER_ERROR;
166 }
167
168 ngx_memcpy(ctx->redirect.data, ctx->cache->data.value.data,
169 ctx->redirect.len + 1);
170 ngx_http_cache_unlock(ilcf->index_cache, ctx->cache, log);
171
172 return ngx_http_internal_redirect(r, &ctx->redirect, NULL);
173 }
174 }
175
176 #endif
177
178 #if 0
179 ctx->path.data = ngx_palloc(r->pool, clcf->root.len + r->uri.len
180 + ilcf->max_index_len
181 - clcf->alias * clcf->name.len);
182 if (ctx->path.data == NULL) {
183 return NGX_HTTP_INTERNAL_SERVER_ERROR;
184 }
185
186 ctx->redirect.data = ngx_cpymem(ctx->path.data, clcf->root.data,
187 clcf->root.len);
188 #endif
189
190 if (clcf->alias) {
191 ctx->path.data = ngx_palloc(r->pool, clcf->root.len
192 + r->uri.len + 1 - clcf->name.len
193 + ilcf->max_index_len);
194 if (ctx->path.data == NULL) {
195 return NGX_HTTP_INTERNAL_SERVER_ERROR;
196 }
197
198 ctx->redirect.data = ngx_palloc(r->pool, r->uri.len
199 + ilcf->max_index_len);
200 if (ctx->redirect.data == NULL) {
201 return NGX_HTTP_INTERNAL_SERVER_ERROR;
202 }
203
204 ngx_memcpy(ctx->path.data, clcf->root.data, clcf->root.len);
205
206 ctx->last = ngx_cpystrn(ctx->path.data + clcf->root.len,
207 r->uri.data + clcf->name.len,
208 r->uri.len + 1 - clcf->name.len);
209
210 #if 0
211 /*
212 * aliases usually have trailling "/",
213 * set it in the start of the possible redirect
214 */
215
216 if (*ctx->redirect.data != '/') {
217 ctx->redirect.data--;
218 }
219 #endif
220
221 } else {
222 ctx->path.data = ngx_palloc(r->pool, clcf->root.len + r->uri.len
223 + ilcf->max_index_len);
224 if (ctx->path.data == NULL) {
225 return NGX_HTTP_INTERNAL_SERVER_ERROR;
226 }
227
228 ctx->redirect.data = ngx_cpymem(ctx->path.data, clcf->root.data,
229 clcf->root.len);
230
231 ctx->last = ngx_cpystrn(ctx->redirect.data, r->uri.data,
232 r->uri.len + 1);
233 }
234 }
235
236 ctx->path.len = ctx->last - ctx->path.data;
237
238 index = ilcf->indices.elts;
239 for (/* void */; ctx->index < ilcf->indices.nelts; ctx->index++) {
240
241 if (index[ctx->index].data[0] == '/') {
242 name = index[ctx->index].data;
243
244 } else {
245 ngx_memcpy(ctx->last, index[ctx->index].data,
246 index[ctx->index].len + 1);
247 name = ctx->path.data;
248 }
249
250 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
251 "open index \"%s\"", name);
252
253 fd = ngx_open_file(name, NGX_FILE_RDONLY, NGX_FILE_OPEN);
254
255 if (fd == (ngx_fd_t) NGX_AGAIN) {
256 return NGX_AGAIN;
257 }
258
259 if (fd == NGX_INVALID_FILE) {
260 err = ngx_errno;
261
262 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, err,
263 ngx_open_file_n " \"%s\" failed", name);
264
265 if (err == NGX_ENOTDIR) {
266 return ngx_http_index_error(r, ctx, err);
267
268 } else if (err == NGX_EACCES) {
269 return ngx_http_index_error(r, ctx, err);
270 }
271
272 if (!ctx->tested) {
273 rc = ngx_http_index_test_dir(r, ctx);
274
275 if (rc != NGX_OK) {
276 return rc;
277 }
278
279 ctx->tested = 1;
280 }
281
282 if (err == NGX_ENOENT) {
283 continue;
284 }
285
286 ngx_log_error(NGX_LOG_ERR, log, err,
287 ngx_open_file_n " \"%s\" failed", name);
288
289 return NGX_HTTP_INTERNAL_SERVER_ERROR;
290 }
291
292
293 /* STUB: open file cache */
294
295 r->file.name.data = name;
296 r->file.fd = fd;
297
298 if (index[ctx->index].data[0] == '/') {
299 r->file.name.len = index[ctx->index].len;
300 ctx->redirect.len = index[ctx->index].len;
301 ctx->redirect.data = index[ctx->index].data;
302
303 } else {
304 if (clcf->alias) {
305 name = ngx_cpymem(ctx->redirect.data, r->uri.data, r->uri.len);
306 ngx_memcpy(name, index[ctx->index].data,
307 index[ctx->index].len + 1);
308 }
309
310 ctx->redirect.len = r->uri.len + index[ctx->index].len;
311 r->file.name.len = clcf->root.len + r->uri.len
312 - clcf->alias * clcf->name.len
313 + index[ctx->index].len;
314 }
315
316 /**/
317
318
319 #if (NGX_HTTP_CACHE)
320
321 if (ilcf->index_cache) {
322
323 if (ctx->cache) {
324 if (ctx->redirect.len == ctx->cache->data.value.len
325 && ngx_memcmp(ctx->cache->data.value.data,
326 ctx->redirect.data, ctx->redirect.len) == 0)
327 {
328 ctx->cache->accessed = ngx_cached_time;
329 ctx->cache->updated = ngx_cached_time;
330 ngx_http_cache_unlock(ilcf->index_cache, ctx->cache, log);
331
332 return ngx_http_internal_redirect(r, &ctx->redirect, NULL);
333 }
334 }
335
336 ctx->redirect.len++;
337 ctx->cache = ngx_http_cache_alloc(ilcf->index_cache, ctx->cache,
338 NULL, &r->uri, crc,
339 &ctx->redirect, log);
340 ctx->redirect.len--;
341
342 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
343 "http index cache alloc: %p", ctx->cache);
344
345 if (ctx->cache) {
346 ctx->cache->fd = NGX_INVALID_FILE;
347 ctx->cache->accessed = ngx_cached_time;
348 ctx->cache->last_modified = 0;
349 ctx->cache->updated = ngx_cached_time;
350 ctx->cache->memory = 1;
351 ngx_http_cache_unlock(ilcf->index_cache, ctx->cache, log);
352 }
353 }
354
355 #endif
356
357 return ngx_http_internal_redirect(r, &ctx->redirect, NULL);
358 }
359
360 return NGX_DECLINED;
361 }
362
363
364 static ngx_int_t ngx_http_index_test_dir(ngx_http_request_t *r,
365 ngx_http_index_ctx_t *ctx)
366 {
367 ngx_err_t err;
368
369 ctx->path.data[ctx->path.len - 1] = '\0';
370 ctx->path.data[ctx->path.len] = '\0';
371
372 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
373 "http check dir: \"%s\"", ctx->path.data);
374
375 if (ngx_file_info(ctx->path.data, &r->file.info) == -1) {
376
377 err = ngx_errno;
378
379 if (err == NGX_ENOENT) {
380 ctx->path.data[ctx->path.len - 1] = '/';
381 return ngx_http_index_error(r, ctx, err);
382 }
383
384 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
385 ngx_file_info_n " \"%s\" failed", ctx->path.data);
386
387 return NGX_HTTP_INTERNAL_SERVER_ERROR;
388 }
389
390 ctx->path.data[ctx->path.len - 1] = '/';
391
392 if (ngx_is_dir(&r->file.info)) {
393 return NGX_OK;
394 }
395
396 /* THINK: not reached ??? */
397 return ngx_http_index_error(r, ctx, 0);
398 }
399
400
401 static ngx_int_t ngx_http_index_error(ngx_http_request_t *r,
402 ngx_http_index_ctx_t *ctx, ngx_err_t err)
403 {
404 if (err == NGX_EACCES) {
405 ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
406 "\"%s\" is forbidden", ctx->path.data);
407
408 return NGX_HTTP_FORBIDDEN;
409 }
410
411 ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
412 "\"%s\" is not found", ctx->path.data);
413 return NGX_HTTP_NOT_FOUND;
414 }
415
416
417 static ngx_int_t ngx_http_index_init(ngx_cycle_t *cycle)
418 {
419 ngx_http_handler_pt *h;
420 ngx_http_core_main_conf_t *cmcf;
421
422 cmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_core_module);
423
424 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
425 if (h == NULL) {
426 return NGX_ERROR;
427 }
428
429 *h = ngx_http_index_handler;
430
431 return NGX_OK;
432 }
433
434
435 static void *ngx_http_index_create_loc_conf(ngx_conf_t *cf)
436 {
437 ngx_http_index_loc_conf_t *conf;
438
439 conf = ngx_palloc(cf->pool, sizeof(ngx_http_index_loc_conf_t));
440 if (conf == NULL) {
441 return NGX_CONF_ERROR;
442 }
443
444 if (ngx_array_init(&conf->indices, cf->pool, 2, sizeof(ngx_str_t))
445 == NGX_ERROR)
446 {
447 return NGX_CONF_ERROR;
448 }
449
450 conf->max_index_len = 0;
451
452 conf->index_cache = NULL;
453
454 return conf;
455 }
456
457
458 /* TODO: remove duplicate indices */
459
460 static char *ngx_http_index_merge_loc_conf(ngx_conf_t *cf,
461 void *parent, void *child)
462 {
463 ngx_http_index_loc_conf_t *prev = parent;
464 ngx_http_index_loc_conf_t *conf = child;
465
466 ngx_str_t *index;
467
468 if (conf->max_index_len == 0) {
469 if (prev->max_index_len != 0) {
470 ngx_memcpy(conf, prev, sizeof(ngx_http_index_loc_conf_t));
471 return NGX_CONF_OK;
472 }
473
474 index = ngx_array_push(&conf->indices);
475 if (index == NULL) {
476 return NGX_CONF_ERROR;
477 }
478
479 index->len = sizeof(NGX_HTTP_DEFAULT_INDEX) - 1;
480 index->data = (u_char *) NGX_HTTP_DEFAULT_INDEX;
481 conf->max_index_len = sizeof(NGX_HTTP_DEFAULT_INDEX);
482
483 return NGX_CONF_OK;
484 }
485
486 #if 0
487
488 if (prev->max_index_len != 0) {
489
490 prev_index = prev->indices.elts;
491 for (i = 0; i < prev->indices.nelts; i++) {
492 index = ngx_array_push(&conf->indices);
493 if (index == NULL) {
494 return NGX_CONF_ERROR;
495 }
496
497 index->len = prev_index[i].len;
498 index->data = prev_index[i].data;
499 }
500 }
501
502 if (conf->max_index_len < prev->max_index_len) {
503 conf->max_index_len = prev->max_index_len;
504 }
505
506 #endif
507
508 if (conf->index_cache == NULL) {
509 conf->index_cache = prev->index_cache;
510 }
511
512 return NGX_CONF_OK;
513 }
514
515
516 /* TODO: warn about duplicate indices */
517
518 static char *ngx_http_index_set_index(ngx_conf_t *cf, ngx_command_t *cmd,
519 void *conf)
520 {
521 ngx_http_index_loc_conf_t *ilcf = conf;
522
523 ngx_uint_t i;
524 ngx_str_t *index, *value;
525
526 value = cf->args->elts;
527
528 if (value[1].data[0] == '/' && ilcf->indices.nelts == 0) {
529 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
530 "first index \"%V\" in \"%V\" directive "
531 "must not be absolute",
532 &value[1], &cmd->name);
533 return NGX_CONF_ERROR;
534 }
535
536 for (i = 1; i < cf->args->nelts; i++) {
537 if (value[i].len == 0) {
538 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
539 "index \"%V\" in \"%V\" directive is invalid",
540 &value[1], &cmd->name);
541 return NGX_CONF_ERROR;
542 }
543
544 index = ngx_array_push(&ilcf->indices);
545 if (index == NULL) {
546 return NGX_CONF_ERROR;
547 }
548
549 index->len = value[i].len;
550 index->data = value[i].data;
551
552 if (ilcf->max_index_len < index->len + 1) {
553 ilcf->max_index_len = index->len + 1;
554 }
555 }
556
557 return NGX_CONF_OK;
558 }