0
|
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_t *cache;
|
|
25 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 child */
|
|
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() failed 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 shows only
|
|
100 * that path contains the usual file in place of the directory.
|
|
101 */
|
|
102
|
|
103 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 log = r->connection->log;
|
|
124
|
|
125 /*
|
|
126 * we use context because the handler supports an async file opening
|
|
127 * and thus can be called several times
|
|
128 */
|
|
129
|
|
130 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
131 ilcf = ngx_http_get_module_loc_conf(r, ngx_http_index_module);
|
|
132
|
|
133 ctx = ngx_http_get_module_ctx(r, ngx_http_index_module);
|
|
134 if (ctx == NULL) {
|
|
135 ngx_http_create_ctx(r, ctx, ngx_http_index_module,
|
|
136 sizeof(ngx_http_index_ctx_t),
|
|
137 NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
138
|
|
139 #if (NGX_HTTP_CACHE)
|
|
140
|
|
141 if (ilcf->index_cache) {
|
|
142 ctx->cache = ngx_http_cache_get(ilcf->index_cache, NULL,
|
|
143 &r->uri, &crc);
|
|
144
|
|
145 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
|
|
146 "http index cache get: " PTR_FMT, ctx->cache);
|
|
147
|
|
148 if (ctx->cache && !ctx->cache->expired) {
|
|
149
|
|
150 ctx->cache->accessed = ngx_cached_time;
|
|
151
|
|
152 ctx->redirect.len = ctx->cache->data.value.len;
|
|
153 ctx->redirect.data = ngx_palloc(r->pool, ctx->redirect.len + 1);
|
|
154 if (ctx->redirect.data == NULL) {
|
|
155 ngx_http_cache_unlock(ilcf->index_cache, ctx->cache, log);
|
|
156 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
157 }
|
|
158
|
|
159 ngx_memcpy(ctx->redirect.data, ctx->cache->data.value.data,
|
|
160 ctx->redirect.len + 1);
|
|
161 ngx_http_cache_unlock(ilcf->index_cache, ctx->cache, log);
|
|
162
|
|
163 return ngx_http_internal_redirect(r, &ctx->redirect, NULL);
|
|
164 }
|
|
165 }
|
|
166
|
|
167 #endif
|
|
168
|
|
169 #if 0
|
|
170 ctx->path.data = ngx_palloc(r->pool, clcf->root.len + r->uri.len
|
|
171 + ilcf->max_index_len
|
|
172 - clcf->alias * clcf->name.len);
|
|
173 if (ctx->path.data == NULL) {
|
|
174 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
175 }
|
|
176
|
|
177 ctx->redirect.data = ngx_cpymem(ctx->path.data, clcf->root.data,
|
|
178 clcf->root.len);
|
|
179 #endif
|
|
180
|
|
181 if (clcf->alias) {
|
|
182 ctx->path.data = ngx_palloc(r->pool, clcf->root.len
|
|
183 + r->uri.len + 1 - clcf->name.len
|
|
184 + ilcf->max_index_len);
|
|
185 if (ctx->path.data == NULL) {
|
|
186 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
187 }
|
|
188
|
|
189 ctx->redirect.data = ngx_palloc(r->pool, r->uri.len
|
|
190 + ilcf->max_index_len);
|
|
191 if (ctx->redirect.data == NULL) {
|
|
192 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
193 }
|
|
194
|
|
195 ngx_memcpy(ctx->path.data, clcf->root.data, clcf->root.len);
|
|
196
|
|
197 ctx->last = ngx_cpystrn(ctx->path.data + clcf->root.len,
|
|
198 r->uri.data + clcf->name.len,
|
|
199 r->uri.len + 1 - clcf->name.len);
|
|
200
|
|
201 #if 0
|
|
202 /*
|
|
203 * aliases usually have trailling "/",
|
|
204 * set it in the start of the possible redirect
|
|
205 */
|
|
206
|
|
207 if (*ctx->redirect.data != '/') {
|
|
208 ctx->redirect.data--;
|
|
209 }
|
|
210 #endif
|
|
211
|
|
212 } else {
|
|
213 ctx->path.data = ngx_palloc(r->pool, clcf->root.len + r->uri.len
|
|
214 + ilcf->max_index_len);
|
|
215 if (ctx->path.data == NULL) {
|
|
216 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
217 }
|
|
218
|
|
219 ctx->redirect.data = ngx_cpymem(ctx->path.data, clcf->root.data,
|
|
220 clcf->root.len);
|
|
221
|
|
222 ctx->last = ngx_cpystrn(ctx->redirect.data, r->uri.data,
|
|
223 r->uri.len + 1);
|
|
224 }
|
|
225 }
|
|
226
|
|
227 ctx->path.len = ctx->last - ctx->path.data;
|
|
228
|
|
229 index = ilcf->indices.elts;
|
|
230 for (/* void */; ctx->index < ilcf->indices.nelts; ctx->index++) {
|
|
231
|
|
232 if (index[ctx->index].data[0] == '/') {
|
|
233 name = index[ctx->index].data;
|
|
234
|
|
235 } else {
|
|
236 ngx_memcpy(ctx->last, index[ctx->index].data,
|
|
237 index[ctx->index].len + 1);
|
|
238 name = ctx->path.data;
|
|
239 }
|
|
240
|
|
241 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
|
|
242 "open index \"%s\"", name);
|
|
243
|
|
244 fd = ngx_open_file(name, NGX_FILE_RDONLY, NGX_FILE_OPEN);
|
|
245
|
|
246 if (fd == (ngx_fd_t) NGX_AGAIN) {
|
|
247 return NGX_AGAIN;
|
|
248 }
|
|
249
|
|
250 if (fd == NGX_INVALID_FILE) {
|
|
251 err = ngx_errno;
|
|
252
|
|
253 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, err,
|
|
254 ngx_open_file_n " %s failed", name);
|
|
255
|
|
256 if (err == NGX_ENOTDIR) {
|
|
257 return ngx_http_index_error(r, ctx, err);
|
|
258
|
|
259 } else if (err == NGX_EACCES) {
|
|
260 return ngx_http_index_error(r, ctx, err);
|
|
261 }
|
|
262
|
|
263 if (!ctx->tested) {
|
|
264 rc = ngx_http_index_test_dir(r, ctx);
|
|
265
|
|
266 if (rc != NGX_OK) {
|
|
267 return rc;
|
|
268 }
|
|
269
|
|
270 ctx->tested = 1;
|
|
271 }
|
|
272
|
|
273 if (err == NGX_ENOENT) {
|
|
274 continue;
|
|
275 }
|
|
276
|
|
277 ngx_log_error(NGX_LOG_ERR, log, err,
|
|
278 ngx_open_file_n " %s failed", name);
|
|
279
|
|
280 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
281 }
|
|
282
|
|
283
|
|
284 /* STUB: open file cache */
|
|
285
|
|
286 r->file.name.data = name;
|
|
287 r->file.fd = fd;
|
|
288
|
|
289 if (index[ctx->index].data[0] == '/') {
|
|
290 r->file.name.len = index[ctx->index].len;
|
|
291 ctx->redirect.len = index[ctx->index].len;
|
|
292 ctx->redirect.data = index[ctx->index].data;
|
|
293
|
|
294 } else {
|
|
295 if (clcf->alias) {
|
|
296 name = ngx_cpymem(ctx->redirect.data, r->uri.data, r->uri.len);
|
|
297 ngx_memcpy(name, index[ctx->index].data,
|
|
298 index[ctx->index].len + 1);
|
|
299 }
|
|
300
|
|
301 ctx->redirect.len = r->uri.len + index[ctx->index].len;
|
|
302 r->file.name.len = clcf->root.len + r->uri.len
|
|
303 - clcf->alias * clcf->name.len
|
|
304 + index[ctx->index].len;
|
|
305 }
|
|
306
|
|
307 /**/
|
|
308
|
|
309
|
|
310 #if (NGX_HTTP_CACHE)
|
|
311
|
|
312 if (ilcf->index_cache) {
|
|
313
|
|
314 if (ctx->cache) {
|
|
315 if (ctx->redirect.len == ctx->cache->data.value.len
|
|
316 && ngx_memcmp(ctx->cache->data.value.data,
|
|
317 ctx->redirect.data, ctx->redirect.len) == 0)
|
|
318 {
|
|
319 ctx->cache->accessed = ngx_cached_time;
|
|
320 ctx->cache->updated = ngx_cached_time;
|
|
321 ngx_http_cache_unlock(ilcf->index_cache, ctx->cache, log);
|
|
322
|
|
323 return ngx_http_internal_redirect(r, &ctx->redirect, NULL);
|
|
324 }
|
|
325 }
|
|
326
|
|
327 ctx->redirect.len++;
|
|
328 ctx->cache = ngx_http_cache_alloc(ilcf->index_cache, ctx->cache,
|
|
329 NULL, &r->uri, crc,
|
|
330 &ctx->redirect, log);
|
|
331 ctx->redirect.len--;
|
|
332
|
|
333 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
|
|
334 "http index cache alloc: " PTR_FMT, ctx->cache);
|
|
335
|
|
336 if (ctx->cache) {
|
|
337 ctx->cache->fd = NGX_INVALID_FILE;
|
|
338 ctx->cache->accessed = ngx_cached_time;
|
|
339 ctx->cache->last_modified = 0;
|
|
340 ctx->cache->updated = ngx_cached_time;
|
|
341 ctx->cache->memory = 1;
|
|
342 ngx_http_cache_unlock(ilcf->index_cache, ctx->cache, log);
|
|
343 }
|
|
344 }
|
|
345
|
|
346 #endif
|
|
347
|
|
348 return ngx_http_internal_redirect(r, &ctx->redirect, NULL);
|
|
349 }
|
|
350
|
|
351 return NGX_DECLINED;
|
|
352 }
|
|
353
|
|
354
|
|
355 static ngx_int_t ngx_http_index_test_dir(ngx_http_request_t *r,
|
|
356 ngx_http_index_ctx_t *ctx)
|
|
357 {
|
|
358 ngx_err_t err;
|
|
359
|
|
360 ctx->path.data[ctx->path.len - 1] = '\0';
|
|
361 ctx->path.data[ctx->path.len] = '\0';
|
|
362
|
|
363 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
364 "http check dir: \"%s\"", ctx->path.data);
|
|
365
|
|
366 if (ngx_file_info(ctx->path.data, &r->file.info) == -1) {
|
|
367
|
|
368 err = ngx_errno;
|
|
369
|
|
370 if (err == NGX_ENOENT) {
|
|
371 ctx->path.data[ctx->path.len - 1] = '/';
|
|
372 return ngx_http_index_error(r, ctx, err);
|
|
373 }
|
|
374
|
|
375 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
|
|
376 ngx_file_info_n " %s failed", ctx->path.data);
|
|
377
|
|
378 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
379 }
|
|
380
|
|
381 ctx->path.data[ctx->path.len - 1] = '/';
|
|
382
|
|
383 if (ngx_is_dir(&r->file.info)) {
|
|
384 return NGX_OK;
|
|
385 }
|
|
386
|
|
387 /* THINK: not reached ??? */
|
|
388 return ngx_http_index_error(r, ctx, 0);
|
|
389 }
|
|
390
|
|
391
|
|
392 static ngx_int_t ngx_http_index_error(ngx_http_request_t *r,
|
|
393 ngx_http_index_ctx_t *ctx, ngx_err_t err)
|
|
394 {
|
|
395 if (err == NGX_EACCES) {
|
|
396 ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
|
|
397 "\"%s\" is forbidden", ctx->path.data);
|
|
398
|
|
399 return NGX_HTTP_FORBIDDEN;
|
|
400 }
|
|
401
|
|
402 ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
|
|
403 "\"%s\" is not found", ctx->path.data);
|
|
404 return NGX_HTTP_NOT_FOUND;
|
|
405 }
|
|
406
|
|
407
|
|
408 static ngx_int_t ngx_http_index_init(ngx_cycle_t *cycle)
|
|
409 {
|
|
410 ngx_http_handler_pt *h;
|
|
411 ngx_http_core_main_conf_t *cmcf;
|
|
412
|
|
413 cmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_core_module);
|
|
414
|
|
415 h = ngx_push_array(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
|
|
416 if (h == NULL) {
|
|
417 return NGX_ERROR;
|
|
418 }
|
|
419
|
|
420 *h = ngx_http_index_handler;
|
|
421
|
|
422 return NGX_OK;
|
|
423 }
|
|
424
|
|
425
|
|
426 static void *ngx_http_index_create_loc_conf(ngx_conf_t *cf)
|
|
427 {
|
|
428 ngx_http_index_loc_conf_t *conf;
|
|
429
|
|
430 ngx_test_null(conf, ngx_palloc(cf->pool, sizeof(ngx_http_index_loc_conf_t)),
|
|
431 NGX_CONF_ERROR);
|
|
432
|
|
433 ngx_init_array(conf->indices, cf->pool, 3, sizeof(ngx_str_t),
|
|
434 NGX_CONF_ERROR);
|
|
435 conf->max_index_len = 0;
|
|
436
|
|
437 conf->index_cache = NULL;
|
|
438
|
|
439 return conf;
|
|
440 }
|
|
441
|
|
442
|
|
443 /* TODO: remove duplicate indices */
|
|
444
|
|
445 static char *ngx_http_index_merge_loc_conf(ngx_conf_t *cf,
|
|
446 void *parent, void *child)
|
|
447 {
|
|
448 ngx_http_index_loc_conf_t *prev = parent;
|
|
449 ngx_http_index_loc_conf_t *conf = child;
|
|
450
|
|
451 ngx_uint_t i;
|
|
452 ngx_str_t *index, *prev_index;
|
|
453
|
|
454 if (conf->max_index_len == 0) {
|
|
455 if (prev->max_index_len != 0) {
|
|
456 ngx_memcpy(conf, prev, sizeof(ngx_http_index_loc_conf_t));
|
|
457 return NGX_CONF_OK;
|
|
458 }
|
|
459
|
|
460 ngx_test_null(index, ngx_push_array(&conf->indices), NGX_CONF_ERROR);
|
|
461 index->len = sizeof(NGX_HTTP_DEFAULT_INDEX) - 1;
|
|
462 index->data = (u_char *) NGX_HTTP_DEFAULT_INDEX;
|
|
463 conf->max_index_len = sizeof(NGX_HTTP_DEFAULT_INDEX);
|
|
464
|
|
465 return NGX_CONF_OK;
|
|
466 }
|
|
467
|
|
468 if (prev->max_index_len != 0) {
|
|
469
|
|
470 prev_index = prev->indices.elts;
|
|
471 for (i = 0; i < prev->indices.nelts; i++) {
|
|
472 ngx_test_null(index, ngx_push_array(&conf->indices),
|
|
473 NGX_CONF_ERROR);
|
|
474 index->len = prev_index[i].len;
|
|
475 index->data = prev_index[i].data;
|
|
476 }
|
|
477 }
|
|
478
|
|
479 if (conf->max_index_len < prev->max_index_len) {
|
|
480 conf->max_index_len = prev->max_index_len;
|
|
481 }
|
|
482
|
|
483 if (conf->index_cache == NULL) {
|
|
484 conf->index_cache = prev->index_cache;
|
|
485 }
|
|
486
|
|
487 return NGX_CONF_OK;
|
|
488 }
|
|
489
|
|
490
|
|
491 /* TODO: warn about duplicate indices */
|
|
492
|
|
493 static char *ngx_http_index_set_index(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
494 void *conf)
|
|
495 {
|
|
496 ngx_http_index_loc_conf_t *ilcf = conf;
|
|
497
|
|
498 ngx_uint_t i;
|
|
499 ngx_str_t *index, *value;
|
|
500
|
|
501 value = cf->args->elts;
|
|
502
|
|
503 if (value[1].data[0] == '/' && ilcf->indices.nelts == 0) {
|
|
504 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
505 "first index \"%s\" in \"%s\" directive "
|
|
506 "must not be absolute",
|
|
507 value[1].data, cmd->name.data);
|
|
508 return NGX_CONF_ERROR;
|
|
509 }
|
|
510
|
|
511 for (i = 1; i < cf->args->nelts; i++) {
|
|
512 if (value[i].len == 0) {
|
|
513 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
514 "index \"%s\" in \"%s\" directive is invalid",
|
|
515 value[1].data, cmd->name.data);
|
|
516 return NGX_CONF_ERROR;
|
|
517 }
|
|
518
|
|
519 ngx_test_null(index, ngx_push_array(&ilcf->indices), NGX_CONF_ERROR);
|
|
520 index->len = value[i].len;
|
|
521 index->data = value[i].data;
|
|
522
|
|
523 if (ilcf->max_index_len < index->len + 1) {
|
|
524 ilcf->max_index_len = index->len + 1;
|
|
525 }
|
|
526 }
|
|
527
|
|
528 return NGX_CONF_OK;
|
|
529 }
|