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
|
230
|
43 static ngx_int_t ngx_http_index_init(ngx_conf_t *cf);
|
50
|
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
|
216
|
75 static ngx_http_module_t ngx_http_index_module_ctx = {
|
58
|
76 NULL, /* preconfiguration */
|
230
|
77 ngx_http_index_init, /* 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 */
|
230
|
96 NULL, /* 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;
|
122
|
183 e.flushed = 1;
|
58
|
184
|
112
|
185 /* 1 byte for terminating '\0' */
|
106
|
186
|
112
|
187 len = 1;
|
50
|
188
|
58
|
189 while (*(uintptr_t *) e.ip) {
|
|
190 lcode = *(ngx_http_script_len_code_pt *) e.ip;
|
|
191 len += lcode(&e);
|
|
192 }
|
50
|
193
|
58
|
194 ctx->index.len = len;
|
112
|
195
|
|
196 /* 16 bytes are preallocation */
|
|
197
|
|
198 len += 16;
|
58
|
199 }
|
50
|
200
|
230
|
201 if (len > (size_t) (ctx->path.data + ctx->path.len - ctx->index.data)) {
|
106
|
202
|
|
203 last = ngx_http_map_uri_to_path(r, &ctx->path, len);
|
|
204 if (last == NULL) {
|
|
205 return NGX_ERROR;
|
50
|
206 }
|
106
|
207
|
|
208 ctx->index.data = last;
|
50
|
209 }
|
|
210
|
58
|
211 if (index[i].values == NULL) {
|
106
|
212
|
|
213 /* index[i].name.len includes the terminating '\0' */
|
|
214
|
|
215 ngx_memcpy(ctx->index.data, index[i].name.data, index[i].name.len);
|
50
|
216
|
58
|
217 } else {
|
|
218 e.ip = index[i].values->elts;
|
|
219 e.pos = ctx->index.data;
|
50
|
220
|
58
|
221 while (*(uintptr_t *) e.ip) {
|
|
222 code = *(ngx_http_script_code_pt *) e.ip;
|
|
223 code((ngx_http_script_engine_t *) &e);
|
50
|
224 }
|
|
225
|
58
|
226 if (*ctx->index.data == '/') {
|
|
227 ctx->index.len--;
|
|
228 return ngx_http_internal_redirect(r, &ctx->index, &r->args);
|
50
|
229 }
|
|
230
|
58
|
231 *e.pos++ = '\0';
|
50
|
232 }
|
|
233
|
106
|
234 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
|
|
235 "open index \"%s\"", ctx->path.data);
|
50
|
236
|
112
|
237 cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_pool_cleanup_file_t));
|
|
238 if (cln == NULL) {
|
|
239 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
240 }
|
|
241
|
232
|
242 fd = ngx_open_file(ctx->path.data, NGX_FILE_RDONLY|NGX_FILE_NOATIME,
|
|
243 NGX_FILE_OPEN);
|
50
|
244
|
|
245 if (fd == (ngx_fd_t) NGX_AGAIN) {
|
58
|
246 ctx->current = i;
|
50
|
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,
|
106
|
254 ngx_open_file_n " \"%s\" failed", ctx->path.data);
|
50
|
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,
|
106
|
278 ngx_open_file_n " \"%s\" failed", ctx->path.data);
|
50
|
279
|
|
280 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
281 }
|
|
282
|
112
|
283 cln->handler = ngx_pool_cleanup_file;
|
|
284 clnf = cln->data;
|
50
|
285
|
112
|
286 clnf->fd = fd;
|
|
287 clnf->name = ctx->path.data;
|
|
288 clnf->log = r->pool->log;
|
50
|
289
|
106
|
290 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
50
|
291
|
106
|
292 uri.len = r->uri.len + ctx->index.len - 1;
|
|
293
|
|
294 if (!clcf->alias) {
|
144
|
295 uri.data = ctx->path.data + r->root_length;
|
106
|
296
|
|
297 } else {
|
|
298 uri.data = ngx_palloc(r->pool, uri.len);
|
|
299 if (uri.data == NULL) {
|
|
300 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
301 }
|
|
302
|
122
|
303 last = ngx_copy(uri.data, r->uri.data, r->uri.len);
|
106
|
304 ngx_memcpy(last, ctx->index.data, ctx->index.len - 1);
|
50
|
305 }
|
|
306
|
106
|
307 return ngx_http_internal_redirect(r, &uri, &r->args);
|
50
|
308 }
|
|
309
|
|
310 return NGX_DECLINED;
|
|
311 }
|
|
312
|
|
313
|
58
|
314 static ngx_int_t
|
|
315 ngx_http_index_test_dir(ngx_http_request_t *r, ngx_http_index_ctx_t *ctx)
|
|
316 {
|
|
317 ngx_err_t err;
|
|
318 ngx_file_info_t fi;
|
|
319
|
|
320 *(ctx->index.data - 1) = '\0';
|
50
|
321
|
|
322 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
106
|
323 "http index check dir: \"%s\"", ctx->path.data);
|
50
|
324
|
106
|
325 if (ngx_file_info(ctx->path.data, &fi) == -1) {
|
50
|
326
|
|
327 err = ngx_errno;
|
|
328
|
|
329 if (err == NGX_ENOENT) {
|
58
|
330 *(ctx->index.data - 1) = '/';
|
50
|
331 return ngx_http_index_error(r, ctx, err);
|
|
332 }
|
|
333
|
|
334 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
|
106
|
335 ngx_file_info_n " \"%s\" failed", ctx->path.data);
|
50
|
336
|
|
337 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
338 }
|
|
339
|
58
|
340 *(ctx->index.data - 1) = '/';
|
50
|
341
|
58
|
342 if (ngx_is_dir(&fi)) {
|
50
|
343 return NGX_OK;
|
|
344 }
|
|
345
|
58
|
346 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
106
|
347 "\"%s\" is not a directory", ctx->path.data);
|
58
|
348
|
|
349 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
50
|
350 }
|
|
351
|
|
352
|
58
|
353 static ngx_int_t
|
|
354 ngx_http_index_error(ngx_http_request_t *r, ngx_http_index_ctx_t *ctx,
|
|
355 ngx_err_t err)
|
50
|
356 {
|
|
357 if (err == NGX_EACCES) {
|
|
358 ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
|
106
|
359 "\"%s\" is forbidden", ctx->path.data);
|
126
|
360
|
50
|
361 return NGX_HTTP_FORBIDDEN;
|
|
362 }
|
|
363
|
|
364 ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
|
106
|
365 "\"%s\" is not found", ctx->path.data);
|
58
|
366
|
50
|
367 return NGX_HTTP_NOT_FOUND;
|
|
368 }
|
|
369
|
|
370
|
58
|
371 static void *
|
|
372 ngx_http_index_create_loc_conf(ngx_conf_t *cf)
|
|
373 {
|
|
374 ngx_http_index_loc_conf_t *conf;
|
|
375
|
|
376 conf = ngx_palloc(cf->pool, sizeof(ngx_http_index_loc_conf_t));
|
|
377 if (conf == NULL) {
|
|
378 return NGX_CONF_ERROR;
|
|
379 }
|
|
380
|
|
381 conf->indices = NULL;
|
230
|
382 conf->max_index_len = 0;
|
58
|
383
|
|
384 return conf;
|
|
385 }
|
|
386
|
|
387
|
|
388 static char *
|
|
389 ngx_http_index_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
390 {
|
|
391 ngx_http_index_loc_conf_t *prev = parent;
|
|
392 ngx_http_index_loc_conf_t *conf = child;
|
|
393
|
|
394 ngx_http_index_t *index;
|
|
395
|
|
396 if (conf->indices == NULL) {
|
|
397 conf->indices = prev->indices;
|
|
398 conf->max_index_len = prev->max_index_len;
|
|
399 }
|
|
400
|
|
401 if (conf->indices == NULL) {
|
|
402 conf->indices = ngx_array_create(cf->pool, 1, sizeof(ngx_http_index_t));
|
|
403 if (conf->indices == NULL) {
|
|
404 return NGX_CONF_ERROR;
|
|
405 }
|
|
406
|
|
407 index = ngx_array_push(conf->indices);
|
|
408 if (index == NULL) {
|
|
409 return NGX_CONF_ERROR;
|
|
410 }
|
|
411
|
|
412 index->name.len = sizeof(NGX_HTTP_DEFAULT_INDEX);
|
|
413 index->name.data = (u_char *) NGX_HTTP_DEFAULT_INDEX;
|
|
414 index->lengths = NULL;
|
|
415 index->values = NULL;
|
|
416
|
|
417 conf->max_index_len = sizeof(NGX_HTTP_DEFAULT_INDEX);
|
|
418
|
|
419 return NGX_CONF_OK;
|
|
420 }
|
|
421
|
|
422 return NGX_CONF_OK;
|
|
423 }
|
|
424
|
|
425
|
230
|
426 static ngx_int_t
|
|
427 ngx_http_index_init(ngx_conf_t *cf)
|
|
428 {
|
|
429 ngx_http_handler_pt *h;
|
|
430 ngx_http_core_main_conf_t *cmcf;
|
|
431
|
|
432 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
|
433
|
|
434 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
|
|
435 if (h == NULL) {
|
|
436 return NGX_ERROR;
|
|
437 }
|
|
438
|
|
439 *h = ngx_http_index_handler;
|
|
440
|
|
441 return NGX_OK;
|
|
442 }
|
|
443
|
|
444
|
58
|
445 /* TODO: warn about duplicate indices */
|
|
446
|
|
447 static char *
|
|
448 ngx_http_index_set_index(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
449 {
|
|
450 ngx_http_index_loc_conf_t *ilcf = conf;
|
|
451
|
230
|
452 ngx_str_t *value;
|
58
|
453 ngx_uint_t i, n;
|
|
454 ngx_http_index_t *index;
|
|
455 ngx_http_script_compile_t sc;
|
|
456
|
|
457 if (ilcf->indices == NULL) {
|
|
458 ilcf->indices = ngx_array_create(cf->pool, 2, sizeof(ngx_http_index_t));
|
|
459 if (ilcf->indices == NULL) {
|
|
460 return NGX_CONF_ERROR;
|
|
461 }
|
|
462 }
|
|
463
|
|
464 value = cf->args->elts;
|
|
465
|
|
466 for (i = 1; i < cf->args->nelts; i++) {
|
|
467 if (value[i].data[0] == '/' && i != cf->args->nelts - 1) {
|
|
468 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
469 "only the last index in \"index\" directive "
|
|
470 "may be absolute");
|
|
471 return NGX_CONF_ERROR;
|
|
472 }
|
|
473
|
|
474 if (value[i].len == 0) {
|
|
475 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
476 "index \"%V\" in \"index\" directive is invalid",
|
|
477 &value[1]);
|
|
478 return NGX_CONF_ERROR;
|
|
479 }
|
|
480
|
|
481 index = ngx_array_push(ilcf->indices);
|
|
482 if (index == NULL) {
|
|
483 return NGX_CONF_ERROR;
|
|
484 }
|
|
485
|
|
486 index->name.len = value[i].len;
|
|
487 index->name.data = value[i].data;
|
|
488 index->lengths = NULL;
|
|
489 index->values = NULL;
|
|
490
|
|
491 n = ngx_http_script_variables_count(&value[i]);
|
|
492
|
|
493 if (n == 0) {
|
230
|
494 if (ilcf->max_index_len < index->name.len) {
|
58
|
495 ilcf->max_index_len = index->name.len;
|
|
496 }
|
|
497
|
122
|
498 /* include the terminating '\0' to the length to use ngx_copy() */
|
106
|
499 index->name.len++;
|
|
500
|
58
|
501 continue;
|
|
502 }
|
|
503
|
|
504 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
505
|
|
506 sc.cf = cf;
|
|
507 sc.source = &value[i];
|
|
508 sc.lengths = &index->lengths;
|
|
509 sc.values = &index->values;
|
|
510 sc.variables = n;
|
|
511 sc.complete_lengths = 1;
|
|
512 sc.complete_values = 1;
|
|
513
|
|
514 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
515 return NGX_CONF_ERROR;
|
|
516 }
|
|
517 }
|
|
518
|
|
519 return NGX_CONF_OK;
|
|
520 }
|