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