50
|
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 {
|
58
|
13 ngx_str_t name;
|
|
14 ngx_array_t *lengths;
|
|
15 ngx_array_t *values;
|
|
16 } ngx_http_index_t;
|
|
17
|
|
18
|
|
19 typedef struct {
|
|
20 ngx_array_t *indices; /* array of ngx_http_index_t */
|
50
|
21 size_t max_index_len;
|
|
22 } ngx_http_index_loc_conf_t;
|
|
23
|
|
24
|
|
25 typedef struct {
|
58
|
26 ngx_uint_t current;
|
|
27
|
106
|
28 ngx_str_t path;
|
58
|
29 ngx_str_t index;
|
|
30
|
|
31 ngx_uint_t tested; /* unsigned tested:1 */
|
50
|
32 } ngx_http_index_ctx_t;
|
|
33
|
|
34
|
|
35 #define NGX_HTTP_DEFAULT_INDEX "index.html"
|
|
36
|
|
37
|
|
38 static ngx_int_t ngx_http_index_test_dir(ngx_http_request_t *r,
|
58
|
39 ngx_http_index_ctx_t *ctx);
|
50
|
40 static ngx_int_t ngx_http_index_error(ngx_http_request_t *r,
|
58
|
41 ngx_http_index_ctx_t *ctx, ngx_err_t err);
|
50
|
42
|
|
43 static ngx_int_t ngx_http_index_init(ngx_cycle_t *cycle);
|
|
44 static void *ngx_http_index_create_loc_conf(ngx_conf_t *cf);
|
|
45 static char *ngx_http_index_merge_loc_conf(ngx_conf_t *cf,
|
58
|
46 void *parent, void *child);
|
50
|
47 static char *ngx_http_index_set_index(ngx_conf_t *cf, ngx_command_t *cmd,
|
58
|
48 void *conf);
|
50
|
49
|
|
50
|
|
51 static ngx_command_t ngx_http_index_commands[] = {
|
|
52
|
|
53 { ngx_string("index"),
|
58
|
54 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
|
50
|
55 ngx_http_index_set_index,
|
|
56 NGX_HTTP_LOC_CONF_OFFSET,
|
|
57 0,
|
|
58 NULL },
|
|
59
|
|
60 #if (NGX_HTTP_CACHE)
|
|
61
|
|
62 { ngx_string("index_cache"),
|
|
63 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE3,
|
|
64 ngx_http_set_cache_slot,
|
|
65 NGX_HTTP_LOC_CONF_OFFSET,
|
|
66 offsetof(ngx_http_index_loc_conf_t, index_cache),
|
|
67 NULL },
|
|
68
|
|
69 #endif
|
|
70
|
|
71 ngx_null_command
|
|
72 };
|
|
73
|
|
74
|
|
75 ngx_http_module_t ngx_http_index_module_ctx = {
|
58
|
76 NULL, /* preconfiguration */
|
|
77 NULL, /* postconfiguration */
|
50
|
78
|
|
79 NULL, /* create main configuration */
|
|
80 NULL, /* init main configuration */
|
|
81
|
|
82 NULL, /* create server configuration */
|
|
83 NULL, /* merge server configuration */
|
|
84
|
|
85 ngx_http_index_create_loc_conf, /* create location configration */
|
|
86 ngx_http_index_merge_loc_conf /* merge location configration */
|
|
87 };
|
|
88
|
|
89
|
|
90 ngx_module_t ngx_http_index_module = {
|
58
|
91 NGX_MODULE_V1,
|
50
|
92 &ngx_http_index_module_ctx, /* module context */
|
|
93 ngx_http_index_commands, /* module directives */
|
|
94 NGX_HTTP_MODULE, /* module type */
|
90
|
95 NULL, /* init master */
|
50
|
96 ngx_http_index_init, /* init module */
|
90
|
97 NULL, /* init process */
|
|
98 NULL, /* init thread */
|
|
99 NULL, /* exit thread */
|
|
100 NULL, /* exit process */
|
|
101 NULL, /* exit master */
|
|
102 NGX_MODULE_V1_PADDING
|
50
|
103 };
|
|
104
|
|
105
|
|
106 /*
|
|
107 * Try to open the first index file before the test of the directory existence
|
|
108 * because the valid requests should be many more than invalid ones.
|
|
109 * If open() would fail, then stat() should be more quickly because some data
|
|
110 * is already cached in the kernel.
|
|
111 * Besides, Win32 has ERROR_PATH_NOT_FOUND (NGX_ENOTDIR).
|
|
112 * Unix has ENOTDIR error, although it less helpfull - it points only
|
|
113 * that path contains the usual file in place of the directory.
|
|
114 */
|
|
115
|
58
|
116 static ngx_int_t
|
|
117 ngx_http_index_handler(ngx_http_request_t *r)
|
50
|
118 {
|
106
|
119 u_char *last;
|
58
|
120 size_t len;
|
|
121 ngx_fd_t fd;
|
|
122 ngx_int_t rc;
|
|
123 ngx_err_t err;
|
106
|
124 ngx_str_t uri;
|
58
|
125 ngx_log_t *log;
|
|
126 ngx_uint_t i;
|
|
127 ngx_http_index_t *index;
|
|
128 ngx_http_index_ctx_t *ctx;
|
112
|
129 ngx_pool_cleanup_t *cln;
|
|
130 ngx_pool_cleanup_file_t *clnf;
|
58
|
131 ngx_http_script_code_pt code;
|
|
132 ngx_http_script_engine_t e;
|
|
133 ngx_http_core_loc_conf_t *clcf;
|
|
134 ngx_http_index_loc_conf_t *ilcf;
|
|
135 ngx_http_script_len_code_pt lcode;
|
50
|
136
|
|
137 if (r->uri.data[r->uri.len - 1] != '/') {
|
|
138 return NGX_DECLINED;
|
|
139 }
|
|
140
|
|
141 /* TODO: Win32 */
|
|
142 if (r->zero_in_uri) {
|
|
143 return NGX_DECLINED;
|
|
144 }
|
|
145
|
|
146 log = r->connection->log;
|
|
147
|
|
148 /*
|
58
|
149 * we use context because the handler supports an async file opening,
|
|
150 * and may be called several times
|
50
|
151 */
|
|
152
|
|
153 ilcf = ngx_http_get_module_loc_conf(r, ngx_http_index_module);
|
|
154
|
|
155 ctx = ngx_http_get_module_ctx(r, ngx_http_index_module);
|
|
156 if (ctx == NULL) {
|
|
157
|
|
158 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_index_ctx_t));
|
|
159 if (ctx == NULL) {
|
|
160 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
161 }
|
|
162
|
|
163 ngx_http_set_ctx(r, ctx, ngx_http_index_module);
|
58
|
164 }
|
50
|
165
|
58
|
166 index = ilcf->indices->elts;
|
|
167 for (i = ctx->current; i < ilcf->indices->nelts; i++) {
|
|
168
|
|
169 if (index[i].lengths == NULL) {
|
50
|
170
|
58
|
171 if (index[i].name.data[0] == '/') {
|
|
172 return ngx_http_internal_redirect(r, &index[i].name, &r->args);
|
|
173 }
|
50
|
174
|
58
|
175 len = ilcf->max_index_len;
|
|
176 ctx->index.len = index[i].name.len;
|
50
|
177
|
58
|
178 } else {
|
|
179 ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
|
50
|
180
|
58
|
181 e.ip = index[i].lengths->elts;
|
|
182 e.request = r;
|
|
183
|
112
|
184 /* 1 byte for terminating '\0' */
|
106
|
185
|
112
|
186 len = 1;
|
50
|
187
|
58
|
188 while (*(uintptr_t *) e.ip) {
|
|
189 lcode = *(ngx_http_script_len_code_pt *) e.ip;
|
|
190 len += lcode(&e);
|
|
191 }
|
50
|
192
|
58
|
193 ctx->index.len = len;
|
112
|
194
|
|
195 /* 16 bytes are preallocation */
|
|
196
|
|
197 len += 16;
|
58
|
198 }
|
50
|
199
|
106
|
200 if (len > ctx->path.len) {
|
|
201
|
|
202 last = ngx_http_map_uri_to_path(r, &ctx->path, len);
|
|
203 if (last == NULL) {
|
|
204 return NGX_ERROR;
|
50
|
205 }
|
106
|
206
|
|
207 ctx->index.data = last;
|
50
|
208 }
|
|
209
|
58
|
210 if (index[i].values == NULL) {
|
106
|
211
|
|
212 /* index[i].name.len includes the terminating '\0' */
|
|
213
|
|
214 ngx_memcpy(ctx->index.data, index[i].name.data, index[i].name.len);
|
50
|
215
|
58
|
216 } else {
|
|
217 e.ip = index[i].values->elts;
|
|
218 e.pos = ctx->index.data;
|
50
|
219
|
58
|
220 while (*(uintptr_t *) e.ip) {
|
|
221 code = *(ngx_http_script_code_pt *) e.ip;
|
|
222 code((ngx_http_script_engine_t *) &e);
|
50
|
223 }
|
|
224
|
58
|
225 if (*ctx->index.data == '/') {
|
|
226 ctx->index.len--;
|
|
227 return ngx_http_internal_redirect(r, &ctx->index, &r->args);
|
50
|
228 }
|
|
229
|
58
|
230 *e.pos++ = '\0';
|
50
|
231 }
|
|
232
|
106
|
233 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
|
|
234 "open index \"%s\"", ctx->path.data);
|
50
|
235
|
112
|
236 cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_pool_cleanup_file_t));
|
|
237 if (cln == NULL) {
|
|
238 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
239 }
|
|
240
|
106
|
241 fd = ngx_open_file(ctx->path.data, NGX_FILE_RDONLY, NGX_FILE_OPEN);
|
50
|
242
|
|
243 if (fd == (ngx_fd_t) NGX_AGAIN) {
|
58
|
244 ctx->current = i;
|
50
|
245 return NGX_AGAIN;
|
|
246 }
|
|
247
|
|
248 if (fd == NGX_INVALID_FILE) {
|
|
249 err = ngx_errno;
|
|
250
|
|
251 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, err,
|
106
|
252 ngx_open_file_n " \"%s\" failed", ctx->path.data);
|
50
|
253
|
|
254 if (err == NGX_ENOTDIR) {
|
|
255 return ngx_http_index_error(r, ctx, err);
|
|
256
|
|
257 } else if (err == NGX_EACCES) {
|
|
258 return ngx_http_index_error(r, ctx, err);
|
|
259 }
|
|
260
|
|
261 if (!ctx->tested) {
|
|
262 rc = ngx_http_index_test_dir(r, ctx);
|
|
263
|
|
264 if (rc != NGX_OK) {
|
|
265 return rc;
|
|
266 }
|
|
267
|
|
268 ctx->tested = 1;
|
|
269 }
|
|
270
|
|
271 if (err == NGX_ENOENT) {
|
|
272 continue;
|
|
273 }
|
|
274
|
|
275 ngx_log_error(NGX_LOG_ERR, log, err,
|
106
|
276 ngx_open_file_n " \"%s\" failed", ctx->path.data);
|
50
|
277
|
|
278 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
279 }
|
|
280
|
112
|
281 cln->handler = ngx_pool_cleanup_file;
|
|
282 clnf = cln->data;
|
50
|
283
|
112
|
284 clnf->fd = fd;
|
|
285 clnf->name = ctx->path.data;
|
|
286 clnf->log = r->pool->log;
|
50
|
287
|
106
|
288 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
50
|
289
|
106
|
290 uri.len = r->uri.len + ctx->index.len - 1;
|
|
291
|
|
292 if (!clcf->alias) {
|
|
293 uri.data = ctx->path.data + clcf->root.len;
|
|
294
|
|
295 } else {
|
|
296 uri.data = ngx_palloc(r->pool, uri.len);
|
|
297 if (uri.data == NULL) {
|
|
298 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
299 }
|
|
300
|
|
301 last = ngx_cpymem(uri.data, r->uri.data, r->uri.len);
|
|
302 ngx_memcpy(last, ctx->index.data, ctx->index.len - 1);
|
50
|
303 }
|
|
304
|
106
|
305 return ngx_http_internal_redirect(r, &uri, &r->args);
|
50
|
306 }
|
|
307
|
|
308 return NGX_DECLINED;
|
|
309 }
|
|
310
|
|
311
|
58
|
312 static ngx_int_t
|
|
313 ngx_http_index_test_dir(ngx_http_request_t *r, ngx_http_index_ctx_t *ctx)
|
|
314 {
|
|
315 ngx_err_t err;
|
|
316 ngx_file_info_t fi;
|
|
317
|
|
318 *(ctx->index.data - 1) = '\0';
|
50
|
319
|
|
320 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
106
|
321 "http index check dir: \"%s\"", ctx->path.data);
|
50
|
322
|
106
|
323 if (ngx_file_info(ctx->path.data, &fi) == -1) {
|
50
|
324
|
|
325 err = ngx_errno;
|
|
326
|
|
327 if (err == NGX_ENOENT) {
|
58
|
328 *(ctx->index.data - 1) = '/';
|
50
|
329 return ngx_http_index_error(r, ctx, err);
|
|
330 }
|
|
331
|
|
332 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
|
106
|
333 ngx_file_info_n " \"%s\" failed", ctx->path.data);
|
50
|
334
|
|
335 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
336 }
|
|
337
|
58
|
338 *(ctx->index.data - 1) = '/';
|
50
|
339
|
58
|
340 if (ngx_is_dir(&fi)) {
|
50
|
341 return NGX_OK;
|
|
342 }
|
|
343
|
58
|
344 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
106
|
345 "\"%s\" is not a directory", ctx->path.data);
|
58
|
346
|
|
347 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
50
|
348 }
|
|
349
|
|
350
|
58
|
351 static ngx_int_t
|
|
352 ngx_http_index_error(ngx_http_request_t *r, ngx_http_index_ctx_t *ctx,
|
|
353 ngx_err_t err)
|
50
|
354 {
|
|
355 if (err == NGX_EACCES) {
|
|
356 ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
|
106
|
357 "\"%s\" is forbidden", ctx->path.data);
|
50
|
358
|
|
359 return NGX_HTTP_FORBIDDEN;
|
|
360 }
|
|
361
|
|
362 ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
|
106
|
363 "\"%s\" is not found", ctx->path.data);
|
58
|
364
|
50
|
365 return NGX_HTTP_NOT_FOUND;
|
|
366 }
|
|
367
|
|
368
|
58
|
369 static void *
|
|
370 ngx_http_index_create_loc_conf(ngx_conf_t *cf)
|
|
371 {
|
|
372 ngx_http_index_loc_conf_t *conf;
|
|
373
|
|
374 conf = ngx_palloc(cf->pool, sizeof(ngx_http_index_loc_conf_t));
|
|
375 if (conf == NULL) {
|
|
376 return NGX_CONF_ERROR;
|
|
377 }
|
|
378
|
|
379 conf->indices = NULL;
|
|
380 conf->max_index_len = 1;
|
|
381
|
|
382 return conf;
|
|
383 }
|
|
384
|
|
385
|
|
386 static char *
|
|
387 ngx_http_index_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
388 {
|
|
389 ngx_http_index_loc_conf_t *prev = parent;
|
|
390 ngx_http_index_loc_conf_t *conf = child;
|
|
391
|
|
392 ngx_http_index_t *index;
|
|
393
|
|
394 if (conf->indices == NULL) {
|
|
395 conf->indices = prev->indices;
|
|
396 conf->max_index_len = prev->max_index_len;
|
|
397 }
|
|
398
|
|
399 if (conf->indices == NULL) {
|
|
400 conf->indices = ngx_array_create(cf->pool, 1, sizeof(ngx_http_index_t));
|
|
401 if (conf->indices == NULL) {
|
|
402 return NGX_CONF_ERROR;
|
|
403 }
|
|
404
|
|
405 index = ngx_array_push(conf->indices);
|
|
406 if (index == NULL) {
|
|
407 return NGX_CONF_ERROR;
|
|
408 }
|
|
409
|
|
410 index->name.len = sizeof(NGX_HTTP_DEFAULT_INDEX);
|
|
411 index->name.data = (u_char *) NGX_HTTP_DEFAULT_INDEX;
|
|
412 index->lengths = NULL;
|
|
413 index->values = NULL;
|
|
414
|
|
415 conf->max_index_len = sizeof(NGX_HTTP_DEFAULT_INDEX);
|
|
416
|
|
417 return NGX_CONF_OK;
|
|
418 }
|
|
419
|
|
420 return NGX_CONF_OK;
|
|
421 }
|
|
422
|
|
423
|
|
424 /* TODO: warn about duplicate indices */
|
|
425
|
|
426 static char *
|
|
427 ngx_http_index_set_index(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
428 {
|
|
429 ngx_http_index_loc_conf_t *ilcf = conf;
|
|
430
|
|
431 ngx_uint_t i, n;
|
|
432 ngx_str_t *value;
|
|
433 ngx_http_index_t *index;
|
|
434 ngx_http_script_compile_t sc;
|
|
435
|
|
436 if (ilcf->indices == NULL) {
|
|
437 ilcf->indices = ngx_array_create(cf->pool, 2, sizeof(ngx_http_index_t));
|
|
438 if (ilcf->indices == NULL) {
|
|
439 return NGX_CONF_ERROR;
|
|
440 }
|
|
441 }
|
|
442
|
|
443 value = cf->args->elts;
|
|
444
|
|
445 for (i = 1; i < cf->args->nelts; i++) {
|
|
446 if (value[i].data[0] == '/' && i != cf->args->nelts - 1) {
|
|
447 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
448 "only the last index in \"index\" directive "
|
|
449 "may be absolute");
|
|
450 return NGX_CONF_ERROR;
|
|
451 }
|
|
452
|
|
453 if (value[i].len == 0) {
|
|
454 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
455 "index \"%V\" in \"index\" directive is invalid",
|
|
456 &value[1]);
|
|
457 return NGX_CONF_ERROR;
|
|
458 }
|
|
459
|
|
460 index = ngx_array_push(ilcf->indices);
|
|
461 if (index == NULL) {
|
|
462 return NGX_CONF_ERROR;
|
|
463 }
|
|
464
|
|
465 index->name.len = value[i].len;
|
|
466 index->name.data = value[i].data;
|
|
467 index->lengths = NULL;
|
|
468 index->values = NULL;
|
|
469
|
|
470 n = ngx_http_script_variables_count(&value[i]);
|
|
471
|
|
472 if (n == 0) {
|
|
473 if (ilcf->max_index_len != 0
|
|
474 && ilcf->max_index_len < index->name.len)
|
|
475 {
|
|
476 ilcf->max_index_len = index->name.len;
|
|
477 }
|
|
478
|
106
|
479 /* include the terminating '\0' to the length to use ngx_memcpy() */
|
|
480 index->name.len++;
|
|
481
|
58
|
482 continue;
|
|
483 }
|
|
484
|
|
485 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
486
|
|
487 sc.cf = cf;
|
|
488 sc.source = &value[i];
|
|
489 sc.lengths = &index->lengths;
|
|
490 sc.values = &index->values;
|
|
491 sc.variables = n;
|
|
492 sc.complete_lengths = 1;
|
|
493 sc.complete_values = 1;
|
|
494
|
|
495 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
496 return NGX_CONF_ERROR;
|
|
497 }
|
|
498
|
|
499 ilcf->max_index_len = 0;
|
|
500 }
|
|
501
|
|
502 return NGX_CONF_OK;
|
|
503 }
|
|
504
|
|
505
|
|
506 static ngx_int_t
|
|
507 ngx_http_index_init(ngx_cycle_t *cycle)
|
50
|
508 {
|
|
509 ngx_http_handler_pt *h;
|
|
510 ngx_http_core_main_conf_t *cmcf;
|
|
511
|
|
512 cmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_core_module);
|
|
513
|
|
514 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
|
|
515 if (h == NULL) {
|
|
516 return NGX_ERROR;
|
|
517 }
|
|
518
|
|
519 *h = ngx_http_index_handler;
|
|
520
|
|
521 return NGX_OK;
|
|
522 }
|