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